Start computing wallet entries
[aquarium] / src / main / scala / gr / grnet / aquarium / user / UserState.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.user
37
38 import gr.grnet.aquarium.util.json.{JsonHelpers, JsonSupport}
39 import net.liftweb.json.{JsonAST, Xml}
40 import gr.grnet.aquarium.logic.accounting.dsl.DSLAgreement
41 import com.ckkloverdos.maybe.{Failed, Maybe}
42 import gr.grnet.aquarium.logic.events.WalletEntry
43
44
45 /**
46  * A comprehensive representation of the User's state.
47  *
48  * Note that it is made of autonomous parts that are actually data snapshots.
49  *
50  * The different snapshots need not agree on the snapshot time, ie. some state
51  * part may be stale, while other may be fresh.
52  *
53  * The user state is meant to be partially updated according to relevant events landing on Aquarium.
54  *
55  * @author Christos KK Loverdos <loverdos@gmail.com>
56  */
57
58 case class UserState(
59     userId: String,
60
61     /**
62      * When the user was created in the system (not Aquarium). We use this as a basis for billing periods. Set to
63      * zero if unknown.
64      * 
65      */
66     userCreationMillis: Long,
67
68     /**
69      * Each time the user state is updated, this must be increased.
70      * The counter is used when accessing user state from the cache (user state store)
71      * in order to get the latest value for a particular billing period.
72      */
73     stateChangeCounter: Long,
74
75     /**
76      * True iff this user state refers to a full billing period, that is a full billing month.
77      */
78     isFullBillingMonthState: Boolean,
79
80     /**
81      * The full billing period for which this user state refers to.
82      * This is set when the user state refers to a full billing period (= month)
83      * and is used to cache the user state for subsequent queries.
84      */
85     theFullBillingMonth: BillingMonth,
86
87     /**
88      * If this is a state for a full billing month, then keep here the implicit OFF
89      * resource events.
90      *
91      * The use case is this: A VM may have been started (ON state) before the end of the billing period
92      * and ended (OFF state) after the beginning of the next billing period. In order to bill this, we must assume
93      * an implicit OFF even right at the end of the billing period and an implicit ON event with the beginning of the
94      * next billing period.
95      */
96     implicitOFFs: ImplicitOFFResourceEventsSnapshot,
97
98     /**
99      * So far computed wallet entries for the current billing month.
100      */
101     billingMonthWalletEntries: List[WalletEntry],
102
103     /**
104      * Wallet entries that were computed for out of sync events.
105      * (for the current billing month ??)
106      */
107     outOfSyncWalletEntries: List[WalletEntry],
108
109     /**
110      * The latest resource events per resource instance
111      */
112     latestResourceEvents: LatestResourceEventsSnapshot,
113
114     /**
115      * Counts the number of resource events used to produce this user state for
116      * the billing period recorded by `billingPeriodSnapshot`
117      */
118     resourceEventsCounter: Long,
119
120     active: ActiveStateSnapshot,
121     credits: CreditSnapshot,
122     agreements: AgreementSnapshot,
123     roles: RolesSnapshot,
124     ownedResources: OwnedResourcesSnapshot
125 ) extends JsonSupport {
126
127   private[this] def _allSnapshots: List[Long] = {
128     List(
129       active.snapshotTime,
130       credits.snapshotTime, agreements.snapshotTime, roles.snapshotTime,
131       ownedResources.snapshotTime)
132   }
133
134   def oldestSnapshotTime: Long = _allSnapshots min
135
136   def newestSnapshotTime: Long  = _allSnapshots max
137
138 //  def userCreationDate = new Date(userCreationMillis)
139 //
140 //  def userCreationFormatedDate = new DateCalculator(userCreationMillis).toString
141
142   def maybeDSLAgreement(at: Long): Maybe[DSLAgreement] = {
143     agreements match {
144       case snapshot @ AgreementSnapshot(data, _) ⇒
145         snapshot.getAgreement(at)
146       case _ ⇒
147        Failed(new Exception("No agreement snapshot found for user %s".format(userId)))
148     }
149   }
150
151   def findResourceInstanceSnapshot(resource: String, instanceId: String): Maybe[ResourceInstanceSnapshot] = {
152     ownedResources.findResourceInstanceSnapshot(resource, instanceId)
153   }
154
155   def getResourceInstanceAmount(resource: String, instanceId: String, defaultValue: Double): Double = {
156     ownedResources.getResourceInstanceAmount(resource, instanceId, defaultValue)
157   }
158
159   def copyForResourcesSnapshotUpdate(resource: String,   // resource name
160                                      instanceId: String, // resource instance id
161                                      newAmount: Double,
162                                      snapshotTime: Long): UserState = {
163
164     val (newResources, _, _) = ownedResources.computeResourcesSnapshotUpdate(resource, instanceId, newAmount, snapshotTime)
165
166     this.copy(
167       ownedResources = newResources,
168       stateChangeCounter = this.stateChangeCounter + 1)
169   }
170
171   def resourcesMap = ownedResources.toResourcesMap
172   
173   def safeCredits = credits match {
174     case c @ CreditSnapshot(_, _) ⇒ c
175     case _ ⇒ CreditSnapshot(0.0, 0)
176   }
177 }
178
179
180 object UserState {
181   def fromJson(json: String): UserState = {
182     JsonHelpers.jsonToObject[UserState](json)
183   }
184
185   def fromJValue(jsonAST: JsonAST.JValue): UserState = {
186     JsonHelpers.jValueToObject[UserState](jsonAST)
187   }
188
189   def fromBytes(bytes: Array[Byte]): UserState = {
190     JsonHelpers.jsonBytesToObject[UserState](bytes)
191   }
192
193   def fromXml(xml: String): UserState = {
194     fromJValue(Xml.toJson(scala.xml.XML.loadString(xml)))
195   }
196
197   object JsonNames {
198     final val _id = "_id"
199     final val userId = "userId"
200   }
201 }
202
203 case class BillingMonth(yearOfBillingMonth: Int, billingMonth: Int)