From d7fd0c372c1f535c5d4e25e656ffec1432f5089b Mon Sep 17 00:00:00 2001 From: Georgios Gousios Date: Wed, 23 Nov 2011 16:41:51 +0200 Subject: [PATCH] Term renaming in Aquarium policy DSL creditpolicy -> aquariumpolicy polic{y,ies} -> algorith{m,ms} --- .../logic/accounting/AccountingEvent.scala | 4 +- .../aquarium/logic/accounting/Agreement.scala | 2 +- .../grnet/aquarium/logic/accounting/dsl/DSL.scala | 124 ++++++++++---------- .../logic/accounting/dsl/DSLSemanticChecks.scala | 14 +-- .../aquarium/logic/accounting/dsl/Vocabulary.scala | 6 +- logic/src/test/resources/policy.yaml | 20 +++- .../gr/grnet/aquarium/logic/test/DSLTest.scala | 12 +- 7 files changed, 95 insertions(+), 87 deletions(-) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEvent.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEvent.scala index fc2107b..b7c989c 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEvent.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/AccountingEvent.scala @@ -72,9 +72,9 @@ class AccountingEvent(et: AccountingEventType.Value, start: Date, entity().agreement) }*/ - /*agr.policy(et, when) match { + /*agr.algorithm(et, when) match { case Some(x) => x - case None => throw new Exception("No charging policy for event type:" + + case None => throw new Exception("No charging algorithm for event type:" + et + " in period starting from:" + when.getTime) }*/ List() diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Agreement.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Agreement.scala index a4be50a..8c0f28c 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Agreement.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/Agreement.scala @@ -51,7 +51,7 @@ abstract class Agreement { policies = policies ++ Map(et -> Map(d -> p)) } - /** Get applicable policy at the resource that the event occured */ + /** Get applicable algorithm at the resource that the event occured */ def policy(et: AccountingEventType.Value, d: Date) : Option[Policy] = { val ruleset = policies.getOrElse(et, new HashMap[Date, Policy]) val keyset = List(new Date(0)) ++ diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSL.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSL.scala index 073bd3b..b3bd8ef 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSL.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSL.scala @@ -40,7 +40,7 @@ import com.kenai.crontabparser.impl.CronTabParserBridge import java.io.{InputStreamReader, InputStream} import gr.grnet.aquarium.util.Loggable import gr.grnet.aquarium.util.yaml._ -import java.util.{Calendar, Date} +import java.util.Date /** * A parser for the Aquarium accounting DSL. @@ -49,28 +49,28 @@ import java.util.{Calendar, Date} */ trait DSL extends Loggable { - /** An empty policy */ - val emptyPolicy = DSLPolicy("", None, Map(), DSLTimeFrame(new Date(0), None, Option(List()))) + /** An empty algorithm */ + val emptyAlgorithm = DSLAlgorithm("", None, Map(), DSLTimeFrame(new Date(0), None, Option(List()))) /** An empty pricelist */ val emptyPriceList = DSLPriceList("", None, Map(), DSLTimeFrame(new Date(0), None, Option(List()))) /** An empty agreement*/ - val emptyAgreement = DSLAgreement("", None, emptyPolicy, emptyPriceList) + val emptyAgreement = DSLAgreement("", None, emptyAlgorithm, emptyPriceList) /** - * Parse an InputStream containing an Aquarium DSL policy. + * Parse an InputStream containing an Aquarium DSL algorithm. */ - def parse(input: InputStream) : DSLCreditPolicy = { + def parse(input: InputStream) : DSLPolicy = { logger.debug("Policy parsing started") val document = YAMLHelpers.loadYAML(new InputStreamReader(input)) - val policy = document / (Vocabulary.creditpolicy) + val policy = document / (Vocabulary.aquariumpolicy) val resources = parseResources(policy./(Vocabulary.resources).asInstanceOf[YAMLListNode]) logger.debug("Resources: %s".format(resources)) - val policies = parsePolicies(policy./(Vocabulary.policies).asInstanceOf[YAMLListNode],resources, List()) + val policies = parseAlgorithms(policy./(Vocabulary.algorithms).asInstanceOf[YAMLListNode],resources, List()) logger.debug("Policies: %s".format(policies)) val pricelists = parsePriceLists( @@ -85,7 +85,7 @@ trait DSL extends Loggable { ) logger.debug("Agreements: %s".format(agreements)) - DSLCreditPolicy(policies, pricelists, resources, agreements) + DSLPolicy(policies, pricelists, resources, agreements) } /** Parse top level resources declarations */ @@ -100,72 +100,72 @@ trait DSL extends Loggable { } } - /** Parse top level policy declarations */ - private def parsePolicies(policies: YAMLListNode, + /** Parse top level algorithm declarations */ + private def parseAlgorithms(algorithms: YAMLListNode, resources: List[DSLResource], - results: List[DSLPolicy]): List[DSLPolicy] = { + results: List[DSLAlgorithm]): List[DSLAlgorithm] = { - policies.head match { + algorithms.head match { case YAMLEmptyNode => return List() case _ => } - val superName = policies.head / Vocabulary.overrides - val policyTmpl = superName match { + val superName = algorithms.head / Vocabulary.overrides + val algoTmpl = superName match { case y: YAMLStringNode => results.find(p => p.name.equals(y.string)) match { case Some(x) => x - case None => throw new DSLParseException("Cannot find super policy %s".format(superName)) + case None => throw new DSLParseException("Cannot find super algorithm %s".format(superName)) } - case YAMLEmptyNode => emptyPolicy - case _ => throw new DSLParseException("Super policy name %s not a string".format()) + case YAMLEmptyNode => emptyAlgorithm + case _ => throw new DSLParseException("Super algorithm name %s not a string".format()) } - val policy = constructPolicy(policies.head.asInstanceOf[YAMLMapNode], - policyTmpl, resources) + val algorithm = constructAlgorithm(algorithms.head.asInstanceOf[YAMLMapNode], + algoTmpl, resources) - val tmpresults = results ++ List(policy) - List(policy) ++ parsePolicies(policies.tail, resources, tmpresults) + val tmpresults = results ++ List(algorithm) + List(algorithm) ++ parseAlgorithms(algorithms.tail, resources, tmpresults) } - /** Construct a policy object from a yaml node*/ - def constructPolicy(policy: YAMLMapNode, - policyTmpl: DSLPolicy, - resources: List[DSLResource]): DSLPolicy = { - val name = policy / Vocabulary.name match { + /** Construct an algorithm object from a yaml node*/ + def constructAlgorithm(algorithm: YAMLMapNode, + algoTmpl: DSLAlgorithm, + resources: List[DSLResource]): DSLAlgorithm = { + val name = algorithm / Vocabulary.name match { case x: YAMLStringNode => x.string - case YAMLEmptyNode => throw new DSLParseException("Policy does not have a name") + case YAMLEmptyNode => throw new DSLParseException("Algorithm does not have a name") } - val overr = policy / Vocabulary.overrides match { - case x: YAMLStringNode => Some(policyTmpl) + val overr = algorithm / Vocabulary.overrides match { + case x: YAMLStringNode => Some(algoTmpl) case YAMLEmptyNode => None } val algos = resources.map { r => - val algo = policy / r.name match { + val algo = algorithm / r.name match { case x: YAMLStringNode => x.string case y: YAMLIntNode => y.int.toString - case YAMLEmptyNode => policyTmpl.equals(emptyPolicy) match { - case false => policyTmpl.algorithms.getOrElse(r, - throw new DSLParseException(("Superpolicy does not specify an algorithm for resource:%s").format(r.name))) + case YAMLEmptyNode => algoTmpl.equals(emptyAlgorithm) match { + case false => algoTmpl.algorithms.getOrElse(r, + throw new DSLParseException(("Superalgo does not specify an algorithm for resource:%s").format(r.name))) case true => throw new DSLParseException(("Cannot find calculation algorithm for resource %s in either " + - "policy %s or a superpolicy").format(r.name, name)) + "algorithm %s or a superalgorithm").format(r.name, name)) } } Map(r -> algo) }.foldLeft(Map[DSLResource, String]())((x,y) => x ++ y) - val timeframe = policy / Vocabulary.effective match { + val timeframe = algorithm / Vocabulary.effective match { case x: YAMLMapNode => parseTimeFrame(x) - case YAMLEmptyNode => policyTmpl.equals(emptyPolicy) match { - case false => policyTmpl.effective - case true => throw new DSLParseException(("Cannot find effectivity period for policy %s ").format(name)) + case YAMLEmptyNode => algoTmpl.equals(emptyAlgorithm) match { + case false => algoTmpl.effective + case true => throw new DSLParseException(("Cannot find effectivity period for algorithm %s ").format(name)) } } - DSLPolicy(name, overr, algos, timeframe) + DSLAlgorithm(name, overr, algos, timeframe) } /** Parse top level pricelist declarations */ @@ -203,7 +203,7 @@ trait DSL extends Loggable { val name = pl / Vocabulary.name match { case x: YAMLStringNode => x.string case YAMLEmptyNode => throw new DSLParseException( - "Policy does not have a name") + "Pricelist does not have a name") } val overr = pl / Vocabulary.overrides match { @@ -217,9 +217,9 @@ trait DSL extends Loggable { case y: YAMLIntNode => y.int.toFloat case z: YAMLDoubleNode => z.double.toFloat case a: YAMLStringNode => a.string.toFloat - case YAMLEmptyNode => tmpl.equals(emptyPolicy) match { + case YAMLEmptyNode => tmpl.equals(emptyAlgorithm) match { case false => tmpl.prices.getOrElse(r, - throw new DSLParseException(("Superpolicy does not specify a price for resource:%s").format(r.name))) + throw new DSLParseException(("Superpricelist does not specify a price for resource:%s").format(r.name))) case true => throw new DSLParseException(("Cannot find price for resource %s in either pricelist %s or " + "its super pricelist").format(r.name, name)) } @@ -229,7 +229,7 @@ trait DSL extends Loggable { val timeframe = pl / Vocabulary.effective match { case x: YAMLMapNode => parseTimeFrame(x) - case YAMLEmptyNode => tmpl.equals(emptyPolicy) match { + case YAMLEmptyNode => tmpl.equals(emptyAlgorithm) match { case false => tmpl.effective case true => throw new DSLParseException(("Cannot find effectivity period for pricelist %s ").format(name)) } @@ -239,7 +239,7 @@ trait DSL extends Loggable { /** Parse top level agreements */ private def parseAgreements(agreements: YAMLListNode, - policies: List[DSLPolicy], + policies: List[DSLAlgorithm], pricelists: List[DSLPriceList], resources: List[DSLResource], results: List[DSLAgreement]): List[DSLAgreement] = { @@ -269,7 +269,7 @@ trait DSL extends Loggable { def constructAgreement(agr: YAMLMapNode, tmpl: DSLAgreement, - policies: List[DSLPolicy], + policies: List[DSLAlgorithm], pricelists: List[DSLPriceList], resources: List[DSLResource]) : DSLAgreement = { val name = agr / Vocabulary.name match { @@ -277,20 +277,20 @@ trait DSL extends Loggable { case YAMLEmptyNode => throw new DSLParseException("Agreement does not have a name") } - val policy = agr / Vocabulary.policy match { + val algorithm = agr / Vocabulary.algorithm match { case x: YAMLStringNode => policies.find(p => p.name.equals(x.string)) match { case Some(y) => y - case None => throw new DSLParseException(("Cannot find policy named %s").format(x)) + case None => throw new DSLParseException(("Cannot find algorithm named %s").format(x)) } case y: YAMLMapNode => tmpl.equals(emptyAgreement) match { - case true => throw new DSLParseException(("Incomplete policy definition for agreement %s").format(name)) + case true => throw new DSLParseException(("Incomplete algorithm definition for agreement %s").format(name)) case false => - y.map += ("name" -> YAMLStringNode("/","%s-policy".format(name))) - constructPolicy(y, tmpl.policy, resources) + y.map += ("name" -> YAMLStringNode("/","%s-algorithm".format(name))) + constructAlgorithm(y, tmpl.algorithm, resources) } case YAMLEmptyNode => tmpl.equals(emptyAgreement) match { - case true => throw new DSLParseException(("No policy for agreement %s").format(name)) - case false => tmpl.policy + case true => throw new DSLParseException(("No algorithm for agreement %s").format(name)) + case false => tmpl.algorithm } } @@ -306,7 +306,7 @@ trait DSL extends Loggable { constructPriceList(y, tmpl.pricelist, resources) } case YAMLEmptyNode => tmpl.equals(emptyAgreement) match { - case true => throw new DSLParseException(("No policy for agreement %s").format(name)) + case true => throw new DSLParseException(("No algorithm for agreement %s").format(name)) case false => tmpl.pricelist } } @@ -316,7 +316,7 @@ trait DSL extends Loggable { case false => None } - DSLAgreement(name, overrides, policy, pricelist) + DSLAgreement(name, overrides, algorithm, pricelist) } /** Parse a timeframe declaration */ @@ -408,8 +408,8 @@ trait DSL extends Loggable { } } -case class DSLCreditPolicy ( - policies: List[DSLPolicy], +case class DSLPolicy ( + algorithms: List[DSLAlgorithm], pricelists: List[DSLPriceList], resources: List[DSLResource], agreements: List[DSLAgreement] @@ -425,8 +425,8 @@ case class DSLCreditPolicy ( } /** Find a pricelist by name */ - def findPolicy(name: String): Option[DSLPolicy] = { - policies.find(a => a.name.equals(name)) + def findAlgorithm(name: String): Option[DSLAlgorithm] = { + algorithms.find(a => a.name.equals(name)) } /** Find an agreement by name */ @@ -442,13 +442,13 @@ case class DSLResource( case class DSLAgreement ( name: String, overrides: Option[DSLAgreement], - policy : DSLPolicy, + algorithm : DSLAlgorithm, pricelist : DSLPriceList ) -case class DSLPolicy ( +case class DSLAlgorithm ( name: String, - overrides: Option[DSLPolicy], + overrides: Option[DSLAlgorithm], algorithms: Map[DSLResource, String], effective: DSLTimeFrame ) diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLSemanticChecks.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLSemanticChecks.scala index c4a4fa4..9903cb5 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLSemanticChecks.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/DSLSemanticChecks.scala @@ -47,9 +47,9 @@ trait DSLSemanticChecks { /** * Functions to apply by default when checking consistency for - * [[gr.grnet.aquarium.logic.dsl.DSLPolicy]] resources. + * [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] resources. */ - val policyChecks = List[DSLPolicy => List[DSLConsistencyMsg]]( + val policyChecks = List[DSLAlgorithm => List[DSLConsistencyMsg]]( ) /** @@ -88,20 +88,20 @@ trait DSLSemanticChecks { /** * Top level consistency check functions. Applies all tests on all resources. */ - def check(creditPolicy: DSLCreditPolicy) : List[DSLConsistencyMsg] = { + def check(creditPolicy: DSLPolicy) : List[DSLConsistencyMsg] = { List[DSLConsistencyMsg]() ++ creditPolicy.pricelists.flatMap(p => check(p)) ++ - creditPolicy.policies.flatMap(p => check(p)) ++ + creditPolicy.algorithms.flatMap(p => check(p)) ++ creditPolicy.agreements.flatMap(a => check(a)) } /** Apply [[gr.grnet.aquarium.logic.dsl.DSLPriceList]] related checks on a pricelist */ def check(pl: DSLPriceList): List[DSLConsistencyMsg] = check(pl, priceListChecks) - /** Apply [[gr.grnet.aquarium.logic.dsl.DSLPolicy]] related checks on a policy */ - def check(pl: DSLPolicy): List[DSLConsistencyMsg] = check(pl, policyChecks) + /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] related checks on a algorithm */ + def check(pl: DSLAlgorithm): List[DSLConsistencyMsg] = check(pl, policyChecks) - /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] related checks on a policy */ + /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] related checks on a algorithm */ def check(pl: DSLAgreement): List[DSLConsistencyMsg] = check(pl, agreementChecks) /** Apply [[gr.grnet.aquarium.logic.dsl.DSLTimeframe]] related checks on a timeframe */ diff --git a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Vocabulary.scala b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Vocabulary.scala index 99e124c..c3b92aa 100644 --- a/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Vocabulary.scala +++ b/logic/src/main/scala/gr/grnet/aquarium/logic/accounting/dsl/Vocabulary.scala @@ -39,10 +39,10 @@ package gr.grnet.aquarium.logic.accounting.dsl * Vocabulary of terms for the DSL */ object Vocabulary { - val creditpolicy = "creditpolicy" + val aquariumpolicy = "aquariumpolicy" val resources = "resources" - val policies = "policies" - val policy = "policy" + val algorithms = "algorithms" + val algorithm = "algorithm" val pricelists = "pricelists" val pricelist = "pricelist" val agreements = "agreements" diff --git a/logic/src/test/resources/policy.yaml b/logic/src/test/resources/policy.yaml index accfc01..ee2c402 100644 --- a/logic/src/test/resources/policy.yaml +++ b/logic/src/test/resources/policy.yaml @@ -1,4 +1,4 @@ -creditpolicy: +aquariumpolicy: resources: - bandwidthup - bandwidthdown @@ -9,8 +9,8 @@ creditpolicy: - price - volume - policies: - - policy: + algorithms: + - algorithm: name: default bandwidthup: $price times $volume bandwidthdown: $price times $volume @@ -24,7 +24,7 @@ creditpolicy: end: "00 00 20 * *" from: 0 to: 124443 - - policy: + - algorithm: name: freedisk overrides: default diskspace: 0 @@ -63,16 +63,24 @@ creditpolicy: end: "00 20 * * *" from: 0 + creditplans: + - creditplan: + name: default + credits: 100 + at: "00 00 * * *" + effective: + from: 0 + agreements: - agreement: name: default - policy: default + algorithm: default pricelist: default - agreement: overrides: default name: scaledbandwidth - policy: + algorithm: bandwidthup: | if $volume gt 15 then $volume times $price diff --git a/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLTest.scala b/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLTest.scala index 2c7533f..ad7a70b 100644 --- a/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLTest.scala +++ b/logic/src/test/scala/gr/grnet/aquarium/logic/test/DSLTest.scala @@ -42,7 +42,7 @@ import gr.grnet.aquarium.util.TestMethods class DSLTest extends DSL with TestMethods { - var creditpolicy : DSLCreditPolicy = _ + var creditpolicy : DSLPolicy = _ def before = { creditpolicy = parse( @@ -54,17 +54,17 @@ class DSLTest extends DSL with TestMethods { @Test def testParsePolicies = { before - assertEquals(creditpolicy.policies.size, 2) - assertEquals(creditpolicy.policies(0).algorithms.size, + assertEquals(creditpolicy.algorithms.size, 2) + assertEquals(creditpolicy.algorithms(0).algorithms.size, creditpolicy.resources.size) - assertEquals(creditpolicy.policies(1).algorithms.size, + assertEquals(creditpolicy.algorithms(1).algorithms.size, creditpolicy.resources.size) val d = creditpolicy.findResource("diskspace").get assertNotNone(d) - assertNotSame(creditpolicy.policies(0).algorithms(d), - creditpolicy.policies(1).algorithms(d)) + assertNotSame(creditpolicy.algorithms(0).algorithms(d), + creditpolicy.algorithms(1).algorithms(d)) } @Test -- 1.7.10.4