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