2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-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;
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
26 import java.io.IOException;
27 import java.nio.file.Paths;
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.appc.Request;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
41 public class VfwTest extends UsecasesBase {
44 * VFW Tosca Policy File.
46 private static final String TOSCA_LEGACY_POLICY_VFW = "src/test/resources/vfw/tosca-vfw.json";
47 private static final String TOSCA_COMPLIANT_POLICY_VFW = "src/test/resources/vfw/tosca-compliant-vfw.json";
50 * VFW Use case Messages.
52 private static final String APPC_SUCCESS = "src/test/resources/vfw/vfw.appc.success.json";
53 private static final String ONSET = "src/test/resources/vfw/vfw.onset.json";
56 * Topic trackers used by the VFW use case.
58 private TopicCallback<VirtualControlLoopNotification> policyClMgt;
59 private TopicCallback<Response> appcClSource;
60 private TopicCallback<Request> appcClSink;
65 private ToscaPolicy policy;
68 * Prepare PDP-D Framework for testing.
71 public static void prepareResouces() throws InterruptedException, CoderException, IOException {
78 * Take down the resources used by the test framework.
81 public static void takeDownResources() {
87 * Sunny day scenario for the VFW use case.
89 private void sunnyDay() throws IOException {
91 /* Inject an ONSET event over the DCAE topic */
92 injectOnTopic(DCAE_TOPIC, Paths.get(ONSET));
94 /* Wait to acquire a LOCK and a PDP-X PERMIT */
95 waitForLockAndPermit(policy, policyClMgt);
97 /* --- VFW Operation Execution --- */
99 /* Ensure that an APPC RESTART request was sent in response to the matching ONSET */
100 await().until(() -> !appcClSink.getMessages().isEmpty());
101 assertEquals("ModifyConfig", appcClSink.getMessages().remove().getAction());
103 /* Inject a 400 APPC Response Return over the APPC topic */
104 injectOnTopic(APPC_CL_TOPIC, Paths.get(APPC_SUCCESS));
106 /* Ensure that RESTART response is received */
107 await().until(() -> !appcClSource.getMessages().isEmpty());
108 assertEquals("SUCCESS", appcClSource.getMessages().remove().getStatus().getValue());
110 /* --- VFW Operation Completed --- */
112 /* Ensure that the VFW RESTART Operation is successfully completed */
113 await().until(() -> !policyClMgt.getMessages().isEmpty());
114 assertEquals(ControlLoopNotificationType.OPERATION_SUCCESS,
115 policyClMgt.getMessages().remove().getNotification());
117 /* --- VFW Transaction Completed --- */
118 waitForFinalSuccess(policy, policyClMgt);
122 * Sunny Day with Legacy Tosca Policy.
125 public void sunnyDayLegacy() throws InterruptedException, CoderException, IOException {
126 assertEquals(0, usecases.getDrools().factCount(USECASES));
127 policy = setupPolicy(TOSCA_LEGACY_POLICY_VFW);
128 assertEquals(2, usecases.getDrools().factCount(USECASES));
134 * Sunny Day with Tosca Compliant Policy.
137 public void sunnyDayCompliant() throws InterruptedException, CoderException, IOException {
138 assertEquals(0, usecases.getDrools().factCount(USECASES));
139 policy = setupPolicy(TOSCA_COMPLIANT_POLICY_VFW);
140 assertEquals(2, usecases.getDrools().factCount(USECASES));
149 public void topicsRegistration() {
150 policyClMgt = createTopicSinkCallback(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class);
151 appcClSink = createTopicSinkCallback(APPC_CL_TOPIC, Request.class);
152 appcClSource = createTopicSourceCallback(APPC_CL_TOPIC, Response.class);
156 * Unregister Topic Callbacks.
159 public void topicsUnregistration() {
160 if (policyClMgt != null) {
161 policyClMgt.unregister();
164 if (appcClSource != null) {
165 appcClSource.unregister();
168 if (appcClSink != null) {
169 appcClSink.unregister();
177 public void uninstallPolicy() throws InterruptedException {
178 assertEquals(2, usecases.getDrools().factCount(USECASES));
179 if (policy != null) {
180 deletePolicy(policy);
182 assertEquals(0, usecases.getDrools().factCount(USECASES));