From 28a85ac0ca59357fb54b0c3bbe1960faa0155c2e Mon Sep 17 00:00:00 2001 From: Georgios Gousios Date: Tue, 31 Jan 2012 13:13:36 +0200 Subject: [PATCH] Update available policies when on configuration file update --- .../grnet/aquarium/logic/accounting/Policy.scala | 27 +++++++++++++++----- .../aquarium/store/mongodb/MongoDBStore.scala | 8 ++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/main/scala/gr/grnet/aquarium/logic/accounting/Policy.scala b/src/main/scala/gr/grnet/aquarium/logic/accounting/Policy.scala index a130a6b..69eb007 100644 --- a/src/main/scala/gr/grnet/aquarium/logic/accounting/Policy.scala +++ b/src/main/scala/gr/grnet/aquarium/logic/accounting/Policy.scala @@ -37,10 +37,12 @@ package gr.grnet.aquarium.logic.accounting import dsl.{Timeslot, DSLPolicy, DSL} import gr.grnet.aquarium.Configurator._ -import gr.grnet.aquarium.util.Loggable import java.io.{InputStream, FileInputStream, File} import java.util.Date import com.ckkloverdos.maybe.{Maybe, Just} +import gr.grnet.aquarium.util.date.TimeHelpers +import gr.grnet.aquarium.logic.events.PolicyEntry +import gr.grnet.aquarium.util.{CryptoUtils, Loggable} /** * Searches for and loads the applicable accounting policy @@ -105,12 +107,13 @@ object Policy extends DSL with Loggable { val policies = MasterConfigurator.policyEventStore.loadPolicies(0) //2. Check whether policy file has been updated - val latestPolicyChange = policies.last.validFrom + val latestPolicyChange = if (policies.isEmpty) 0 else policies.last.validFrom val policyf = policyFile var updated = false if (policyf.exists) { if (policyf.lastModified > latestPolicyChange) { + logger.info("Policy changed since last check, reloading") updated = true } else { logger.info("Policy not changed since last check") @@ -120,12 +123,24 @@ object Policy extends DSL with Loggable { "using stored policy information".format(policyf.getAbsolutePath)) } - if (updated) { - //read from db etc + val toAdd = updated match { + case true => + val ts = TimeHelpers.nowMillis + val toUpdate = policies.last.copy(validTo = ts) + val parsedNew = loadPolicyFromFile(policyf) + val yaml = parsedNew.toYAML + val newPolicy = PolicyEntry(CryptoUtils.sha1(yaml), ts, ts, yaml, ts + 1, -1) + + MasterConfigurator.policyEventStore.updatePolicy(toUpdate) + MasterConfigurator.policyEventStore.storePolicy(newPolicy) + + List(toUpdate, newPolicy) + case false => List() } - policies.foldLeft(Map[Timeslot, DSLPolicy]())( + policies.init.++(toAdd).foldLeft(Map[Timeslot, DSLPolicy]()){ (acc, p) => - acc ++ Map(Timeslot(new Date(p.validFrom), new Date(p.validTo)) -> parse(p.policyYAML))) + acc ++ Map(Timeslot(new Date(p.validFrom), new Date(p.validTo)) -> parse(p.policyYAML)) + } } } \ No newline at end of file diff --git a/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala b/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala index ad60547..16721ed 100644 --- a/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala +++ b/src/main/scala/gr/grnet/aquarium/store/mongodb/MongoDBStore.scala @@ -313,6 +313,14 @@ class MongoDBStore( } def storePolicy(policy: PolicyEntry): Maybe[RecordID] = MongoDBStore.storeAquariumEvent(policy, policies) + + + def updatePolicy(policy: PolicyEntry) = { + //Find the entry + val query = new BasicDBObject(PolicyEntry.JsonNames.id, policy.id) + val policyObject = MongoDBStore.jsonSupportToDBObject(policy) + policies.update(query, policyObject, true, false) + } //-PolicyStore } -- 1.7.10.4