Revision 07f63cb1 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 | ||
---|---|---|
1 | 1 |
package gr.grnet.aquarium.logic.accounting.dsl |
2 | 2 |
|
3 | 3 |
import scala.collection.JavaConversions._ |
4 |
import java.util.Date |
|
5 | 4 |
import com.kenai.crontabparser.impl.CronTabParserBridge |
6 | 5 |
import java.io.{InputStreamReader, InputStream} |
7 | 6 |
import gr.grnet.aquarium.util.Loggable |
8 | 7 |
import gr.grnet.aquarium.util.yaml._ |
8 |
import java.util.Date |
|
9 | 9 |
|
10 | 10 |
/** |
11 |
* |
|
11 |
* A parser and semantic analyser for credit DSL files |
|
12 |
* |
|
12 | 13 |
* @author Georgios Gousios <gousiosg@gmail.com> |
13 | 14 |
*/ |
14 | 15 |
object DSL extends Loggable { |
... | ... | |
43 | 44 |
|
44 | 45 |
logger.debug("Policies: %s".format(policies)) |
45 | 46 |
|
46 |
DSLCreditPolicy(List(), List(), List(), List())
|
|
47 |
DSLCreditPolicy(policies, List(), resources, List())
|
|
47 | 48 |
} |
48 | 49 |
|
49 | 50 |
/** Parse top level resources declarations */ |
... | ... | |
63 | 64 |
resources: List[DSLResource], |
64 | 65 |
results: List[DSLPolicy]): List[DSLPolicy] = { |
65 | 66 |
|
67 |
policies.head match { |
|
68 |
case YAMLEmptyNode => return List() |
|
69 |
case _ => |
|
70 |
} |
|
71 |
|
|
72 |
val policy = constructPolicy(policies.head.asInstanceOf[YAMLMapNode], resources) |
|
66 | 73 |
val supr = policies.head / Vocabulary.overrides |
67 | 74 |
|
68 |
val result = constructPolicy(policies.head.asInstanceOf[YAMLMapNode], resources) |
|
69 |
val tmpresults = results ++ List(result) |
|
70 |
List(result) ++ parsePolicies(policies.tail, resources, tmpresults) |
|
75 |
val tmpresults = results ++ List(policy) |
|
76 |
List(policy) ++ parsePolicies(policies.tail, resources, tmpresults) |
|
71 | 77 |
} |
72 | 78 |
|
73 | 79 |
/** Construct a policy object from a yaml node*/ |
... | ... | |
101 | 107 |
mergeMaps(algos)((v1: String, v2: String) => v1), timeframe) |
102 | 108 |
} |
103 | 109 |
|
104 |
/** Merge two policies, field by field */ |
|
105 |
def mergePolicy(policy: DSLPolicy, onto: DSLPolicy) : DSLPolicy = { |
|
106 |
DSLPolicy(onto.name, onto.overrides, |
|
107 |
mergeMaps(policy.algorithms, onto.algorithms), null) |
|
108 |
} |
|
109 |
|
|
110 | 110 |
/** Parse a timeframe declaration */ |
111 | 111 |
def parseTimeFrame(timeframe: YAMLMapNode): DSLTimeFrame = { |
112 | 112 |
val from = timeframe / Vocabulary.from match { |
... | ... | |
194 | 194 |
).flatten.toList |
195 | 195 |
} |
196 | 196 |
|
197 |
/** Merge two policies, field by field */ |
|
198 |
def mergePolicy(policy: DSLPolicy, onto: DSLPolicy): DSLPolicy = { |
|
199 |
DSLPolicy(onto.name, onto.overrides, |
|
200 |
mergeMaps(policy.algorithms, onto.algorithms), |
|
201 |
mergeTimeFrames(policy.effective, onto.effective)) |
|
202 |
} |
|
203 |
|
|
204 |
/** Merge two timeframes */ |
|
205 |
def mergeTimeFrames(timeframe: DSLTimeFrame, |
|
206 |
onto: DSLTimeFrame) : DSLTimeFrame = { |
|
207 |
DSLTimeFrame(new Date(), new Date(), Some(List())) |
|
208 |
} |
|
209 |
|
|
210 |
/** Merge input maps on a field by field basis. In case of duplicate keys |
|
211 |
* values from the first map are prefered. |
|
212 |
*/ |
|
197 | 213 |
def mergeMaps[A, B](a: Map[A, B], b: Map[A, B]): Map[A, B] = { |
198 | 214 |
a ++ b.map{ case (k,v) => k -> (a.getOrElse(k,v)) } |
199 | 215 |
} |
200 | 216 |
|
217 |
/** Merge input maps on a field by field basis. In case of duplicate keys, |
|
218 |
* the provided function is used to determine which value to keep in the |
|
219 |
* merged map. |
|
220 |
*/ |
|
201 | 221 |
def mergeMaps[A, B](ms: List[Map[A, B]])(f: (B, B) => B): Map[A, B] = |
202 | 222 |
(Map[A, B]() /: (for (m <- ms; kv <- m) yield kv)) { |
203 | 223 |
(a, kv) => |
... | ... | |
258 | 278 |
mon: Int |
259 | 279 |
) |
260 | 280 |
|
261 |
class DSLParseException(msg: String) extends Exception(msg) |
|
281 |
class DSLParseException(msg: String) extends Exception(msg) |
Also available in: Unified diff