0753aacd38d332223cdcd9709c7c97685522a48c
[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 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.LinkedList;
33 import java.util.Map;
34 import java.util.Queue;
35 import java.util.function.Function;
36 import java.util.function.Predicate;
37 import java.util.function.Supplier;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.appc.CommonHeader;
46 import org.onap.policy.appc.Request;
47 import org.onap.policy.appclcm.AppcLcmBody;
48 import org.onap.policy.appclcm.AppcLcmCommonHeader;
49 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
50 import org.onap.policy.appclcm.AppcLcmInput;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
53 import org.onap.policy.controlloop.ControlLoopNotificationType;
54 import org.onap.policy.controlloop.VirtualControlLoopNotification;
55 import org.onap.policy.drools.controller.DroolsController;
56 import org.onap.policy.drools.system.PolicyController;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
58 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
59 import org.onap.policy.sdnr.PciBody;
60 import org.onap.policy.sdnr.PciCommonHeader;
61 import org.onap.policy.sdnr.PciMessage;
62 import org.onap.policy.sdnr.PciRequest;
63 import org.powermock.reflect.Whitebox;
64
65 public class BaseRuleTestTest {
66     private static final String CONTROLLER_NAME = "my-controller-name";
67     private static final String POLICY_NAME = "my-policy-name";
68
69     // saved values
70     private static Function<String, Rules> ruleMaker;
71     private static Supplier<HttpClients> httpClientMaker;
72     private static Supplier<Simulators> simMaker;
73     private static Supplier<Topics> topicMaker;
74
75     private BaseRuleTest base;
76     private LinkedList<VirtualControlLoopNotification> clMgtQueue;
77     private Queue<AppcLcmDmaapWrapper> appcLcmQueue;
78     private Queue<Request> appcLegacyQueue;
79     private Queue<PciMessage> sdnrQueue;
80     private int permitCount;
81     private int finalCount;
82
83     @Mock
84     private PolicyController controller;
85     @Mock
86     private Rules rules;
87     @Mock
88     private HttpClients httpClients;
89     @Mock
90     private Simulators simulators;
91     @Mock
92     private Topics topics;
93     @Mock
94     private Listener<VirtualControlLoopNotification> policyClMgt;
95     @Mock
96     private Listener<Request> appcClSink;
97     @Mock
98     private Listener<AppcLcmDmaapWrapper> appcLcmRead;
99     @Mock
100     private Listener<PciMessage> sdnrClSink;
101     @Mock
102     private DroolsController drools;
103     @Mock
104     private ToscaPolicy policy;
105     @Mock
106     private ToscaPolicyIdentifier policyIdent;
107
108
109     /**
110      * Saves static values from the class.
111      */
112     @BeforeClass
113     public static void setUpBeforeClass() {
114         ruleMaker = Whitebox.getInternalState(BaseRuleTest.class, "ruleMaker");
115         httpClientMaker = Whitebox.getInternalState(BaseRuleTest.class, "httpClientMaker");
116         simMaker = Whitebox.getInternalState(BaseRuleTest.class, "simMaker");
117         topicMaker = Whitebox.getInternalState(BaseRuleTest.class, "topicMaker");
118     }
119
120     /**
121      * Restores static values.
122      */
123     @AfterClass
124     public static void tearDownAfterClass() {
125         Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
126         Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
127         Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
128         Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
129     }
130
131     /**
132      * Sets up.
133      */
134     @Before
135     public void setUp() {
136         MockitoAnnotations.initMocks(this);
137
138         when(policy.getIdentifier()).thenReturn(policyIdent);
139         when(policyIdent.getName()).thenReturn(POLICY_NAME);
140
141         when(drools.factCount(CONTROLLER_NAME)).thenReturn(0L);
142         when(controller.getDrools()).thenReturn(drools);
143
144         when(rules.getControllerName()).thenReturn(CONTROLLER_NAME);
145         when(rules.getController()).thenReturn(controller);
146         when(rules.setupPolicyFromFile(any())).thenAnswer(args -> {
147             when(drools.factCount(CONTROLLER_NAME)).thenReturn(2L);
148             return policy;
149         });
150
151         when(topics.createListener(BaseRuleTest.POLICY_CL_MGT_TOPIC, VirtualControlLoopNotification.class, controller))
152                         .thenReturn(policyClMgt);
153         when(topics.createListener(eq(BaseRuleTest.APPC_LCM_READ_TOPIC), eq(AppcLcmDmaapWrapper.class),
154                         any(StandardCoder.class))).thenReturn(appcLcmRead);
155         when(topics.createListener(eq(BaseRuleTest.APPC_CL_TOPIC), eq(Request.class),
156                         any(StandardCoderInstantAsMillis.class))).thenReturn(appcClSink);
157         when(topics.createListener(eq(BaseRuleTest.SDNR_CL_TOPIC), eq(PciMessage.class),
158             any(StandardCoder.class))).thenReturn(sdnrClSink);
159
160         Function<String, Rules> ruleMaker = this::makeRules;
161         Supplier<HttpClients> httpClientMaker = this::makeHttpClients;
162         Supplier<Simulators> simMaker = this::makeSim;
163         Supplier<Topics> topicMaker = this::makeTopics;
164
165         Whitebox.setInternalState(BaseRuleTest.class, "ruleMaker", ruleMaker);
166         Whitebox.setInternalState(BaseRuleTest.class, "httpClientMaker", httpClientMaker);
167         Whitebox.setInternalState(BaseRuleTest.class, "simMaker", simMaker);
168         Whitebox.setInternalState(BaseRuleTest.class, "topicMaker", topicMaker);
169
170         clMgtQueue = new LinkedList<>();
171         appcLcmQueue = new LinkedList<>();
172         appcLegacyQueue = new LinkedList<>();
173         sdnrQueue = new LinkedList<>();
174
175         when(policyClMgt.await(any())).thenAnswer(args -> {
176             VirtualControlLoopNotification notif = clMgtQueue.remove();
177             Predicate<VirtualControlLoopNotification> pred = args.getArgument(0);
178             assertTrue(pred.test(notif));
179             return notif;
180         });
181
182         when(appcLcmRead.await(any())).thenAnswer(args -> {
183             AppcLcmDmaapWrapper req = appcLcmQueue.remove();
184             Predicate<AppcLcmDmaapWrapper> pred = args.getArgument(0);
185             assertTrue(pred.test(req));
186             return req;
187         });
188
189         when(appcClSink.await(any())).thenAnswer(args -> {
190             Request req = appcLegacyQueue.remove();
191             Predicate<Request> pred = args.getArgument(0);
192             assertTrue(pred.test(req));
193             return req;
194         });
195
196         when(sdnrClSink.await(any())).thenAnswer(args -> {
197             PciMessage pcireq = sdnrQueue.remove();
198             Predicate<PciMessage> pred = args.getArgument(0);
199             assertTrue(pred.test(pcireq));
200             return pcireq;
201         });
202
203         permitCount = 0;
204         finalCount = 0;
205
206         base = new MyTest();
207
208         BaseRuleTest.initStatics(CONTROLLER_NAME);
209         base.init();
210     }
211
212     @Test
213     public void testInitStatics() {
214         assertSame(rules, BaseRuleTest.rules);
215         assertSame(httpClients, BaseRuleTest.httpClients);
216         assertSame(simulators, BaseRuleTest.simulators);
217     }
218
219     @Test
220     public void testFinishStatics() {
221         BaseRuleTest.finishStatics();
222
223         verify(rules).destroy();
224         verify(httpClients).destroy();
225         verify(simulators).destroy();
226     }
227
228     @Test
229     public void testInit() {
230         assertSame(topics, base.getTopics());
231         assertSame(controller, base.controller);
232     }
233
234     @Test
235     public void testFinish() {
236         base.finish();
237
238         verify(topics).destroy();
239         verify(rules).resetFacts();
240     }
241
242     @Test
243     public void testTestService123Compliant() {
244         enqueueAppcLcm("restart", "restart", "restart", "restart", "rebuild", "migrate");
245         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
246         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
247
248         base.testService123Compliant();
249
250         assertEquals(1, permitCount);
251         assertEquals(1, finalCount);
252
253         assertTrue(appcLcmQueue.isEmpty());
254         assertTrue(clMgtQueue.isEmpty());
255
256         // initial event
257         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
258
259         // replies to each APPC request
260         verify(topics, times(6)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
261     }
262
263     @Test
264     public void testTestDuplicatesEvents() {
265         enqueueAppcLcm("restart", "restart");
266         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
267         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
268         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
269
270         clMgtQueue.get(1).setAai(Map.of("generic-vnf.vnf-id", "duplicate-VNF"));
271         clMgtQueue.get(2).setAai(Map.of("generic-vnf.vnf-id", "vCPE_Infrastructure_vGMUX_demo_app"));
272
273         base.testDuplicatesEvents();
274
275         assertEquals(0, permitCount);
276         assertEquals(3, finalCount);
277
278         assertTrue(appcLcmQueue.isEmpty());
279         assertTrue(clMgtQueue.isEmpty());
280
281         // initial events
282         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
283         verify(topics, times(2)).inject(eq(BaseRuleTest.DCAE_TOPIC), any(), any());
284
285         // two restarts
286         verify(topics, times(2)).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
287     }
288
289     @Test
290     public void testTestVcpeSunnyDayLegacy() {
291         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayLegacy);
292     }
293
294     @Test
295     public void testTestVcpeSunnyDayCompliant() {
296         checkAppcLcmPolicy("restart", base::testVcpeSunnyDayCompliant);
297     }
298
299     @Test
300     public void testTestVcpeOnsetFloodPrevention() {
301         enqueueAppcLcm("restart");
302         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
303         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
304
305         base.testVcpeOnsetFloodPrevention();
306
307         assertEquals(1, permitCount);
308         assertEquals(1, finalCount);
309
310         assertTrue(appcLcmQueue.isEmpty());
311         assertTrue(clMgtQueue.isEmpty());
312
313         // initial events
314         verify(topics, times(3)).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
315
316         // one restart
317         verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
318     }
319
320     @Test
321     public void testTestVdnsSunnyDayLegacy() {
322         checkHttpPolicy(base::testVdnsSunnyDayLegacy);
323     }
324
325     @Test
326     public void testTestVdnsSunnyDayCompliant() {
327         checkHttpPolicy(base::testVdnsSunnyDayCompliant);
328     }
329
330     @Test
331     public void testTestVfwSunnyDayLegacy() {
332         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayLegacy);
333     }
334
335     @Test
336     public void testTestVfwSunnyDayCompliant() {
337         checkAppcLegacyPolicy("ModifyConfig", base::testVfwSunnyDayCompliant);
338     }
339
340     @Test
341     public void testTestVfwRainyDayLegacyFailure() {
342         checkAppcLegacyPolicyOperationFailure("ModifyConfig", base::testVfwRainyDayLegacyFailure);
343     }
344
345     @Test
346     public void testTestVfwRainyDayOverallTimeout() {
347         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayOverallTimeout);
348     }
349
350     @Test
351     public void testTestVfwRainyDayCompliantTimeout() {
352         checkAppcLegacyPolicyFinalFailure("ModifyConfig", base::testVfwRainyDayCompliantTimeout);
353     }
354
355     @Test
356     public void testTestVpciSunnyDayLegacy() {
357         checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayLegacy);
358     }
359
360     @Test
361     public void testTestVpciSunnyDayCompliant() {
362         checkSdnrPolicy("ModifyConfig", base::testVpciSunnyDayCompliant);
363     }
364
365     @Test
366     public void testTestVsonhSunnyDayLegacy() {
367         checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayLegacy);
368     }
369
370     @Test
371     public void testTestVsonhSunnyDayCompliant() {
372         checkSdnrPolicy("ModifyConfigANR", base::testVsonhSunnyDayCompliant);
373     }
374
375     protected void checkAppcLcmPolicy(String operation, Runnable test) {
376         enqueueAppcLcm(operation);
377         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
378         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
379
380         test.run();
381
382         assertEquals(1, permitCount);
383         assertEquals(1, finalCount);
384
385         assertTrue(appcLcmQueue.isEmpty());
386         assertTrue(clMgtQueue.isEmpty());
387
388         // initial event
389         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
390
391         // reply to each APPC request
392         verify(topics).inject(eq(BaseRuleTest.APPC_LCM_WRITE_TOPIC), any(), any());
393     }
394
395     protected void checkAppcLegacyPolicy(String operation, Runnable test) {
396         enqueueAppcLegacy(operation);
397         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
398         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
399
400         test.run();
401
402         assertEquals(1, permitCount);
403         assertEquals(1, finalCount);
404
405         assertTrue(appcLcmQueue.isEmpty());
406         assertTrue(clMgtQueue.isEmpty());
407
408         // initial event
409         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
410
411         // reply to each APPC request
412         verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
413     }
414
415     protected void checkAppcLegacyPolicyOperationFailure(String operation, Runnable test) {
416         enqueueAppcLegacy(operation);
417         enqueueClMgt(ControlLoopNotificationType.OPERATION_FAILURE);
418         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
419
420         test.run();
421
422         assertEquals(1, permitCount);
423         assertEquals(1, finalCount);
424
425         assertTrue(appcLcmQueue.isEmpty());
426         assertTrue(clMgtQueue.isEmpty());
427
428         // initial event
429         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
430
431         // reply to each APPC request
432         verify(topics).inject(eq(BaseRuleTest.APPC_CL_TOPIC), any(), any());
433     }
434
435     protected void checkAppcLegacyPolicyFinalFailure(String operation, Runnable test) {
436         enqueueAppcLegacy(operation);
437         enqueueClMgt(ControlLoopNotificationType.FINAL_FAILURE);
438
439         test.run();
440
441         assertEquals(1, permitCount);
442         assertEquals(1, finalCount);
443
444         assertTrue(appcLcmQueue.isEmpty());
445         assertTrue(clMgtQueue.isEmpty());
446
447         // initial event
448         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
449
450         // There were no requests sent
451     }
452
453     protected void checkSdnrPolicy(String operation, Runnable test) {
454         enqueueSdnr(operation);
455         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
456         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
457
458         test.run();
459
460         assertEquals(1, permitCount);
461         assertEquals(1, finalCount);
462
463         assertTrue(sdnrQueue.isEmpty());
464         assertTrue(clMgtQueue.isEmpty());
465
466         // initial event
467         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
468
469         // reply to each SDNR request
470         verify(topics).inject(eq(BaseRuleTest.SDNR_CL_RSP_TOPIC), any(), any());
471     }
472
473     protected void checkHttpPolicy(Runnable test) {
474         enqueueClMgt(ControlLoopNotificationType.OPERATION_SUCCESS);
475         enqueueClMgt(ControlLoopNotificationType.FINAL_SUCCESS);
476
477         test.run();
478
479         assertEquals(1, permitCount);
480         assertEquals(1, finalCount);
481
482         assertTrue(clMgtQueue.isEmpty());
483
484         // initial event
485         verify(topics).inject(eq(BaseRuleTest.DCAE_TOPIC), any());
486     }
487
488     private void enqueueClMgt(ControlLoopNotificationType type) {
489         VirtualControlLoopNotification notif = new VirtualControlLoopNotification();
490         notif.setNotification(type);
491         notif.setPolicyName(POLICY_NAME + ".EVENT.MANAGER.FINAL");
492
493         clMgtQueue.add(notif);
494     }
495
496     private void enqueueAppcLcm(String... operationNames) {
497         for (String oper : operationNames) {
498             AppcLcmDmaapWrapper req = new AppcLcmDmaapWrapper();
499             req.setRpcName(oper);
500
501             AppcLcmBody body = new AppcLcmBody();
502             req.setBody(body);
503
504             AppcLcmInput input = new AppcLcmInput();
505             body.setInput(input);
506
507             AppcLcmCommonHeader header = new AppcLcmCommonHeader();
508             input.setCommonHeader(header);
509
510             header.setSubRequestId("my-subrequest-id");
511
512             appcLcmQueue.add(req);
513         }
514     }
515
516     private void enqueueAppcLegacy(String... operationNames) {
517         for (String oper : operationNames) {
518             Request req = new Request();
519             req.setAction(oper);
520
521             CommonHeader header = new CommonHeader();
522             req.setCommonHeader(header);
523
524             header.setSubRequestId("my-subrequest-id");
525
526             appcLegacyQueue.add(req);
527         }
528     }
529
530     private void enqueueSdnr(String... operationNames) {
531         for (String oper : operationNames) {
532             PciMessage pcimessage = new PciMessage();
533             PciRequest req = new PciRequest();
534             PciBody body = new PciBody();
535             body.setInput(req);
536             pcimessage.setBody(body);
537             pcimessage.getBody().getInput().setAction(oper);
538             PciCommonHeader header = new PciCommonHeader();
539             pcimessage.getBody().getInput().setCommonHeader(header);
540
541             header.setSubRequestId("my-subrequest-id");
542
543             sdnrQueue.add(pcimessage);
544         }
545     }
546
547     private Rules makeRules(String controllerName) {
548         return rules;
549     }
550
551     private HttpClients makeHttpClients() {
552         return httpClients;
553     }
554
555     private Simulators makeSim() {
556         return simulators;
557     }
558
559     private Topics makeTopics() {
560         return topics;
561     }
562
563     /*
564      * We don't want junit trying to run this, so it's marked "Ignore".
565      */
566     @Ignore
567     private class MyTest extends BaseRuleTest {
568
569         @Override
570         protected void waitForLockAndPermit(ToscaPolicy policy, Listener<VirtualControlLoopNotification> policyClMgt) {
571             permitCount++;
572         }
573
574         @Override
575         protected VirtualControlLoopNotification waitForFinal(ToscaPolicy policy,
576                         Listener<VirtualControlLoopNotification> policyClMgt, ControlLoopNotificationType finalType) {
577             finalCount++;
578             return policyClMgt.await(notif -> notif.getNotification() == finalType);
579         }
580     }
581 }