2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2022 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.Collections;
26 import java.util.List;
28 import java.util.UUID;
29 import java.util.concurrent.TimeUnit;
30 import java.util.function.Supplier;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import lombok.AccessLevel;
36 import org.apache.commons.collections.MapUtils;
37 import org.awaitility.Awaitility;
38 import org.junit.Test;
39 import org.onap.policy.appc.Request;
40 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
45 import org.onap.policy.controlloop.ControlLoopNotificationType;
46 import org.onap.policy.controlloop.VirtualControlLoopNotification;
47 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
48 import org.onap.policy.drools.system.PolicyEngineConstants;
49 import org.onap.policy.drools.system.internal.SimpleLockManager;
50 import org.onap.policy.drools.system.internal.SimpleLockManager.SimpleLock;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.sdnr.PciMessage;
53 import org.powermock.reflect.Whitebox;
56 * Superclass used for rule tests.
58 public abstract class BaseTest {
59 private static final String APPC_RESTART_OP = "restart";
60 private static final String APPC_MODIFY_CONFIG_OP = "ModifyConfig";
62 private static final String SDNR_MODIFY_CONFIG_OP = "ModifyConfig";
63 private static final String SNDR_MODIFY_CONFIG_ANR_OP = "ModifyConfigANR";
66 * Canonical Topic Names.
68 protected static final String DCAE_TOPIC = "DCAE_TOPIC";
69 protected static final String APPC_LCM_WRITE_TOPIC = "APPC-LCM-WRITE";
70 protected static final String POLICY_CL_MGT_TOPIC = "POLICY-CL-MGT";
71 protected static final String APPC_LCM_READ_TOPIC = "APPC-LCM-READ";
72 protected static final String APPC_CL_TOPIC = "APPC-CL";
73 protected static final String SDNR_CL_TOPIC = "SDNR-CL";
74 protected static final String SDNR_CL_RSP_TOPIC = "SDNR-CL-RSP";
77 * Constants for each test case.
80 // service123 (i.e., multi-operation policy)
81 private static final String SERVICE123_TOSCA_COMPLIANT_POLICY = "service123/tosca-compliant-service123.json";
82 private static final String SERVICE123_ONSET = "service123/service123.onset.json";
83 private static final String SERVICE123_APPC_RESTART_FAILURE = "service123/service123.appc.restart.failure.json";
84 private static final String SERVICE123_APPC_REBUILD_FAILURE = "service123/service123.appc.rebuild.failure.json";
85 private static final String SERVICE123_APPC_MIGRATE_SUCCESS = "service123/service123.appc.migrate.success.json";
87 // duplicates (i.e., mutliple events in the engine at the same time)
88 private static final String DUPLICATES_TOSCA_COMPLIANT_POLICY = "duplicates/tosca-compliant-duplicates.json";
89 private static final String DUPLICATES_ONSET_1 = "duplicates/duplicates.onset.1.json";
90 private static final String DUPLICATES_ONSET_2 = "duplicates/duplicates.onset.2.json";
91 private static final String DUPLICATES_APPC_SUCCESS = "duplicates/duplicates.appc.success.json";
94 private static final String VCPE_TOSCA_COMPLIANT_POLICY = "vcpe/tosca-compliant-vcpe.json";
95 private static final String VCPE_ONSET_1 = "vcpe/vcpe.onset.1.json";
96 private static final String VCPE_ONSET_2 = "vcpe/vcpe.onset.2.json";
97 private static final String VCPE_ONSET_3 = "vcpe/vcpe.onset.3.json";
98 private static final String VCPE_APPC_SUCCESS = "vcpe/vcpe.appc.success.json";
101 private static final String VDNS_TOSCA_COMPLIANT_POLICY = "vdns/tosca-compliant-vdns.json";
102 private static final String VDNS_TOSCA_COMPLIANT_RAINY_POLICY = "vdns/tosca-compliant-vdns-rainy.json";
103 private static final String VDNS_ONSET = "vdns/vdns.onset.json";
106 private static final String VFW_TOSCA_COMPLIANT_POLICY = "vfw/tosca-compliant-vfw.json";
107 private static final String VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY = "vfw/tosca-compliant-timeout-vfw.json";
108 private static final String VFW_ONSET = "vfw/vfw.onset.json";
109 private static final String VFW_APPC_SUCCESS = "vfw/vfw.appc.success.json";
110 private static final String VFW_APPC_FAILURE = "vfw/vfw.appc.failure.json";
112 // 5G SON Legacy - PCI
113 private static final String VPCI_TOSCA_COMPLIANT_POLICY = "vpci/tosca-compliant-vpci.json";
114 private static final String VPCI_ONSET = "vpci/vpci.onset.json";
115 private static final String VPCI_SDNR_SUCCESS = "vpci/vpci.sdnr.success.json";
117 // 5G SON Legacy - ANR
118 private static final String VSONH_TOSCA_COMPLIANT_POLICY = "vsonh/tosca-compliant-vsonh.json";
119 private static final String VSONH_ONSET = "vsonh/vsonh.onset.json";
120 private static final String VSONH_SDNR_SUCCESS = "vsonh/vsonh.sdnr.success.json";
122 // 5G SON Use case Policies (Kohn+)
124 private static final String V5G_SON_O1_TOSCA_POLICY = "policies/v5gSonO1.policy.operational.input.tosca.json";
125 private static final String V5G_SON_O1_ONSET = "vpci/v5G.son.O1.onset.json";
126 private static final String V5G_SON_O1_SDNR_SUCCESS = "vpci/v5G.son.O1.sdnr.success.json";
127 private static final String MODIFY_O1_CONFIG_OPERATION = "ModifyO1Config";
129 private static final String V5G_SON_A1_TOSCA_POLICY = "policies/v5gSonA1.policy.operational.input.tosca.json";
130 private static final String V5G_SON_A1_ONSET = "vsonh/v5G.son.A1.onset.json";
131 private static final String V5G_SON_A1_SDNR_SUCCESS = "vsonh/v5G.son.A1.sdnr.success.json";
132 private static final String MODIFY_A1_POLICY_OPERATION = "ModifyA1Policy";
134 * Coders used to decode requests and responses.
136 protected static final Coder APPC_LEGACY_CODER = new StandardCoderInstantAsMillis();
137 protected static final Coder APPC_LCM_CODER = new StandardCoder();
138 protected static final Coder POLICY_CL_MGT_CODER = new PolicyClMgtCoder();
141 * Coders used to decode requests and responses.
143 private static final Coder SDNR_CODER = new StandardCoder();
145 // these may be overridden by junit tests
146 protected static Supplier<HttpClients> httpClientMaker = HttpClients::new;
147 protected static Supplier<Simulators> simMaker = Simulators::new;
148 protected static Supplier<Topics> topicMaker = Topics::new;
150 protected static Rules rules;
151 protected static HttpClients httpClients;
152 protected static Simulators simulators;
154 // used to inject and wait for messages
155 @Getter(AccessLevel.PROTECTED)
156 @Setter(AccessLevel.PROTECTED)
157 protected static Topics topics;
159 // used to wait for messages on SINK topics
160 protected Listener<VirtualControlLoopNotification> policyClMgt;
161 protected Listener<Request> appcClSink;
162 protected Listener<AppcLcmDmaapWrapper> appcLcmRead;
163 protected Listener<PciMessage> sdnrClSink;
166 * Tosca Policy that was loaded.
168 protected ToscaPolicy policy;
171 * Initializes {@link #rules}, {@link #httpClients}, and {@link #simulators}.
173 public static void initStatics() {
174 httpClients = httpClientMaker.get();
175 simulators = simMaker.get();
179 * Destroys {@link #httpClients}, {@link #simulators}, and {@link #rules}.
181 public static void finishStatics() {
182 httpClients.destroy();
183 simulators.destroy();
187 * Initializes {@link #topics}.
190 setTopics(topicMaker.get());
192 Map<String, SimpleLock> locks = getLockMap();
193 if (!MapUtils.isEmpty(locks)) {
199 * Destroys {@link #topics} and resets the rule facts.
201 public void finish() {
205 // Service123 (i.e., Policy with multiple operations)
208 * Service123 with Tosca Compliant Policy.
211 public void testService123Compliant() {
212 policyClMgt = createNoficationTopicListener();
213 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
214 policy = checkPolicy(SERVICE123_TOSCA_COMPLIANT_POLICY);
216 // inject an ONSET event over the DCAE topic
217 topics.inject(DCAE_TOPIC, SERVICE123_ONSET);
218 /* Wait to acquire a LOCK and a PDP-X PERMIT */
220 waitForLockAndPermit(policy, policyClMgt);
222 // restart request should be sent and fail four times (i.e., because retry=3)
223 for (var count = 0; count < 4; ++count) {
224 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
226 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_RESTART_FAILURE,
227 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
229 // rebuild request should be sent and fail once
230 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> "rebuild".equals(req.getRpcName()));
231 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_REBUILD_FAILURE,
232 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
233 // migrate request should be sent and succeed
234 appcreq = appcLcmRead.await(req -> "migrate".equals(req.getRpcName()));
235 topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_MIGRATE_SUCCESS,
236 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
237 /* --- Operation Completed --- */
238 waitForOperationSuccess();
239 /* --- Transaction Completed --- */
240 waitForFinalSuccess(policy, policyClMgt);
248 * This test case tests the scenario where 3 events occur and 2 of the requests refer
249 * to the same target entity while the 3rd is for another entity. The expected result
250 * is that the event with the duplicate target entity will have a final success result
251 * for one of the events, and a rejected message for the one that was unable to obtain
252 * the lock. The event that is referring to a different target entity should be able
253 * to obtain a lock since it is a different target. After processing of all events
254 * there should only be the policy and params objects left in memory.
257 public void testDuplicatesEvents() {
258 policyClMgt = createNoficationTopicListener();
259 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
261 policy = checkPolicy(DUPLICATES_TOSCA_COMPLIANT_POLICY);
263 final long initCount = getCreateCount();
266 * Inject ONSET events over the DCAE topic. First and last have the same target
267 * entity, but different request IDs - only one should succeed. The middle one is
268 * for a different target entity, so it should succeed.
270 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
271 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_2);
272 topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
274 // should see two restarts
275 for (var count = 0; count < 2; ++count) {
276 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
279 topics.inject(APPC_LCM_WRITE_TOPIC, DUPLICATES_APPC_SUCCESS,
280 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
283 // should see two FINAL successes
284 VirtualControlLoopNotification notif1 = waitForFinalSuccess(policy, policyClMgt);
285 VirtualControlLoopNotification notif2 = waitForFinalSuccess(policy, policyClMgt);
287 // get the list of target names, so we can ensure there's one of each
288 List<String> actual = Stream.of(notif1, notif2).map(notif -> notif.getAai().get("generic-vnf.vnf-id"))
289 .sorted().collect(Collectors.toList());
291 assertEquals(List.of("duplicate-VNF", "vCPE_Infrastructure_vGMUX_demo_app").toString(), actual.toString());
293 long added = getCreateCount() - initCount;
294 assertEquals(2, added);
302 * Sunny Day with Tosca Compliant Policy.
305 public void testVcpeSunnyDayCompliant() {
306 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
310 * An ONSET flood prevention test that injects a few ONSETs at once. It attempts to
311 * simulate the flooding behavior of the DCAE TCA microservice. TCA could blast tens
312 * or hundreds of ONSETs within sub-second intervals.
315 public void testVcpeOnsetFloodPrevention() {
316 appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, List.of(VCPE_ONSET_1, VCPE_ONSET_2, VCPE_ONSET_3),
323 * Sunny Day with Tosca Compliant Policy.
326 public void testVdnsSunnyDayCompliant() {
327 httpSunnyDay(VDNS_TOSCA_COMPLIANT_POLICY, VDNS_ONSET);
331 * Vdns Rainy Day with Compliant Tosca Policy.
334 public void testVdnsRainyDayCompliant() {
335 httpRainyDay(VDNS_TOSCA_COMPLIANT_RAINY_POLICY, VDNS_ONSET);
342 * VFW Sunny Day with Tosca Compliant Policy.
345 public void testVfwSunnyDayCompliant() {
346 appcLegacySunnyDay(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
350 * VFW Rainy Day using compliant tosca policy (final failure).
353 public void testVfwRainyDayOverallTimeout() {
354 appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
358 * VFW Rainy day using compliant tosca policy (final failure due to timeout).
361 public void testVfwRainyDayCompliantTimeout() {
362 appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
366 * VPCI Sunny Day Tosca Policy.
369 public void testVpciSunnyDayCompliant() {
370 sdnrSunnyDay(VPCI_TOSCA_COMPLIANT_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS, SDNR_MODIFY_CONFIG_OP);
376 * VSONH Sunny Day with Tosca Policy.
379 public void testVsonhSunnyDayCompliant() {
380 sdnrSunnyDay(VSONH_TOSCA_COMPLIANT_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS, SNDR_MODIFY_CONFIG_ANR_OP);
384 * Sunny day 5G SON 01 Modify01Config Operational Policy.
387 public void test5gSonO1SunnyDayCompliant() {
388 sdnrSunnyDay(V5G_SON_O1_TOSCA_POLICY, V5G_SON_O1_ONSET, V5G_SON_O1_SDNR_SUCCESS, MODIFY_O1_CONFIG_OPERATION);
392 * Sunny day 5G SON A1 ModifyA1Policy Operational Policy.
395 public void test5gSonA1SunnyDayCompliant() {
396 sdnrSunnyDay(V5G_SON_A1_TOSCA_POLICY, V5G_SON_A1_ONSET, V5G_SON_A1_SDNR_SUCCESS, MODIFY_A1_POLICY_OPERATION);
400 * Sunny day scenario for use cases that use APPC-LCM.
402 * @param policyFile file containing the ToscaPolicy to be loaded
403 * @param onsetFile file containing the ONSET to be injected
404 * @param operation expected APPC operation request
406 protected void appcLcmSunnyDay(String policyFile, String onsetFile, String operation) {
407 appcLcmSunnyDay(policyFile, List.of(onsetFile), operation);
411 * Sunny day scenario for use cases that use APPC-LCM.
413 * @param policyFile file containing the ToscaPolicy to be loaded
414 * @param onsetFiles list of files containing the ONSET to be injected
415 * @param operation expected APPC operation request
417 protected void appcLcmSunnyDay(String policyFile, List<String> onsetFiles, String operation) {
418 policyClMgt = createNoficationTopicListener();
419 appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
421 policy = checkPolicy(policyFile);
424 // inject several ONSET events over the DCAE topic
425 for (String onsetFile : onsetFiles) {
426 topics.inject(DCAE_TOPIC, onsetFile);
429 /* Wait to acquire a LOCK and a PDP-X PERMIT */
430 waitForLockAndPermit(policy, policyClMgt);
433 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
435 AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> operation.equals(req.getRpcName()));
438 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
441 topics.inject(APPC_LCM_WRITE_TOPIC, VCPE_APPC_SUCCESS,
442 appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
444 /* --- Operation Completed --- */
446 waitForOperationSuccess();
448 /* --- Transaction Completed --- */
449 waitForFinalSuccess(policy, policyClMgt);
455 * Sunny day scenario for use cases that use Legacy APPC.
457 * @param policyFile file containing the ToscaPolicy to be loaded
458 * @param onsetFile file containing the ONSET to be injected
459 * @param operation expected APPC operation request
461 protected void appcLegacySunnyDay(String policyFile, String onsetFile, String operation) {
462 policyClMgt = createNoficationTopicListener();
463 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
465 policy = checkPolicy(policyFile);
467 /* Inject an ONSET event over the DCAE topic */
468 topics.inject(DCAE_TOPIC, onsetFile);
470 /* Wait to acquire a LOCK and a PDP-X PERMIT */
471 waitForLockAndPermit(policy, policyClMgt);
474 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
476 Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
479 * Inject a 400 APPC Response Return over the APPC topic, with appropriate
482 topics.inject(APPC_CL_TOPIC, VFW_APPC_SUCCESS, appcreq.getCommonHeader().getSubRequestId());
484 /* --- Operation Completed --- */
486 waitForOperationSuccess();
488 /* --- Transaction Completed --- */
489 waitForFinalSuccess(policy, policyClMgt);
495 * Rainy day scenario for use cases that use Legacy APPC.
497 * @param policyFile file containing the ToscaPolicy to be loaded
498 * @param onsetFile file containing the ONSET to be injected
499 * @param operation expected APPC operation request
501 protected void appcLegacyRainyDay(String policyFile, String onsetFile, String operation) {
502 policyClMgt = createNoficationTopicListener();
503 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
505 policy = checkPolicy(policyFile);
507 /* Inject an ONSET event over the DCAE topic */
508 topics.inject(DCAE_TOPIC, onsetFile);
510 /* Wait to acquire a LOCK and a PDP-X PERMIT */
511 waitForLockAndPermit(policy, policyClMgt);
514 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
516 Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
519 * Inject a 401 APPC Response Return over the APPC topic, with appropriate
522 topics.inject(APPC_CL_TOPIC, VFW_APPC_FAILURE, appcreq.getCommonHeader().getSubRequestId());
524 /* --- Operation Completed --- */
525 waitForOperationFailure();
527 /* --- Transaction Completed --- */
528 waitForFinalFailure(policy, policyClMgt);
534 * Rainy day scenario for use cases that use Legacy APPC. Expected to fail due to
537 * @param policyFile file containing the ToscaPolicy to be loaded
538 * @param onsetFile file containing the ONSET to be injected
539 * @param operation expected APPC operation request
541 protected void appcLegacyRainyDayNoResponse(String policyFile, String onsetFile, String operation) {
542 policyClMgt = createNoficationTopicListener();
543 appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
545 policy = checkPolicy(policyFile);
547 /* Inject an ONSET event over the DCAE topic */
548 topics.inject(DCAE_TOPIC, onsetFile);
550 /* Wait to acquire a LOCK and a PDP-X PERMIT */
551 waitForLockAndPermit(policy, policyClMgt);
554 * Ensure that an APPC RESTART request was sent in response to the matching ONSET
556 appcClSink.await(req -> operation.equals(req.getAction()));
559 * Do not inject an APPC Response.
562 /* --- Transaction Completed --- */
563 waitForFinalFailure(policy, policyClMgt);
569 * Sunny day scenario for use cases that use SDNR.
571 * @param policyFile file containing the ToscaPolicy to be loaded
572 * @param onsetFile file containing the ONSET to be injected
573 * @param operation expected SDNR operation request
575 protected void sdnrSunnyDay(String policyFile, String onsetFile, String successFile, String operation) {
576 policyClMgt = createNoficationTopicListener();
577 sdnrClSink = topics.createListener(SDNR_CL_TOPIC, PciMessage.class, SDNR_CODER);
579 policy = checkPolicy(policyFile);
581 /* Inject an ONSET event over the DCAE topic */
582 topics.inject(DCAE_TOPIC, onsetFile);
584 /* Wait to acquire a LOCK and a PDP-X PERMIT */
585 waitForLockAndPermit(policy, policyClMgt);
588 * Ensure that an SDNR RESTART request was sent in response to the matching ONSET
590 PciMessage pcireq = sdnrClSink.await(req -> operation.equals(req.getBody().getInput().getAction()));
595 topics.inject(SDNR_CL_RSP_TOPIC, successFile, pcireq.getBody().getInput().getCommonHeader().getSubRequestId());
597 /* --- Operation Completed --- */
599 waitForOperationSuccess();
601 /* --- Transaction Completed --- */
602 waitForFinalSuccess(policy, policyClMgt);
608 * Sunny day scenario for use cases that use an HTTP simulator.
610 * @param policyFile file containing the ToscaPolicy to be loaded
611 * @param onsetFile file containing the ONSET to be injected
613 protected void httpSunnyDay(String policyFile, String onsetFile) {
614 policyClMgt = createNoficationTopicListener();
616 policy = checkPolicy(policyFile);
618 /* Inject an ONSET event over the DCAE topic */
619 topics.inject(DCAE_TOPIC, onsetFile);
621 /* Wait to acquire a LOCK and a PDP-X PERMIT */
622 waitForLockAndPermit(policy, policyClMgt);
624 /* --- Operation Completed --- */
626 waitForOperationSuccess();
628 /* --- Transaction Completed --- */
629 waitForFinalSuccess(policy, policyClMgt);
635 * Rainy day scenario for use cases that use an HTTP simulator.
637 * @param policyFile file containing the ToscaPolicy to be loaded
638 * @param onsetFile file containing the ONSET to be injected
640 protected void httpRainyDay(String policyFile, String onsetFile) {
641 policyClMgt = createNoficationTopicListener();
643 policy = checkPolicy(policyFile);
645 /* Inject an ONSET event over the DCAE topic */
646 topics.inject(DCAE_TOPIC, onsetFile);
648 /* Wait to acquire a LOCK and a PDP-X PERMIT */
649 waitForLockAndPermit(policy, policyClMgt);
651 /* --- Operation Completed --- */
652 waitForOperationFailure();
654 /* --- Transaction Completed --- */
655 waitForFinalFailure(policy, policyClMgt);
660 protected long getCreateCount() {
661 return ControlLoopEventManager.getCreateCount();
665 * Waits for a OPERATION SUCCESS transaction notification.
667 protected void waitForOperationSuccess() {
668 policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_SUCCESS);
672 * Waits for a FINAL SUCCESS transaction notification.
674 * @return the FINAL SUCCESS notification
676 protected VirtualControlLoopNotification waitForFinalSuccess(ToscaPolicy policy,
677 Listener<VirtualControlLoopNotification> policyClMgt) {
679 return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_SUCCESS);
683 * Waits for a OPERATION FAILURE transaction notification.
685 protected void waitForOperationFailure() {
686 policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_FAILURE);
690 * Waits for a FINAL FAILURE transaction notification.
692 * @return the FINAL FAILURE notification
694 protected VirtualControlLoopNotification waitForFinalFailure(ToscaPolicy policy,
695 Listener<VirtualControlLoopNotification> policyClMgt) {
697 return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_FAILURE);
701 * Waits for notifications for LOCK acquisition and GUARD Permit so that event
702 * processing may proceed.
704 protected abstract void waitForLockAndPermit(ToscaPolicy policy,
705 Listener<VirtualControlLoopNotification> policyClMgt);
708 * Waits for a FINAL transaction notification.
710 * @param finalType FINAL_xxx type for which to wait
712 * @return the FINAL notification
714 protected abstract VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
715 Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType);
718 * Returns ToscaPolicy from File.
720 * @param fileName a path name
721 * @return ToscaPolicy
723 protected ToscaPolicy checkPolicy(String fileName) {
725 return Rules.getPolicyFromFile(fileName);
726 } catch (CoderException e) {
727 throw new IllegalArgumentException(fileName, e);
732 * Creates a Coder for PolicyClMgt from StandardCoder.
735 public static class PolicyClMgtCoder extends StandardCoder {
736 public PolicyClMgtCoder() {
737 super(org.onap.policy.controlloop.util.Serialization.gson,
738 org.onap.policy.controlloop.util.Serialization.gsonPretty);
743 * Returns Listener from createListener based on Coder.
745 * @return the Listener
747 protected Listener<VirtualControlLoopNotification> createNoficationTopicListener() {
748 return topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, POLICY_CL_MGT_CODER);
752 * Verifies that all locks have been released, waiting a bit, if necessary.
754 private void verifyUnlocked() {
755 Map<String, SimpleLock> locks = getLockMap();
756 if (!MapUtils.isEmpty(locks)) {
757 Awaitility.await().atMost(5, TimeUnit.SECONDS).until(locks::isEmpty);
761 private Map<String, SimpleLock> getLockMap() {
762 Object lockMgr = Whitebox.getInternalState(PolicyEngineConstants.getManager(), "lockManager");
763 if (lockMgr instanceof SimpleLockManager) {
764 return Whitebox.getInternalState(lockMgr, "resource2lock");
767 return Collections.emptyMap();