2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 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.processor;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.charset.StandardCharsets;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import org.apache.commons.io.IOUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.controlloop.ControlLoopException;
41 import org.onap.policy.controlloop.policy.FinalResult;
42 import org.onap.policy.controlloop.policy.Policy;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class ControlLoopProcessorTest {
50 private static final Logger logger = LoggerFactory.getLogger(ControlLoopProcessorTest.class);
51 private static final StandardCoder coder = new StandardCoder();
54 public void testControlLoopProcessor() throws IOException, ControlLoopException {
55 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
56 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
57 this.testSuccess(yamlString);
58 this.testFailure(yamlString);
61 private ToscaPolicy getPolicyFromResource(String resourcePath, String policyName) throws CoderException {
62 String policyJson = ResourceUtils.getResourceAsString(resourcePath);
63 ToscaServiceTemplate serviceTemplate = coder.decode(policyJson, ToscaServiceTemplate.class);
64 ToscaPolicy policy = serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
65 assertNotNull(policy);
68 * name and version are used within a drl. api component and drools core will ensure that these
71 if (StringUtils.isBlank(policy.getName())) {
72 policy.setName(policyName);
75 if (StringUtils.isBlank(policy.getVersion())) {
76 policy.setVersion(policy.getTypeVersion());
79 return serviceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyName);
83 public void testControlLoopFromToscaLegacy() throws IOException, CoderException, ControlLoopException {
85 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
87 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
90 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vdns.json")));
92 new ControlLoopProcessor(coder.decode(policy, ToscaPolicy.class)).getCurrentPolicy());
96 public void testControlLoopFromToscaCompliant()
97 throws CoderException, ControlLoopException {
99 new ControlLoopProcessor(
100 getPolicyFromResource(
101 "policies/vCPE.policy.operational.input.tosca.json", "operational.restart")
102 ).getCurrentPolicy());
106 new ControlLoopProcessor(
107 getPolicyFromResource(
108 "policies/vFirewall.policy.operational.input.tosca.json", "operational.modifyconfig")
109 ).getCurrentPolicy());
112 new ControlLoopProcessor(
113 getPolicyFromResource(
114 "policies/vDNS.policy.operational.input.tosca.json", "operational.scaleout")
115 ).getCurrentPolicy());
119 public void testControlLoopFromToscaCompliantBad() throws CoderException {
120 ToscaPolicy toscaPolicy = getPolicyFromResource(
121 "policies/vCPE.policy.operational.input.tosca.json", "operational.restart");
122 toscaPolicy.setType("onap.policies.controlloop.Operational");
123 assertThatThrownBy(() -> new ControlLoopProcessor(toscaPolicy)).hasCauseInstanceOf(CoderException.class);
127 public void testControlLoopProcessorBadYaml() throws IOException {
128 InputStream is = new FileInputStream(new File("src/test/resources/string.yaml"));
129 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
131 assertThatThrownBy(() -> new ControlLoopProcessor(yamlString))
132 .hasMessageStartingWith("Cannot create property=string for JavaBean=ControlLoopPolicy");
136 public void testControlLoopProcessorBadTriggerYaml() throws IOException, ControlLoopException {
137 InputStream is = new FileInputStream(new File("src/test/resources/badtriggerpolicy.yaml"));
138 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
140 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
141 assertNull(clProcessor.getCurrentPolicy());
143 assertThatThrownBy(() -> clProcessor.nextPolicyForResult(PolicyResult.SUCCESS))
144 .hasMessageStartingWith("There is no current policy to determine where to go to.");
148 public void testControlLoopProcessorNoPolicyYaml() throws IOException, ControlLoopException {
149 InputStream is = new FileInputStream(new File("src/test/resources/nopolicy.yaml"));
150 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
152 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
153 assertThatThrownBy(clProcessor::getCurrentPolicy)
154 .hasMessage("There are no policies defined.");
158 public void testControlLoopProcessorNextPolicyForResult() throws IOException, ControlLoopException {
159 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
160 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
162 for (PolicyResult result : PolicyResult.values()) {
163 checkResult(yamlString, result);
167 private void checkResult(String yamlString, PolicyResult result) throws ControlLoopException {
168 ControlLoopProcessor clProcessor = new ControlLoopProcessor(yamlString);
169 clProcessor.getCurrentPolicy();
170 clProcessor.nextPolicyForResult(result);
174 * Test policies in the given yaml following the successful path.
176 * @param yaml yaml containing the policies to test
177 * @throws ControlLoopException if an error occurs
179 public void testSuccess(String yaml) throws ControlLoopException {
180 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
181 logger.debug("testSuccess: {}", processor.getControlLoop());
183 FinalResult result = processor.checkIsCurrentPolicyFinal();
184 if (result != null) {
185 logger.debug("{}", result);
188 Policy policy = processor.getCurrentPolicy();
189 assertNotNull(policy);
190 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
191 processor.nextPolicyForResult(PolicyResult.SUCCESS);
196 * Test policies in the given yaml following the failure path.
198 * @param yaml yaml containing the policies to test
199 * @throws ControlLoopException if an error occurs
201 public void testFailure(String yaml) throws ControlLoopException {
202 ControlLoopProcessor processor = new ControlLoopProcessor(yaml);
203 logger.debug("testFailure: {}", processor.getControlLoop());
205 FinalResult result = processor.checkIsCurrentPolicyFinal();
206 if (result != null) {
207 logger.debug("{}", result);
210 Policy policy = processor.getCurrentPolicy();
211 assertNotNull(policy);
212 logger.debug("current policy is: {} {}", policy.getId(), policy.getName());
213 processor.nextPolicyForResult(PolicyResult.FAILURE);