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