Tosca compliant vFirewall
[policy/drools-applications.git] / controlloop / common / controller-usecases / src / test / java / org / onap / policy / controlloop / VfwTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop;
22
23 import static org.awaitility.Awaitility.await;
24 import static org.junit.Assert.assertEquals;
25
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;
37
38 /**
39  * VFW Use Case Tests.
40  */
41 public class VfwTest extends UsecasesBase {
42
43     /**
44      * VFW Tosca Policy File.
45      */
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";
48
49     /*
50      * VFW Use case Messages.
51      */
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";
54
55     /*
56      * Topic trackers used by the VFW use case.
57      */
58     private TopicCallback<VirtualControlLoopNotification> policyClMgt;
59     private TopicCallback<Response> appcClSource;
60     private TopicCallback<Request> appcClSink;
61
62     /*
63      * VFW Tosca Policy.
64      */
65     private ToscaPolicy policy;
66
67     /**
68      * Prepare PDP-D Framework for testing.
69      */
70     @BeforeClass
71     public static void prepareResouces() throws InterruptedException, CoderException, IOException {
72         setupLogging();
73         preparePdpD();
74         setupSimulators();
75     }
76
77     /**
78      * Take down the resources used by the test framework.
79      */
80     @AfterClass
81     public static void takeDownResources() {
82         stopPdpD();
83         stopSimulators();
84     }
85
86     /**
87      * Sunny day scenario for the VFW use case.
88      */
89     private void sunnyDay() throws IOException {
90
91         /* Inject an ONSET event over the DCAE topic */
92         injectOnTopic(DCAE_TOPIC, Paths.get(ONSET));
93
94         /* Wait to acquire a LOCK and a PDP-X PERMIT */
95         waitForLockAndPermit(policy, policyClMgt);
96
97         /* --- VFW Operation Execution --- */
98
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());
102
103         /* Inject a 400 APPC Response Return over the APPC topic */
104         injectOnTopic(APPC_CL_TOPIC, Paths.get(APPC_SUCCESS));
105
106         /* Ensure that RESTART response is received */
107         await().until(() -> !appcClSource.getMessages().isEmpty());
108         assertEquals("SUCCESS", appcClSource.getMessages().remove().getStatus().getValue());
109
110         /* --- VFW Operation Completed --- */
111
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());
116
117         /* --- VFW Transaction Completed --- */
118         waitForFinalSuccess(policy, policyClMgt);
119     }
120
121     /**
122      * Sunny Day with Legacy Tosca Policy.
123      */
124     @Test
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));
129
130         sunnyDay();
131     }
132
133     /**
134      * Sunny Day with Tosca Compliant Policy.
135      */
136     @Test
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));
141
142         sunnyDay();
143     }
144
145     /**
146      * Observe Topics.
147      */
148     @Before
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);
153     }
154
155     /**
156      * Unregister Topic Callbacks.
157      */
158     @After
159     public void topicsUnregistration() {
160         if (policyClMgt != null) {
161             policyClMgt.unregister();
162         }
163
164         if (appcClSource != null) {
165             appcClSource.unregister();
166         }
167
168         if (appcClSink != null) {
169             appcClSink.unregister();
170         }
171     }
172
173     /**
174      * Uninstall Policy.
175      */
176     @After
177     public void uninstallPolicy() throws InterruptedException {
178         assertEquals(2, usecases.getDrools().factCount(USECASES));
179         if (policy != null) {
180             deletePolicy(policy);
181         }
182         assertEquals(0, usecases.getDrools().factCount(USECASES));
183     }
184
185 }