[POLICY-117] Resolve the Policy Critical issues
[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.common.logging.flexlogger.FlexLogger;
25 import org.openecomp.policy.common.logging.flexlogger.Logger;
26 import org.openecomp.policy.controlloop.compiler.CompilerException;
27 import org.openecomp.policy.controlloop.compiler.ControlLoopCompilerCallback;
28 import org.openecomp.policy.controlloop.guard.compiler.ControlLoopGuardCompiler;
29 import org.openecomp.policy.controlloop.policy.builder.BuilderException;
30 import org.openecomp.policy.controlloop.policy.builder.MessageLevel;
31 import org.openecomp.policy.controlloop.policy.builder.Results;
32 import org.openecomp.policy.controlloop.policy.builder.impl.MessageImpl;
33 import org.openecomp.policy.controlloop.policy.builder.impl.ResultsImpl;
34 import org.openecomp.policy.controlloop.policy.guard.Constraint;
35 import org.openecomp.policy.controlloop.policy.guard.ControlLoopGuard;
36 import org.openecomp.policy.controlloop.policy.guard.Guard;
37 import org.openecomp.policy.controlloop.policy.guard.GuardPolicy;
38 import org.openecomp.policy.controlloop.policy.guard.builder.ControlLoopGuardBuilder;
39 import org.yaml.snakeyaml.DumperOptions;
40 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
41 import org.yaml.snakeyaml.Yaml;
42
43 public class ControlLoopGuardBuilderImpl implements ControlLoopGuardBuilder {
44         private static Logger logger = FlexLogger.getLogger(ControlLoopGuardBuilderImpl.class.getName());
45         private ControlLoopGuard cLGuard;
46         
47         public ControlLoopGuardBuilderImpl(Guard guard) {
48                 cLGuard = new ControlLoopGuard();
49                 cLGuard.setGuard(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.getGuards() == null) {
62                                 cLGuard.setGuards(new LinkedList<>());
63                         }
64                         cLGuard.getGuards().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.getGuards() == 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.getGuards().remove(policy);
82             if (!removed) {
83                 throw new BuilderException("Unknown guard policy: " + policy.getName());
84             }
85         }
86         return this;
87         }
88
89         @Override
90         public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
91                 cLGuard.getGuards().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                 if (!addLimitConstraints(id,constraints)) {
104                         throw new BuilderException("No existing guard policy matching the id: " + id);
105                 }
106                 return this;
107         }
108
109         private boolean addLimitConstraints(String id, Constraint... constraints) throws BuilderException {
110                 boolean exist = false;
111                 for (GuardPolicy policy: cLGuard.getGuards()) {
112                         //
113                         // We could have only one guard policy matching the id
114                         //
115                         if (policy.getId().equals(id)) {
116                                 exist = true;
117                                 for (Constraint cons: constraints) {
118                                         if (!cons.isValid()) {
119                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
120                                         }
121                                         if (policy.getLimit_constraints() == null) {
122                                                 policy.setLimit_constraints(new LinkedList<>());
123                                         }
124                                         policy.getLimit_constraints().add(cons);
125                                 }
126                                 break;
127                         }
128                 }
129                 return exist;
130         }
131
132         @Override
133         public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
134                 if (id == null) {
135                         throw new BuilderException("The id of target guard policy must not be null");
136                 }
137                 if (constraints == null) {
138                         throw new BuilderException("Constraint much not be null");
139                 }
140                 if (!removeConstraints(id, constraints)) {
141                         throw new BuilderException("No existing guard policy matching the id: " + id);
142                 }
143                 return this;
144         }
145
146         private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
147                 boolean exist = false;
148                 for (GuardPolicy policy: cLGuard.getGuards()) {
149                         //
150                         // We could have only one guard policy matching the id
151                         //
152                         if (policy.getId().equals(id)) {
153                                 exist = true;
154                                 for (Constraint cons: constraints) {
155                                         if (!cons.isValid()) {
156                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
157                                         }
158                                         boolean removed = policy.getLimit_constraints().remove(cons);
159                                         if (!removed) {
160                                                 throw new BuilderException("Unknown guard constraint: " + cons);
161                                         }
162                                 }
163                                 break;
164                         }
165                 }
166                 return exist;
167         }
168
169         @Override
170         public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
171                 if (cLGuard.getGuards() == null || cLGuard.getGuards().isEmpty()) {
172                         throw new BuilderException("No guard policies exist");
173                 } 
174                 if (id == null) {
175                         throw new BuilderException("The id of target guard policy must not be null");
176                 }
177                 boolean exist = false;
178                 for (GuardPolicy policy: cLGuard.getGuards()) {
179                         if (policy.getId().equals(id)) {
180                                 exist = true;
181                                 policy.getLimit_constraints().clear();
182                         }
183                 }
184                 if (!exist) {
185                         throw new BuilderException("No existing guard policy matching the id: " + id);
186                 }
187                 return this;
188         }
189
190         
191         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
192
193                 private ResultsImpl results = new ResultsImpl();
194                 
195                 @Override
196                 public boolean onWarning(String message) {
197                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
198                         return false;
199                 }
200
201                 @Override
202                 public boolean onError(String message) {
203                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
204                         return false;
205                 }
206         }
207         
208         @Override
209         public ControlLoopGuard getControlLoopGuard() {
210                 return new ControlLoopGuard(this.cLGuard);
211         }       
212         
213         
214         @Override
215         public Results buildSpecification() {
216                 //
217                 // Dump the specification
218                 //
219                 DumperOptions options = new DumperOptions();
220                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
221                 options.setPrettyFlow(true);
222                 Yaml yaml = new Yaml(options);
223                 String dumpedYaml = yaml.dump(cLGuard);
224                 //
225                 // This is our callback class for our compiler
226                 //
227                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
228                 //
229                 // Compile it
230                 //
231                 try {
232                         ControlLoopGuardCompiler.compile(cLGuard, callback);
233                 } catch (CompilerException e) {
234                         logger.error(e.getMessage() + e);
235                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
236                 }
237                 //
238                 // Save the spec
239                 //
240                 callback.results.setSpecification(dumpedYaml);
241                 return callback.results;
242         }
243
244 }