3a4b277701f2efb8e63b0bf2fe288532f2f7b660
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.eventmanager;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.time.Instant;
36 import java.util.HashMap;
37 import java.util.Map;
38 import java.util.UUID;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.ExpectedException;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.aai.AaiGetVnfResponse;
48 import org.onap.policy.aai.AaiGetVserverResponse;
49 import org.onap.policy.aai.AaiNqRequestError;
50 import org.onap.policy.aai.AaiNqResponseWrapper;
51 import org.onap.policy.aai.AaiNqVServer;
52 import org.onap.policy.aai.RelatedToProperty;
53 import org.onap.policy.aai.Relationship;
54 import org.onap.policy.aai.RelationshipData;
55 import org.onap.policy.aai.RelationshipList;
56 import org.onap.policy.aai.util.AaiException;
57 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
58 import org.onap.policy.common.utils.io.Serializer;
59 import org.onap.policy.controlloop.ControlLoopEventStatus;
60 import org.onap.policy.controlloop.ControlLoopException;
61 import org.onap.policy.controlloop.ControlLoopNotificationType;
62 import org.onap.policy.controlloop.SupportUtil;
63 import org.onap.policy.controlloop.VirtualControlLoopEvent;
64 import org.onap.policy.controlloop.VirtualControlLoopNotification;
65 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
66 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
67 import org.onap.policy.controlloop.policy.PolicyResult;
68 import org.onap.policy.drools.system.PolicyEngine;
69 import org.onap.policy.guard.GuardResult;
70 import org.onap.policy.guard.PolicyGuard;
71 import org.onap.policy.guard.PolicyGuard.LockResult;
72 import org.onap.policy.guard.TargetLock;
73 import org.powermock.reflect.Whitebox;
74
75 public class ControlLoopEventManagerTest {
76     private static final String PROCESS_VSERVER_RESPONSE = "processVServerResponse";
77     private static final String ONSET_ONE = "onsetOne";
78     private static final String VSERVER_NAME = "vserver.vserver-name";
79     private static final String TEST_YAML = "src/test/resources/test.yaml";
80     private static final String SERVICE_TYPE = "service-subscription.service-type";
81     private static final String SERVICE_INSTANCE_NAME = "service-instance.service-instance-name";
82     private static final String SERVICE_INSTANCE_ID = "service-instance.service-instance-id";
83     private static final String SERVICE_INSTANCE = "service-instance";
84     private static final String VNF_NAME_TEXT = "lll_vnf_010317";
85     private static final String SERVICE_INSTANCE_NAME_TEXT = "lll_svc_010317";
86     private static final String VNF_NAME = "generic-vnf.vnf-name";
87     private static final String VNF_ID = "generic-vnf.vnf-id";
88     private static final String SERVICE_INSTANCE_UUID = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970";
89     private static final String MSO_CUSTOMER_ID = "customer.global-customer-id";
90     private static final String AAI_USERNAME = "aai.username";
91     private static final String AAI_URL = "aai.url";
92     private static final String AAI_PASS = "aai.password";
93     private static final String TWO_ONSET_TEST = "TwoOnsetTest";
94     private static final String MSO_1610_ST = "MSO_1610_ST";
95     private static final String MSO_DEV_SERVICE_TYPE = "MSO-dev-service-type";
96     private static final String VNF_UUID = "83f674e8-7555-44d7-9a39-bdc3770b0491";
97     private static final String AAI_SERVICE_SUBSCRIPTION_URI =
98                     "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription";
99     private static final String MSO_SERVICE_INSTANCE_URI = "/MSO-dev-service-type/service-instances/service-instance/";
100
101     private static final String PROCESS_VNF_RESPONSE_METHOD_NAME = "processVnfResponse";
102
103     private static final String INVALID_URL = "http://localhost:9999";
104
105
106     @Rule
107     public ExpectedException thrown = ExpectedException.none();
108
109     private VirtualControlLoopEvent onset;
110
111     /**
112      * Set up test class.
113      */
114     @BeforeClass
115     public static void setUpSimulator() throws Exception {
116         org.onap.policy.simulators.Util.buildAaiSim();
117
118         PolicyEngine.manager.setEnvironmentProperty(AAI_USERNAME, "AAI");
119         PolicyEngine.manager.setEnvironmentProperty(AAI_PASS, "AAI");
120         PolicyEngine.manager.setEnvironmentProperty(AAI_URL, "http://localhost:6666");
121     }
122
123     @AfterClass
124     public static void tearDownSimulator() {
125         HttpServletServer.factory.destroy();
126     }
127
128     /**
129      * Setup.
130      */
131     @Before
132     public void setUp() {
133         onset = new VirtualControlLoopEvent();
134         onset.setClosedLoopControlName("ControlLoop-vUSP");
135         onset.setRequestId(UUID.randomUUID());
136         onset.setTarget("VM_NAME");
137         onset.setClosedLoopAlarmStart(Instant.now());
138         onset.setAai(new HashMap<String, String>());
139         onset.getAai().put("cloud-region.identity-url", "foo");
140         onset.getAai().put("vserver.selflink", "bar");
141         onset.getAai().put(VNF_ID, VNF_UUID);
142         onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
143
144         PolicyEngine.manager.setEnvironmentProperty(AAI_URL, "http://localhost:6666");
145     }
146
147     @Test
148     public void testAaiVnfInfo() throws IOException {
149         final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
150         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
151         AaiGetVnfResponse response = getQueryByVnfId2();
152         assertNotNull(response);
153     }
154
155     @Test
156     public void testAaiVnfInfo2() throws IOException {
157         final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
158         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
159         AaiGetVnfResponse response = getQueryByVnfName2();
160         assertNotNull(response);
161     }
162
163     @Test
164     public void testAaiVserver() throws IOException {
165         final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
166         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
167         AaiGetVserverResponse response = getQueryByVserverName2();
168         assertNotNull(response);
169     }
170
171     @Test
172     public void abatementCheckEventSyntaxTest() throws ControlLoopException {
173         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
174         event.setClosedLoopControlName("abatementAAI");
175         event.setRequestId(UUID.randomUUID());
176         event.setTarget(VNF_ID);
177         event.setClosedLoopAlarmStart(Instant.now());
178         event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
179         ControlLoopEventManager manager = makeManager(event);
180         assertNull(manager.getVnfResponse());
181         assertNull(manager.getVserverResponse());
182         manager.checkEventSyntax(event);
183         assertNull(manager.getVnfResponse());
184         assertNull(manager.getVserverResponse());
185
186
187         event.setAai(new HashMap<>());
188         event.getAai().put(VNF_NAME, "abatementTest");
189         manager.checkEventSyntax(event);
190         assertNull(manager.getVnfResponse());
191         assertNull(manager.getVserverResponse());
192     }
193
194     @Test
195     public void subsequentOnsetTest() throws Exception {
196         UUID requestId = UUID.randomUUID();
197         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
198         event.setClosedLoopControlName(TWO_ONSET_TEST);
199         event.setRequestId(requestId);
200         event.setTarget(VNF_ID);
201         event.setClosedLoopAlarmStart(Instant.now());
202         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
203         event.setAai(new HashMap<>());
204         event.getAai().put(VNF_NAME, ONSET_ONE);
205
206         ControlLoopEventManager manager = makeManager(event);
207         VirtualControlLoopNotification notification = manager.activate(event);
208
209         assertNotNull(notification);
210         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
211
212         ControlLoopEventManager.NewEventStatus status = null;
213         status = manager.onNewEvent(event);
214         assertNotNull(status);
215         assertEquals(ControlLoopEventManager.NewEventStatus.FIRST_ONSET, status);
216
217         AaiGetVnfResponse response = manager.getVnfResponse();
218         assertNotNull(response);
219         assertNull(manager.getVserverResponse());
220
221         VirtualControlLoopEvent event2 = new VirtualControlLoopEvent();
222         event2.setClosedLoopControlName(TWO_ONSET_TEST);
223         event2.setRequestId(requestId);
224         event2.setTarget(VNF_ID);
225         event2.setClosedLoopAlarmStart(Instant.now());
226         event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
227         event2.setAai(new HashMap<>());
228         event2.getAai().put(VNF_NAME, "onsetTwo");
229
230
231         status = manager.onNewEvent(event2);
232         assertEquals(ControlLoopEventManager.NewEventStatus.SUBSEQUENT_ONSET, status);
233         AaiGetVnfResponse response2 = manager.getVnfResponse();
234         assertNotNull(response2);
235         // We should not have queried AAI, so the stored response should be the same
236         assertEquals(response, response2);
237         assertNull(manager.getVserverResponse());
238     }
239
240     /**
241      * Simulate a response.
242      */
243     public static AaiGetVnfResponse getQueryByVnfId2() {
244         AaiGetVnfResponse response = new AaiGetVnfResponse();
245
246         response.setVnfId(VNF_UUID);
247         response.setVnfName(VNF_NAME_TEXT);
248         response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
249         response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
250         response.setOrchestrationStatus("Created");
251         response.setInMaint(false);
252         response.setIsClosedLoopDisabled(false);
253         response.setResourceVersion("1494001988835");
254         response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
255
256         final RelationshipList relationshipList = new RelationshipList();
257         final Relationship relationship = new Relationship();
258         RelationshipData relationshipDataItem = new RelationshipData();
259
260         relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
261         relationshipDataItem.setRelationshipValue(MSO_1610_ST);
262         relationship.getRelationshipData().add(relationshipDataItem);
263
264         relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
265         relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
266         relationship.getRelationshipData().add(relationshipDataItem);
267
268         relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
269         relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
270         relationship.getRelationshipData().add(relationshipDataItem);
271
272         RelatedToProperty item = new RelatedToProperty();
273         item.setPropertyKey(SERVICE_INSTANCE_NAME);
274         item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
275         relationship.getRelatedToProperty().add(item);
276
277         relationship.setRelatedTo(SERVICE_INSTANCE);
278         relationship.setRelatedLink(
279                 AAI_SERVICE_SUBSCRIPTION_URI
280                         + MSO_SERVICE_INSTANCE_URI
281                         + SERVICE_INSTANCE_UUID);
282
283         relationshipList.getRelationships().add(relationship);
284         response.setRelationshipList(relationshipList);
285
286         return response;
287     }
288
289     /**
290      * Simulate a response.
291      */
292     public static AaiGetVnfResponse getQueryByVnfName2() {
293         AaiGetVnfResponse response = new AaiGetVnfResponse();
294
295         response.setVnfId(VNF_UUID);
296         response.setVnfName(VNF_NAME_TEXT);
297         response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
298         response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
299         response.setOrchestrationStatus("Created");
300         response.setInMaint(false);
301         response.setIsClosedLoopDisabled(false);
302         response.setResourceVersion("1494001988835");
303         response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
304
305         final RelationshipList relationshipList = new RelationshipList();
306         final Relationship relationship = new Relationship();
307         RelationshipData relationshipDataItem = new RelationshipData();
308
309         relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
310         relationshipDataItem.setRelationshipValue(MSO_1610_ST);
311         relationship.getRelationshipData().add(relationshipDataItem);
312
313         relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
314         relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
315         relationship.getRelationshipData().add(relationshipDataItem);
316
317         relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
318         relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
319         relationship.getRelationshipData().add(relationshipDataItem);
320
321         RelatedToProperty item = new RelatedToProperty();
322         item.setPropertyKey(SERVICE_INSTANCE_NAME);
323         item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
324         relationship.getRelatedToProperty().add(item);
325
326         relationship.setRelatedTo(SERVICE_INSTANCE);
327         relationship.setRelatedLink(
328                 AAI_SERVICE_SUBSCRIPTION_URI
329                         + MSO_SERVICE_INSTANCE_URI
330                         + SERVICE_INSTANCE_UUID);
331
332         relationshipList.getRelationships().add(relationship);
333         response.setRelationshipList(relationshipList);
334
335         return response;
336     }
337
338     /**
339      * Simulate a response.
340      */
341     public static AaiGetVserverResponse getQueryByVserverName2() {
342         final AaiGetVserverResponse response = new AaiGetVserverResponse();
343
344         AaiNqVServer svr = new AaiNqVServer();
345
346         svr.setVserverId("d0668d4f-c25e-4a1b-87c4-83845c01efd8");
347         svr.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0");
348         svr.setVserverName2("vjunos0");
349         svr.setVserverSelflink(
350                 "https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8");
351         svr.setInMaint(false);
352         svr.setIsClosedLoopDisabled(false);
353         svr.setResourceVersion("1494001931513");
354
355         final RelationshipList relationshipList = new RelationshipList();
356         final Relationship relationship = new Relationship();
357         RelationshipData relationshipDataItem = new RelationshipData();
358
359         relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
360         relationshipDataItem.setRelationshipValue(MSO_1610_ST);
361         relationship.getRelationshipData().add(relationshipDataItem);
362
363         relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
364         relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
365         relationship.getRelationshipData().add(relationshipDataItem);
366
367         relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
368         relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
369         relationship.getRelationshipData().add(relationshipDataItem);
370
371         RelatedToProperty item = new RelatedToProperty();
372         item.setPropertyKey(SERVICE_INSTANCE_NAME);
373         item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
374         relationship.getRelatedToProperty().add(item);
375
376         relationship.setRelatedTo(SERVICE_INSTANCE);
377         relationship.setRelatedLink(
378                 AAI_SERVICE_SUBSCRIPTION_URI
379                         + MSO_SERVICE_INSTANCE_URI
380                         + SERVICE_INSTANCE_UUID);
381
382         relationshipList.getRelationships().add(relationship);
383         svr.setRelationshipList(relationshipList);
384
385         response.getVserver().add(svr);
386
387         return response;
388     }
389
390     @Test
391     public void testMethods() {
392         UUID requestId = UUID.randomUUID();
393         ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
394
395         assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
396         assertEquals(requestId, clem.getRequestId());
397
398         clem.setActivated(true);
399         assertEquals(true, clem.isActivated());
400
401         clem.setControlLoopResult("SUCCESS");
402         assertEquals("SUCCESS", clem.getControlLoopResult());
403
404         clem.setControlLoopTimedOut();
405         assertEquals(true, clem.isControlLoopTimedOut());
406
407         clem.setNumAbatements(12345);
408         assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
409
410         clem.setNumOnsets(54321);
411         assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
412
413         assertNull(clem.getOnsetEvent());
414         assertNull(clem.getAbatementEvent());
415         assertNull(clem.getProcessor());
416
417         assertEquals(true, clem.isActive());
418         assertEquals(false, clem.releaseLock());
419         assertEquals(true, clem.isControlLoopTimedOut());
420
421         assertNull(clem.unlockCurrentOperation());
422     }
423
424     @Test
425     public void testAlreadyActivated() {
426         UUID requestId = UUID.randomUUID();
427         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
428         event.setClosedLoopControlName(TWO_ONSET_TEST);
429         event.setRequestId(requestId);
430         event.setTarget(VNF_ID);
431         event.setClosedLoopAlarmStart(Instant.now());
432         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
433         event.setAai(new HashMap<>());
434         event.getAai().put(VNF_NAME, ONSET_ONE);
435
436         ControlLoopEventManager manager = makeManager(event);
437         manager.setActivated(true);
438         VirtualControlLoopNotification notification = manager.activate(event);
439         assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
440     }
441
442     @Test
443     public void testActivationYaml() throws IOException {
444         InputStream is = new FileInputStream(new File(TEST_YAML));
445         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
446
447         InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
448         final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
449
450         UUID requestId = UUID.randomUUID();
451         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
452         event.setClosedLoopControlName(TWO_ONSET_TEST);
453         event.setRequestId(requestId);
454         event.setTarget(VNF_ID);
455         event.setClosedLoopAlarmStart(Instant.now());
456         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
457         event.setAai(new HashMap<>());
458         event.getAai().put(VNF_NAME, ONSET_ONE);
459
460         ControlLoopEventManager manager = makeManager(event);
461
462         // Null YAML should fail
463         VirtualControlLoopNotification notificationNull = manager.activate(null, event);
464         assertNotNull(notificationNull);
465         assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
466
467         // Empty YAML should fail
468         VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
469         assertNotNull(notificationEmpty);
470         assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
471
472         // Bad YAML should fail
473         VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
474         assertNotNull(notificationBad);
475         assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
476
477         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
478         assertNotNull(notification);
479         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
480
481         // Another activate should fail
482         VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
483         assertNotNull(notificationActive);
484         assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
485     }
486
487     @Test
488     public void testControlLoopFinal() throws Exception {
489         InputStream is = new FileInputStream(new File(TEST_YAML));
490         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
491
492         UUID requestId = UUID.randomUUID();
493         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
494         event.setClosedLoopControlName(TWO_ONSET_TEST);
495         event.setRequestId(requestId);
496         event.setTarget(VNF_ID);
497         event.setClosedLoopAlarmStart(Instant.now());
498         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
499         event.setAai(new HashMap<>());
500         event.getAai().put(VNF_NAME, ONSET_ONE);
501
502         ControlLoopEventManager manager = makeManager(event);
503         ControlLoopEventManager manager2 = manager;
504         assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
505                         .hasMessage("ControlLoopEventManager MUST be activated first.");
506
507         manager.setActivated(true);
508         assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
509                         .hasMessage("No onset event for ControlLoopEventManager.");
510
511         manager.setActivated(false);
512         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
513         assertNotNull(notification);
514         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
515
516         VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
517         assertNull(clfNotification);
518
519         // serialize and de-serialize manager
520         manager = Serializer.roundTrip(manager);
521
522         manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
523         clfNotification = manager.isControlLoopFinal();
524         assertNotNull(clfNotification);
525         assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
526
527         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
528         notification = manager.activate(yamlString, event);
529         assertNotNull(notification);
530         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
531
532         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
533         clfNotification = manager.isControlLoopFinal();
534         assertNotNull(clfNotification);
535         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
536
537         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
538         notification = manager.activate(yamlString, event);
539         assertNotNull(notification);
540         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
541
542         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
543         clfNotification = manager.isControlLoopFinal();
544         assertNotNull(clfNotification);
545         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
546
547         manager.setControlLoopTimedOut();
548         clfNotification = manager.isControlLoopFinal();
549         assertNotNull(clfNotification);
550         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
551     }
552
553     @Test
554     public void testProcessControlLoop() throws Exception {
555         InputStream is = new FileInputStream(new File(TEST_YAML));
556         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
557
558         UUID requestId = UUID.randomUUID();
559         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
560         event.setClosedLoopControlName(TWO_ONSET_TEST);
561         event.setRequestId(requestId);
562         event.setTarget(VNF_ID);
563         event.setClosedLoopAlarmStart(Instant.now());
564         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
565         event.setAai(new HashMap<>());
566         event.getAai().put(VNF_NAME, ONSET_ONE);
567
568         ControlLoopEventManager manager = makeManager(event);
569         ControlLoopEventManager manager2 = manager;
570         assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
571                         .hasMessage("ControlLoopEventManager MUST be activated first.");
572
573         manager.setActivated(true);
574         assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
575                         .hasMessage("No onset event for ControlLoopEventManager.");
576
577         manager.setActivated(false);
578         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
579         assertNotNull(notification);
580         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
581
582         ControlLoopOperationManager clom = manager.processControlLoop();
583         assertNotNull(clom);
584         assertNull(clom.getOperationResult());
585
586         // serialize and de-serialize manager
587         manager = Serializer.roundTrip(manager);
588
589         // Test operation in progress
590         ControlLoopEventManager manager3 = manager;
591         assertThatThrownBy(manager3::processControlLoop).isInstanceOf(ControlLoopException.class)
592                         .hasMessage("Already working an Operation, do not call this method.");
593
594         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
595         notification = manager.activate(yamlString, event);
596         assertNotNull(notification);
597         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
598
599         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
600         VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
601         assertNotNull(clfNotification);
602         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
603
604         // Test operation completed
605         ControlLoopEventManager manager4 = manager;
606         assertThatThrownBy(manager4::processControlLoop).isInstanceOf(ControlLoopException.class)
607                         .hasMessage("Control Loop is in FINAL state, do not call this method.");
608
609         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
610         notification = manager.activate(yamlString, event);
611         assertNotNull(notification);
612         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
613         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE);
614
615         // Test operation with no next policy defined
616         ControlLoopEventManager manager5 = manager;
617         assertThatThrownBy(manager5::processControlLoop).isInstanceOf(ControlLoopException.class)
618                         .hasMessage("The target type is null");
619     }
620
621     @Test
622     public void testFinishOperation() throws Exception {
623         InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
624         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
625
626         InputStream isStd = new FileInputStream(new File(TEST_YAML));
627         final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
628
629         UUID requestId = UUID.randomUUID();
630         VirtualControlLoopEvent event = new VirtualControlLoopEvent();
631         event.setClosedLoopControlName(TWO_ONSET_TEST);
632         event.setRequestId(requestId);
633         event.setTarget(VNF_ID);
634         event.setClosedLoopAlarmStart(Instant.now());
635         event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
636         event.setAai(new HashMap<>());
637         event.getAai().put(VNF_ID, ONSET_ONE);
638
639         ControlLoopEventManager manager = makeManager(event);
640         ControlLoopEventManager manager2 = manager;
641         assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
642                         .hasMessage("No operation to finish.");
643
644         manager.setActivated(true);
645         assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
646                         .hasMessage("No operation to finish.");
647
648         manager.setActivated(false);
649         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
650         assertNotNull(notification);
651         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
652
653         assertThatThrownBy(manager2::lockCurrentOperation).isInstanceOf(ControlLoopException.class)
654                         .hasMessage("Do not have a current operation.");
655
656         assertNull(manager.unlockCurrentOperation());
657
658         // serialize and de-serialize manager
659         manager = Serializer.roundTrip(manager);
660
661         ControlLoopOperationManager clom = manager.processControlLoop();
662         assertNotNull(clom);
663         assertNull(clom.getOperationResult());
664
665         LockResult<GuardResult, TargetLock> lockLock = manager.lockCurrentOperation();
666         assertNotNull(lockLock);
667         assertEquals(GuardResult.LOCK_ACQUIRED, lockLock.getA());
668
669         LockResult<GuardResult, TargetLock> lockLockAgain = manager.lockCurrentOperation();
670         assertNotNull(lockLockAgain);
671         assertEquals(GuardResult.LOCK_ACQUIRED, lockLockAgain.getA());
672         assertEquals(lockLock.getB(), lockLockAgain.getB());
673
674         assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
675         assertNull(manager.unlockCurrentOperation());
676
677         lockLock = manager.lockCurrentOperation();
678         assertNotNull(lockLock);
679         PolicyGuard.unlockTarget(lockLock.getB());
680         assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
681
682         clom.startOperation(event);
683
684         // This call should be exception free
685         manager.finishOperation(clom);
686
687         ControlLoopEventManager otherManager = makeManager(event);
688         VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
689         assertNotNull(otherNotification);
690         assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
691
692         ControlLoopOperationManager otherClom = otherManager.processControlLoop();
693         assertNotNull(otherClom);
694         assertNull(otherClom.getOperationResult());
695
696         otherManager.finishOperation(clom);
697     }
698
699     @Test
700     public void testOnNewEvent() throws Exception {
701         InputStream is = new FileInputStream(new File(TEST_YAML));
702         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
703
704         UUID requestId = UUID.randomUUID();
705         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
706         onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
707         onsetEvent.setRequestId(requestId);
708         onsetEvent.setTarget(VNF_ID);
709         onsetEvent.setClosedLoopAlarmStart(Instant.now());
710         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
711         onsetEvent.setAai(new HashMap<>());
712         onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
713
714         VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent();
715         abatedEvent.setClosedLoopControlName(TWO_ONSET_TEST);
716         abatedEvent.setRequestId(requestId);
717         abatedEvent.setTarget(VNF_ID);
718         abatedEvent.setClosedLoopAlarmStart(Instant.now());
719         abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
720         abatedEvent.setAai(new HashMap<>());
721         abatedEvent.getAai().put(VNF_NAME, ONSET_ONE);
722
723         ControlLoopEventManager manager = makeManager(onsetEvent);
724         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
725         assertNotNull(notification);
726         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
727
728         assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
729         assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
730         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
731
732         VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent();
733         checkSyntaxEvent.setAai(null);
734         checkSyntaxEvent.setClosedLoopAlarmEnd(null);
735         checkSyntaxEvent.setClosedLoopAlarmStart(null);
736         checkSyntaxEvent.setClosedLoopControlName(null);
737         checkSyntaxEvent.setClosedLoopEventClient(null);
738         checkSyntaxEvent.setClosedLoopEventStatus(null);
739         checkSyntaxEvent.setFrom(null);
740         checkSyntaxEvent.setPolicyName(null);
741         checkSyntaxEvent.setPolicyScope(null);
742         checkSyntaxEvent.setPolicyVersion(null);
743         checkSyntaxEvent.setRequestId(null);
744         checkSyntaxEvent.setTarget(null);
745         checkSyntaxEvent.setTargetType(null);
746         checkSyntaxEvent.setVersion(null);
747
748         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
749
750         checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
751         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
752
753         checkSyntaxEvent.setClosedLoopControlName(null);
754         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
755
756         checkSyntaxEvent.setClosedLoopControlName("");
757         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
758
759         checkSyntaxEvent.setClosedLoopControlName(TWO_ONSET_TEST);
760         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
761
762         checkSyntaxEvent.setRequestId(null);
763         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
764
765         checkSyntaxEvent.setRequestId(requestId);
766         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
767
768         checkSyntaxEvent.setAai(null);
769         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
770
771         checkSyntaxEvent.setAai(new HashMap<>());
772         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
773
774         checkSyntaxEvent.setTarget("");
775         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
776
777         checkSyntaxEvent.setTarget(null);
778         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
779
780         checkSyntaxEvent.setTarget("");
781         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
782
783         checkSyntaxEvent.setTarget("OZ");
784         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
785
786         checkSyntaxEvent.setTarget("VM_NAME");
787         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
788
789         checkSyntaxEvent.setTarget("VNF_NAME");
790         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
791
792         checkSyntaxEvent.setTarget(VSERVER_NAME);
793         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
794
795         checkSyntaxEvent.setTarget(VNF_ID);
796         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
797
798         checkSyntaxEvent.setTarget(VNF_NAME);
799         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
800
801         checkSyntaxEvent.setAai(null);
802         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
803
804         checkSyntaxEvent.setAai(new HashMap<>());
805         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
806
807         checkSyntaxEvent.getAai().put(VNF_NAME, ONSET_ONE);
808         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
809
810         checkSyntaxEvent.getAai().put(VSERVER_NAME, ONSET_ONE);
811         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
812
813         checkSyntaxEvent.getAai().put(VNF_ID, ONSET_ONE);
814         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
815     }
816
817     @Test
818     public void testControlLoopTimeout() throws IOException {
819         InputStream is = new FileInputStream(new File(TEST_YAML));
820         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
821
822         UUID requestId = UUID.randomUUID();
823         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
824         onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
825         onsetEvent.setRequestId(requestId);
826         onsetEvent.setTarget(VNF_ID);
827         onsetEvent.setClosedLoopAlarmStart(Instant.now());
828         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
829         onsetEvent.setAai(new HashMap<>());
830         onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
831
832         ControlLoopEventManager manager = makeManager(onsetEvent);
833         assertTrue(0 == manager.getControlLoopTimeout(null));
834         assertTrue(120 == manager.getControlLoopTimeout(120));
835
836         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
837         assertNotNull(notification);
838         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
839
840         assertEquals(60, manager.getControlLoopTimeout(null));
841     }
842
843     @Test
844     public void testControlLoopTimeout_ZeroTimeout() throws IOException {
845         InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
846         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
847
848         UUID requestId = UUID.randomUUID();
849         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
850         onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
851         onsetEvent.setRequestId(requestId);
852         onsetEvent.setTarget(VNF_ID);
853         onsetEvent.setClosedLoopAlarmStart(Instant.now());
854         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
855         onsetEvent.setAai(new HashMap<>());
856         onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
857
858         ControlLoopEventManager manager = makeManager(onsetEvent);
859
860         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
861         assertNotNull(notification);
862         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
863
864         assertTrue(0 == manager.getControlLoopTimeout(null));
865         assertTrue(120 == manager.getControlLoopTimeout(120));
866     }
867
868     @Test
869     public void testControlLoopTimeout_NullTimeout() throws IOException {
870         InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
871         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
872
873         UUID requestId = UUID.randomUUID();
874         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
875         onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
876         onsetEvent.setRequestId(requestId);
877         onsetEvent.setTarget(VNF_ID);
878         onsetEvent.setClosedLoopAlarmStart(Instant.now());
879         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
880         onsetEvent.setAai(new HashMap<>());
881         onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
882
883         ControlLoopEventManager manager = makeManager(onsetEvent);
884
885         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
886         assertNotNull(notification);
887         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
888
889         assertTrue(0 == manager.getControlLoopTimeout(null));
890         assertTrue(120 == manager.getControlLoopTimeout(120));
891     }
892
893     @Test
894     public void testQueryAai_AlreadyDisabled() throws AaiException {
895         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
896         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
897
898         ControlLoopEventManager mgr = makeManager(onset);
899
900         assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
901                         .hasMessage("is-closed-loop-disabled is set to true on VServer or VNF");
902         assertNull(mgr.getVnfResponse());
903         assertNull(mgr.getVserverResponse());
904     }
905
906     @Test
907     public void testQueryAai_AlreadyInactive() throws AaiException {
908         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
909         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "not-active2");
910
911         ControlLoopEventManager mgr = makeManager(onset);
912
913         assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
914                         .hasMessage("prov-status is not ACTIVE on VServer or VNF");
915         assertNull(mgr.getVnfResponse());
916         assertNull(mgr.getVserverResponse());
917     }
918
919     @Test
920     public void testQueryAai_QueryVnfById() throws AaiException {
921         ControlLoopEventManager mgr = null;
922
923         mgr = makeManager(onset);
924         mgr.queryAai(onset);
925
926         assertNotNull(mgr.getVnfResponse());
927         assertNull(mgr.getVserverResponse());
928
929         AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
930
931         // should not re-query
932         mgr.queryAai(onset);
933
934         assertEquals(vnfresp, mgr.getVnfResponse());
935         assertNull(mgr.getVserverResponse());
936     }
937
938     @Test
939     public void testQueryAai_QueryVnfByName() throws AaiException {
940         ControlLoopEventManager mgr = null;
941
942         // vnf query by name
943         onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
944         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName");
945
946         mgr = makeManager(onset);
947         mgr.queryAai(onset);
948
949         assertNotNull(mgr.getVnfResponse());
950         assertNull(mgr.getVserverResponse());
951
952         AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
953
954         // should not re-query
955         mgr.queryAai(onset);
956
957         assertEquals(vnfresp, mgr.getVnfResponse());
958         assertNull(mgr.getVserverResponse());
959     }
960
961     @Test
962     public void testQueryAai_QueryVnfById_Disabled() {
963         onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop");
964
965         ControlLoopEventManager mgr = makeManager(onset);
966
967         assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
968                         .hasMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
969
970         assertNotNull(mgr.getVnfResponse());
971         assertNull(mgr.getVserverResponse());
972     }
973
974     @Test
975     public void testQueryAai_QueryVserver() throws AaiException {
976         onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
977         onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "AVserver");
978
979         ControlLoopEventManager mgr = makeManager(onset);
980         mgr.queryAai(onset);
981
982         assertNull(mgr.getVnfResponse());
983         assertNotNull(mgr.getVserverResponse());
984
985         AaiGetVserverResponse vsvresp = mgr.getVserverResponse();
986
987         // should not re-query
988         mgr.queryAai(onset);
989
990         assertNull(mgr.getVnfResponse());
991         assertEquals(vsvresp, mgr.getVserverResponse());
992     }
993
994     @Test
995     public void testQueryAai_QueryVserver_Disabled() {
996         onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
997         onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop");
998
999         ControlLoopEventManager mgr = makeManager(onset);
1000
1001         assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
1002                         .hasMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1003
1004         assertNull(mgr.getVnfResponse());
1005         assertNotNull(mgr.getVserverResponse());
1006     }
1007
1008     @Test(expected = AaiException.class)
1009     public void testQueryAai_QueryException() throws AaiException {
1010         // Force AAI errors
1011         PolicyEngine.manager.setEnvironmentProperty(AAI_URL, INVALID_URL);
1012
1013         makeManager(onset).queryAai(onset);
1014     }
1015
1016     @Test
1017     public void testProcessVnfResponse_Success() throws Exception {
1018         AaiGetVnfResponse resp = new AaiGetVnfResponse();
1019         resp.setIsClosedLoopDisabled(false);
1020         resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1021         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1022     }
1023
1024     @Test
1025     public void testProcessVnfResponse_NullResponse() throws Exception {
1026         thrown.expect(AaiException.class);
1027         thrown.expectMessage("AAI Response is null (query by vnf-id)");
1028
1029         AaiGetVnfResponse resp = null;
1030         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1031     }
1032
1033     @Test
1034     public void testProcessVnfResponse_Error() throws Exception {
1035         thrown.expect(AaiException.class);
1036         thrown.expectMessage("AAI Responded with a request error (query by vnf-name)");
1037
1038         AaiGetVnfResponse resp = new AaiGetVnfResponse();
1039
1040         resp.setRequestError(new AaiNqRequestError());
1041
1042         resp.setIsClosedLoopDisabled(false);
1043         resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1044         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1045     }
1046
1047     @Test
1048     public void testProcessVnfResponse_Disabled() throws Exception {
1049         thrown.expect(AaiException.class);
1050         thrown.expectMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
1051
1052         AaiGetVnfResponse resp = new AaiGetVnfResponse();
1053         resp.setIsClosedLoopDisabled(true);
1054         resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1055         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1056     }
1057
1058     @Test
1059     public void testProcessVnfResponse_Inactive() throws Exception {
1060         thrown.expect(AaiException.class);
1061         thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)");
1062
1063         AaiGetVnfResponse resp = new AaiGetVnfResponse();
1064         resp.setIsClosedLoopDisabled(false);
1065         resp.setProvStatus("inactive1");
1066         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1067     }
1068
1069     @Test
1070     public void testProcessVserverResponse_Success() throws Exception {
1071         AaiGetVserverResponse resp = new AaiGetVserverResponse();
1072
1073         AaiNqVServer svr = new AaiNqVServer();
1074         resp.getVserver().add(svr);
1075
1076         svr.setIsClosedLoopDisabled(false);
1077         svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1078         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1079     }
1080
1081     @Test
1082     public void testProcessVserverResponse_NullResponse() throws Exception {
1083         thrown.expect(AaiException.class);
1084         thrown.expectMessage("AAI Response is null (query by vserver-name)");
1085
1086         AaiGetVserverResponse resp = null;
1087         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1088     }
1089
1090     @Test
1091     public void testProcessVserverResponse_Error() throws Exception {
1092         thrown.expect(AaiException.class);
1093         thrown.expectMessage("AAI Responded with a request error (query by vserver-name)");
1094
1095         AaiGetVserverResponse resp = new AaiGetVserverResponse();
1096
1097         resp.setRequestError(new AaiNqRequestError());
1098
1099         AaiNqVServer svr = new AaiNqVServer();
1100         resp.getVserver().add(svr);
1101
1102         svr.setIsClosedLoopDisabled(false);
1103         svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1104
1105         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1106     }
1107
1108     @Test
1109     public void testProcessVserverResponse_Disabled() throws Exception {
1110         thrown.expect(AaiException.class);
1111         thrown.expectMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1112
1113         AaiGetVserverResponse resp = new AaiGetVserverResponse();
1114         AaiNqVServer svr = new AaiNqVServer();
1115         resp.getVserver().add(svr);
1116
1117         svr.setIsClosedLoopDisabled(true);
1118         svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1119         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1120     }
1121
1122     @Test
1123     public void testProcessVserverResponse_Inactive() throws Exception {
1124         thrown.expect(AaiException.class);
1125         thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)");
1126
1127         AaiGetVserverResponse resp = new AaiGetVserverResponse();
1128         AaiNqVServer svr = new AaiNqVServer();
1129         resp.getVserver().add(svr);
1130
1131         svr.setIsClosedLoopDisabled(false);
1132         svr.setProvStatus("inactive1");
1133         Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1134     }
1135
1136     @Test
1137     public void testIsClosedLoopDisabled() {
1138         Map<String, String> aai = onset.getAai();
1139
1140         // null, null
1141         aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1142         aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1143         assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1144
1145         // null, false
1146         aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1147         aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1148         assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1149
1150         // false, null
1151         aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1152         aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1153         assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1154
1155         // null, true
1156         aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1157         aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1158         assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1159
1160         // true, null
1161         aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1162         aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1163         assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1164     }
1165
1166     @Test
1167     public void testIsProvStatusInactive() {
1168         Map<String, String> aai = onset.getAai();
1169
1170         // null, null
1171         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1172         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1173         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1174
1175         // null, active
1176         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1177         aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1178         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1179
1180         // active, null
1181         aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1182         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1183         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1184
1185         // null, inactive
1186         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1187         aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
1188         assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1189
1190         // inactive, null
1191         aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
1192         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1193         assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1194     }
1195
1196     @Test
1197     public void testIsAaiTrue() {
1198         assertTrue(ControlLoopEventManager.isAaiTrue("tRuE"));
1199         assertTrue(ControlLoopEventManager.isAaiTrue("T"));
1200         assertTrue(ControlLoopEventManager.isAaiTrue("t"));
1201         assertTrue(ControlLoopEventManager.isAaiTrue("yES"));
1202         assertTrue(ControlLoopEventManager.isAaiTrue("Y"));
1203         assertTrue(ControlLoopEventManager.isAaiTrue("y"));
1204
1205         assertFalse(ControlLoopEventManager.isAaiTrue("no"));
1206         assertFalse(ControlLoopEventManager.isAaiTrue(null));
1207     }
1208
1209     @Test
1210     public void testGetNqVserverFromAai() {
1211
1212         // empty vserver name
1213         ControlLoopEventManager manager = makeManager(onset);
1214         manager.activate(onset);
1215         assertNull(manager.getNqVserverFromAai());
1216
1217
1218         // re-create manager with a vserver name in the onset
1219         onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "my-name");
1220         manager = makeManager(onset);
1221         manager.activate(onset);
1222
1223         AaiNqResponseWrapper resp = manager.getNqVserverFromAai();
1224         assertNotNull(resp);
1225         assertEquals(onset.getRequestId(), resp.getRequestId());
1226         assertNotNull(resp.getAaiNqResponse());
1227         assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty());
1228
1229         // re-query should return the same object
1230         assertTrue(resp == manager.getNqVserverFromAai());
1231
1232
1233         // Force AAI error
1234         PolicyEngine.manager.setEnvironmentProperty(AAI_URL, INVALID_URL);
1235
1236         // re-create manager
1237         manager = makeManager(onset);
1238         manager.activate(onset);
1239         assertNull(manager.getNqVserverFromAai());
1240     }
1241
1242     @Test
1243     public void testGetCqResponseEmptyVserver() throws AaiException {
1244         ControlLoopEventManager mgr = makeManager(onset);
1245         mgr.queryAai(onset);
1246
1247         assertThatThrownBy(() -> mgr.getCqResponse(onset)).isInstanceOf(AaiException.class)
1248                         .hasMessage("Vserver name is missing");
1249     }
1250
1251     @Test
1252     public void testGetCqResponse() throws AaiException {
1253         ControlLoopEventManager mgr = makeManager(onset);
1254         mgr.queryAai(onset);
1255         onset.getAai().put(VSERVER_NAME, "sample");
1256
1257         AaiCqResponse aaiCqResponse = mgr.getCqResponse(onset);
1258         assertNotNull(aaiCqResponse);
1259     }
1260
1261
1262     private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
1263         return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
1264     }
1265 }