0baac9e8cdb12b980be48d76cfad80913664b44c
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.common.rules.test;
22
23 import static org.junit.Assert.assertEquals;
24
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;
31 import lombok.Getter;
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.controlloop.eventmanager.ControlLoopEventManager2;
41 import org.onap.policy.drools.system.PolicyController;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
43 import org.onap.policy.sdnr.PciMessage;
44
45 /**
46  * Superclass used for rule tests.
47  */
48 public abstract class BaseRuleTest {
49     private static final String APPC_RESTART_OP = "restart";
50     private static final String APPC_MODIFY_CONFIG_OP = "ModifyConfig";
51
52     /*
53      * Canonical Topic Names.
54      */
55     protected static final String DCAE_TOPIC = "DCAE_TOPIC";
56     protected static final String APPC_LCM_WRITE_TOPIC = "APPC-LCM-WRITE";
57     protected static final String POLICY_CL_MGT_TOPIC = "POLICY-CL-MGT";
58     protected static final String APPC_LCM_READ_TOPIC = "APPC-LCM-READ";
59     protected static final String APPC_CL_TOPIC = "APPC-CL";
60     protected static final String SDNR_CL_TOPIC = "SDNR-CL";
61     protected static final String SDNR_CL_RSP_TOPIC = "SDNR-CL-RSP";
62
63     /*
64      * Constants for each test case.
65      */
66
67     // service123 (i.e., multi-operation policy)
68     private static final String SERVICE123_TOSCA_COMPLIANT_POLICY = "service123/tosca-compliant-service123.json";
69     private static final String SERVICE123_ONSET = "service123/service123.onset.json";
70     private static final String SERVICE123_APPC_RESTART_FAILURE = "service123/service123.appc.restart.failure.json";
71     private static final String SERVICE123_APPC_REBUILD_FAILURE = "service123/service123.appc.rebuild.failure.json";
72     private static final String SERVICE123_APPC_MIGRATE_SUCCESS = "service123/service123.appc.migrate.success.json";
73
74     // duplicates (i.e., mutliple events in the engine at the same time)
75     private static final String DUPLICATES_TOSCA_COMPLIANT_POLICY = "duplicates/tosca-compliant-duplicates.json";
76     private static final String DUPLICATES_ONSET_1 = "duplicates/duplicates.onset.1.json";
77     private static final String DUPLICATES_ONSET_2 = "duplicates/duplicates.onset.2.json";
78     private static final String DUPLICATES_APPC_SUCCESS = "duplicates/duplicates.appc.success.json";
79
80     // VCPE
81     private static final String VCPE_TOSCA_LEGACY_POLICY = "vcpe/tosca-legacy-vcpe.json";
82     private static final String VCPE_TOSCA_COMPLIANT_POLICY = "vcpe/tosca-compliant-vcpe.json";
83     private static final String VCPE_ONSET_1 = "vcpe/vcpe.onset.1.json";
84     private static final String VCPE_ONSET_2 = "vcpe/vcpe.onset.2.json";
85     private static final String VCPE_ONSET_3 = "vcpe/vcpe.onset.3.json";
86     private static final String VCPE_APPC_SUCCESS = "vcpe/vcpe.appc.success.json";
87
88     // VDNS
89     private static final String VDNS_TOSCA_LEGACY_POLICY = "vdns/tosca-legacy-vdns.json";
90     private static final String VDNS_TOSCA_COMPLIANT_POLICY = "vdns/tosca-compliant-vdns.json";
91     private static final String VDNS_ONSET = "vdns/vdns.onset.json";
92
93     // VFW
94     private static final String VFW_TOSCA_LEGACY_POLICY = "vfw/tosca-vfw.json";
95     private static final String VFW_TOSCA_COMPLIANT_POLICY = "vfw/tosca-compliant-vfw.json";
96     private static final String VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY = "vfw/tosca-compliant-timeout-vfw.json";
97     private static final String VFW_ONSET = "vfw/vfw.onset.json";
98     private static final String VFW_APPC_SUCCESS = "vfw/vfw.appc.success.json";
99     private static final String VFW_APPC_FAILURE = "vfw/vfw.appc.failure.json";
100
101     // VPCI
102     private static final String VPCI_TOSCA_POLICY = "vpci/tosca-vpci.json";
103     private static final String VPCI_TOSCA_COMPLIANT_POLICY = "vpci/tosca-compliant-vpci.json";
104     private static final String VPCI_ONSET = "vpci/vpci.onset.json";
105     private static final String VPCI_SDNR_SUCCESS = "vpci/vpci.sdnr.success.json";
106
107     // VSONH
108     private static final String VSONH_TOSCA_POLICY = "vsonh/tosca-vsonh.json";
109     private static final String VSONH_TOSCA_COMPLIANT_POLICY = "vsonh/tosca-compliant-vsonh.json";
110     private static final String VSONH_ONSET = "vsonh/vsonh.onset.json";
111     private static final String VSONH_SDNR_SUCCESS = "vsonh/vsonh.sdnr.success.json";
112
113     /*
114      * Coders used to decode requests and responses.
115      */
116     private static final Coder APPC_LEGACY_CODER = new StandardCoderInstantAsMillis();
117     private static final Coder APPC_LCM_CODER = new StandardCoder();
118
119     /*
120      * Coders used to decode requests and responses.
121      */
122     private static final Coder SDNR_CODER = new StandardCoder();
123
124     // these may be overridden by junit tests
125     private static Function<String, Rules> ruleMaker = Rules::new;
126     private static Supplier<HttpClients> httpClientMaker = HttpClients::new;
127     private static Supplier<Simulators> simMaker = Simulators::new;
128     private static Supplier<Topics> topicMaker = Topics::new;
129
130     protected static Rules rules;
131     protected static HttpClients httpClients;
132     protected static Simulators simulators;
133
134     // used to inject and wait for messages
135     @Getter(AccessLevel.PROTECTED)
136     private Topics topics;
137
138     // used to wait for messages on SINK topics
139     protected Listener<VirtualControlLoopNotification> policyClMgt;
140     protected Listener<Request> appcClSink;
141     protected Listener<AppcLcmDmaapWrapper> appcLcmRead;
142     protected Listener<PciMessage> sdnrClSink;
143
144     protected PolicyController controller;
145
146     /*
147      * Tosca Policy that was loaded.
148      */
149     protected ToscaPolicy policy;
150
151
152     /**
153      * Initializes {@link #rules}, {@link #httpClients}, and {@link #simulators}.
154      *
155      * @param controllerName the rule controller name
156      */
157     public static void initStatics(String controllerName) {
158         rules = ruleMaker.apply(controllerName);
159         httpClients = httpClientMaker.get();
160         simulators = simMaker.get();
161     }
162
163     /**
164      * Destroys {@link #httpClients}, {@link #simulators}, and {@link #rules}.
165      */
166     public static void finishStatics() {
167         httpClients.destroy();
168         simulators.destroy();
169         rules.destroy();
170     }
171
172     /**
173      * Initializes {@link #topics} and {@link #controller}.
174      */
175     public void init() {
176         topics = topicMaker.get();
177         controller = rules.getController();
178     }
179
180     /**
181      * Destroys {@link #topics} and resets the rule facts.
182      */
183     public void finish() {
184         topics.destroy();
185         rules.resetFacts();
186     }
187
188     // Service123 (i.e., Policy with multiple operations)
189
190     /**
191      * Service123 with Tosca Compliant Policy.
192      */
193     @Test
194     public void testService123Compliant() {
195         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
196         appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
197
198         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
199         policy = rules.setupPolicyFromFile(SERVICE123_TOSCA_COMPLIANT_POLICY);
200         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
201
202         // inject an ONSET event over the DCAE topic
203         topics.inject(DCAE_TOPIC, SERVICE123_ONSET);
204
205         /* Wait to acquire a LOCK and a PDP-X PERMIT */
206         waitForLockAndPermit(policy, policyClMgt);
207
208         // restart request should be sent and fail four times (i.e., because retry=3)
209         for (int count = 0; count < 4; ++count) {
210             AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
211
212             topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_RESTART_FAILURE,
213                             appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
214         }
215
216         // rebuild request should be sent and fail once
217         AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> "rebuild".equals(req.getRpcName()));
218
219         topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_REBUILD_FAILURE,
220                         appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
221
222         // migrate request should be sent and succeed
223         appcreq = appcLcmRead.await(req -> "migrate".equals(req.getRpcName()));
224
225         topics.inject(APPC_LCM_WRITE_TOPIC, SERVICE123_APPC_MIGRATE_SUCCESS,
226                         appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
227
228         /* --- Operation Completed --- */
229
230         waitForOperationSuccess();
231
232         /* --- Transaction Completed --- */
233         waitForFinalSuccess(policy, policyClMgt);
234     }
235
236     // Duplicate events
237
238     /**
239      * This test case tests the scenario where 3 events occur and 2 of the requests refer
240      * to the same target entity while the 3rd is for another entity. The expected result
241      * is that the event with the duplicate target entity will have a final success result
242      * for one of the events, and a rejected message for the one that was unable to obtain
243      * the lock. The event that is referring to a different target entity should be able
244      * to obtain a lock since it is a different target. After processing of all events
245      * there should only be the policy and params objects left in memory.
246      */
247     @Test
248     public void testDuplicatesEvents() {
249         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
250         appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
251
252         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
253         policy = rules.setupPolicyFromFile(DUPLICATES_TOSCA_COMPLIANT_POLICY);
254         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
255
256         final long initCount = getCreateCount();
257
258         /*
259          * Inject ONSET events over the DCAE topic. First and last have the same target
260          * entity, but different request IDs - only one should succeed. The middle one is
261          * for a different target entity, so it should succeed.
262          */
263         topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
264         topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_2);
265         topics.inject(DCAE_TOPIC, DUPLICATES_ONSET_1, UUID.randomUUID().toString());
266
267         // should see two restarts
268         for (int count = 0; count < 2; ++count) {
269             AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> APPC_RESTART_OP.equals(req.getRpcName()));
270
271             // indicate success
272             topics.inject(APPC_LCM_WRITE_TOPIC, DUPLICATES_APPC_SUCCESS,
273                             appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
274         }
275
276         // should see two FINAL successes
277         VirtualControlLoopNotification notif1 = waitForFinalSuccess(policy, policyClMgt);
278         VirtualControlLoopNotification notif2 = waitForFinalSuccess(policy, policyClMgt);
279
280         // get the list of target names so we can ensure there's one of each
281         List<String> actual = List.of(notif1, notif2).stream().map(notif -> notif.getAai().get("generic-vnf.vnf-id"))
282                         .sorted().collect(Collectors.toList());
283
284         assertEquals(List.of("duplicate-VNF", "vCPE_Infrastructure_vGMUX_demo_app").toString(), actual.toString());
285
286         long added = getCreateCount() - initCount;
287         assertEquals(2, added);
288     }
289
290     // VCPE
291
292     /**
293      * Sunny Day with Legacy Tosca Policy.
294      */
295     @Test
296     public void testVcpeSunnyDayLegacy() {
297         appcLcmSunnyDay(VCPE_TOSCA_LEGACY_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
298     }
299
300     /**
301      * Sunny Day with Tosca Compliant Policy.
302      */
303     @Test
304     public void testVcpeSunnyDayCompliant() {
305         appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, VCPE_ONSET_1, APPC_RESTART_OP);
306     }
307
308     /**
309      * An ONSET flood prevention test that injects a few ONSETs at once. It attempts to
310      * simulate the flooding behavior of the DCAE TCA microservice. TCA could blast tens
311      * or hundreds of ONSETs within sub-second intervals.
312      */
313     @Test
314     public void testVcpeOnsetFloodPrevention() {
315         appcLcmSunnyDay(VCPE_TOSCA_COMPLIANT_POLICY, List.of(VCPE_ONSET_1, VCPE_ONSET_2, VCPE_ONSET_3),
316                         APPC_RESTART_OP);
317     }
318
319     // VDNS
320
321     /**
322      * Sunny Day with Legacy Tosca Policy.
323      */
324     @Test
325     public void testVdnsSunnyDayLegacy() {
326         httpSunnyDay(VDNS_TOSCA_LEGACY_POLICY, VDNS_ONSET);
327     }
328
329     /**
330      * Sunny Day with Tosca Compliant Policy.
331      */
332     @Test
333     public void testVdnsSunnyDayCompliant() {
334         httpSunnyDay(VDNS_TOSCA_COMPLIANT_POLICY, VDNS_ONSET);
335     }
336
337     // VFW
338
339     /**
340      * VFW Sunny Day with Legacy Tosca Policy.
341      */
342     @Test
343     public void testVfwSunnyDayLegacy() {
344         appcLegacySunnyDay(VFW_TOSCA_LEGACY_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
345     }
346
347     /**
348      * VFW Sunny Day with Tosca Compliant Policy.
349      */
350     @Test
351     public void testVfwSunnyDayCompliant() {
352         appcLegacySunnyDay(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
353     }
354
355     /**
356      * VFW Rainy Day using legacy tosca policy (operation and final failure).
357      */
358     @Test
359     public void testVfwRainyDayLegacyFailure() {
360         appcLegacyRainyDay(VFW_TOSCA_LEGACY_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
361     }
362
363     /**
364      * VFW Rainy Day using compliant tosca policy (final failure).
365      */
366     @Test
367     public void testVfwRainyDayOverallTimeout() {
368         appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_TIME_OUT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
369     }
370
371     /**
372      * VFW Rainy day using compliant tosca policy (final failure due to timeout).
373      */
374     @Test
375     public void testVfwRainyDayCompliantTimeout() {
376         appcLegacyRainyDayNoResponse(VFW_TOSCA_COMPLIANT_POLICY, VFW_ONSET, APPC_MODIFY_CONFIG_OP);
377     }
378
379     /**
380      * VPCI Sunny Day with Legacy Tosca Policy.
381      */
382     @Test
383     public void testVpciSunnyDayLegacy() {
384         sdnrSunnyDay(VPCI_TOSCA_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS, "ModifyConfig");
385     }
386
387     /**
388      * VPCI Sunny Day Tosca Policy.
389      */
390     @Test
391     public void testVpciSunnyDayCompliant() {
392         sdnrSunnyDay(VPCI_TOSCA_COMPLIANT_POLICY, VPCI_ONSET, VPCI_SDNR_SUCCESS, "ModifyConfig");
393     }
394
395     // VSONH
396
397     /**
398      * VSONH Sunny Day with Legacy Tosca Policy.
399      */
400     @Test
401     public void testVsonhSunnyDayLegacy() {
402         sdnrSunnyDay(VSONH_TOSCA_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS, "ModifyConfigANR");
403     }
404
405     /**
406      * VSONH Sunny Day with Tosca Policy.
407      */
408     @Test
409     public void testVsonhSunnyDayCompliant() {
410         sdnrSunnyDay(VSONH_TOSCA_COMPLIANT_POLICY, VSONH_ONSET, VSONH_SDNR_SUCCESS, "ModifyConfigANR");
411     }
412
413     /**
414      * Sunny day scenario for use cases that use APPC-LCM.
415      *
416      * @param policyFile file containing the ToscaPolicy to be loaded
417      * @param onsetFile file containing the ONSET to be injected
418      * @param operation expected APPC operation request
419      */
420     protected void appcLcmSunnyDay(String policyFile, String onsetFile, String operation) {
421         appcLcmSunnyDay(policyFile, List.of(onsetFile), operation);
422     }
423
424     /**
425      * Sunny day scenario for use cases that use APPC-LCM.
426      *
427      * @param policyFile file containing the ToscaPolicy to be loaded
428      * @param onsetFiles list of files containing the ONSET to be injected
429      * @param operation expected APPC operation request
430      */
431     protected void appcLcmSunnyDay(String policyFile, List<String> onsetFiles, String operation) {
432         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
433         appcLcmRead = topics.createListener(APPC_LCM_READ_TOPIC, AppcLcmDmaapWrapper.class, APPC_LCM_CODER);
434
435         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
436         policy = rules.setupPolicyFromFile(policyFile);
437         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
438
439         // inject several ONSET events over the DCAE topic
440         for (String onsetFile : onsetFiles) {
441             topics.inject(DCAE_TOPIC, onsetFile);
442         }
443
444         /* Wait to acquire a LOCK and a PDP-X PERMIT */
445         waitForLockAndPermit(policy, policyClMgt);
446
447         /*
448          * Ensure that an APPC RESTART request was sent in response to the matching ONSET
449          */
450         AppcLcmDmaapWrapper appcreq = appcLcmRead.await(req -> operation.equals(req.getRpcName()));
451
452         /*
453          * Inject a 400 APPC Response Return over the APPC topic, with appropriate
454          * subRequestId
455          */
456         topics.inject(APPC_LCM_WRITE_TOPIC, VCPE_APPC_SUCCESS,
457                         appcreq.getBody().getInput().getCommonHeader().getSubRequestId());
458
459         /* --- Operation Completed --- */
460
461         waitForOperationSuccess();
462
463         /* --- Transaction Completed --- */
464         waitForFinalSuccess(policy, policyClMgt);
465     }
466
467     /**
468      * Sunny day scenario for use cases that use Legacy APPC.
469      *
470      * @param policyFile file containing the ToscaPolicy to be loaded
471      * @param onsetFile file containing the ONSET to be injected
472      * @param operation expected APPC operation request
473      */
474     protected void appcLegacySunnyDay(String policyFile, String onsetFile, String operation) {
475         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
476         appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
477
478         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
479         policy = rules.setupPolicyFromFile(policyFile);
480         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
481
482         /* Inject an ONSET event over the DCAE topic */
483         topics.inject(DCAE_TOPIC, onsetFile);
484
485         /* Wait to acquire a LOCK and a PDP-X PERMIT */
486         waitForLockAndPermit(policy, policyClMgt);
487
488         /*
489          * Ensure that an APPC RESTART request was sent in response to the matching ONSET
490          */
491         Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
492
493         /*
494          * Inject a 400 APPC Response Return over the APPC topic, with appropriate
495          * subRequestId
496          */
497         topics.inject(APPC_CL_TOPIC, VFW_APPC_SUCCESS, appcreq.getCommonHeader().getSubRequestId());
498
499         /* --- Operation Completed --- */
500
501         waitForOperationSuccess();
502
503         /* --- Transaction Completed --- */
504         waitForFinalSuccess(policy, policyClMgt);
505     }
506
507     /**
508      * Rainy day scenario for use cases that use Legacy APPC.
509      *
510      * @param policyFile file containing the ToscaPolicy to be loaded
511      * @param onsetFile file containing the ONSET to be injected
512      * @param operation expected APPC operation request
513      * @param checkOperation flag to determine whether or not to wait for operation timeout
514      */
515     protected void appcLegacyRainyDay(String policyFile, String onsetFile, String operation) {
516         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
517         appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
518
519         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
520         policy = rules.setupPolicyFromFile(policyFile);
521         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
522
523         /* Inject an ONSET event over the DCAE topic */
524         topics.inject(DCAE_TOPIC, onsetFile);
525
526         /* Wait to acquire a LOCK and a PDP-X PERMIT */
527         waitForLockAndPermit(policy, policyClMgt);
528
529         /*
530          * Ensure that an APPC RESTART request was sent in response to the matching ONSET
531          */
532         Request appcreq = appcClSink.await(req -> operation.equals(req.getAction()));
533
534         /*
535          * Inject a 401 APPC Response Return over the APPC topic, with appropriate
536          * subRequestId
537          */
538         topics.inject(APPC_CL_TOPIC, VFW_APPC_FAILURE, appcreq.getCommonHeader().getSubRequestId());
539
540         /* --- Operation Completed --- */
541         waitForOperationFailure();
542
543         /* --- Transaction Completed --- */
544         waitForFinalFailure(policy, policyClMgt);
545     }
546
547     /**
548      * Rainy day scenario for use cases that use Legacy APPC.
549      * Expected to fail due to timeout.
550      *
551      * @param policyFile file containing the ToscaPolicy to be loaded
552      * @param onsetFile file containing the ONSET to be injected
553      * @param operation expected APPC operation request
554      */
555     protected void appcLegacyRainyDayNoResponse(String policyFile, String onsetFile, String operation) {
556         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
557         appcClSink = topics.createListener(APPC_CL_TOPIC, Request.class, APPC_LEGACY_CODER);
558
559         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
560         policy = rules.setupPolicyFromFile(policyFile);
561         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
562
563         /* Inject an ONSET event over the DCAE topic */
564         topics.inject(DCAE_TOPIC, onsetFile);
565
566         /* Wait to acquire a LOCK and a PDP-X PERMIT */
567         waitForLockAndPermit(policy, policyClMgt);
568
569         /*
570          * Ensure that an APPC RESTART request was sent in response to the matching ONSET
571          */
572         appcClSink.await(req -> operation.equals(req.getAction()));
573
574         /*
575          * Do not inject an APPC Response.
576          */
577
578         /* --- Transaction Completed --- */
579         waitForFinalFailure(policy, policyClMgt);
580     }
581
582     /**
583      * Sunny day scenario for use cases that use SDNR.
584      *
585      * @param policyFile file containing the ToscaPolicy to be loaded
586      * @param onsetFile file containing the ONSET to be injected
587      * @param operation expected SDNR operation request
588      */
589     protected void sdnrSunnyDay(String policyFile, String onsetFile, String successFile, String operation) {
590         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC,
591             VirtualControlLoopNotification.class, controller);
592         sdnrClSink = topics.createListener(SDNR_CL_TOPIC, PciMessage.class, SDNR_CODER);
593
594         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
595         policy = rules.setupPolicyFromFile(policyFile);
596         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
597
598         /* Inject an ONSET event over the DCAE topic */
599         topics.inject(DCAE_TOPIC, onsetFile);
600
601         /* Wait to acquire a LOCK and a PDP-X PERMIT */
602         waitForLockAndPermit(policy, policyClMgt);
603
604         /*
605          * Ensure that an SDNR RESTART request was sent in response to the matching ONSET
606          */
607         PciMessage pcireq = sdnrClSink.await(req -> operation.equals(req.getBody().getInput().getAction()));
608
609         /*
610          * Inject response.
611          */
612         topics.inject(SDNR_CL_RSP_TOPIC, successFile, pcireq.getBody().getInput().getCommonHeader().getSubRequestId());
613
614         /* --- Operation Completed --- */
615
616         waitForOperationSuccess();
617
618         /* --- Transaction Completed --- */
619         waitForFinalSuccess(policy, policyClMgt);
620     }
621
622     /**
623      * Sunny day scenario for use cases that use an HTTP simulator.
624      *
625      * @param policyFile file containing the ToscaPolicy to be loaded
626      * @param onsetFile file containing the ONSET to be injected
627      * @param operation expected APPC operation request
628      */
629     protected void httpSunnyDay(String policyFile, String onsetFile) {
630         policyClMgt = topics.createListener(POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller);
631
632         assertEquals(0, controller.getDrools().factCount(rules.getControllerName()));
633         policy = rules.setupPolicyFromFile(policyFile);
634         assertEquals(2, controller.getDrools().factCount(rules.getControllerName()));
635
636         /* Inject an ONSET event over the DCAE topic */
637         topics.inject(DCAE_TOPIC, onsetFile);
638
639         /* Wait to acquire a LOCK and a PDP-X PERMIT */
640         waitForLockAndPermit(policy, policyClMgt);
641
642         /* --- Operation Completed --- */
643
644         waitForOperationSuccess();
645
646         /* --- Transaction Completed --- */
647         waitForFinalSuccess(policy, policyClMgt);
648     }
649
650     protected long getCreateCount() {
651         return ControlLoopEventManager2.getCreateCount();
652     }
653
654     /**
655      * Waits for a OPERATION SUCCESS transaction notification.
656      */
657     protected void waitForOperationSuccess() {
658         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_SUCCESS);
659     }
660
661     /**
662      * Waits for a FINAL SUCCESS transaction notification.
663      *
664      * @return the FINAL SUCCESS notification
665      */
666     protected VirtualControlLoopNotification waitForFinalSuccess(ToscaPolicy policy,
667                     Listener<VirtualControlLoopNotification> policyClMgt) {
668
669         return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_SUCCESS);
670     }
671
672     /**
673      * Waits for a OPERATION FAILURE transaction notification.
674      */
675     protected void waitForOperationFailure() {
676         policyClMgt.await(notif -> notif.getNotification() == ControlLoopNotificationType.OPERATION_FAILURE);
677     }
678
679     /**
680      * Waits for a FINAL FAILURE transaction notification.
681      *
682      * @return the FINAL FAILURE notification
683      */
684     protected VirtualControlLoopNotification waitForFinalFailure(ToscaPolicy policy,
685                     Listener<VirtualControlLoopNotification> policyClMgt) {
686
687         return this.waitForFinal(policy, policyClMgt, ControlLoopNotificationType.FINAL_FAILURE);
688     }
689
690     /**
691      * Waits for notifications for LOCK acquisition and GUARD Permit so that event
692      * processing may proceed.
693      */
694     protected abstract void waitForLockAndPermit(ToscaPolicy policy,
695                     Listener<VirtualControlLoopNotification> policyClMgt);
696
697     /**
698      * Waits for a FINAL transaction notification.
699      *
700      * @param finalType FINAL_xxx type for which to wait
701      *
702      * @return the FINAL notification
703      */
704     protected abstract VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
705                     Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType);
706 }