Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / ECOMP-ControlloopPolicy / src / main / java / org / openecomp / policy / controlloop / policy / guard / builder / impl / ControlLoopGuardBuilderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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 package org.openecomp.policy.controlloop.policy.guard.builder.impl;
21
22 import java.util.LinkedList;
23
24 import org.openecomp.policy.controlloop.compiler.CompilerException;
25 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
26 import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
27 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
28 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
29 import org.openecomp.policy.controlloop.policy.builder.Results;
30 import org.openecomp.policy.controlloop.policy.builder.impl.MessageImpl;
31 import org.openecomp.policy.controlloop.policy.builder.impl.ResultsImpl;
32 import org.openecomp.policy.controlloop.policy.guard.Constraint;
33 import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
34 import org.openecomp.policy.controlloop.policy.guard.Guard;
35 import org.openecomp.policy.controlloop.policy.guard.GuardPolicy;
36 import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
37 import org.yaml.snakeyaml.DumperOptions;
38 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
39 import org.yaml.snakeyaml.Yaml;
40
41 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
42
43         private ControlLoopGuard cLGuard;
44         
45         public ControlLoopGuardBuilderImpl(Guard guard) {
46                 cLGuard = new ControlLoopGuard();
47                 cLGuard.setGuard(guard);
48         }
49         
50         @Override
51         public ControlLoopGuardBuilder addGuardPolicy(GuardPolicy... policies) throws BuilderException {
52                 if (policies == null) {
53                         throw new BuilderException("GuardPolicy must not be null");
54                 }
55                 for (GuardPolicy policy : policies) {
56                         if (!policy.isValid()) {
57                                 throw new BuilderException("Invalid guard policy - some required fields are missing");
58                         }
59                         if (cLGuard.getGuards() == null) {
60                                 cLGuard.setGuards(new LinkedList<>());
61                         }
62                         cLGuard.getGuards().add(policy);
63                 }
64                 return this;
65         }
66
67         @Override
68         public ControlLoopGuardBuilder removeGuardPolicy(GuardPolicy... policies) throws BuilderException {
69                 if (policies == null) {
70             throw new BuilderException("GuardPolicy must not be null");
71         }
72         if (cLGuard.getGuards() == null) {
73             throw new BuilderException("No existing guard policies to remove");
74         }
75         for (GuardPolicy policy : policies) {
76                 if (!policy.isValid()) {
77                                 throw new BuilderException("Invalid guard policy - some required fields are missing");
78                         }
79             boolean removed = cLGuard.getGuards().remove(policy);
80             if (!removed) {
81                 throw new BuilderException("Unknown guard policy: " + policy.getName());
82             }
83         }
84         return this;
85         }
86
87         @Override
88         public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
89                 cLGuard.getGuards().clear();
90         return this;
91         }
92
93         @Override
94         public ControlLoopGuardBuilder addLimitConstraint(String id, Constraint... constraints) throws BuilderException {
95                 if (id == null) {
96                         throw new BuilderException("The id of target guard policy must not be null");
97                 }
98                 if (constraints == null) {
99                         throw new BuilderException("Constraint much not be null");
100                 }
101                 if (!addLimitConstraints(id,constraints)) {
102                         throw new BuilderException("No existing guard policy matching the id: " + id);
103                 }
104                 return this;
105         }
106
107         private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
108                 boolean exist = false;
109                 for (GuardPolicy policy: cLGuard.getGuards()) {
110                         //
111                         // We could have only one guard policy matching the id
112                         //
113                         if (policy.getId().equals(id)) {
114                                 exist = true;
115                                 for (Constraint cons: constraints) {
116                                         if (!cons.isValid()) {
117                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
118                                         }
119                                         if (policy.getLimit_constraints() == null) {
120                                                 policy.setLimit_constraints(new LinkedList<>());
121                                         }
122                                         policy.getLimit_constraints().add(cons);
123                                 }
124                                 break;
125                         }
126                 }
127                 return exist;
128         }
129
130         @Override
131         public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
132                 if (id == null) {
133                         throw new BuilderException("The id of target guard policy must not be null");
134                 }
135                 if (constraints == null) {
136                         throw new BuilderException("Constraint much not be null");
137                 }
138                 if (!removeConstraints(id, constraints)) {
139                         throw new BuilderException("No existing guard policy matching the id: " + id);
140                 }
141                 return this;
142         }
143
144         private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
145                 boolean exist = false;
146                 for (GuardPolicy policy: cLGuard.getGuards()) {
147                         //
148                         // We could have only one guard policy matching the id
149                         //
150                         if (policy.getId().equals(id)) {
151                                 exist = true;
152                                 for (Constraint cons: constraints) {
153                                         if (!cons.isValid()) {
154                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
155                                         }
156                                         boolean removed = policy.getLimit_constraints().remove(cons);
157                                         if (!removed) {
158                                                 throw new BuilderException("Unknown guard constraint: " + cons);
159                                         }
160                                 }
161                                 break;
162                         }
163                 }
164                 return exist;
165         }
166
167         @Override
168         public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
169                 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
170                         throw new BuilderException("No guard policies exist");
171                 } 
172                 if (id == null) {
173                         throw new BuilderException("The id of target guard policy must not be null");
174                 }
175                 boolean exist = false;
176                 for (GuardPolicy policy: cLGuard.getGuards()) {
177                         if (policy.getId().equals(id)) {
178                                 exist = true;
179                                 policy.getLimit_constraints().clear();
180                         }
181                 }
182                 if (!exist) {
183                         throw new BuilderException("No existing guard policy matching the id: " + id);
184                 }
185                 return this;
186         }
187
188         
189         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
190
191                 private ResultsImpl results = new ResultsImpl();
192                 
193                 @Override
194                 public boolean onWarning(String message) {
195                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
196                         return false;
197                 }
198
199                 @Override
200                 public boolean onError(String message) {
201                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
202                         return false;
203                 }
204         }
205         
206         @Override
207         public ControlLoopGuard getControlLoopGuard() {
208                 return new ControlLoopGuard(this.cLGuard);
209         }       
210         
211         
212         @Override
213         public Results buildSpecification() {
214                 //
215                 // Dump the specification
216                 //
217                 DumperOptions options = new DumperOptions();
218                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
219                 options.setPrettyFlow(true);
220                 Yaml yaml = new Yaml(options);
221                 String dumpedYaml = yaml.dump(cLGuard);
222                 //
223                 // This is our callback class for our compiler
224                 //
225                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
226                 //
227                 // Compile it
228                 //
229                 try {
230                         ControlLoopGuardCompiler.compile(cLGuard, callback);
231                 } catch (CompilerException e) {
232                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
233                 }
234                 //
235                 // Save the spec
236                 //
237                 callback.results.setSpecification(dumpedYaml);
238                 return callback.results;
239         }
240
241 }