First cut of the new policy configuration system
[aquarium] / src / main / scala / gr / grnet / aquarium / logic / accounting / dsl / DSLSemanticChecks.scala
1 /*
2  * Copyright 2011-2012 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35
36 package gr.grnet.aquarium.logic.accounting.dsl
37
38 import java.util.Date
39 import scala.collection._
40 //TODO: REMOVE THIS CLASS
41 /**
42  * A semantic checker for the Aquarium accounting DSL. 
43  *
44  * @author Georgios Gousios <gousiosg@gmail.com>
45  */
46 /*trait DSLSemanticChecks {
47
48   /**
49    * Functions to apply by default when checking consistency for
50    * [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] resources.
51    */
52   val policyChecks = List[DSLAlgorithm => List[DSLConsistencyMsg]](
53   )
54
55   /**
56    * Functions to apply by default when checking consistency for
57    * [[gr.grnet.aquarium.logic.dsl.DSLPriceList]] resources.
58    */
59   val priceListChecks = List[DSLPriceList => List[DSLConsistencyMsg]](
60   )
61
62   /**
63    * Functions to apply by default when checking consistency for
64    * [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] resources.
65    */
66   val agreementChecks = List[DSLAgreement => List[DSLConsistencyMsg]](
67   )
68
69   /**
70    * Functions to apply by default when checking consistency for
71    * [[gr.grnet.aquarium.logic.dsl.DSLTimeFrame]] resources.
72    */
73   val timeFrameChecks = List[DSLTimeFrame => List[DSLConsistencyMsg]](
74     checkTimeFrameFromTo,
75     checkTimeNotInitialized,
76     checkRepeatHoles
77   )
78
79   /**
80    * Apply a list of consistency checking functions to a DSL entity and
81    * report results.
82    */
83   def check[A](resource: A, checks: List[A => List[DSLConsistencyMsg]]) :
84     List[DSLConsistencyMsg] = {
85     checks.map(f => f(resource)).flatten.toList
86   }
87
88   /**
89    * Top level consistency check functions. Applies all tests on all resources.
90    */
91   def check(creditPolicy: DSLPolicy) : List[DSLConsistencyMsg] = {
92     List[DSLConsistencyMsg]() ++
93       creditPolicy.pricelists.flatMap(p => check(p)) ++
94       creditPolicy.algorithms.flatMap(p => check(p)) ++
95       creditPolicy.agreements.flatMap(a => check(a))
96   }
97
98   /** Apply [[gr.grnet.aquarium.logic.dsl.DSLPriceList]] related checks on a pricelist */
99   def check(pl: DSLPriceList): List[DSLConsistencyMsg] = check(pl, priceListChecks)
100
101   /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAlgorithm]] related checks on a algorithm */
102   def check(pl: DSLAlgorithm): List[DSLConsistencyMsg] = check(pl, policyChecks)
103
104   /** Apply [[gr.grnet.aquarium.logic.dsl.DSLAgreement]] related checks on a algorithm */
105   def check(pl: DSLAgreement): List[DSLConsistencyMsg] = check(pl, agreementChecks)
106
107   /** Apply [[gr.grnet.aquarium.logic.dsl.DSLTimeframe]] related checks on a timeframe */
108   def check(time: DSLTimeFrame): List[DSLConsistencyMsg] = check(time, timeFrameChecks)
109
110   /* -- Checker functions -- */
111   private def checkTimeFrameFromTo(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
112     if (time.from.after(time.to.getOrElse(new Date(0))))
113       List(DSLConsistencyError("Validity period %s ends before starting".format(time)))
114     else
115       List()
116   }
117
118   private def checkTimeNotInitialized(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
119     if (time.repeat.isEmpty)
120       return List()
121
122     val result = new mutable.ListBuffer[DSLConsistencyMsg]
123
124     time.repeat.foreach {
125       r =>
126         r.start.foreach {
127           r => if (r.hour == -1 || r.min == -1)
128             result += DSLConsistencyError(
129               "Hours and mins must always be initialized: %s".format(time))
130         }
131
132         r.end.foreach {
133           e =>  if (e.hour == -1 || e.min == -1)
134             result += DSLConsistencyError(
135               "Hours and mins must always be initialized: %s".format(time))
136         }
137     }
138     result.toList
139   }
140
141   private def checkRepeatHoles(time: DSLTimeFrame) : List[DSLConsistencyMsg] = {
142     val repeat = time.repeat
143
144     List()
145   }
146 }
147
148 sealed trait DSLConsistencyMsg
149 case class DSLConsistencyWarn(warn: String) extends DSLConsistencyMsg
150 case class DSLConsistencyError(err: String) extends DSLConsistencyMsg*/