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