Implement mem-based resource event store
[aquarium] / src / main / scala / gr / grnet / aquarium / logic / accounting / Policy.scala
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.logic.accounting
37
38 import dsl.{Timeslot, DSLPolicy, DSL}
39 import gr.grnet.aquarium.Configurator._
40 import java.io.{InputStream, FileInputStream, File}
41 import java.util.Date
42 import com.ckkloverdos.maybe.{Maybe, Just}
43 import gr.grnet.aquarium.util.date.TimeHelpers
44 import gr.grnet.aquarium.logic.events.PolicyEntry
45 import gr.grnet.aquarium.util.{CryptoUtils, Loggable}
46 import java.util.concurrent.atomic.AtomicReference
47
48 /**
49  * Searches for and loads the applicable accounting policy
50  *
51  * @author Georgios Gousios <gousiosg@gmail.com>
52  */
53 object Policy extends DSL with Loggable {
54
55   /* Pointer to the latest policy */
56   private lazy val policies = new AtomicReference[Map[Timeslot, DSLPolicy]](reloadPolicies)
57
58   /* Pointer to the latest policy */
59   private val currentPolicy = new AtomicReference[DSLPolicy](policies.get.last._2)
60
61   /**
62    * Get the latest defined policy.
63    */
64   def policy = currentPolicy.get
65
66   /**
67    * Get the policy that is valid at the specified time instance.
68    */
69   def policy(at: Date): Maybe[DSLPolicy] = Maybe {
70     policies.get.find {
71       a => (a._1.from.before(at) && a._1.to.after(at)) ||
72            (a._1.from.before(at) && a._1.to == -1)
73     } match {
74       case Some(x) => x._2
75       case None =>
76         throw new AccountingException("No valid policy for date: %s".format(at))
77     }
78   }
79
80   /**
81    * Get the policies that are valid between the specified time instances
82    */
83   def policies(from: Date, to: Date): List[DSLPolicy] = {
84     policies.get.filter {
85       a => a._1.from.before(from) &&
86            a._1.to.after(to)
87     }.values.toList
88   }
89
90   /**
91    * Get the policies that are valid throughout the specified
92    * [[gr.grnet.aquarium.logic.accounting.dsl.Timeslot]]
93    */
94   def policies(t: Timeslot): List[DSLPolicy] = policies(t.from, t.to)
95
96   /**
97    * Load and parse a policy from file.
98    */
99   def loadPolicyFromFile(pol: File): DSLPolicy = {
100
101     val stream = pol.exists() match {
102       case true =>
103         logger.info("Using policy file %s".format(pol.getAbsolutePath))
104         new FileInputStream(pol)
105       case false =>
106         logger.warn(("Cannot find user configured policy file %s, " +
107           "looking for default policy").format(pol.getAbsolutePath))
108         getClass.getClassLoader.getResourceAsStream("policy.yaml") match {
109           case x: InputStream =>
110             logger.warn("Using default policy, this is problably not what you want")
111             x
112           case null =>
113             logger.error("No valid policy file found, Aquarium will fail")
114             null
115         }
116     }
117     parse(stream)
118   }
119
120   /**
121    * Trigger a policy update cycle.
122    */
123   def updatePolicies = synchronized {
124     //XXX: The following update should happen as one transaction
125     val tmpPol = reloadPolicies
126     currentPolicy.set(tmpPol.last._2)
127     policies.set(tmpPol)
128   }
129
130   /**
131    * Search for and open a stream to a policy.
132    */
133   private def policyFile: File =
134     MasterConfigurator.props.get(Keys.aquarium_policy) match {
135       case Just(x) => new File(x)
136       case _ => new File("/etc/aquarium/policy.yaml")
137     }
138
139   /**
140    * Check whether the policy definition file (in whichever path) is
141    * newer than the latest stored policy, reload and set it as current.
142    * This method has side-effects to this object's state.
143    */
144   private def reloadPolicies: Map[Timeslot, DSLPolicy] = {
145     //1. Load policies from db
146     val policies = MasterConfigurator.policyEventStore.loadPolicies(0)
147
148     //2. Check whether policy file has been updated
149     val latestPolicyChange = if (policies.isEmpty) 0 else policies.last.validFrom
150     val policyf = policyFile
151     var updated = false
152
153     if (policyf.exists) {
154       if (policyf.lastModified > latestPolicyChange) {
155         logger.info("Policy changed since last check, reloading")
156         updated = true
157       } else {
158         logger.info("Policy not changed since last check")
159       }
160     } else {
161       logger.warn("User specified policy file %s does not exist, " +
162         "using stored policy information".format(policyf.getAbsolutePath))
163     }
164
165     val toAdd = updated match {
166       case true =>
167         val ts = TimeHelpers.nowMillis
168         val toUpdate = policies.last.copy(validTo = ts)
169         val parsedNew = loadPolicyFromFile(policyf)
170         val yaml = parsedNew.toYAML
171         val newPolicy = PolicyEntry(CryptoUtils.sha1(yaml), ts, ts, yaml, ts + 1, -1)
172
173         MasterConfigurator.policyEventStore.updatePolicy(toUpdate)
174         MasterConfigurator.policyEventStore.storePolicy(newPolicy)
175
176         List(toUpdate, newPolicy)
177       case false => List()
178     }
179
180     policies.init.++(toAdd).foldLeft(Map[Timeslot, DSLPolicy]()){
181       (acc, p) =>
182         acc ++ Map(Timeslot(new Date(p.validFrom), new Date(p.validTo)) -> parse(p.policyYAML))
183     }
184   }
185 }