[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / policy-yaml / src / main / java / org / onap / policy / controlloop / guard / compiler / ControlLoopGuardCompiler.java
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.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
31
32 import org.onap.policy.controlloop.compiler.CompilerException;
33 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
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.GuardPolicy;
37
38 public class ControlLoopGuardCompiler {
39         
40         public static ControlLoopGuard compile(ControlLoopGuard CLGuard, ControlLoopCompilerCallback callback) throws CompilerException {
41                 //
42                 // Ensure ControlLoopGuard has at least one guard policies
43                 //
44                 validateControlLoopGuard(CLGuard, callback);
45                 //
46                 // Ensure each guard policy has at least one constraints and all guard policies are unique
47                 //
48                 validateGuardPolicies(CLGuard.guards, callback);
49                 //
50                 // Ensure constraints for each guard policy are unique
51                 //
52                 validateConstraints(CLGuard.guards, callback);
53                 
54                 return CLGuard;
55         }
56         
57         public static ControlLoopGuard  compile(InputStream yamlSpecification, ControlLoopCompilerCallback callback) throws CompilerException {
58                 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
59                 Object obj = yaml.load(yamlSpecification);
60                 if (obj == null) {
61                         throw new CompilerException("Could not parse yaml specification.");
62                 }
63                 if (! (obj instanceof ControlLoopGuard)) {
64                         throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
65                 }
66                 return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
67         }
68         
69         private static void validateControlLoopGuard(ControlLoopGuard CLGuard, ControlLoopCompilerCallback callback) throws CompilerException {
70                 if (CLGuard == null) {
71                         if (callback != null) {
72                                 callback.onError("ControlLoop Guard cannot be null");
73                         }
74                         throw new CompilerException("ControlLoop Guard cannot be null");
75                 }
76                 if (CLGuard.guard == null) {
77                         if (callback != null) {
78                                 callback.onError("Guard version cannot be null");
79                         }
80                 }
81                 if (CLGuard.guards == null) {
82                         if (callback != null) {
83                                 callback.onError("ControlLoop Guard should have at least one guard policies");
84                         }
85                 } else if (CLGuard.guards.size() < 1) {
86                         if (callback != null) {
87                                 callback.onError("ControlLoop Guard should have at least one guard policies");
88                         }
89                 }
90         }
91         
92         private static void validateGuardPolicies(List<GuardPolicy> policies, ControlLoopCompilerCallback callback) throws CompilerException {
93                 if (policies == null) {
94                         if (callback != null) {
95                                 callback.onError("Guard policies should not be null");
96                         }
97                         throw new CompilerException("Guard policies should not be null");
98                 }
99                 //
100                 // Ensure all guard policies are unique
101                 //
102                 Set<GuardPolicy> newSet = new HashSet<GuardPolicy>(policies);
103                 if (newSet.size() != policies.size()) {
104                         if (callback != null) {
105                                 callback.onWarning("There are duplicate guard policies");
106                         }
107                 }
108                 //
109                 // Ensure each guard policy has at least one constraints
110                 //
111                 for (GuardPolicy policy : policies) {
112                         if (policy.limit_constraints == null || policy.limit_constraints.size() < 1) {
113                                 if (callback != null) {
114                                         callback.onError("Guard policy " + policy.name + " does not have any limit constraint");
115                                 }
116                                 throw new CompilerException("Guard policy " + policy.name + " does not have any limit constraint");
117                         }
118                 }
119         }
120         
121         private static void validateConstraints(List<GuardPolicy> policies, ControlLoopCompilerCallback callback) throws CompilerException {
122                 if (policies == null) {
123                         if (callback != null) {
124                                 callback.onError("Guard policies should not be null");
125                         }
126                         throw new CompilerException("Guard policies should not be null");
127                 }
128                 for (GuardPolicy policy : policies) {
129                         Set<Constraint> newSet = new HashSet<Constraint>(policy.limit_constraints);
130                         if (newSet.size() != policy.limit_constraints.size()) {
131                                 if (callback != null) {
132                                         callback.onWarning("Guard policy " + policy.name + " has duplicate limit constraints");
133                                 }
134                         }
135                 }
136         }
137         
138 }