WIP: New state machine for message processing
[aquarium] / src / main / scala / gr / grnet / aquarium / policy / PolicyModel.scala
index 27023e7..df1b794 100644 (file)
 
 package gr.grnet.aquarium.policy
 
-import gr.grnet.aquarium.{AquariumInternalError, Timespan}
+import gr.grnet.aquarium.message.avro.ModelFactory
+import gr.grnet.aquarium.message.avro.gen.PolicyMsg
 import gr.grnet.aquarium.util.json.JsonSupport
-import gr.grnet.aquarium.charging.ChargingBehavior
+import scala.collection.JavaConverters.asScalaBufferConverter
+import scala.collection.JavaConverters.mapAsScalaMapConverter
+import scala.collection.immutable
 
 /**
  * A policy is the fundamental business-related configuration of Aquarium.
@@ -49,69 +52,55 @@ import gr.grnet.aquarium.charging.ChargingBehavior
  * @author Christos KK Loverdos <loverdos@gmail.com>
  */
 
-trait PolicyModel extends Ordered[PolicyModel] with JsonSupport {
-  final def compare(that: PolicyModel): Int = {
-    if(this.validFrom < that.validFrom) {
-      -1
-    } else if(this.validFrom == that.validFrom) {
-      0
-    } else {
-      1
-    }
-  }
-
-  def id: String
-
-  def parentID: Option[String]
-
-  def idInStore: Option[Any]
+case class PolicyModel(
+    msg: PolicyMsg
+) extends Ordered[PolicyModel] with JsonSupport {
 
   /**
-   * The time period within which this policy is valid.
+   * Each role is mapped to a full price table.
    */
-  def validityTimespan: Timespan
-
-  final def validFrom: Long = validityTimespan.fromMillis
-
-  final def validTo: Long = validityTimespan.toMillis
+  val roleMapping = ModelFactory.newRoleMapping(msg.getRoleMapping)
 
   /**
-   * All known resource types for the policy's validity period.
+   * All known charging behaviors for the policy's validity period. These are the fully
+   * qualified class names that implement [[gr.grnet.aquarium.charging.ChargingBehavior]]<p/>
+   * Note than since a charging behavior is semantically attached to an implementation,
+   * a change in the set of known charging behaviors normally means a change in the
+   * implementation of Aquarium.
    */
-  def resourceTypes: Set[ResourceType]
+  val chargingBehaviors = immutable.Set(msg.getChargingBehaviors().asScala.toSeq: _*)
 
   /**
-   * All known charging behaviors for the policy's validity period.<p/>
-   *
-   * Note than since a charging behavior is semantically attached to an implementation, a change in the set
-   * of known charging behaviors normally means a change in the implementation of Aquarium.
+   * All known resource types for the policy's validity period.
    */
-  def chargingBehaviors: Set[String/*ImplementationClassName*/]
+  val resourceTypes = immutable.Set(msg.getResourceMapping().asScala.valuesIterator.toSeq.map(ModelFactory.newResourceType).toSeq: _*)
 
-  /**
-   * Each role is mapped to a full price table.
-   */
-  def roleMapping: Map[String/*Role*/, FullPriceTable]
+  def validFromMillis = msg.getValidFromMillis: Long
 
+  def validToMillis = msg.getValidToMillis: Long
+
+  final def compare(that: PolicyModel): Int = {
+    if(this.validFromMillis < that.validFromMillis) {
+      -1
+    } else if(this.validFromMillis == that.validFromMillis) {
+      0
+    } else {
+      1
+    }
+  }
 
   /**
    * All the known roles for the policy's validity period.
    * These names must be common between all communicating parties, i.e. the IM component that sends
-   * [[gr.grnet.aquarium.event.model.im.IMEventModel]] events.
+   * [[gr.grnet.aquarium.message.avro.gen.IMEventMsg]] events.
    *
    * This is a derived set, from the keys of `roleMapping`
    */
-  def roles: Set[String] = roleMapping.keySet
-
-  def resourceTypesMap: Map[String, ResourceType] = Map(resourceTypes.map(rt ⇒ (rt.name, rt)).toSeq: _*)
-
-  def resourceTypeByName(resource: String): Option[ResourceType] = resourceTypes.find(_.name == resource)
-}
+  val roles = roleMapping.keySet
 
-object PolicyModel {
-  trait NamesT {
-    final val a = 1
+  val resourceTypesMap: Map[String, ResourceType] = {
+    msg.getResourceMapping.asScala.map(kv ⇒ (kv._1, ModelFactory.newResourceType(kv._2))).toMap
   }
 
-  final object Names extends NamesT
+  def resourceTypeByName(resource: String) = resourceTypes.find(_.name == resource)
 }