2 * ============LICENSE_START=======================================================
3 * policy-yaml unit test
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.policy;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
29 import java.io.FileInputStream;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.UUID;
35 import org.junit.Test;
36 import org.yaml.snakeyaml.Yaml;
37 import org.yaml.snakeyaml.constructor.Constructor;
38 import org.yaml.snakeyaml.error.YAMLException;
40 import org.onap.policy.aai.PNF;
41 import org.onap.policy.aai.PNFType;
42 import org.onap.policy.controlloop.policy.builder.BuilderException;
43 import org.onap.policy.controlloop.policy.builder.ControlLoopPolicyBuilder;
44 import org.onap.policy.controlloop.policy.builder.Message;
45 import org.onap.policy.controlloop.policy.builder.MessageLevel;
46 import org.onap.policy.controlloop.policy.builder.Results;
47 import org.onap.policy.sdc.Resource;
48 import org.onap.policy.sdc.ResourceType;
49 import org.onap.policy.sdc.Service;
52 public class ControlLoopPolicyBuilderTest {
55 public void testControlLoop() {
58 // Create a builder for our policy
60 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
64 Service vSCP = new Service("vSCP");
65 Service vUSP = new Service("vUSP");
66 Service vTrinity = new Service("Trinity");
67 builder = builder.addService(vSCP, vUSP, vTrinity);
68 assertTrue(builder.getControlLoop().services.size() == 3);
70 // Test remove services
72 builder = builder.removeService(vSCP);
73 assertTrue(builder.getControlLoop().services.size() == 2);
74 builder = builder.removeAllServices();
75 assertTrue(builder.getControlLoop().services.size() == 0);
79 Resource vCTS = new Resource("vCTS", ResourceType.VF);
80 Resource vCOM = new Resource("vCTS", ResourceType.VF);
81 Resource vRAR = new Resource("vCTS", ResourceType.VF);
82 builder = builder.addResource(vCTS, vCOM, vRAR);
83 assertTrue(builder.getControlLoop().resources.size() == 3);
85 // Test remove resources
87 builder = builder.removeResource(vCTS);
88 assertTrue(builder.getControlLoop().resources.size() == 2);
89 builder = builder.removeAllResources();
90 assertTrue(builder.getControlLoop().resources.size() == 0);
95 pnf.PNFName = UUID.randomUUID().toString();
96 pnf.PNFType = PNFType.ENODEB;
97 assertTrue(builder.getControlLoop().pnf == null);
98 builder = builder.setPNF(pnf);
99 assertTrue(builder.getControlLoop().pnf != null);
103 builder = builder.removePNF();
104 assertTrue(builder.getControlLoop().pnf == null);
106 // Test set abatement
108 assertFalse(builder.getControlLoop().abatement);
109 builder = builder.setAbatement(true);
110 assertTrue(builder.getControlLoop().abatement);
111 } catch (BuilderException e) {
112 fail(e.getMessage());
117 public void testTimeout() {
120 // Create a builder for our policy
122 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
126 assertTrue(builder.getControlLoop().timeout == 2400);
127 builder = builder.setTimeout(800);
128 assertTrue(builder.getControlLoop().timeout == 800);
130 // Test calculateTimeout
132 Policy trigger = builder.setTriggerPolicy(
134 "Upon getting the trigger event, restart the VM",
136 new Target(TargetType.VM),
141 @SuppressWarnings("unused")
142 Policy onRestartFailurePolicy = builder.setPolicyForPolicyResult(
144 "If the restart fails, rebuild it",
146 new Target(TargetType.VM),
152 PolicyResult.FAILURE,
153 PolicyResult.FAILURE_RETRIES,
154 PolicyResult.FAILURE_TIMEOUT);
155 assertTrue(builder.calculateTimeout().equals(new Integer(300 + 600)));
157 } catch (BuilderException e) {
158 fail(e.getMessage());
163 public void testTriggerPolicyMethods() {
165 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
169 assertTrue(builder.isOpenLoop());
171 // Test set initial trigger policy
173 Policy triggerPolicy1 = builder.setTriggerPolicy(
175 "Upon getting the trigger event, restart the VM",
177 new Target(TargetType.VM),
182 assertTrue(builder.isOpenLoop() == false);
183 assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id));
185 // Set trigger policy to a new policy
187 @SuppressWarnings("unused")
188 Policy triggerPolicy2 = builder.setTriggerPolicy(
190 "Upon getting the trigger event, rebuild the VM",
192 new Target(TargetType.VM),
198 // Test set trigger policy to another existing policy
200 @SuppressWarnings("unused")
201 ControlLoop cl = builder.setTriggerPolicy(triggerPolicy1.id);
202 assertTrue(builder.getControlLoop().trigger_policy.equals(triggerPolicy1.id));
204 // Test get trigger policy
206 assertTrue(builder.getTriggerPolicy().equals(triggerPolicy1));
208 } catch (BuilderException e) {
209 fail(e.getMessage());
214 public void testAddRemovePolicies() {
216 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
217 Policy triggerPolicy = builder.setTriggerPolicy(
219 "Upon getting the trigger event, restart the VM",
221 new Target(TargetType.VM),
227 // Test create a policy and chain it to the results of trigger policy
229 Policy onRestartFailurePolicy1 = builder.setPolicyForPolicyResult(
231 "If the restart fails, rebuild it.",
233 new Target(TargetType.VM),
239 PolicyResult.FAILURE,
240 PolicyResult.FAILURE_RETRIES,
241 PolicyResult.FAILURE_TIMEOUT,
242 PolicyResult.FAILURE_GUARD);
244 assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy1.id));
245 assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy1.id));
246 assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy1.id));
247 assertTrue(builder.getTriggerPolicy().failure_guard.equals(onRestartFailurePolicy1.id));
249 // Test remove policy
251 boolean removed = builder.removePolicy(onRestartFailurePolicy1.id);
253 assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString()));
254 assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
255 assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
256 assertTrue(builder.getTriggerPolicy().failure_guard.equals(FinalResult.FINAL_FAILURE_GUARD.toString()));
258 // Create another policy and chain it to the results of trigger policy
260 Policy onRestartFailurePolicy2 = builder.setPolicyForPolicyResult(
262 "If the restart fails, rebuild it.",
264 new Target(TargetType.VM),
270 PolicyResult.FAILURE,
271 PolicyResult.FAILURE_RETRIES,
272 PolicyResult.FAILURE_TIMEOUT);
274 // Test reset policy results
276 triggerPolicy = builder.resetPolicyResults(triggerPolicy.id);
277 assertTrue(builder.getTriggerPolicy().failure.equals(FinalResult.FINAL_FAILURE.toString()));
278 assertTrue(builder.getTriggerPolicy().failure_retries.equals(FinalResult.FINAL_FAILURE_RETRIES.toString()));
279 assertTrue(builder.getTriggerPolicy().failure_timeout.equals(FinalResult.FINAL_FAILURE_TIMEOUT.toString()));
281 // Test set the policy results to an existing operational policy
283 onRestartFailurePolicy2 = builder.setPolicyForPolicyResult(
284 onRestartFailurePolicy2.id,
286 PolicyResult.FAILURE,
287 PolicyResult.FAILURE_RETRIES,
288 PolicyResult.FAILURE_TIMEOUT);
289 assertTrue(builder.getTriggerPolicy().failure.equals(onRestartFailurePolicy2.id));
290 assertTrue(builder.getTriggerPolicy().failure_retries.equals(onRestartFailurePolicy2.id));
291 assertTrue(builder.getTriggerPolicy().failure_timeout.equals(onRestartFailurePolicy2.id));
294 // Test remove all existing operational policies
296 builder = builder.removeAllPolicies();
297 assertTrue(builder.getControlLoop().trigger_policy.equals(FinalResult.FINAL_OPENLOOP.toString()));
299 } catch (BuilderException e) {
300 fail(e.getMessage());
305 public void testAddOperationsAccumulateParams() {
307 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 2400);
308 Policy triggerPolicy = builder.setTriggerPolicy(
309 "Restart the eNodeB",
310 "Upon getting the trigger event, restart the eNodeB",
312 new Target(TargetType.PNF),
318 // Add the operationsAccumulateParams
320 triggerPolicy = builder.addOperationsAccumulateParams(triggerPolicy.id, new OperationsAccumulateParams("15m", 5));
321 assertNotNull(builder.getTriggerPolicy().operationsAccumulateParams);
322 assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.period.equals("15m"));
323 assertTrue(builder.getTriggerPolicy().operationsAccumulateParams.limit == 5);
325 } catch (BuilderException e) {
326 fail(e.getMessage());
332 public void testBuildSpecification() {
335 // Create the builder
337 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(UUID.randomUUID().toString(), 800);
339 // Set the first invalid trigger policy
341 Policy policy1 = builder.setTriggerPolicy(
343 "Upon getting the trigger event, restart the VM",
350 Results results = builder.buildSpecification();
352 // Check that ERRORs are in results for invalid policy arguments
354 boolean invalid_actor = false;
355 boolean invalid_recipe = false;
356 boolean invalid_target = false;
357 for (Message m : results.getMessages()) {
358 if (m.getMessage().equals("Policy actor is null") && m.getLevel() == MessageLevel.ERROR) {
359 invalid_actor = true;
361 if (m.getMessage().equals("Policy recipe is invalid") && m.getLevel() == MessageLevel.ERROR) {
362 invalid_recipe = true;
364 if (m.getMessage().equals("Policy target is null") && m.getLevel() == MessageLevel.ERROR) {
365 invalid_target = true;
369 assertTrue(invalid_actor);
370 assertTrue(invalid_recipe);
371 assertTrue(invalid_target);
373 // Remove the invalid policy
375 //@SuppressWarnings("unused")
376 boolean removed = builder.removePolicy(policy1.id);
378 assertTrue(builder.getTriggerPolicy() == null);
380 // Set a valid trigger policy
382 policy1 = builder.setTriggerPolicy(
384 "If the restart fails, rebuild it.",
386 new Target(TargetType.VM),
392 // Set a second valid trigger policy
394 Policy policy2 = builder.setTriggerPolicy(
396 "Upon getting the trigger event, restart the VM",
398 new Target(TargetType.VM),
404 // Now, we have policy1 unreachable
406 results = builder.buildSpecification();
407 boolean unreachable = false;
408 for (Message m : results.getMessages()) {
409 if (m.getMessage().equals("Policy " + policy1.id + " is not reachable.") && m.getLevel() == MessageLevel.WARNING) {
414 assertTrue(unreachable);
416 // Set policy1 for the failure results of policy2
418 policy1 = builder.setPolicyForPolicyResult(
421 PolicyResult.FAILURE,
422 PolicyResult.FAILURE_RETRIES,
423 PolicyResult.FAILURE_TIMEOUT);
424 results = builder.buildSpecification();
425 boolean invalid_timeout = false;
426 for (Message m : results.getMessages()) {
427 if (m.getMessage().equals("controlLoop overall timeout is less than the sum of operational policy timeouts.") && m.getLevel() == MessageLevel.ERROR) {
428 invalid_timeout = true;
432 assertTrue(invalid_timeout);
434 // Remove policy2 (revert controlLoop back to open loop)
436 removed = builder.removePolicy(policy2.id);
438 // ControlLoop is open loop now, but it still has policies (policy1)
440 results = builder.buildSpecification();
442 for (Message m : results.getMessages()) {
443 if (m.getMessage().equals("Open Loop policy contains policies. The policies will never be invoked.") && m.getLevel() == MessageLevel.WARNING) {
448 assertTrue(unreachable);
450 } catch (BuilderException e) {
451 fail(e.getMessage());
458 this.test("src/test/resources/v1.0.0/policy_Test.yaml");
462 public void testEvilYaml() {
463 try (InputStream is = new FileInputStream(new File("src/test/resources/v1.0.0/test_evil.yaml"))) {
465 // Read the yaml into our Java Object
467 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
469 } catch (FileNotFoundException e) {
470 fail(e.getLocalizedMessage());
471 } catch (IOException e) {
472 fail(e.getLocalizedMessage());
473 } catch (YAMLException e) {
480 public void test(String testFile) {
481 try (InputStream is = new FileInputStream(new File(testFile))) {
483 // Read the yaml into our Java Object
485 Yaml yaml = new Yaml(new Constructor(ControlLoopPolicy.class));
486 Object obj = yaml.load(is);
488 assertTrue(obj instanceof ControlLoopPolicy);
489 ControlLoopPolicy policyTobuild = (ControlLoopPolicy) obj;
491 // Now we're going to try to use the builder to build this.
493 ControlLoopPolicyBuilder builder = ControlLoopPolicyBuilder.Factory.buildControlLoop(
494 policyTobuild.controlLoop.controlLoopName,
495 policyTobuild.controlLoop.timeout);
499 if (policyTobuild.controlLoop.services != null) {
500 builder = builder.addService(policyTobuild.controlLoop.services.toArray(new Service[policyTobuild.controlLoop.services.size()]));
505 if (policyTobuild.controlLoop.resources != null) {
506 builder = builder.addResource(policyTobuild.controlLoop.resources.toArray(new Resource[policyTobuild.controlLoop.resources.size()]));
511 if (policyTobuild.controlLoop.pnf != null) {
512 builder = builder.setPNF(policyTobuild.controlLoop.pnf);
515 // Add the policies and be sure to set the trigger policy
517 if (policyTobuild.policies != null) {
518 for (Policy policy : policyTobuild.policies) {
519 if (policy.id == policyTobuild.controlLoop.trigger_policy) {
520 builder.setTriggerPolicy(policy.name, policy.description, policy.actor, policy.target, policy.recipe, null, policy.retry, policy.timeout);
525 // Question : how to change policy ID and results by using builder ??
527 @SuppressWarnings("unused")
528 Results results = builder.buildSpecification();
530 } catch (FileNotFoundException e) {
531 fail(e.getLocalizedMessage());
532 } catch (IOException e) {
533 fail(e.getLocalizedMessage());
534 } catch (BuilderException e) {
535 fail(e.getLocalizedMessage());