2 * Copyright 2011-2012 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.charging
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
50 * The new [[gr.grnet.aquarium.charging.ChargingBehavior]] for VMs usage.
52 * @author Christos KK Loverdos <loverdos@gmail.com>
54 final class VMChargingBehavior extends ChargingBehaviorSkeleton(List(PowerStatus)) {
55 def computeCreditsToSubtract(
56 workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
58 timeDeltaMillis: Long,
60 ): (Double /* credits */, String /* explanation */) = {
62 val credits = hrs(timeDeltaMillis) * unitPrice
63 val explanation = "Time(%s) * Unit(%s)".format(hrs(timeDeltaMillis), unitPrice)
65 (credits, explanation)
69 def computeSelectorPath(
70 workingChargingBehaviorDetails: mutable.Map[String, Any],
71 workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
72 currentResourceEvent: ResourceEventModel,
73 referenceTimeslot: Timeslot,
77 List(Power.powerOn) // compute prices for power-on state
80 def initialChargingDetails: Map[String, Any] = Map()
82 def computeNewAccumulatingAmount(
83 workingResourceInstanceChargingState: WorkingResourceInstanceChargingState,
84 eventDetails: Map[String, String]
86 workingResourceInstanceChargingState.currentValue
89 def constructImplicitEndEventFor(resourceEvent: ResourceEventModel, newOccurredMillis: Long) = {
90 assert(VMChargingBehaviorValues.isONValue(resourceEvent.value))
92 val details = resourceEvent.details
93 val newDetails = ResourceEventModel.setAquariumSyntheticAndImplicitEnd(details)
94 val newValue = VMChargingBehaviorValues.OFF
96 resourceEvent.withDetailsAndValue(newDetails, newValue, newOccurredMillis)
99 def constructImplicitStartEventFor(resourceEvent: ResourceEventModel) = {
100 throw new AquariumInternalError("constructImplicitStartEventFor() Not compliant with %s".format(this))
105 * @return The number of wallet entries recorded and the new total credits
107 override def processResourceEvent(
109 currentResourceEvent: ResourceEventModel,
110 resourceType: ResourceType,
111 billingMonthInfo: BillingMonthInfo,
112 workingState: WorkingResourcesChargingState,
113 userAgreements: AgreementHistoryModel,
114 totalCredits: Double,
115 walletEntryRecorder: WalletEntry ⇒ Unit
124 object VMChargingBehavior {
125 object SelectorLabels {
126 final val PowerStatus = "Power Status (ON/OFF)"
131 // When the VM is powered on
132 final val powerOn = "powerOn"
133 // When the VM is powered off
134 final val powerOff = "powerOff"