import com.ckkloverdos.maybe.{Failed, NoVal, Just, Maybe}
import gr.grnet.aquarium.util.{ContextualLogger, Loggable, justForSure, failedForSure}
-import gr.grnet.aquarium.logic.accounting.algorithm.SimpleCostPolicyAlgorithmCompiler
import gr.grnet.aquarium.logic.events.{NewWalletEntry, ResourceEvent}
import gr.grnet.aquarium.util.date.{TimeHelpers, MutableDateCalc}
import gr.grnet.aquarium.logic.accounting.dsl.{DSLAgreement, DSLCostPolicy, DSLResourcesMap, DSLPolicy}
import gr.grnet.aquarium.logic.accounting.{Chargeslot, Accounting}
import scala.collection.mutable
import gr.grnet.aquarium.logic.events.ResourceEvent._
+import gr.grnet.aquarium.logic.accounting.algorithm.{CostPolicyAlgorithmCompiler, SimpleCostPolicyAlgorithmCompiler}
/**
*
currentUserState: UserState,
defaultResourcesMap: DSLResourcesMap,
accounting: Accounting,
+ algorithmCompiler: CostPolicyAlgorithmCompiler,
calculationReason: UserStateChangeReason,
contextualLogger: Maybe[ContextualLogger] = NoVal): Maybe[UserState] = {
currentUserState,
defaultResourcesMap,
accounting,
+ algorithmCompiler,
calculationReason,
Just(clog))
}
stateChangeReason: UserStateChangeReason,
billingMonthInfo: BillingMonthInfo,
walletEntriesBuffer: mutable.Buffer[NewWalletEntry],
+ algorithmCompiler: CostPolicyAlgorithmCompiler,
clogM: Maybe[ContextualLogger] = NoVal): UserState = {
val clog = ContextualLogger.fromOther(clogM, logger, "walletEntriesForResourceEvent(%s)", currentResourceEvent.id)
dslResource,
resourcesMap,
alltimeAgreements,
- SimpleCostPolicyAlgorithmCompiler,
+ algorithmCompiler,
policyStore,
Just(clog)
)
stateChangeReason: UserStateChangeReason,
billingMonthInfo: BillingMonthInfo,
walletEntriesBuffer: mutable.Buffer[NewWalletEntry],
+ algorithmCompiler: CostPolicyAlgorithmCompiler,
clogM: Maybe[ContextualLogger] = NoVal): UserState = {
var _workingUserState = startingUserState
stateChangeReason,
billingMonthInfo,
walletEntriesBuffer,
+ algorithmCompiler,
clogM
)
}
currentUserState: UserState,
defaultResourcesMap: DSLResourcesMap,
accounting: Accounting,
+ algorithmCompiler: CostPolicyAlgorithmCompiler,
calculationReason: UserStateChangeReason = NoSpecificChangeReason,
contextualLogger: Maybe[ContextualLogger] = NoVal): Maybe[UserState] = Maybe {
currentUserState,
defaultResourcesMap,
accounting,
+ algorithmCompiler,
calculationReason.forPreviousBillingMonth,
clogJ
)
calculationReason,
billingMonthInfo,
newWalletEntries,
+ algorithmCompiler,
clogJ
)
calculationReason,
billingMonthInfo,
newWalletEntries,
+ algorithmCompiler,
clogJ
)
import gr.grnet.aquarium.util.{Loggable, ContextualLogger}
import gr.grnet.aquarium.simulation._
import gr.grnet.aquarium.simulation.uid.{UIDGenerator, ConcurrentVMLocalUIDGenerator}
-import com.ckkloverdos.maybe.{Failed, Just, NoVal}
+import gr.grnet.aquarium.logic.accounting.algorithm.SimpleCostPolicyAlgorithmCompiler
+import com.ckkloverdos.maybe.{Maybe, Failed, Just, NoVal}
/**
"""
val DefaultPolicy = new DSL{} parse PolicyYAML
+ val DefaultAccounting = new Accounting{}
+ val DefaultCompiler = SimpleCostPolicyAlgorithmCompiler
// For this to work, the definitions must match those in the YAML above.
// Those StdXXXResourceSim are just for debugging convenience anyway, so they must match by design.
val Synnefo = ClientSim("synnefo")(TheUIDGenerator)
val Pithos = ClientSim("pithos" )(TheUIDGenerator)
- @Test
- def testOne: Unit = {
- val clog = ContextualLogger.fromOther(NoVal, logger, "testOne()")
- val StartOfBillingYearDateCalc = new MutableDateCalc(2012, 1, 1)
- val UserCreationDate = new MutableDateCalc(2011, 11, 1).toDate
+ val mc = Configurator.MasterConfigurator.withStoreProviderClass(classOf[MemStore])
+ Policy.withConfigurator(mc)
+ val DefaultStoreProvider = mc.storeProvider
- val computations = new UserStateComputations
+ val StartOfBillingYearDateCalc = new MutableDateCalc(2012, 1, 1)
+ val UserCreationDate = new MutableDateCalc(2011, 11, 1).toDate
- val mc = Configurator.MasterConfigurator.withStoreProviderClass(classOf[MemStore])
- Policy.withConfigurator(mc)
+ // Store the default policy
+ val policyDateCalc = StartOfBillingYearDateCalc.copy
+ val policyOccurredMillis = policyDateCalc.toMillis
+ val policyValidFromMillis = policyDateCalc.copy.goPreviousYear.toMillis
+ val policyValidToMillis = policyDateCalc.copy.goNextYear.toMillis
+ DefaultStoreProvider.policyStore.storePolicyEntry(DefaultPolicy.toPolicyEntry(policyOccurredMillis, policyValidFromMillis, policyValidToMillis))
- val storeProvider = mc.storeProvider
- val userStateStore = storeProvider.userStateStore
- val resourceEventStore = storeProvider.resourceEventStore
- val policyStore = storeProvider.policyStore
+ val Aquarium = AquariumSim(List(VMTimeResource, DiskspaceResource, BandwidthResource), DefaultStoreProvider.resourceEventStore)
+ val DefaultResourcesMap = Aquarium.resourcesMap
- // Store the default policy
- val policyDateCalc = StartOfBillingYearDateCalc.copy
- val policyOccurredMillis = policyDateCalc.toMillis
- val policyValidFromMillis = policyDateCalc.copy.goPreviousYear.toMillis
- val policyValidToMillis = policyDateCalc.copy.goNextYear.toMillis
- policyStore.storePolicyEntry(DefaultPolicy.toPolicyEntry(policyOccurredMillis, policyValidFromMillis, policyValidToMillis))
+ val UserCKKL = Aquarium.newUser("CKKL", UserCreationDate)
- val Aquarium = AquariumSim(List(VMTimeResource, DiskspaceResource, BandwidthResource), storeProvider.resourceEventStore)
- val DefaultResourcesMap = Aquarium.resourcesMap
+ // By convention
+ // - synnefo is for VMTime and Bandwidth
+ // - pithos is for Diskspace
+ val VMTimeInstance = VMTimeResource.newInstance ("VM.1", UserCKKL, Synnefo)
+ val BandwidthInstance = BandwidthResource.newInstance("3G.1", UserCKKL, Synnefo)
+ val DiskInstance = DiskspaceResource.newInstance("DISK.1", UserCKKL, Pithos)
+
+ val Computations = new UserStateComputations
+
+ def showUserState(clog: ContextualLogger, userStateM: Maybe[UserState]) {
+ userStateM match {
+ case Just(userState) ⇒
+ val _id = userState._id
+ val parentId = userState.parentUserStateId
+ val credits = userState.creditsSnapshot.creditAmount
+ val newWalletEntries = userState.newWalletEntries
+ val changeReasonCode = userState.lastChangeReasonCode
+ val changeReason = userState.lastChangeReason
+ userState.implicitlyIssuedSnapshot
+
+ clog.indent()
+ clog.debug("_id = %s", _id)
+ clog.debug("parentId = %s", parentId)
+ clog.debug("credits = %s", credits)
+ clog.debug("changeReasonCode = %s", changeReasonCode)
+ clog.debug("changeReason = %s", changeReason)
+ clog.debugSeq("newWalletEntries", newWalletEntries.map(_.toDebugString), 0)
+ clog.unindent()
+
+ case NoVal ⇒
+
+ case failed@Failed(_, _) ⇒
+ clog.error(failed)
+ failed.exception.printStackTrace()
+ }
+ }
+
+ @Test
+ def testOne: Unit = {
+ val clog = ContextualLogger.fromOther(NoVal, logger, "testOne()")
- // A new user is created on 2012-01-15 00:00:00.000
- val UserCKKL = Aquarium.newUser("CKKL", UserCreationDate)
- // By convention
- // - synnefo is for VMTime and Bandwidth
- // - pithos is for Diskspace
- val VMTimeInstance = VMTimeResource.newInstance ("VM.1", UserCKKL, Synnefo)
- val BandwidthInstance = BandwidthResource.newInstance("3G.1", UserCKKL, Synnefo)
- val DiskInstance = DiskspaceResource.newInstance("DISK.1", UserCKKL, Pithos)
// Let's create our dates of interest
val vmStartDateCalc = StartOfBillingYearDateCalc.copy.goPlusDays(1).goPlusHours(1)
val billingMonthInfo = BillingMonthInfo.fromDateCalc(StartOfBillingYearDateCalc)
- val initialUserState = computations.createInitialUserState(
+ val initialUserState = Computations.createInitialUserState(
userId = UserCKKL.userId,
userCreationMillis = UserCreationDate.getTime,
isActive = true,
clog.debugMap("DefaultResourcesMap", DefaultResourcesMap.map, 1)
- val userStateM = computations.doFullMonthlyBilling(
+ val userStateM = Computations.doFullMonthlyBilling(
UserCKKL.userId,
billingMonthInfo,
- storeProvider,
+ DefaultStoreProvider,
initialUserState,
DefaultResourcesMap,
- new Accounting{},
+ DefaultAccounting,
+ DefaultCompiler,
MonthlyBillingCalculation(billingMonthInfo),
Just(clog)
)
- clog.debug("userStateM = %s".format(userStateM))
-
- userStateM match {
- case Just(userState) ⇒
- val _id = userState._id
- val parentId = userState.parentUserStateId
- val credits = userState.creditsSnapshot.creditAmount
- val newWalletEntries = userState.newWalletEntries
- val changeReasonCode = userState.lastChangeReasonCode
- val changeReason = userState.lastChangeReason
- userState.implicitlyIssuedSnapshot
-
- clog.indent()
- clog.debug("_id = %s", _id)
- clog.debug("parentId = %s", parentId)
- clog.debug("credits = %s", credits)
- clog.debug("changeReasonCode = %s", changeReasonCode)
- clog.debug("changeReason = %s", changeReason)
- clog.debugSeq("newWalletEntries", newWalletEntries.map(_.toDebugString), 0)
- clog.unindent()
-
- case NoVal ⇒
-
- case failed @ Failed(_, _) ⇒
- clog.error(failed)
- failed.exception.printStackTrace()
- }
+ showUserState(clog, userStateM)
}
}
\ No newline at end of file