435963a9edfdc488b43b1f96e1738c737d83b937
[policy/models.git] / models-interactions / model-yaml / src / main / java / org / onap / policy / controlloop / guard / compiler / ControlLoopGuardCompiler.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.guard.compiler;
23
24 import java.io.InputStream;
25
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.onap.policy.controlloop.compiler.CompilerException;
31 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
32 import org.onap.policy.controlloop.policy.guard.Constraint;
33 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
34 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.constructor.Constructor;
37
38 public class ControlLoopGuardCompiler {
39     
40     private static final String GUARD_POLICIES_SHOULD_NOT_BE_NULL = "Guard policies should not be null";
41     private static final String GUARD_POLICY = "Guard policy ";
42
43     private ControlLoopGuardCompiler(){
44         // Private Constructor 
45     }
46     
47     /**
48      * Compile the control loop guard.
49      * 
50      * @param clGuard the guard
51      * @param callback callback routine
52      * @return the guard object
53      * @throws CompilerException compilation exception
54      */
55     public static ControlLoopGuard compile(ControlLoopGuard clGuard, 
56                     ControlLoopCompilerCallback callback) throws CompilerException {
57         //
58         // Ensure ControlLoopGuard has at least one guard policies
59         //
60         validateControlLoopGuard(clGuard, callback);
61         //
62         // Ensure each guard policy has at least one constraints and all guard policies are unique
63         //
64         validateGuardPolicies(clGuard.getGuards(), callback);
65         //
66         // Ensure constraints for each guard policy are unique
67         //
68         validateConstraints(clGuard.getGuards(), callback);
69         
70         return clGuard;
71     }
72     
73     /**
74      * Compile the control loop guard.
75      * 
76      * @param yamlSpecification yaml specification as a stream
77      * @param callback callback method
78      * @return guard object
79      * @throws CompilerException throws compile exception
80      */
81     public static ControlLoopGuard  compile(InputStream yamlSpecification, 
82                     ControlLoopCompilerCallback callback) throws CompilerException {
83         Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
84         Object obj = yaml.load(yamlSpecification);
85         if (obj == null) {
86             throw new CompilerException("Could not parse yaml specification.");
87         }
88         if (! (obj instanceof ControlLoopGuard)) {
89             throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
90         }
91         return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
92     }
93     
94     private static void validateControlLoopGuard(ControlLoopGuard clGuard, 
95                     ControlLoopCompilerCallback callback) throws CompilerException {
96         if (clGuard == null) {
97             if (callback != null) {
98                 callback.onError("ControlLoop Guard cannot be null");
99             }
100             throw new CompilerException("ControlLoop Guard cannot be null");
101         }
102         if (clGuard.getGuard() == null && callback != null) {
103             callback.onError("Guard version cannot be null");
104         }
105         if (clGuard.getGuards() == null) {
106             if (callback != null) {
107                 callback.onError("ControlLoop Guard should have at least one guard policies");
108             }
109         } else if (clGuard.getGuards().isEmpty() && callback != null) {
110             callback.onError("ControlLoop Guard should have at least one guard policies");
111         }
112     }
113     
114     private static void validateGuardPolicies(List<GuardPolicy> policies, 
115                     ControlLoopCompilerCallback callback) throws CompilerException {
116         if (policies == null) {
117             if (callback != null) {
118                 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
119             }
120             throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
121         }
122         //
123         // Ensure all guard policies are unique
124         //
125         Set<GuardPolicy> newSet = new HashSet<>(policies);
126         if (newSet.size() != policies.size() && callback != null) {
127             callback.onWarning("There are duplicate guard policies");
128         }
129         //
130         // Ensure each guard policy has at least one constraints
131         //
132         for (GuardPolicy policy : policies) {
133             if (policy.getLimit_constraints() == null || policy.getLimit_constraints().isEmpty()) {
134                 if (callback != null) {
135                     callback.onError(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
136                 }
137                 throw new CompilerException(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
138             }
139         }
140     }
141     
142     private static void validateConstraints(List<GuardPolicy> policies, 
143                     ControlLoopCompilerCallback callback) throws CompilerException {
144         if (policies == null) {
145             if (callback != null) {
146                 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
147             }
148             throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
149         }
150         for (GuardPolicy policy : policies) {
151             Set<Constraint> newSet = new HashSet<>(policy.getLimit_constraints());
152             if (newSet.size() != policy.getLimit_constraints().size() && callback != null) {
153                 callback.onWarning(GUARD_POLICY + policy.getName() + " has duplicate limit constraints");
154             }
155         }
156     }
157     
158 }