[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / main / java / org / onap / policy / controlloop / policy / guard / builder / impl / ControlLoopGuardBuilderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.policy.guard.builder.impl;
22
23 import java.util.LinkedList;
24
25 import org.yaml.snakeyaml.DumperOptions;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
28
29 import org.onap.policy.controlloop.compiler.CompilerException;
30 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
31 import org.onap.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
32 import org.onap.policy.controlloop.policy.builder.BuilderException;
33 import org.onap.policy.controlloop.policy.builder.MessageLevel;
34 import org.onap.policy.controlloop.policy.builder.Results;
35 import org.onap.policy.controlloop.policy.builder.impl.MessageImpl;
36 import org.onap.policy.controlloop.policy.builder.impl.ResultsImpl;
37 import org.onap.policy.controlloop.policy.guard.Constraint;
38 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
39 import org.onap.policy.controlloop.policy.guard.Guard;
40 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
41 import org.onap.policy.controlloop.poligy.guard.builder.ControlLoopGuardBuilder;
42
43 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
44
45         private ControlLoopGuard CLGuard;
46         
47         public ControlLoopGuardBuilderImpl(Guard guard) {
48                 CLGuard = new ControlLoopGuard();
49                 CLGuard.guard = guard;
50         }
51         
52         @Override
53         public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
54                 if (policies == null) {
55                         throw new BuilderException("GuardPolicy must not be null");
56                 }
57                 for (GuardPolicy policy : policies) {
58                         if (!policy.isValid()) {
59                                 throw new BuilderException("Invalid guard policy - some required fields are missing");
60                         }
61                         if (CLGuard.guards == null) {
62                                 CLGuard.guards = new LinkedList<GuardPolicy>();
63                         }
64                         CLGuard.guards.add(policy);
65                 }
66                 return this;
67         }
68
69         @Override
70         public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
71                 if (policies == null) {
72             throw new BuilderException("GuardPolicy must not be null");
73         }
74         if (CLGuard.guards == null) {
75             throw new BuilderException("No existing guard policies to remove");
76         }
77         for (GuardPolicy policy : policies) {
78                 if (!policy.isValid()) {
79                                 throw new BuilderException("Invalid guard policy - some required fields are missing");
80                         }
81             boolean removed = CLGuard.guards.remove(policy);    
82             if (!removed) {
83                 throw new BuilderException("Unknown guard policy: " + policy.name);
84             }
85         }
86         return this;
87         }
88
89         @Override
90         public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
91                 CLGuard.guards.clear();
92         return this;
93         }
94
95         @Override
96         public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
97                 if (id == null) {
98                         throw new BuilderException("The id of target guard policy must not be null");
99                 }
100                 if (constraints == null) {
101                         throw new BuilderException("Constraint much not be null");
102                 }
103                 boolean exist = false;
104                 for (GuardPolicy policy: CLGuard.guards) {
105                         //
106                         // We could have only one guard policy matching the id
107                         //
108                         if (policy.id.equals(id)) {
109                                 exist = true;
110                                 for (Constraint cons: constraints) {
111                                         if (!cons.isValid()) {
112                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
113                                         }
114                                         if (policy.limit_constraints == null) {
115                                                 policy.limit_constraints = new LinkedList<Constraint>();
116                                         }
117                                         policy.limit_constraints.add(cons);
118                                 }
119                                 break;
120                         }
121                 }
122                 if (exist == false) {
123                         throw new BuilderException("No existing guard policy matching the id: " + id);
124                 }
125                 return this;
126         }
127
128         @Override
129         public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
130                 if (id == null) {
131                         throw new BuilderException("The id of target guard policy must not be null");
132                 }
133                 if (constraints == null) {
134                         throw new BuilderException("Constraint much not be null");
135                 }
136                 boolean exist = false;
137                 for (GuardPolicy policy: CLGuard.guards) {
138                         //
139                         // We could have only one guard policy matching the id
140                         //
141                         if (policy.id.equals(id)) {
142                                 exist = true;
143                                 for (Constraint cons: constraints) {
144                                         if (!cons.isValid()) {
145                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
146                                         }
147                                         boolean removed = policy.limit_constraints.remove(cons);
148                                         if (!removed) {
149                                                 throw new BuilderException("Unknown guard constraint: " + cons);
150                                         }
151                                 }
152                                 break;
153                         }
154                 }
155                 if (exist == false) {
156                         throw new BuilderException("No existing guard policy matching the id: " + id);
157                 }
158                 return this;
159         }
160
161         @Override
162         public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
163                 if (CLGuard.guards == null || CLGuard.guards.isEmpty()) {
164                         throw new BuilderException("No guard policies exist");
165                 } 
166                 if (id == null) {
167                         throw new BuilderException("The id of target guard policy must not be null");
168                 }
169                 boolean exist = false;
170                 for (GuardPolicy policy: CLGuard.guards) {
171                         if (policy.id.equals(id)) {
172                                 exist = true;
173                                 policy.limit_constraints.clear();
174                         }
175                 }
176                 if (exist == false) {
177                         throw new BuilderException("No existing guard policy matching the id: " + id);
178                 }
179                 return this;
180         }
181
182         
183         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
184
185                 public ResultsImpl results = new ResultsImpl();
186                 
187                 @Override
188                 public boolean onWarning(String message) {
189                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
190                         return false;
191                 }
192
193                 @Override
194                 public boolean onError(String message) {
195                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
196                         return false;
197                 }
198         }
199         
200         @Override
201         public ControlLoopGuard getControlLoopGuard() {
202                 ControlLoopGuard guard = new ControlLoopGuard(this.CLGuard);
203                 return guard;
204         }       
205         
206         
207         @Override
208         public Results buildSpecification() {
209                 //
210                 // Dump the specification
211                 //
212                 DumperOptions options = new DumperOptions();
213                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
214                 options.setPrettyFlow(true);
215                 Yaml yaml = new Yaml(options);
216                 String dumpedYaml = yaml.dump(CLGuard);
217                 //
218                 // This is our callback class for our compiler
219                 //
220                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
221                 //
222                 // Compile it
223                 //
224                 try {
225                         ControlLoopGuardCompiler.compile(CLGuard, callback);
226                 } catch (CompilerException e) {
227                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
228                 }
229                 //
230                 // Save the spec
231                 //
232                 callback.results.setSpecification(dumpedYaml);
233                 return callback.results;
234         }
235
236 }