2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.guard.compiler;
24 import java.io.InputStream;
25 import java.util.HashSet;
26 import java.util.List;
28 import org.onap.policy.controlloop.compiler.CompilerException;
29 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
30 import org.onap.policy.controlloop.policy.guard.Constraint;
31 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
32 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
33 import org.yaml.snakeyaml.Yaml;
34 import org.yaml.snakeyaml.constructor.Constructor;
36 public class ControlLoopGuardCompiler {
38 private static final String GUARD_POLICIES_SHOULD_NOT_BE_NULL = "Guard policies should not be null";
39 private static final String GUARD_POLICY = "Guard policy ";
41 private ControlLoopGuardCompiler(){
42 // Private Constructor
46 * Compile the control loop guard.
48 * @param clGuard the guard
49 * @param callback callback routine
50 * @return the guard object
51 * @throws CompilerException compilation exception
53 public static ControlLoopGuard compile(ControlLoopGuard clGuard,
54 ControlLoopCompilerCallback callback) throws CompilerException {
56 // Ensure ControlLoopGuard has at least one guard policies
58 validateControlLoopGuard(clGuard, callback);
60 // Ensure each guard policy has at least one constraints and all guard policies are unique
62 validateGuardPolicies(clGuard.getGuards(), callback);
64 // Ensure constraints for each guard policy are unique
66 validateConstraints(clGuard.getGuards(), callback);
72 * Compile the control loop guard.
74 * @param yamlSpecification yaml specification as a stream
75 * @param callback callback method
76 * @return guard object
77 * @throws CompilerException throws compile exception
79 public static ControlLoopGuard compile(InputStream yamlSpecification,
80 ControlLoopCompilerCallback callback) throws CompilerException {
81 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
82 Object obj = yaml.load(yamlSpecification);
84 throw new CompilerException("Could not parse yaml specification.");
86 if (! (obj instanceof ControlLoopGuard)) {
87 throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
89 return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
92 private static void validateControlLoopGuard(ControlLoopGuard clGuard,
93 ControlLoopCompilerCallback callback) throws CompilerException {
94 if (clGuard == null) {
95 if (callback != null) {
96 callback.onError("ControlLoop Guard cannot be null");
98 throw new CompilerException("ControlLoop Guard cannot be null");
100 if (clGuard.getGuard() == null && callback != null) {
101 callback.onError("Guard version cannot be null");
103 if (clGuard.getGuards() == null) {
104 if (callback != null) {
105 callback.onError("ControlLoop Guard should have at least one guard policies");
107 } else if (clGuard.getGuards().isEmpty() && callback != null) {
108 callback.onError("ControlLoop Guard should have at least one guard policies");
112 private static void validateGuardPolicies(List<GuardPolicy> policies,
113 ControlLoopCompilerCallback callback) throws CompilerException {
114 if (policies == null) {
115 if (callback != null) {
116 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
118 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
121 // Ensure all guard policies are unique
123 Set<GuardPolicy> newSet = new HashSet<>(policies);
124 if (newSet.size() != policies.size() && callback != null) {
125 callback.onWarning("There are duplicate guard policies");
128 // Ensure each guard policy has at least one constraints
130 for (GuardPolicy policy : policies) {
131 if (policy.getLimit_constraints() == null || policy.getLimit_constraints().isEmpty()) {
132 if (callback != null) {
133 callback.onError(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
135 throw new CompilerException(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
140 private static void validateConstraints(List<GuardPolicy> policies,
141 ControlLoopCompilerCallback callback) throws CompilerException {
142 if (policies == null) {
143 if (callback != null) {
144 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
146 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
148 for (GuardPolicy policy : policies) {
149 Set<Constraint> newSet = new HashSet<>(policy.getLimit_constraints());
150 if (newSet.size() != policy.getLimit_constraints().size() && callback != null) {
151 callback.onWarning(GUARD_POLICY + policy.getName() + " has duplicate limit constraints");