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