173e3e26346c1e841d92d4f518113749ea8baebb
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-yaml
4  * ================================================================================
5  * Copyright (C) 2017 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
24 import java.io.InputStream;
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 ControlLoopGuardCompiler(){
40         // Private Constructor 
41     }
42     
43     public static ControlLoopGuard compile(ControlLoopGuard cLGuard, ControlLoopCompilerCallback callback) throws CompilerException {
44         //
45         // Ensure ControlLoopGuard has at least one guard policies
46         //
47         validateControlLoopGuard(cLGuard, callback);
48         //
49         // Ensure each guard policy has at least one constraints and all guard policies are unique
50         //
51         validateGuardPolicies(cLGuard.getGuards(), callback);
52         //
53         // Ensure constraints for each guard policy are unique
54         //
55         validateConstraints(cLGuard.getGuards(), callback);
56         
57         return cLGuard;
58     }
59     
60     public static ControlLoopGuard  compile(InputStream yamlSpecification, ControlLoopCompilerCallback callback) throws CompilerException {
61         Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
62         Object obj = yaml.load(yamlSpecification);
63         if (obj == null) {
64             throw new CompilerException("Could not parse yaml specification.");
65         }
66         if (! (obj instanceof ControlLoopGuard)) {
67             throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
68         }
69         return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
70     }
71     
72     private static void validateControlLoopGuard(ControlLoopGuard cLGuard, ControlLoopCompilerCallback callback) throws CompilerException {
73         if (cLGuard == null) {
74             if (callback != null) {
75                 callback.onError("ControlLoop Guard cannot be null");
76             }
77             throw new CompilerException("ControlLoop Guard cannot be null");
78         }
79         if (cLGuard.getGuard() == null && callback != null) {
80             callback.onError("Guard version cannot be null");
81         }
82         if (cLGuard.getGuards() == null) {
83             if (callback != null) {
84                 callback.onError("ControlLoop Guard should have at least one guard policies");
85             }
86         } else if (cLGuard.getGuards().isEmpty() && callback != null) {
87             callback.onError("ControlLoop Guard should have at least one guard policies");
88         }
89     }
90     
91     private static void validateGuardPolicies(List<GuardPolicy> policies, ControlLoopCompilerCallback callback) throws CompilerException {
92         if (policies == null) {
93             if (callback != null) {
94                 callback.onError("Guard policies should not be null");
95             }
96             throw new CompilerException("Guard policies should not be null");
97         }
98         //
99         // Ensure all guard policies are unique
100         //
101         Set<GuardPolicy> newSet = new HashSet<>(policies);
102         if (newSet.size() != policies.size() && callback != null) {
103             callback.onWarning("There are duplicate guard policies");
104         }
105         //
106         // Ensure each guard policy has at least one constraints
107         //
108         for (GuardPolicy policy : policies) {
109             if (policy.getLimit_constraints() == null || policy.getLimit_constraints().isEmpty()) {
110                 if (callback != null) {
111                     callback.onError("Guard policy " + policy.getName() + " does not have any limit constraint");
112                 }
113                 throw new CompilerException("Guard policy " + policy.getName() + " does not have any limit constraint");
114             }
115         }
116     }
117     
118     private static void validateConstraints(List<GuardPolicy> policies, ControlLoopCompilerCallback callback) throws CompilerException {
119         if (policies == null) {
120             if (callback != null) {
121                 callback.onError("Guard policies should not be null");
122             }
123             throw new CompilerException("Guard policies should not be null");
124         }
125         for (GuardPolicy policy : policies) {
126             Set<Constraint> newSet = new HashSet<>(policy.getLimit_constraints());
127             if (newSet.size() != policy.getLimit_constraints().size() && callback != null) {
128                 callback.onWarning("Guard policy " + policy.getName() + " has duplicate limit constraints");
129             }
130         }
131     }
132     
133 }