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