2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.common.rules.test;
23 import static org.junit.Assert.assertEquals;
25 import java.util.List;
26 import java.util.UUID;
27 import java.util.function.Function;
28 import java.util.function.Supplier;
29 import java.util.stream.Collectors;
30 import lombok.AccessLevel;
32 import org.junit.Test;
33 import org.onap.policy.appc.Request;
34 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
35 import org.onap.policy.common.utils.coder.Coder;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
38 import org.onap.policy.controlloop.ControlLoopNotificationType;
39 import org.onap.policy.controlloop.VirtualControlLoopNotification;
40 import org.onap.policy.drools.system.PolicyController;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 * Superclass used for rule tests.
46 public abstract class BaseRuleTest {
47 private static final String APPC_RESTART_OP = "restart";
50 * Canonical Topic Names.
52 protected static final String DCAE_TOPIC = "DCAE_TOPIC";
53 protected static final String APPC_LCM_WRITE_TOPIC = "APPC-LCM-WRITE";
54 protected static final String POLICY_CL_MGT_TOPIC = "POLICY-CL-MGT";
55 protected static final String APPC_LCM_READ_TOPIC = "APPC-LCM-READ";
56 protected static final String APPC_CL_TOPIC = "APPC-CL";
59 * Constants for each test case.
62 // service123 (i.e., multi-operation policy)
63 private static final String SERVICE123_TOSCA_COMPLIANT_POLICY = "service123/tosca-compliant-service123.json";
64 private static final String SERVICE123_ONSET = "service123/service123.onset.json";
65 private static final String SERVICE123_APPC_RESTART_FAILURE = "service123/service123.appc.restart.failure.json";
66 private static final String SERVICE123_APPC_REBUILD_FAILURE = "service123/service123.appc.rebuild.failure.json";
67 private static final String SERVICE123_APPC_MIGRATE_SUCCESS = "service123/service123.appc.migrate.success.json";
69 // duplicates (i.e., mutliple events in the engine at the same time)
70 private static final String DUPLICATES_TOSCA_COMPLIANT_POLICY = "duplicates/tosca-compliant-duplicates.json";
71 private static final String DUPLICATES_ONSET_1 = "duplicates/duplicates.onset.1.json";
72 private static final String DUPLICATES_ONSET_2 = "duplicates/duplicates.onset.2.json";
73 private static final String DUPLICATES_APPC_SUCCESS = "duplicates/duplicates.appc.success.json";
76 private static final String VCPE_TOSCA_LEGACY_POLICY = "vcpe/tosca-legacy-vcpe.json";
77 private static final String VCPE_TOSCA_COMPLIANT_POLICY = "vcpe/tosca-compliant-vcpe.json";
78 private static final String VCPE_ONSET_1 = "vcpe/vcpe.onset.1.json";
79 private static final String VCPE_ONSET_2 = "vcpe/vcpe.onset.2.json";
80 private static final String VCPE_ONSET_3 = "vcpe/vcpe.onset.3.json";
81 private static final String VCPE_APPC_SUCCESS = "vcpe/vcpe.appc.success.json";
84 private static final String VDNS_TOSCA_COMPLIANT_POLICY = "vdns/tosca-compliant-vdns.json";
85 private static final String VDNS_ONSET = "vdns/vdns.onset.json";
88 private static final String VFW_TOSCA_LEGACY_POLICY = "vfw/tosca-vfw.json";
89 private static final String VFW_TOSCA_COMPLIANT_POLICY = "vfw/tosca-compliant-vfw.json";
90 private static final String VFW_ONSET = "vfw/vfw.onset.json";
91 private static final String VFW_APPC_SUCCESS = "vfw/vfw.appc.success.json";
94 private static final String VLB_TOSCA_LEGACY_POLICY = "vlb/tosca-vlb.json";
95 private static final String VLB_TOSCA_COMPLIANT_POLICY = "vlb/tosca-compliant-vlb.json";
96 private static final String VLB_ONSET = "vlb/vlb.onset.json";
99 * Coders used to decode requests and responses.
101 private static final Coder APPC_LEGACY_CODER = new StandardCoderInstantAsMillis();
102 private static final Coder APPC_LCM_CODER = new StandardCoder();
104 // these may be overridden by junit tests
105 private static Function<String, Rules> ruleMaker = Rules::new;
106 private static Supplier<HttpClients> httpClientMaker = HttpClients::new;
107 private static Supplier<Simulators> simMaker = Simulators::new;
108 private static Supplier<Topics> topicMaker = Topics::new;
110 protected static Rules rules;
111 protected static HttpClients httpClients;
112 protected static Simulators simulators;
115 // used to inject and wait for messages
116 @Getter(AccessLevel.PROTECTED)
117 private Topics topics;
119 // used to wait for messages on SINK topics
120 protected Listener<VirtualControlLoopNotification> policyClMgt;
121 protected Listener<Request> appcClSink;
122 protected Listener<AppcLcmDmaapWrapper> appcLcmRead;
124 protected PolicyController controller;
127 * Tosca Policy that was loaded.
129 protected ToscaPolicy policy;
133 * Initializes {@link #rules}, {@link #httpClients}, and {@link #simulators}.
135 * @param controllerName the rule controller name
137 public static void initStatics(String controllerName) {
138 rules = ruleMaker.apply(controllerName);
139 httpClients = httpClientMaker.get();
140 simulators = simMaker.get();
144 * Destroys {@link #httpClients}, {@link #simulators}, and {@link #rules}.
146 public static void finishStatics() {
147 httpClients.destroy();
148 simulators.destroy();
153 * Initializes {@link #topics} and {@link #controller}.
156 topics = topicMaker.get();
157 controller = rules.getController();
161 * Destroys {@link #topics} and resets the rule facts.
163 public void finish() {
168 // Service123 (i.e., Policy with multiple operations)
171 * Service123 with Tosca Compliant Policy.
174 public void testService123Compliant() {
175 policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
176 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
178 assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
179 policy = rules.setupPolicyFromFile(SERVICE123_TOSCA_COMPLIANT_POLICY);
180 assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
182 // inject an ONSET event over the DCAE topic
183 topics.inject(DCAE_TOPIC, SERVICE123_ONSET);
185 /* Wait to acquire a LOCK and a PDP-X PERMIT */
186 waitForLockAndPermit(policy, policyClMgt);
188 // restart request should be sent and fail four times (i.e., because retry=3)
189 for (int count = 0; count < 4; ++count) {
190 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
192 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_RESTART_FAILURE,
193 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
196 // rebuild request should be sent and fail once
197 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> "rebuild".equals(req.getRpcName()));
199 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_REBUILD_FAILURE,
200 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
202 // migrate request should be sent and succeed
203 appcreq = appcLcmRead.await(req -> "migrate".equals(req.getRpcName()));
205 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_MIGRATE_SUCCESS,
206 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
208 /* --- Operation Completed --- */
210 waitForOperationSuccess();
212 /* --- Transaction Completed --- */
213 waitForFinalSuccess(policy, policyClMgt);
219 * This test case tests the scenario where 3 events occur and 2 of the requests refer
220 * to the same target entity while the 3rd is for another entity. The expected result
221 * is that the event with the duplicate target entity will have a final success result
222 * for one of the events, and a rejected message for the one that was unable to obtain
223 * the lock. The event that is referring to a different target entity should be able
224 * to obtain a lock since it is a different target. After processing of all events
225 * there should only be the policy and params objects left in memory.
228 public void testDuplicatesEvents() {
229 policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
230 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
232 assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
233 policy = rules.setupPolicyFromFile(DUPLICATES_TOSCA_COMPLIANT_POLICY);
234 assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
237 * Inject ONSET events over the DCAE topic. First and last have the same target
238 * entity, but different request IDs - only one should succeed. The middle one is
239 * for a different target entity, so it should succeed.
241 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
242 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_2);
243 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
245 // one should immediately generate a FINAL failure
246 waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_FAILURE);
248 // should see two restarts
249 for (int count = 0; count < 2; ++count) {
250 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
253 topics.inject(APPC_LCM_WRITE_TOPIC, DUPLICATES_APPC_SUCCESS,
254 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
257 // should see two FINAL successes
258 VirtualControlLoopNotification notif1 = waitForFinalSuccess(policy, policyClMgt);
259 VirtualControlLoopNotification notif2 = waitForFinalSuccess(policy, policyClMgt);
261 // get the list of target names so we can ensure there's one of each
262 List<String> actual = List.of(notif1, notif2).stream().map(notif -> notif.getAai().get("generic-vnf.vnf-id"))
263 .sorted().collect(Collectors.toList());
265 assertEquals(List.of("duplicate-VNF", "vCPE_Infrastructure_vGMUX_demo_app").toString(), actual.toString());
271 * Sunny Day with Legacy Tosca Policy.
274 public void testVcpeSunnyDayLegacy() {
275 appcLcmSunnyDay(VCPE_TOSCA_LEGACY_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
279 * Sunny Day with Tosca Compliant Policy.
282 public void testVcpeSunnyDayCompliant() {
283 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
287 * An ONSET flood prevention test that injects a few ONSETs at once. It attempts to
288 * simulate the flooding behavior of the DCAE TCA microservice. TCA could blast tens
289 * or hundreds of ONSETs within sub-second intervals.
292 public void testVcpeOnsetFloodPrevention() {
293 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, List.of(VCPE_ONSET_1, VCPE_ONSET_2, VCPE_ONSET_3),
300 * Sunny Day with Tosca Compliant Policy.
303 public void testVdnsSunnyDayCompliant() {
304 httpSunnyDay(VDNS_TOSCA_COMPLIANT_POLICY, VDNS_ONSET);
310 * VFW Sunny Day with Legacy Tosca Policy.
313 public void testVfwSunnyDayLegacy() {
314 appcLegacySunnyDay(VFW_TOSCA_LEGACY_POLICY, VFW_ONSET, "ModifyConfig");
318 * VFW Sunny Day with Tosca Compliant Policy.
321 public void testVfwSunnyDayCompliant() {
322 appcLegacySunnyDay(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, "ModifyConfig");
328 * Sunny Day with Legacy Tosca Policy.
331 public void testVlbSunnyDayLegacy() {
332 httpSunnyDay(VLB_TOSCA_LEGACY_POLICY, VLB_ONSET);
336 * Sunny Day with Tosca Compliant Policy.
339 public void testVlbSunnyDayCompliant() {
340 httpSunnyDay(VLB_TOSCA_COMPLIANT_POLICY, VLB_ONSET);
344 * Sunny day scenario for use cases that use APPC-LCM.
346 * @param policyFile file containing the ToscaPolicy to be loaded
347 * @param onsetFile file containing the ONSET to be injected
348 * @param operation expected APPC operation request
350 protected void appcLcmSunnyDay(String policyFile, String onsetFile, String operation) {
351 appcLcmSunnyDay(policyFile, List.of(onsetFile), operation);
355 * Sunny day scenario for use cases that use APPC-LCM.
357 * @param policyFile file containing the ToscaPolicy to be loaded
358 * @param onsetFiles list of files containing the ONSET to be injected
359 * @param operation expected APPC operation request
361 protected void appcLcmSunnyDay(String policyFile, List<String> onsetFiles, String operation) {
362 policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
363 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
365 assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
366 policy = rules.setupPolicyFromFile(policyFile);
367 assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
369 // inject several ONSET events over the DCAE topic
370 for (String onsetFile : onsetFiles) {
371 topics.inject(DCAE_TOPIC, onsetFile);
374 /* Wait to acquire a LOCK and a PDP-X PERMIT */
375 waitForLockAndPermit(policy, policyClMgt);
378 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
380 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> operation.equals(req.getRpcName()));
383 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
386 topics.inject(APPC_LCM_WRITE_TOPIC, VCPE_APPC_SUCCESS,
387 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
389 /* --- Operation Completed --- */
391 waitForOperationSuccess();
393 /* --- Transaction Completed --- */
394 waitForFinalSuccess(policy, policyClMgt);
398 * Sunny day scenario for use cases that use Legacy APPC.
400 * @param policyFile file containing the ToscaPolicy to be loaded
401 * @param onsetFile file containing the ONSET to be injected
402 * @param operation expected APPC operation request
404 protected void appcLegacySunnyDay(String policyFile, String onsetFile, String operation) {
405 policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
406 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
408 assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
409 policy = rules.setupPolicyFromFile(policyFile);
410 assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
412 /* Inject an ONSET event over the DCAE topic */
413 topics.inject(DCAE_TOPIC, onsetFile);
415 /* Wait to acquire a LOCK and a PDP-X PERMIT */
416 waitForLockAndPermit(policy, policyClMgt);
419 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
421 Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
424 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
427 topics.inject(APPC_CL_TOPIC, VFW_APPC_SUCCESS, appcreq.getCommonHeader().getSubRequestId());
429 /* --- Operation Completed --- */
431 waitForOperationSuccess();
433 /* --- Transaction Completed --- */
434 waitForFinalSuccess(policy, policyClMgt);
438 * Sunny day scenario for use cases that use an HTTP simulator.
440 * @param policyFile file containing the ToscaPolicy to be loaded
441 * @param onsetFile file containing the ONSET to be injected
442 * @param operation expected APPC operation request
444 protected void httpSunnyDay(String policyFile, String onsetFile) {
445 policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
447 assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
448 policy = rules.setupPolicyFromFile(policyFile);
449 assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
451 /* Inject an ONSET event over the DCAE topic */
452 topics.inject(DCAE_TOPIC, onsetFile);
454 /* Wait to acquire a LOCK and a PDP-X PERMIT */
455 waitForLockAndPermit(policy, policyClMgt);
457 /* --- Operation Completed --- */
459 waitForOperationSuccess();
461 /* --- Transaction Completed --- */
462 waitForFinalSuccess(policy, policyClMgt);
466 * Waits for a OPERATION SUCCESS transaction notification.
468 protected void waitForOperationSuccess() {
469 policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_SUCCESS);
473 * Waits for a FINAL SUCCESS transaction notification.
475 * @return the FINAL SUCCESS notification
477 protected VirtualControlLoopNotification waitForFinalSuccess(ToscaPolicy policy,
478 Listener<VirtualControlLoopNotification> policyClMgt) {
480 return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_SUCCESS);
484 * Waits for notifications for LOCK acquisition and GUARD Permit so that event
485 * processing may proceed.
487 protected abstract void waitForLockAndPermit(ToscaPolicy policy,
488 Listener<VirtualControlLoopNotification> policyClMgt);
491 * Waits for a FINAL transaction notification.
493 * @param finalType FINAL_xxx type for which to wait
495 * @return the FINAL notification
497 protected abstract VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
498 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType);