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