Separate pricelist from charging policy
authorGeorgios Gousios <gousiosg@gmail.com>
Thu, 6 Oct 2011 07:21:36 +0000 (09:21 +0200)
committerGeorgios Gousios <gousiosg@gmail.com>
Thu, 6 Oct 2011 07:21:36 +0000 (09:21 +0200)
12 files changed:
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntry.scala [new file with mode: 0644]
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntryType.scala [new file with mode: 0644]
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEvent.scala
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEventType.scala [moved from logic/src/main/scala/gr/grnet/aquarium/logic/accounting/InputEventType.scala with 71% similarity]
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Agreement.scala
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/InputEvent.scala [deleted file]
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Policy.scala
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/agreements/DefaultAgreement.scala
logic/src/main/scala/gr/grnet/aquarium/logic/accounting/policies/DefaultRatePolicy.scala
logic/src/main/scala/gr/grnet/aquarium/logic/events/EventProcessor.scala
logic/src/test/scala/gr/grnet/aquarium/logic/test/BillingTest.scala
model/src/main/scala/gr/grnet/aquarium/model/Entity.scala

diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntry.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntry.scala
new file mode 100644 (file)
index 0000000..a28a249
--- /dev/null
@@ -0,0 +1,10 @@
+package gr.grnet.aquarium.logic.accounting
+
+import java.util.Date
+
+/**The result of processing an accounting event*/
+class AccountingEntry(sourceEvents: List[Long], when: Date,
+                      amount: Float, entryType: AccountingEntryType.Value) {
+  
+}
+
diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntryType.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEntryType.scala
new file mode 100644 (file)
index 0000000..ee522fa
--- /dev/null
@@ -0,0 +1,6 @@
+package gr.grnet.aquarium.logic.accounting
+
+object AccountingEntryType extends Enumeration {
+  type InputEvent = Value
+  val STORAGE_CHARGE, NET_CHARGE, DEBIT = Value
+}
index 154eb1e..a7034c7 100644 (file)
@@ -1,8 +1,19 @@
 package gr.grnet.aquarium.logic.accounting
 
 import java.util.Date
+import gr.grnet.aquarium.model.{Entity, DB}
 
-/**The result of processing an input event*/
-class AccountingEvent(e: InputEvent, when: Date, amount: Float) {
+class AccountingEvent(et: AccountingEventType.Value, when: Date,
+                 who: Long, amount: Double, rel: List[Long]) {
 
-}
\ No newline at end of file
+  def process() = {}
+  def policy() : Policy = {null}
+
+  def getRate() : Float = {
+    val ent = DB.find(classOf[Entity], who).get
+
+    0F
+  }
+
+  
+}
@@ -1,7 +1,7 @@
 package gr.grnet.aquarium.logic.accounting
 
 
