2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.common.rules.test;
24 import static org.junit.Assert.assertEquals;
26 import java.util.Collections;
27 import java.util.List;
29 import java.util.UUID;
30 import java.util.concurrent.TimeUnit;
31 import java.util.function.Supplier;
32 import java.util.stream.Collectors;
33 import java.util.stream.Stream;
34 import lombok.AccessLevel;
37 import org.apache.commons.collections.MapUtils;
38 import org.awaitility.Awaitility;
39 import org.junit.Test;
40 import org.onap.policy.appc.Request;
41 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
46 import org.onap.policy.controlloop.ControlLoopNotificationType;
47 import org.onap.policy.controlloop.VirtualControlLoopNotification;
48 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
49 import org.onap.policy.drools.system.PolicyEngineConstants;
50 import org.onap.policy.drools.system.internal.SimpleLockManager;
51 import org.onap.policy.drools.system.internal.SimpleLockManager.SimpleLock;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 import org.onap.policy.sdnr.PciMessage;
54 import org.springframework.test.util.ReflectionTestUtils;
57 * Superclass used for rule tests.
59 public abstract class BaseTest {
60 private static final String APPC_RESTART_OP = "restart";
61 private static final String APPC_MODIFY_CONFIG_OP = "ModifyConfig";
63 private static final String SDNR_MODIFY_CONFIG_OP = "ModifyConfig";
64 private static final String SNDR_MODIFY_CONFIG_ANR_OP = "ModifyConfigANR";
67 * Canonical Topic Names.
69 protected static final String DCAE_TOPIC = "DCAE_TOPIC";
70 protected static final String APPC_LCM_WRITE_TOPIC = "APPC-LCM-WRITE";
71 protected static final String POLICY_CL_MGT_TOPIC = "POLICY-CL-MGT";
72 protected static final String APPC_LCM_READ_TOPIC = "APPC-LCM-READ";
73 protected static final String APPC_CL_TOPIC = "APPC-CL";
74 protected static final String SDNR_CL_TOPIC = "SDNR-CL";
75 protected static final String SDNR_CL_RSP_TOPIC = "SDNR-CL-RSP";
76 protected static final String A1P_CL_TOPIC = "A1-P";
77 protected static final String A1P_CL_RSP_TOPIC = "A1-P-RSP";
80 * Constants for each test case.
83 // service123 (i.e., multi-operation policy)
84 private static final String SERVICE123_TOSCA_COMPLIANT_POLICY = "service123/tosca-compliant-service123.json";
85 private static final String SERVICE123_ONSET = "service123/service123.onset.json";
86 private static final String SERVICE123_APPC_RESTART_FAILURE = "service123/service123.appc.restart.failure.json";
87 private static final String SERVICE123_APPC_REBUILD_FAILURE = "service123/service123.appc.rebuild.failure.json";
88 private static final String SERVICE123_APPC_MIGRATE_SUCCESS = "service123/service123.appc.migrate.success.json";
90 // duplicates (i.e., mutliple events in the engine at the same time)
91 private static final String DUPLICATES_TOSCA_COMPLIANT_POLICY = "duplicates/tosca-compliant-duplicates.json";
92 private static final String DUPLICATES_ONSET_1 = "duplicates/duplicates.onset.1.json";
93 private static final String DUPLICATES_ONSET_2 = "duplicates/duplicates.onset.2.json";
94 private static final String DUPLICATES_APPC_SUCCESS = "duplicates/duplicates.appc.success.json";
97 private static final String VCPE_TOSCA_COMPLIANT_POLICY = "vcpe/tosca-compliant-vcpe.json";
98 private static final String VCPE_ONSET_1 = "vcpe/vcpe.onset.1.json";
99 private static final String VCPE_ONSET_2 = "vcpe/vcpe.onset.2.json";
100 private static final String VCPE_ONSET_3 = "vcpe/vcpe.onset.3.json";
101 private static final String VCPE_APPC_SUCCESS = "vcpe/vcpe.appc.success.json";
104 private static final String VDNS_TOSCA_COMPLIANT_POLICY = "vdns/tosca-compliant-vdns.json";
105 private static final String VDNS_TOSCA_COMPLIANT_RAINY_POLICY = "vdns/tosca-compliant-vdns-rainy.json";
106 private static final String VDNS_ONSET = "vdns/vdns.onset.json";
109 private static final String VFW_TOSCA_COMPLIANT_POLICY = "vfw/tosca-compliant-vfw.json";
110 private static final String VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY = "vfw/tosca-compliant-timeout-vfw.json";
111 private static final String VFW_ONSET = "vfw/vfw.onset.json";
112 private static final String VFW_APPC_SUCCESS = "vfw/vfw.appc.success.json";
113 private static final String VFW_APPC_FAILURE = "vfw/vfw.appc.failure.json";
115 // 5G SON Legacy - PCI
116 private static final String VPCI_TOSCA_COMPLIANT_POLICY = "vpci/tosca-compliant-vpci.json";
117 private static final String VPCI_ONSET = "vpci/vpci.onset.json";
118 private static final String VPCI_SDNR_SUCCESS = "vpci/vpci.sdnr.success.json";
120 // 5G SON Legacy - ANR
121 private static final String VSONH_TOSCA_COMPLIANT_POLICY = "vsonh/tosca-compliant-vsonh.json";
122 private static final String VSONH_ONSET = "vsonh/vsonh.onset.json";
123 private static final String VSONH_SDNR_SUCCESS = "vsonh/vsonh.sdnr.success.json";
125 // 5G SON Use case Policies (Kohn+)
127 private static final String V5G_SON_O1_TOSCA_POLICY = "policies/v5gSonO1.policy.operational.input.tosca.json";
128 private static final String V5G_SON_O1_ONSET = "vpci/v5G.son.O1.onset.json";
129 private static final String V5G_SON_O1_SDNR_SUCCESS = "vpci/v5G.son.O1.sdnr.success.json";
130 private static final String MODIFY_O1_CONFIG_OPERATION = "ModifyO1Config";
132 private static final String V5G_SON_A1_TOSCA_POLICY = "policies/v5gSonA1.policy.operational.input.tosca.json";
133 private static final String V5G_SON_A1_ONSET = "vsonh/v5G.son.A1.onset.json";
134 private static final String V5G_SON_A1_SDNR_SUCCESS = "vsonh/v5G.son.A1.sdnr.success.json";
135 private static final String PUT_A1_POLICY_OPERATION = "putA1Policy";
137 * Coders used to decode requests and responses.
139 protected static final Coder APPC_LEGACY_CODER = new StandardCoderInstantAsMillis();
140 protected static final Coder APPC_LCM_CODER = new StandardCoder();
141 protected static final Coder POLICY_CL_MGT_CODER = new PolicyClMgtCoder();
144 * Coders used to decode requests and responses.
146 private static final Coder SDNR_CODER = new StandardCoder();
148 // these may be overridden by junit tests
149 protected static Supplier<HttpClients> httpClientMaker = HttpClients::new;
150 protected static Supplier<Simulators> simMaker = Simulators::new;
151 protected static Supplier<Topics> topicMaker = Topics::new;
153 protected static Rules rules;
154 protected static HttpClients httpClients;
155 protected static Simulators simulators;
157 // used to inject and wait for messages
158 @Getter(AccessLevel.PROTECTED)
159 @Setter(AccessLevel.PROTECTED)
160 protected static Topics topics;
162 // used to wait for messages on SINK topics
163 protected Listener<VirtualControlLoopNotification> policyClMgt;
164 protected Listener<Request> appcClSink;
165 protected Listener<AppcLcmDmaapWrapper> appcLcmRead;
166 protected Listener<PciMessage> sdnrClSink;
169 * Tosca Policy that was loaded.
171 protected ToscaPolicy policy;
174 * Initializes {@link #rules}, {@link #httpClients}, and {@link #simulators}.
176 public static void initStatics() {
177 httpClients = httpClientMaker.get();
178 simulators = simMaker.get();
182 * Destroys {@link #httpClients}, {@link #simulators}, and {@link #rules}.
184 public static void finishStatics() {
185 httpClients.destroy();
186 simulators.destroy();
190 * Initializes {@link #topics}.
193 setTopics(topicMaker.get());
195 Map<String, SimpleLock> locks = getLockMap();
196 if (!MapUtils.isEmpty(locks)) {
202 * Destroys {@link #topics} and resets the rule facts.
204 public void finish() {
208 // Service123 (i.e., Policy with multiple operations)
211 * Service123 with Tosca Compliant Policy.
214 public void testService123Compliant() {
215 policyClMgt = createNoficationTopicListener();
216 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
217 policy = checkPolicy(SERVICE123_TOSCA_COMPLIANT_POLICY);
219 // inject an ONSET event over the DCAE topic
220 topics.inject(DCAE_TOPIC, SERVICE123_ONSET);
221 /* Wait to acquire a LOCK and a PDP-X PERMIT */
223 waitForLockAndPermit(policy, policyClMgt);
225 // restart request should be sent and fail four times (i.e., because retry=3)
226 for (var count = 0; count < 4; ++count) {
227 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
229 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_RESTART_FAILURE,
230 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
232 // rebuild request should be sent and fail once
233 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> "rebuild".equals(req.getRpcName()));
234 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_REBUILD_FAILURE,
235 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
236 // migrate request should be sent and succeed
237 appcreq = appcLcmRead.await(req -> "migrate".equals(req.getRpcName()));
238 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_MIGRATE_SUCCESS,
239 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
240 /* --- Operation Completed --- */
241 waitForOperationSuccess();
242 /* --- Transaction Completed --- */
243 waitForFinalSuccess(policy, policyClMgt);
251 * This test case tests the scenario where 3 events occur and 2 of the requests refer
252 * to the same target entity while the 3rd is for another entity. The expected result
253 * is that the event with the duplicate target entity will have a final success result
254 * for one of the events, and a rejected message for the one that was unable to obtain
255 * the lock. The event that is referring to a different target entity should be able
256 * to obtain a lock since it is a different target. After processing of all events
257 * there should only be the policy and params objects left in memory.
260 public void testDuplicatesEvents() {
261 policyClMgt = createNoficationTopicListener();
262 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
264 policy = checkPolicy(DUPLICATES_TOSCA_COMPLIANT_POLICY);
266 final long initCount = getCreateCount();
269 * Inject ONSET events over the DCAE topic. First and last have the same target
270 * entity, but different request IDs - only one should succeed. The middle one is
271 * for a different target entity, so it should succeed.
273 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
274 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_2);
275 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
277 // should see two restarts
278 for (var count = 0; count < 2; ++count) {
279 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
282 topics.inject(APPC_LCM_WRITE_TOPIC, DUPLICATES_APPC_SUCCESS,
283 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
286 // should see two FINAL successes
287 VirtualControlLoopNotification notif1 = waitForFinalSuccess(policy, policyClMgt);
288 VirtualControlLoopNotification notif2 = waitForFinalSuccess(policy, policyClMgt);
290 // get the list of target names, so we can ensure there's one of each
291 List<String> actual = Stream.of(notif1, notif2).map(notif -> notif.getAai().get("generic-vnf.vnf-id"))
292 .sorted().collect(Collectors.toList());
294 assertEquals(List.of("duplicate-VNF", "vCPE_Infrastructure_vGMUX_demo_app").toString(), actual.toString());
296 long added = getCreateCount() - initCount;
297 assertEquals(2, added);
305 * Sunny Day with Tosca Compliant Policy.
308 public void testVcpeSunnyDayCompliant() {
309 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
313 * An ONSET flood prevention test that injects a few ONSETs at once. It attempts to
314 * simulate the flooding behavior of the DCAE TCA microservice. TCA could blast tens
315 * or hundreds of ONSETs within sub-second intervals.
318 public void testVcpeOnsetFloodPrevention() {
319 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, List.of(VCPE_ONSET_1, VCPE_ONSET_2, VCPE_ONSET_3),
326 * Sunny Day with Tosca Compliant Policy.
329 public void testVdnsSunnyDayCompliant() {
330 httpSunnyDay(VDNS_TOSCA_COMPLIANT_POLICY, VDNS_ONSET);
334 * Vdns Rainy Day with Compliant Tosca Policy.
337 public void testVdnsRainyDayCompliant() {
338 httpRainyDay(VDNS_TOSCA_COMPLIANT_RAINY_POLICY, VDNS_ONSET);
345 * VFW Sunny Day with Tosca Compliant Policy.
348 public void testVfwSunnyDayCompliant() {
349 appcLegacySunnyDay(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
353 * VFW Rainy Day using compliant tosca policy (final failure).
356 public void testVfwRainyDayOverallTimeout() {
357 appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
361 * VFW Rainy day using compliant tosca policy (final failure due to timeout).
364 public void testVfwRainyDayCompliantTimeout() {
365 appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
369 * VPCI Sunny Day Tosca Policy.
372 public void testVpciSunnyDayCompliant() {
373 sdnrSunnyDay(VPCI_TOSCA_COMPLIANT_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS,
374 SDNR_MODIFY_CONFIG_OP, SDNR_CL_TOPIC, SDNR_CL_RSP_TOPIC);
380 * VSONH Sunny Day with Tosca Policy.
383 public void testVsonhSunnyDayCompliant() {
384 sdnrSunnyDay(VSONH_TOSCA_COMPLIANT_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS,
385 SNDR_MODIFY_CONFIG_ANR_OP, SDNR_CL_TOPIC, SDNR_CL_RSP_TOPIC);
389 * Sunny day 5G SON 01 Modify01Config Operational Policy.
392 public void test5gSonO1SunnyDayCompliant() {
393 sdnrSunnyDay(V5G_SON_O1_TOSCA_POLICY, V5G_SON_O1_ONSET, V5G_SON_O1_SDNR_SUCCESS,
394 MODIFY_O1_CONFIG_OPERATION, SDNR_CL_TOPIC, SDNR_CL_RSP_TOPIC);
398 * Sunny day 5G SON A1 ModifyA1Policy Operational Policy.
401 public void test5gSonA1SunnyDayCompliant() {
402 sdnrSunnyDay(V5G_SON_A1_TOSCA_POLICY, V5G_SON_A1_ONSET, V5G_SON_A1_SDNR_SUCCESS,
403 PUT_A1_POLICY_OPERATION, A1P_CL_TOPIC, A1P_CL_RSP_TOPIC);
407 * Sunny day scenario for use cases that use APPC-LCM.
409 * @param policyFile file containing the ToscaPolicy to be loaded
410 * @param onsetFile file containing the ONSET to be injected
411 * @param operation expected APPC operation request
413 protected void appcLcmSunnyDay(String policyFile, String onsetFile, String operation) {
414 appcLcmSunnyDay(policyFile, List.of(onsetFile), operation);
418 * Sunny day scenario for use cases that use APPC-LCM.
420 * @param policyFile file containing the ToscaPolicy to be loaded
421 * @param onsetFiles list of files containing the ONSET to be injected
422 * @param operation expected APPC operation request
424 protected void appcLcmSunnyDay(String policyFile, List<String> onsetFiles, String operation) {
425 policyClMgt = createNoficationTopicListener();
426 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
428 policy = checkPolicy(policyFile);
431 // inject several ONSET events over the DCAE topic
432 for (String onsetFile : onsetFiles) {
433 topics.inject(DCAE_TOPIC, onsetFile);
436 /* Wait to acquire a LOCK and a PDP-X PERMIT */
437 waitForLockAndPermit(policy, policyClMgt);
440 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
442 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> operation.equals(req.getRpcName()));
445 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
448 topics.inject(APPC_LCM_WRITE_TOPIC, VCPE_APPC_SUCCESS,
449 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
451 /* --- Operation Completed --- */
453 waitForOperationSuccess();
455 /* --- Transaction Completed --- */
456 waitForFinalSuccess(policy, policyClMgt);
462 * Sunny day scenario for use cases that use Legacy APPC.
464 * @param policyFile file containing the ToscaPolicy to be loaded
465 * @param onsetFile file containing the ONSET to be injected
466 * @param operation expected APPC operation request
468 protected void appcLegacySunnyDay(String policyFile, String onsetFile, String operation) {
469 policyClMgt = createNoficationTopicListener();
470 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
472 policy = checkPolicy(policyFile);
474 /* Inject an ONSET event over the DCAE topic */
475 topics.inject(DCAE_TOPIC, onsetFile);
477 /* Wait to acquire a LOCK and a PDP-X PERMIT */
478 waitForLockAndPermit(policy, policyClMgt);
481 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
483 Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
486 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
489 topics.inject(APPC_CL_TOPIC, VFW_APPC_SUCCESS, appcreq.getCommonHeader().getSubRequestId());
491 /* --- Operation Completed --- */
493 waitForOperationSuccess();
495 /* --- Transaction Completed --- */
496 waitForFinalSuccess(policy, policyClMgt);
502 * Rainy day scenario for use cases that use Legacy APPC.
504 * @param policyFile file containing the ToscaPolicy to be loaded
505 * @param onsetFile file containing the ONSET to be injected
506 * @param operation expected APPC operation request
508 protected void appcLegacyRainyDay(String policyFile, String onsetFile, String operation) {
509 policyClMgt = createNoficationTopicListener();
510 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
512 policy = checkPolicy(policyFile);
514 /* Inject an ONSET event over the DCAE topic */
515 topics.inject(DCAE_TOPIC, onsetFile);
517 /* Wait to acquire a LOCK and a PDP-X PERMIT */
518 waitForLockAndPermit(policy, policyClMgt);
521 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
523 Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
526 * Inject a 401 APPC Response Return over the APPC topic, with appropriate
529 topics.inject(APPC_CL_TOPIC, VFW_APPC_FAILURE, appcreq.getCommonHeader().getSubRequestId());
531 /* --- Operation Completed --- */
532 waitForOperationFailure();
534 /* --- Transaction Completed --- */
535 waitForFinalFailure(policy, policyClMgt);
541 * Rainy day scenario for use cases that use Legacy APPC. Expected to fail due to
544 * @param policyFile file containing the ToscaPolicy to be loaded
545 * @param onsetFile file containing the ONSET to be injected
546 * @param operation expected APPC operation request
548 protected void appcLegacyRainyDayNoResponse(String policyFile, String onsetFile, String operation) {
549 policyClMgt = createNoficationTopicListener();
550 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
552 policy = checkPolicy(policyFile);
554 /* Inject an ONSET event over the DCAE topic */
555 topics.inject(DCAE_TOPIC, onsetFile);
557 /* Wait to acquire a LOCK and a PDP-X PERMIT */
558 waitForLockAndPermit(policy, policyClMgt);
561 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
563 appcClSink.await(req -> operation.equals(req.getAction()));
566 * Do not inject an APPC Response.
569 /* --- Transaction Completed --- */
570 waitForFinalFailure(policy, policyClMgt);
576 * Sunny day scenario for use cases that use SDNR.
578 * @param policyFile file containing the ToscaPolicy to be loaded
579 * @param onsetFile file containing the ONSET to be injected
580 * @param operation expected SDNR operation request
582 protected void sdnrSunnyDay(String policyFile, String onsetFile,
583 String successFile, String operation,
584 String requestTopic, String responseTopic) {
585 policyClMgt = createNoficationTopicListener();
586 sdnrClSink = topics.createListener(requestTopic, PciMessage.class, SDNR_CODER);
588 policy = checkPolicy(policyFile);
590 /* Inject an ONSET event over the DCAE topic */
591 topics.inject(DCAE_TOPIC, onsetFile);
593 /* Wait to acquire a LOCK and a PDP-X PERMIT */
594 waitForLockAndPermit(policy, policyClMgt);
597 * Ensure that an SDNR RESTART request was sent in response to the matching ONSET
599 PciMessage pcireq = sdnrClSink.await(req -> operation.equals(req.getBody().getInput().getAction()));
604 topics.inject(responseTopic, successFile, pcireq.getBody().getInput().getCommonHeader().getSubRequestId());
606 /* --- Operation Completed --- */
608 waitForOperationSuccess();
610 /* --- Transaction Completed --- */
611 waitForFinalSuccess(policy, policyClMgt);
617 * Sunny day scenario for use cases that use an HTTP simulator.
619 * @param policyFile file containing the ToscaPolicy to be loaded
620 * @param onsetFile file containing the ONSET to be injected
622 protected void httpSunnyDay(String policyFile, String onsetFile) {
623 policyClMgt = createNoficationTopicListener();
625 policy = checkPolicy(policyFile);
627 /* Inject an ONSET event over the DCAE topic */
628 topics.inject(DCAE_TOPIC, onsetFile);
630 /* Wait to acquire a LOCK and a PDP-X PERMIT */
631 waitForLockAndPermit(policy, policyClMgt);
633 /* --- Operation Completed --- */
635 waitForOperationSuccess();
637 /* --- Transaction Completed --- */
638 waitForFinalSuccess(policy, policyClMgt);
644 * Rainy day scenario for use cases that use an HTTP simulator.
646 * @param policyFile file containing the ToscaPolicy to be loaded
647 * @param onsetFile file containing the ONSET to be injected
649 protected void httpRainyDay(String policyFile, String onsetFile) {
650 policyClMgt = createNoficationTopicListener();
652 policy = checkPolicy(policyFile);
654 /* Inject an ONSET event over the DCAE topic */
655 topics.inject(DCAE_TOPIC, onsetFile);
657 /* Wait to acquire a LOCK and a PDP-X PERMIT */
658 waitForLockAndPermit(policy, policyClMgt);
660 /* --- Operation Completed --- */
661 waitForOperationFailure();
663 /* --- Transaction Completed --- */
664 waitForFinalFailure(policy, policyClMgt);
669 protected long getCreateCount() {
670 return ControlLoopEventManager.getCreateCount();
674 * Waits for a OPERATION SUCCESS transaction notification.
676 protected void waitForOperationSuccess() {
677 policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_SUCCESS);
681 * Waits for a FINAL SUCCESS transaction notification.
683 * @return the FINAL SUCCESS notification
685 protected VirtualControlLoopNotification waitForFinalSuccess(ToscaPolicy policy,
686 Listener<VirtualControlLoopNotification> policyClMgt) {
688 return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_SUCCESS);
692 * Waits for a OPERATION FAILURE transaction notification.
694 protected void waitForOperationFailure() {
695 policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_FAILURE);
699 * Waits for a FINAL FAILURE transaction notification.
701 * @return the FINAL FAILURE notification
703 protected VirtualControlLoopNotification waitForFinalFailure(ToscaPolicy policy,
704 Listener<VirtualControlLoopNotification> policyClMgt) {
706 return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_FAILURE);
710 * Waits for notifications for LOCK acquisition and GUARD Permit so that event
711 * processing may proceed.
713 protected abstract void waitForLockAndPermit(ToscaPolicy policy,
714 Listener<VirtualControlLoopNotification> policyClMgt);
717 * Waits for a FINAL transaction notification.
719 * @param finalType FINAL_xxx type for which to wait
721 * @return the FINAL notification
723 protected abstract VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
724 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType);
727 * Returns ToscaPolicy from File.
729 * @param fileName a path name
730 * @return ToscaPolicy
732 protected ToscaPolicy checkPolicy(String fileName) {
734 return Rules.getPolicyFromFile(fileName);
735 } catch (CoderException e) {
736 throw new IllegalArgumentException(fileName, e);
741 * Creates a Coder for PolicyClMgt from StandardCoder.
744 public static class PolicyClMgtCoder extends StandardCoder {
745 public PolicyClMgtCoder() {
746 super(org.onap.policy.controlloop.util.Serialization.gson,
747 org.onap.policy.controlloop.util.Serialization.gsonPretty);
752 * Returns Listener from createListener based on Coder.
754 * @return the Listener
756 protected Listener<VirtualControlLoopNotification> createNoficationTopicListener() {
757 return topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, POLICY_CL_MGT_CODER);
761 * Verifies that all locks have been released, waiting a bit, if necessary.
763 private void verifyUnlocked() {
764 Map<String, SimpleLock> locks = getLockMap();
765 if (!MapUtils.isEmpty(locks)) {
766 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(locks::isEmpty);
770 @SuppressWarnings("unchecked")
771 private Map<String, SimpleLock> getLockMap() {
772 Object lockMgr = ReflectionTestUtils.getField(PolicyEngineConstants.getManager(), "lockManager");
773 if (lockMgr instanceof SimpleLockManager) {
774 return (Map<String, SimpleLock>) ReflectionTestUtils.getField(lockMgr, "resource2lock");
777 return Collections.emptyMap();