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