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;
26 import java.util.HashSet;
27 import java.util.List;
30 import org.onap.policy.controlloop.compiler.CompilerException;
31 import org.onap.policy.controlloop.compiler.ControlLoopCompilerCallback;
32 import org.onap.policy.controlloop.policy.guard.Constraint;
33 import org.onap.policy.controlloop.policy.guard.ControlLoopGuard;
34 import org.onap.policy.controlloop.policy.guard.GuardPolicy;
35 import org.yaml.snakeyaml.Yaml;
36 import org.yaml.snakeyaml.constructor.Constructor;
38 public class ControlLoopGuardCompiler {
40 private static final String GUARD_POLICIES_SHOULD_NOT_BE_NULL = "Guard policies should not be null";
41 private static final String GUARD_POLICY = "Guard policy ";
43 private ControlLoopGuardCompiler(){
44 // Private Constructor
48 * Compile the control loop guard.
50 * @param clGuard the guard
51 * @param callback callback routine
52 * @return the guard object
53 * @throws CompilerException compilation exception
55 public static ControlLoopGuard compile(ControlLoopGuard clGuard,
56 ControlLoopCompilerCallback callback) throws CompilerException {
58 // Ensure ControlLoopGuard has at least one guard policies
60 validateControlLoopGuard(clGuard, callback);
62 // Ensure each guard policy has at least one constraints and all guard policies are unique
64 validateGuardPolicies(clGuard.getGuards(), callback);
66 // Ensure constraints for each guard policy are unique
68 validateConstraints(clGuard.getGuards(), callback);
74 * Compile the control loop guard.
76 * @param yamlSpecification yaml specification as a stream
77 * @param callback callback method
78 * @return guard object
79 * @throws CompilerException throws compile exception
81 public static ControlLoopGuard compile(InputStream yamlSpecification,
82 ControlLoopCompilerCallback callback) throws CompilerException {
83 Yaml yaml = new Yaml(new Constructor(ControlLoopGuard.class));
84 Object obj = yaml.load(yamlSpecification);
86 throw new CompilerException("Could not parse yaml specification.");
88 if (! (obj instanceof ControlLoopGuard)) {
89 throw new CompilerException("Yaml could not parse specification into required ControlLoopGuard object");
91 return ControlLoopGuardCompiler.compile((ControlLoopGuard) obj, callback);
94 private static void validateControlLoopGuard(ControlLoopGuard clGuard,
95 ControlLoopCompilerCallback callback) throws CompilerException {
96 if (clGuard == null) {
97 if (callback != null) {
98 callback.onError("ControlLoop Guard cannot be null");
100 throw new CompilerException("ControlLoop Guard cannot be null");
102 if (clGuard.getGuard() == null && callback != null) {
103 callback.onError("Guard version cannot be null");
105 if (clGuard.getGuards() == null) {
106 if (callback != null) {
107 callback.onError("ControlLoop Guard should have at least one guard policies");
109 } else if (clGuard.getGuards().isEmpty() && callback != null) {
110 callback.onError("ControlLoop Guard should have at least one guard policies");
114 private static void validateGuardPolicies(List<GuardPolicy> policies,
115 ControlLoopCompilerCallback callback) throws CompilerException {
116 if (policies == null) {
117 if (callback != null) {
118 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
120 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
123 // Ensure all guard policies are unique
125 Set<GuardPolicy> newSet = new HashSet<>(policies);
126 if (newSet.size() != policies.size() && callback != null) {
127 callback.onWarning("There are duplicate guard policies");
130 // Ensure each guard policy has at least one constraints
132 for (GuardPolicy policy : policies) {
133 if (policy.getLimit_constraints() == null || policy.getLimit_constraints().isEmpty()) {
134 if (callback != null) {
135 callback.onError(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
137 throw new CompilerException(GUARD_POLICY + policy.getName() + " does not have any limit constraint");
142 private static void validateConstraints(List<GuardPolicy> policies,
143 ControlLoopCompilerCallback callback) throws CompilerException {
144 if (policies == null) {
145 if (callback != null) {
146 callback.onError(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
148 throw new CompilerException(GUARD_POLICIES_SHOULD_NOT_BE_NULL);
150 for (GuardPolicy policy : policies) {
151 Set<Constraint> newSet = new HashSet<>(policy.getLimit_constraints());
152 if (newSet.size() != policy.getLimit_constraints().size() && callback != null) {
153 callback.onWarning(GUARD_POLICY + policy.getName() + " has duplicate limit constraints");