2 * ============LICENSE_START=======================================================
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
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.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;
37 public class ControlLoopGuardCompiler {
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 ";
42 private ControlLoopGuardCompiler(){
43 // Private Constructor
46 public static ControlLoopGuard compile(ControlLoopGuard cLGuard,
47 ControlLoopCompilerCallback callback) throws CompilerException {
49 // Ensure ControlLoopGuard has at least one guard policies
51 validateControlLoopGuard(cLGuard, callback);
53 // Ensure each guard policy has at least one constraints and all guard policies are unique
55 validateGuardPolicies(cLGuard.getGuards(), callback);
57 // Ensure constraints for each guard policy are unique
59 validateConstraints(cLGuard.getGuards(), callback);
64 public static ControlLoopGuard compile(InputStream yamlSpecification,
65 ControlLoopCompilerCallback callback) throws CompilerException {
66 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
67 Object obj = yaml.load(yamlSpecification);
69 throw new CompilerException("Could not parse yaml specification.");
71 if (! (obj instanceof ControlLoopGuard)) {
72 throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
74 return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
77 private static void validateControlLoopGuard(ControlLoopGuard cLGuard,
78 ControlLoopCompilerCallback callback) throws CompilerException {
79 if (cLGuard == null) {
80 if (callback != null) {
81 callback.onError("ControlLoop Guard cannot be null");
83 throw new CompilerException("ControlLoop Guard cannot be null");
85 if (cLGuard.getGuard() == null && callback != null) {
86 callback.onError("Guard version cannot be null");
88 if (cLGuard.getGuards() == null) {
89 if (callback != null) {
90 callback.onError("ControlLoop Guard should have at least one guard policies");
92 } else if (cLGuard.getGuards().isEmpty() && callback != null) {
93 callback.onError("ControlLoop Guard should have at least one guard policies");
97 private static void validateGuardPolicies(List<GuardPolicy> policies,
98 ControlLoopCompilerCallback callback) throws CompilerException {
99 if (policies == null) {
100 if (callback != null) {
101 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
103 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
106 // Ensure all guard policies are unique
108 Set<GuardPolicy> newSet = new HashSet<>(policies);
109 if (newSet.size() != policies.size() && callback != null) {
110 callback.onWarning("There are duplicate guard policies");
113 // Ensure each guard policy has at least one constraints
115 for (GuardPolicy policy : policies) {
116 if (policy.getLimit_constraints() == null || policy.getLimit_constraints().isEmpty()) {
117 if (callback != null) {
118 callback.onError(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
120 throw new CompilerException(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
125 private static void validateConstraints(List<GuardPolicy> policies,
126 ControlLoopCompilerCallback callback) throws CompilerException {
127 if (policies == null) {
128 if (callback != null) {
129 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
131 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
133 for (GuardPolicy policy : policies) {
134 Set<Constraint> newSet = new HashSet<>(policy.getLimit_constraints());
135 if (newSet.size() != policy.getLimit_constraints().size() && callback != null) {
136 callback.onWarning(GUARD_POLICY + policy.getName() + " has duplicate limit constraints");