70d23bcba87b68e327c39f96b8b1a38f34158cbb
[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-2019 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         for (GuardPolicy policy: clGuard.getGuards()) {
116             //
117             // We could have only one guard policy matching the id
118             //
119             if (policy.getId().equals(id)) {
120                 addConstraints(policy, constraints);
121                 return true;
122             }
123         }
124         return false;
125     }
126
127     private void addConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
128         for (Constraint cons: constraints) {
129             if (!cons.isValid()) {
130                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
131             }
132             if (policy.getLimit_constraints() == null) {
133                 policy.setLimit_constraints(new LinkedList<>());
134             }
135             policy.getLimit_constraints().add(cons);
136         }
137     }
138
139     @Override
140     public ControlLoopGuardBuilder removeLimitConstraint(String id, Constraint... constraints) throws BuilderException {
141         if (id == null) {
142             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
143         }
144         if (constraints == null) {
145             throw new BuilderException("Constraint much not be null");
146         }
147         if (!removeConstraints(id, constraints)) {
148             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
149         }
150         return this;
151     }
152
153     private boolean removeConstraints(String id, Constraint... constraints) throws BuilderException {
154         for (GuardPolicy policy: clGuard.getGuards()) {
155             //
156             // We could have only one guard policy matching the id
157             //
158             if (policy.getId().equals(id)) {
159                 removeConstraints(policy, constraints);
160                 return true;
161             }
162         }
163         return false;
164     }
165
166     private void removeConstraints(GuardPolicy policy, Constraint... constraints) throws BuilderException {
167         for (Constraint cons: constraints) {
168             if (!cons.isValid()) {
169                 throw new BuilderException("Invalid guard constraint - some required fields are missing");
170             }
171             boolean removed = policy.getLimit_constraints().remove(cons);
172             if (!removed) {
173                 throw new BuilderException("Unknown guard constraint: " + cons);
174             }
175         }
176     }
177
178     @Override
179     public ControlLoopGuardBuilder removeAllLimitConstraints(String id) throws BuilderException {
180         if (clGuard.getGuards() == null || clGuard.getGuards().isEmpty()) {
181             throw new BuilderException("No guard policies exist");
182         }
183         if (id == null) {
184             throw new BuilderException(THE_ID_OF_TARGET_GUARD_POLICY_MUST_NOT_BE_NULL);
185         }
186         boolean exist = false;
187         for (GuardPolicy policy: clGuard.getGuards()) {
188             if (policy.getId().equals(id)) {
189                 exist = true;
190                 policy.getLimit_constraints().clear();
191             }
192         }
193         if (!exist) {
194             throw new BuilderException(NO_EXISTING_GUARD_POLICY_MATCHING_THE_ID + id);
195         }
196         return this;
197     }
198
199
200     private class BuilderCompilerCallback implements ControlLoopCompilerCallback {
201
202         private ResultsImpl results = new ResultsImpl();
203
204         @Override
205         public boolean onWarning(String message) {
206             results.addMessage(new MessageImpl(message, MessageLevel.WARNING));
207             return false;
208         }
209
210         @Override
211         public boolean onError(String message) {
212             results.addMessage(new MessageImpl(message, MessageLevel.ERROR));
213             return false;
214         }
215     }
216
217     @Override
218     public ControlLoopGuard getControlLoopGuard() {
219         return new ControlLoopGuard(this.clGuard);
220     }
221
222
223     @Override
224     public Results buildSpecification() {
225         //
226         // Dump the specification
227         //
228         DumperOptions options = new DumperOptions();
229         options.setDefaultFlowStyle(FlowStyle.BLOCK);
230         options.setPrettyFlow(true);
231         Yaml yaml = new Yaml(options);
232         String dumpedYaml = yaml.dump(clGuard);
233         //
234         // This is our callback class for our compiler
235         //
236         BuilderCompilerCallback callback = new BuilderCompilerCallback();
237         //
238         // Compile it
239         //
240         try {
241             ControlLoopGuardCompiler.compile(clGuard, callback);
242         } catch (CompilerException e) {
243             logger.error("Build specification threw ", e);
244             callback.results.addMessage(new MessageImpl(e.getMessage(), MessageLevel.EXCEPTION));
245         }
246         //
247         // Save the spec
248         //
249         callback.results.setSpecification(dumpedYaml);
250         return callback.results;
251     }
252
253 }