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