2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.guard.compiler;
24 import java.io.InputStream;
25 import java.util.HashSet;
26 import java.util.List;
29 import org.yaml.snakeyaml.Yaml;
30 import org.yaml.snakeyaml.constructor.Constructor;
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;
38 public class ControlLoopGuardCompiler {
40 public static ControlLoopGuard compile(ControlLoopGuard CLGuard, ControlLoopCompilerCallback callback) throws CompilerException {
42 // Ensure ControlLoopGuard has at least one guard policies
44 validateControlLoopGuard(CLGuard, callback);
46 // Ensure each guard policy has at least one constraints and all guard policies are unique
48 validateGuardPolicies(CLGuard.guards, callback);
50 // Ensure constraints for each guard policy are unique
52 validateConstraints(CLGuard.guards, callback);
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);
61 throw new CompilerException("Could not parse yaml specification.");
63 if (! (obj instanceof ControlLoopGuard)) {
64 throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
66 return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
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");
74 throw new CompilerException("ControlLoop Guard cannot be null");
76 if (CLGuard.guard == null) {
77 if (callback != null) {
78 callback.onError("Guard version cannot be null");
81 if (CLGuard.guards == null) {
82 if (callback != null) {
83 callback.onError("ControlLoop Guard should have at least one guard policies");
85 } else if (CLGuard.guards.size() < 1) {
86 if (callback != null) {
87 callback.onError("ControlLoop Guard should have at least one guard policies");
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");
97 throw new CompilerException("Guard policies should not be null");
100 // Ensure all guard policies are unique
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");
109 // Ensure each guard policy has at least one constraints
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");
116 throw new CompilerException("Guard policy " + policy.name + " does not have any limit constraint");
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");
126 throw new CompilerException("Guard policies should not be null");
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");