-object InputEventType extends Enumeration {
+object AccountingEventType extends Enumeration {
   type InputEvent = Value
   val NetDataUp, NetDataDown, DiskSpace, VMTime = Value
 }
\ No newline at end of file
index f1dab0a..dea1b8c 100644 (file)
@@ -1,7 +1,19 @@
 package gr.grnet.aquarium.logic.accounting
 
 import java.util.Date
+import collection.mutable.{HashMap, Map}
 
 abstract class Agreement {
-  def policy(et: InputEventType.Value, d: Date) : Policy
+
+  var policies = new HashMap[AccountingEventType.Value, HashMap[Date,Policy]]
+
+  def addPolicy(et: AccountingEventType.Value, p: Policy, d: Date) = {
+    policies + Map(et -> Map(d -> p))
+  }
+
+  def policy(et: AccountingEventType.Value, d: Date) : Option[Policy] = {
+    val ruleset = policies.getOrElse(et, new HashMap[Date, Policy])
+    val key = ruleset.keys.toList.sorted.find(k => k.compareTo(d) >= 0).orElse(new Date())
+    ruleset.get(key)
+  }
 }
\ No newline at end of file
diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/InputEvent.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/InputEvent.scala
deleted file mode 100644 (file)
index e374a42..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-package gr.grnet.aquarium.logic.accounting
-
-import java.util.Date
-import gr.grnet.aquarium.model.{Entity, DB}
-
-class InputEvent(et: InputEventType.Value, when: Date,
-                 who: Long, amount: Double, rel: List[Long]) {
-
-  def process() = {}
-
-  def findRule(): Policy = {
-    val e = DB.find[Entity](classOf[Entity], who)
-    null
-    //who.agreement.policy(et, when)
-  }
-}
-
index cc0212f..1f55eb3 100644 (file)
@@ -1,7 +1,15 @@
 package gr.grnet.aquarium.logic.accounting
 
 
-abstract class Policy {
+abstract class Policy(et: AccountingEntryType.Value) {
+
+  private def makeEntry(event: AccountingEvent, amount: Float) = {
+    //val entry = new AccountingEntry(event.rel(), event.when, )
+  }
+
+  def process(evt: AccountingEvent) = {
+    makeEntry(evt, calculateAmount(evt))
+  }
 
   def calculateAmount(evt: AccountingEvent) : Float
 }
\ No newline at end of file
index 717cadc..0c0d4f3 100644 (file)
@@ -1,11 +1,8 @@
 package gr.grnet.aquarium.logic.accounting.agreements
 
-import java.util.Date
-import gr.grnet.aquarium.logic.accounting.{InputEventType, Policy, Agreement}
+import gr.grnet.aquarium.logic.accounting.{Agreement}
 
 class DefaultAgreement extends Agreement {
 
-  def policy(et: InputEventType.Value, d: Date) : Policy = {
-    null
-  }
+  
 }
\ No newline at end of file
index db55208..71284a8 100644 (file)
@@ -1,17 +1,17 @@
 package gr.grnet.aquarium.logic.accounting.policies
 
-import gr.grnet.aquarium.logic.accounting.{InputEventType, AccountingEvent, Policy}
+import gr.grnet.aquarium.logic.accounting.{AccountingEventType, AccountingEntry, Policy}
 import collection.immutable.HashMap
 
 object DefaultRatePolicy extends Policy {
 
-  val rates = new HashMap[InputEventType.Value, Float]
+  val rates = new HashMap[AccountingEventType.Value, Float]
 
   private def init() = {
     
   }
   
-  def calculateAmount(evt: AccountingEvent) : Float = {
+  def calculateAmount(evt: AccountingEntry) : Float = {
     if (rates.isEmpty) init()
 
     0F
index 46675cc..2e05ca9 100644 (file)
@@ -1,15 +1,15 @@
 package gr.grnet.aquarium.logic.events
 
-import gr.grnet.aquarium.logic.accounting.{InputEventType, InputEvent}
+import gr.grnet.aquarium.logic.accounting.{AccountingEventType, AccountingEvent}
 import java.util.Date
 
 object EventProcessor {
 
   def process(from: Option[Date], to: Option[Date],
-              events: (Option[Date], Option[Date]) => List[Event]): List[InputEvent] = {
+              events: (Option[Date], Option[Date]) => List[Event]): List[AccountingEvent] = {
     val evts = events(from, to)
 
-    val dummy = new InputEvent(InputEventType.VMTime, new Date() , 0, 0, List())
+    val dummy = new AccountingEvent(AccountingEventType.VMTime, new Date() , 0, 0, List())
 
     evts.map {f => f.who}.distinct.map {
       //Events are calculated per user
@@ -28,19 +28,19 @@ object EventProcessor {
                 case Some(x) => x.id
                 case None => -1 //Now
               }
-              new InputEvent(InputEventType.VMTime, e.when(),
+              new AccountingEvent(AccountingEventType.VMTime, e.when(),
                              u, time.getTime - v.w.getTime,
                             List(v.id, stopid))
             //          case v : VMStarted =>None
             //          case v : VMStopped =>None
             case v: DiskSpaceChanged =>
-              new InputEvent(InputEventType.DiskSpace,
+              new AccountingEvent(AccountingEventType.DiskSpace,
                              e.when(), u, v.bytes, List(v.id()))
             case v: DataUploaded =>
-              new InputEvent(InputEventType.NetDataUp, e.when, u,
+              new AccountingEvent(AccountingEventType.NetDataUp, e.when, u,
                              v.bytes, List(v.id()))
             case v: DataDownloaded =>
-              new InputEvent(InputEventType.NetDataDown, e.when, u,
+              new AccountingEvent(AccountingEventType.NetDataDown, e.when, u,
                              v.bytes, List(v.id()))
             //          case v : SSaasVMCreated => None
             //          case v : SSaasVMStarted =>None
index fc2a8dc..a2f2ac2 100644 (file)
@@ -15,13 +15,8 @@ class BillingTest
   }
 
   @Test
-  def testCalcBill = {
-
-  }
-
-  @Test
   def testAccountingRules() = {
-
+    
   }
 
   @After
index 057b35e..62919e5 100644 (file)
@@ -17,6 +17,9 @@ abstract class Entity extends Id {
   @Column(name = "CREDITS", nullable = true)
   var credits: Float = 0
 
+  @Column(name = "AGREEMENT", nullable = true)
+  var agreement: Long = 0
+
   @OneToMany(mappedBy = "entity",  targetEntity = classOf[Permission],
              cascade = Array(CascadeType.ALL))
   var permissions : Set[Permission] = new HashSet[Permission]()
@@ -24,7 +27,4 @@ abstract class Entity extends Id {
   @OneToMany(mappedBy = "owner",  targetEntity = classOf[ServiceItem],
              cascade = Array(CascadeType.ALL))
   var serviceItems : Set[ServiceItem] = new HashSet[ServiceItem]()
-
-  @Column(name = "AGREEMENT", nullable = true)
-  var agreement: Long = 0
 }
\ No newline at end of file