Implement Once behavior with the new scheme. Refactor in the process
[aquarium] / src / main / scala / gr / grnet / aquarium / charging / VMChargingBehavior.scala
1 /*
2  * Copyright 2011-2012 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.charging
37
38 import gr.grnet.aquarium.{Aquarium, AquariumInternalError}
39 import gr.grnet.aquarium.event.model.resource.ResourceEventModel
40 import gr.grnet.aquarium.logic.accounting.dsl.Timeslot
41 import VMChargingBehavior.Selectors.Power
42 import VMChargingBehavior.SelectorLabels.PowerStatus
43 import gr.grnet.aquarium.policy.ResourceType
44 import gr.grnet.aquarium.computation.BillingMonthInfo
45 import gr.grnet.aquarium.charging.state.{WorkingResourceInstanceChargingState, WorkingResourcesChargingState, AgreementHistoryModel}
46 import gr.grnet.aquarium.charging.wallet.WalletEntry
47 import scala.collection.mutable
48
49 /**
50  * The new [[gr.grnet.aquarium.charging.ChargingBehavior]] for VMs usage.
51  *
52  * @author Christos KK Loverdos <loverdos@gmail.com>
53  */
54 final class VMChargingBehavior extends ChargingBehaviorSkeleton(List(PowerStatus)) {
55   def computeCreditsToSubtract(
56       workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
57       oldCredits: Double,
58       timeDeltaMillis: Long,
59       unitPrice: Double
60   ): (Double /* credits */, String /* explanation */) = {
61
62     val credits = hrs(timeDeltaMillis) * unitPrice
63     val explanation = "Time(%s) * Unit(%s)".format(hrs(timeDeltaMillis), unitPrice)
64
65     (credits, explanation)
66
67   }
68
69   def computeSelectorPath(
70       workingChargingBehaviorDetails: mutable.Map[String, Any],
71       workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
72       currentResourceEvent: ResourceEventModel,
73       referenceTimeslot: Timeslot,
74       totalCredits: Double
75   ): List[String] = {
76     // FIXME
77     List(Power.powerOn) // compute prices for power-on state
78   }
79
80   def initialChargingDetails: Map[String, Any] = Map()
81
82   def computeNewAccumulatingAmount(
83       workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
84       eventDetails: Map[String, String]
85   ): Double = {
86     workingResourceInstanceChargingState.currentValue
87   }
88
89   def constructImplicitEndEventFor(resourceEvent: ResourceEventModel, newOccurredMillis: Long) = {
90     assert(VMChargingBehaviorValues.isONValue(resourceEvent.value))
91
92     val details = resourceEvent.details
93     val newDetails = ResourceEventModel.setAquariumSyntheticAndImplicitEnd(details)
94     val newValue   = VMChargingBehaviorValues.OFF
95
96     resourceEvent.withDetailsAndValue(newDetails, newValue, newOccurredMillis)
97   }
98
99   def constructImplicitStartEventFor(resourceEvent: ResourceEventModel) = {
100     throw new AquariumInternalError("constructImplicitStartEventFor() Not compliant with %s".format(this))
101   }
102
103   /**
104    *
105    * @return The number of wallet entries recorded and the new total credits
106    */
107   override def processResourceEvent(
108       aquarium: Aquarium,
109       currentResourceEvent: ResourceEventModel,
110       resourceType: ResourceType,
111       billingMonthInfo: BillingMonthInfo,
112       workingState: WorkingResourcesChargingState,
113       userAgreements: AgreementHistoryModel,
114       totalCredits: Double,
115       walletEntryRecorder: WalletEntry ⇒ Unit
116   ): (Int, Double) = {
117
118
119     (0,0)
120   }
121
122 }
123
124 object VMChargingBehavior {
125   object SelectorLabels {
126     final val PowerStatus = "Power Status (ON/OFF)"
127   }
128
129   object Selectors {
130     object Power {
131       // When the VM is powered on
132       final val powerOn = "powerOn"
133       // When the VM is powered off
134       final val powerOff = "powerOff"
135     }
136   }
137 }