b119620e312e549c0af7c4622480e6241f30824c
[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.guard = 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.guards == null) {
60                                 CLGuard.guards = new LinkedList<GuardPolicy>();
61                         }
62                         CLGuard.guards.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.guards == 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.guards.remove(policy);    
80             if (!removed) {
81                 throw new BuilderException("Unknown guard policy: " + policy.name);
82             }
83         }
84         return this;
85         }
86
87         @Override
88         public ControlLoopGuardBuilder removeAllGuardPolicies() throws BuilderException {
89                 CLGuard.guards.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                 boolean exist = false;
102                 for (GuardPolicy policy: CLGuard.guards) {
103                         //
104                         // We could have only one guard policy matching the id
105                         //
106                         if (policy.id.equals(id)) {
107                                 exist = true;
108                                 for (Constraint cons: constraints) {
109                                         if (!cons.isValid()) {
110                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
111                                         }
112                                         if (policy.limit_constraints == null) {
113                                                 policy.limit_constraints = new LinkedList<Constraint>();
114                                         }
115                                         policy.limit_constraints.add(cons);
116                                 }
117                                 break;
118                         }
119                 }
120                 if (exist == false) {
121                         throw new BuilderException("No existing guard policy matching the id: " + id);
122                 }
123                 return this;
124         }
125
126         @Override
127         public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
128                 if (id == null) {
129                         throw new BuilderException("The id of target guard policy must not be null");
130                 }
131                 if (constraints == null) {
132                         throw new BuilderException("Constraint much not be null");
133                 }
134                 boolean exist = false;
135                 for (GuardPolicy policy: CLGuard.guards) {
136                         //
137                         // We could have only one guard policy matching the id
138                         //
139                         if (policy.id.equals(id)) {
140                                 exist = true;
141                                 for (Constraint cons: constraints) {
142                                         if (!cons.isValid()) {
143                                                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
144                                         }
145                                         boolean removed = policy.limit_constraints.remove(cons);
146                                         if (!removed) {
147                                                 throw new BuilderException("Unknown guard constraint: " + cons);
148                                         }
149                                 }
150                                 break;
151                         }
152                 }
153                 if (exist == false) {
154                         throw new BuilderException("No existing guard policy matching the id: " + id);
155                 }
156                 return this;
157         }
158
159         @Override
160         public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
161                 if (CLGuard.guards == null || CLGuard.guards.isEmpty()) {
162                         throw new BuilderException("No guard policies exist");
163                 } 
164                 if (id == null) {
165                         throw new BuilderException("The id of target guard policy must not be null");
166                 }
167                 boolean exist = false;
168                 for (GuardPolicy policy: CLGuard.guards) {
169                         if (policy.id.equals(id)) {
170                                 exist = true;
171                                 policy.limit_constraints.clear();
172                         }
173                 }
174                 if (exist == false) {
175                         throw new BuilderException("No existing guard policy matching the id: " + id);
176                 }
177                 return this;
178         }
179
180         
181         private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
182
183                 public ResultsImpl results = new ResultsImpl();
184                 
185                 @Override
186                 public boolean onWarning(String message) {
187                         results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
188                         return false;
189                 }
190
191                 @Override
192                 public boolean onError(String message) {
193                         results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
194                         return false;
195                 }
196         }
197         
198         @Override
199         public ControlLoopGuard getControlLoopGuard() {
200                 ControlLoopGuard guard = new ControlLoopGuard(this.CLGuard);
201                 return guard;
202         }       
203         
204         
205         @Override
206         public Results buildSpecification() {
207                 //
208                 // Dump the specification
209                 //
210                 DumperOptions options = new DumperOptions();
211                 options.setDefaultFlowStyle(FlowStyle.BLOCK);
212                 options.setPrettyFlow(true);
213                 Yaml yaml = new Yaml(options);
214                 String dumpedYaml = yaml.dump(CLGuard);
215                 //
216                 // This is our callback class for our compiler
217                 //
218                 BuilderCompilerCallback callback = new BuilderCompilerCallback();
219                 //
220                 // Compile it
221                 //
222                 try {
223                         ControlLoopGuardCompiler.compile(CLGuard, callback);
224                 } catch (CompilerException e) {
225                         callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
226                 }
227                 //
228                 // Save the spec
229                 //
230                 callback.results.setSpecification(dumpedYaml);
231                 return callback.results;
232         }
233
234 }