2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.aquarium.user
38 import gr.grnet.aquarium.util.json.{JsonHelpers, JsonSupport}
39 import net.liftweb.json.{parse => parseJson, JsonAST, Xml}
40 import gr.grnet.aquarium.logic.accounting.dsl.DSLAgreement
41 import com.ckkloverdos.maybe.{Failed, Just, Maybe}
42 import gr.grnet.aquarium.logic.accounting.Policy
46 * A comprehensive representation of the User's state.
48 * Note that it is made of autonomous parts that are actually data snapshots.
50 * The different snapshots need not agree on the snapshot time, ie. some state
51 * part may be stale, while other may be fresh.
53 * The user state is meant to be partially updated according to relevant events landing on Aquarium.
55 * @author Christos KK Loverdos <loverdos@gmail.com>
62 * When the user was created in the system (not Aquarium). We use this as a basis for billing periods. Set to
65 startDateMillis: Long,
68 * Each time the user state is updated, this must be increased.
69 * The counter is used when accessing user state from the cache (user state store)
70 * in order to get the latest value for a particular billing period.
72 stateChangeCounter: Long,
75 * True iff this user state refers to a full billing period, that is a full billing month.
77 isFullBillingPeriod: Boolean,
80 * The full billing period for which this user state refers to.
81 * This is set when the user state refers to a full billing period (= month)
82 * and is used to cache the user state for subsequent queries.
84 fullBillingPeriod: BillingPeriod,
87 * Counts the number of resource events used to produce this user state for
88 * the billing period recorded by `billingPeriodSnapshot`
90 resourceEventsCounter: Long,
92 active: ActiveSuspendedSnapshot,
93 credits: CreditSnapshot,
94 agreements: AgreementSnapshot,
96 paymentOrders: PaymentOrdersSnapshot,
97 ownedGroups: OwnedGroupsSnapshot,
98 groupMemberships: GroupMembershipsSnapshot,
99 ownedResources: OwnedResourcesSnapshot
100 ) extends JsonSupport {
102 private[this] def _allSnapshots: List[Long] = {
105 credits.snapshotTime, agreements.snapshotTime, roles.snapshotTime,
106 paymentOrders.snapshotTime, ownedGroups.snapshotTime, groupMemberships.snapshotTime,
107 ownedResources.snapshotTime)
110 def oldestSnapshotTime: Long = _allSnapshots min
112 def newestSnapshotTime: Long = _allSnapshots max
114 def maybeDSLAgreement(at: Long): Maybe[DSLAgreement] = {
116 case snapshot @ AgreementSnapshot(data, _) ⇒
117 snapshot.getAgreement(at)
119 Failed(new Exception("No agreement snapshot found for user %s".format(userId)))
123 def findResourceInstanceSnapshot(resource: String, instanceId: String): Maybe[ResourceInstanceSnapshot] = {
124 ownedResources.findResourceInstanceSnapshot(resource, instanceId)
127 def getResourceInstanceAmount(resource: String, instanceId: String, defaultValue: Double): Double = {
128 ownedResources.getResourceInstanceAmount(resource, instanceId, defaultValue)
131 def copyForResourcesSnapshotUpdate(resource: String, // resource name
132 instanceId: String, // resource instance id
134 snapshotTime: Long): UserState = {
136 val (newResources, _, _) = ownedResources.computeResourcesSnapshotUpdate(resource, instanceId, newAmount, snapshotTime)
139 ownedResources = newResources,
140 stateChangeCounter = this.stateChangeCounter + 1)
143 def resourcesMap = ownedResources.toResourcesMap
145 def safeCredits = credits match {
146 case c @ CreditSnapshot(_, _) ⇒ c
147 case _ ⇒ CreditSnapshot(0.0, 0)
153 def fromJson(json: String): UserState = {
154 JsonHelpers.jsonToObject[UserState](json)
157 def fromJValue(jsonAST: JsonAST.JValue): UserState = {
158 JsonHelpers.jValueToObject[UserState](jsonAST)
161 def fromBytes(bytes: Array[Byte]): UserState = {
162 JsonHelpers.jsonBytesToObject[UserState](bytes)
165 def fromXml(xml: String): UserState = {
166 fromJValue(Xml.toJson(scala.xml.XML.loadString(xml)))
170 final val _id = "_id"
171 final val userId = "userId"
175 case class BillingPeriod(startMillis: Long, stopMillis: Long)