2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.eventmanager;
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;
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;
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;
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/";
109 private static final String PROCESS_VNF_RESPONSE_METHOD_NAME = "processVnfResponse";
111 private static final String INVALID_URL = "http://localhost:9999";
115 public ExpectedException thrown = ExpectedException.none();
117 private VirtualControlLoopEvent onset;
118 private LockCallback callback;
124 public static void setUpSimulator() throws Exception {
125 org.onap.policy.simulators.Util.buildAaiSim();
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");
134 public static void tearDownSimulator() {
135 HttpServletServerFactoryInstance.getServerFactory().destroy();
142 public void setUp() {
143 callback = mock(LockCallback.class);
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);
157 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, "http://localhost:6666");
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);
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);
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);
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());
200 event.setAai(new HashMap<>());
201 event.getAai().put(VNF_NAME, "abatementTest");
202 manager.checkEventSyntax(event);
203 assertNull(manager.getVnfResponse());
204 assertNull(manager.getVserverResponse());
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);
220 ControlLoopEventManager manager = makeManager(event);
221 VirtualControlLoopNotification notification = manager.activate(event);
223 assertNotNull(notification);
224 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
226 ControlLoopEventManager.NewEventStatus status = null;
227 status = manager.onNewEvent(event);
228 assertNotNull(status);
229 assertEquals(ControlLoopEventManager.NewEventStatus.FIRST_ONSET, status);
231 AaiGetVnfResponse response = manager.getVnfResponse();
232 assertNotNull(response);
233 assertNull(manager.getVserverResponse());
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);
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());
256 * Simulate a response.
258 public static AaiGetVnfResponse getQueryByVnfId2() {
259 AaiGetVnfResponse response = new AaiGetVnfResponse();
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");
271 final RelationshipList relationshipList = new RelationshipList();
272 final Relationship relationship = new Relationship();
273 RelationshipData relationshipDataItem = new RelationshipData();
275 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
276 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
277 relationship.getRelationshipData().add(relationshipDataItem);
279 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
280 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
281 relationship.getRelationshipData().add(relationshipDataItem);
283 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
284 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
285 relationship.getRelationshipData().add(relationshipDataItem);
287 RelatedToProperty item = new RelatedToProperty();
288 item.setPropertyKey(SERVICE_INSTANCE_NAME);
289 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
290 relationship.getRelatedToProperty().add(item);
292 relationship.setRelatedTo(SERVICE_INSTANCE);
293 relationship.setRelatedLink(
294 AAI_SERVICE_SUBSCRIPTION_URI
295 + MSO_SERVICE_INSTANCE_URI
296 + SERVICE_INSTANCE_UUID);
298 relationshipList.getRelationships().add(relationship);
299 response.setRelationshipList(relationshipList);
305 * Simulate a response.
307 public static AaiGetVnfResponse getQueryByVnfName2() {
308 AaiGetVnfResponse response = new AaiGetVnfResponse();
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");
320 final RelationshipList relationshipList = new RelationshipList();
321 final Relationship relationship = new Relationship();
322 RelationshipData relationshipDataItem = new RelationshipData();
324 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
325 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
326 relationship.getRelationshipData().add(relationshipDataItem);
328 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
329 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
330 relationship.getRelationshipData().add(relationshipDataItem);
332 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
333 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
334 relationship.getRelationshipData().add(relationshipDataItem);
336 RelatedToProperty item = new RelatedToProperty();
337 item.setPropertyKey(SERVICE_INSTANCE_NAME);
338 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
339 relationship.getRelatedToProperty().add(item);
341 relationship.setRelatedTo(SERVICE_INSTANCE);
342 relationship.setRelatedLink(
343 AAI_SERVICE_SUBSCRIPTION_URI
344 + MSO_SERVICE_INSTANCE_URI
345 + SERVICE_INSTANCE_UUID);
347 relationshipList.getRelationships().add(relationship);
348 response.setRelationshipList(relationshipList);
354 * Simulate a response.
356 public static AaiGetVserverResponse getQueryByVserverName2() {
357 final AaiGetVserverResponse response = new AaiGetVserverResponse();
359 AaiNqVServer svr = new AaiNqVServer();
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");
370 final RelationshipList relationshipList = new RelationshipList();
371 final Relationship relationship = new Relationship();
372 RelationshipData relationshipDataItem = new RelationshipData();
374 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
375 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
376 relationship.getRelationshipData().add(relationshipDataItem);
378 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
379 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
380 relationship.getRelationshipData().add(relationshipDataItem);
382 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
383 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
384 relationship.getRelationshipData().add(relationshipDataItem);
386 RelatedToProperty item = new RelatedToProperty();
387 item.setPropertyKey(SERVICE_INSTANCE_NAME);
388 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
389 relationship.getRelatedToProperty().add(item);
391 relationship.setRelatedTo(SERVICE_INSTANCE);
392 relationship.setRelatedLink(
393 AAI_SERVICE_SUBSCRIPTION_URI
394 + MSO_SERVICE_INSTANCE_URI
395 + SERVICE_INSTANCE_UUID);
397 relationshipList.getRelationships().add(relationship);
398 svr.setRelationshipList(relationshipList);
400 response.getVserver().add(svr);
406 public void testMethods() {
407 UUID requestId = UUID.randomUUID();
408 ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
410 assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
411 assertEquals(requestId, clem.getRequestId());
413 clem.setActivated(true);
414 assertEquals(true, clem.isActivated());
416 clem.setControlLoopResult("SUCCESS");
417 assertEquals("SUCCESS", clem.getControlLoopResult());
419 clem.setControlLoopTimedOut();
420 assertEquals(true, clem.isControlLoopTimedOut());
422 clem.setNumAbatements(12345);
423 assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
425 clem.setNumOnsets(54321);
426 assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
428 assertNull(clem.getOnsetEvent());
429 assertNull(clem.getAbatementEvent());
430 assertNull(clem.getProcessor());
432 assertEquals(true, clem.isControlLoopTimedOut());
434 assertNull(clem.unlockCurrentOperation());
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);
450 ControlLoopEventManager manager = makeManager(event);
451 manager.setActivated(true);
452 VirtualControlLoopNotification notification = manager.activate(event);
453 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
457 public void testActivationYaml() throws IOException {
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);
470 ControlLoopEventManager manager = makeManager(event);
472 // Null YAML should fail
473 VirtualControlLoopNotification notificationNull = manager.activate(null, event);
474 assertNotNull(notificationNull);
475 assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
477 // Empty YAML should fail
478 VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
479 assertNotNull(notificationEmpty);
480 assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
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);
486 VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
487 assertNotNull(notificationBad);
488 assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
491 InputStream is = new FileInputStream(new File(TEST_YAML));
492 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
494 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
495 assertNotNull(notification);
496 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
498 // Another activate should fail
499 VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
500 assertNotNull(notificationActive);
501 assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
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);
517 ControlLoopEventManager manager = makeManager(event);
518 ControlLoopEventManager manager2 = manager;
519 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
520 .hasMessage("ControlLoopEventManager MUST be activated first.");
522 manager.setActivated(true);
523 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
524 .hasMessage("No onset event for ControlLoopEventManager.");
526 manager.setActivated(false);
528 InputStream is = new FileInputStream(new File(TEST_YAML));
529 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
531 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
532 assertNotNull(notification);
533 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
535 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
536 assertNull(clfNotification);
538 // serialize and de-serialize manager
539 manager = Serializer.roundTrip(manager);
541 manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
542 clfNotification = manager.isControlLoopFinal();
543 assertNotNull(clfNotification);
544 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
546 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
547 notification = manager.activate(yamlString, event);
548 assertNotNull(notification);
549 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
551 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
552 clfNotification = manager.isControlLoopFinal();
553 assertNotNull(clfNotification);
554 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
556 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
557 notification = manager.activate(yamlString, event);
558 assertNotNull(notification);
559 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
561 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
562 clfNotification = manager.isControlLoopFinal();
563 assertNotNull(clfNotification);
564 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
566 manager.setControlLoopTimedOut();
567 clfNotification = manager.isControlLoopFinal();
568 assertNotNull(clfNotification);
569 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
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);
586 ControlLoopEventManager manager = makeManager(event);
587 ControlLoopEventManager manager2 = manager;
588 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
589 .hasMessage("ControlLoopEventManager MUST be activated first.");
591 manager.setActivated(true);
592 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
593 .hasMessage("No onset event for ControlLoopEventManager.");
595 manager.setActivated(false);
597 InputStream is = new FileInputStream(new File(TEST_YAML));
598 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
600 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
601 assertNotNull(notification);
602 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
604 ControlLoopOperationManager clom = manager.processControlLoop();
606 assertNull(clom.getOperationResult());
608 // serialize and de-serialize manager
609 manager = Serializer.roundTrip(manager);
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.");
616 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
617 notification = manager.activate(yamlString, event);
618 assertNotNull(notification);
619 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
621 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
622 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
623 assertNotNull(clfNotification);
624 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
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.");
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);
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");
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);
648 VirtualControlLoopEvent event = makeEvent();
650 ControlLoopEventManager manager = makeManager(event);
651 ControlLoopEventManager manager2 = manager;
652 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
653 .hasMessage("No operation to finish.");
655 manager.setActivated(true);
656 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
657 .hasMessage("No operation to finish.");
659 manager.setActivated(false);
661 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
662 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
664 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
665 assertNotNull(notification);
666 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
668 // serialize and de-serialize manager
669 manager = Serializer.roundTrip(manager);
671 ControlLoopOperationManager clom = manager.processControlLoop();
673 assertNull(clom.getOperationResult());
675 clom.startOperation(event);
677 // This call should be exception free
678 manager.finishOperation(clom);
680 ControlLoopEventManager otherManager = makeManager(event);
681 VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
682 assertNotNull(otherNotification);
683 assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
685 ControlLoopOperationManager otherClom = otherManager.processControlLoop();
686 assertNotNull(otherClom);
687 assertNull(otherClom.getOperationResult());
689 otherManager.finishOperation(clom);
693 public void testLockCurrentOperation_testUnlockCurrentOperation() throws Exception {
694 VirtualControlLoopEvent event = makeEvent();
696 ControlLoopEventManager manager = makeManager(event);
698 manager.setActivated(false);
700 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
701 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
703 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
704 assertNotNull(notification);
705 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
707 ControlLoopEventManager manager2 = manager;
708 assertThatThrownBy(() -> manager2.lockCurrentOperation(callback)).isInstanceOf(ControlLoopException.class)
709 .hasMessage("Do not have a current operation.");
711 assertNull(manager.unlockCurrentOperation());
713 ControlLoopOperationManager clom = manager.processControlLoop();
715 assertNull(clom.getOperationResult());
717 Pair<Lock, Lock> lockPair = manager.lockCurrentOperation(callback);
718 assertNull(lockPair.first());
719 assertNotNull(lockPair.second());
721 // pseudo lock - session should NOT have been notified of the change
722 verify(callback, never()).lockAvailable(any());
723 verify(callback, never()).lockUnavailable(any());
725 // repeat - should cause an extension
726 Lock lock = lockPair.second();
727 lockPair = manager.lockCurrentOperation(callback);
730 * even with a pseudo lock, the session should have been notified that it was
734 verify(callback).lockAvailable(lock);
736 assertSame(lock, manager.unlockCurrentOperation());
738 assertNull(lockPair.first());
739 assertNull(lockPair.second());
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());
747 lock = lockPair.second();
749 lockPair = manager.lockCurrentOperation(callback);
750 assertNull(lockPair.first());
751 assertNull(lockPair.second());
753 // first lock uses a pseudo lock, so it will only update when extended
754 verify(callback).lockAvailable(lock);
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);
761 lockPair = manager.lockCurrentOperation(callback);
762 assertSame(lock, lockPair.first());
763 assertNotNull(lockPair.second());
765 lock = lockPair.second();
767 lockPair = manager.lockCurrentOperation(callback);
768 assertNull(lockPair.first());
769 assertNull(lockPair.second());
771 // first lock uses a pseudo lock, so it won't do an update
772 verify(callback).lockAvailable(lock);
774 assertSame(lock, manager.unlockCurrentOperation());
775 assertNull(manager.unlockCurrentOperation());
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());
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);
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);
805 ControlLoopEventManager manager = makeManager(onsetEvent);
807 InputStream is = new FileInputStream(new File(TEST_YAML));
808 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
810 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
811 assertNotNull(notification);
812 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
814 assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
815 assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
816 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
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);
834 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
836 checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
837 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
839 checkSyntaxEvent.setClosedLoopControlName(null);
840 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
842 checkSyntaxEvent.setClosedLoopControlName("");
843 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
845 checkSyntaxEvent.setClosedLoopControlName(TWO_ONSET_TEST);
846 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
848 checkSyntaxEvent.setRequestId(null);
849 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
851 checkSyntaxEvent.setRequestId(requestId);
852 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
854 checkSyntaxEvent.setAai(null);
855 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
857 checkSyntaxEvent.setAai(new HashMap<>());
858 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
860 checkSyntaxEvent.setTargetType("");
861 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
863 checkSyntaxEvent.setTarget("");
864 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
866 checkSyntaxEvent.setTarget(null);
867 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
869 checkSyntaxEvent.setTarget("");
870 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
872 checkSyntaxEvent.setTarget("OZ");
873 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
875 checkSyntaxEvent.setTarget("VM_NAME");
876 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
878 checkSyntaxEvent.setTarget("VNF_NAME");
879 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
881 checkSyntaxEvent.setTarget(VSERVER_NAME);
882 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
884 checkSyntaxEvent.setTarget(VNF_ID);
885 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
887 checkSyntaxEvent.setTarget(VNF_NAME);
888 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
890 checkSyntaxEvent.setAai(null);
891 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
893 checkSyntaxEvent.setAai(new HashMap<>());
894 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
896 checkSyntaxEvent.getAai().put(VNF_NAME, ONSET_ONE);
897 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
899 checkSyntaxEvent.getAai().put(VSERVER_NAME, ONSET_ONE);
900 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
902 checkSyntaxEvent.getAai().put(VNF_ID, ONSET_ONE);
903 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
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);
919 ControlLoopEventManager manager = makeManager(onsetEvent);
920 assertTrue(0 == manager.getControlLoopTimeout(null));
921 assertTrue(120 == manager.getControlLoopTimeout(120));
923 InputStream is = new FileInputStream(new File(TEST_YAML));
924 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
926 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
927 assertNotNull(notification);
928 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
930 assertEquals(60, manager.getControlLoopTimeout(null));
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);
946 ControlLoopEventManager manager = makeManager(onsetEvent);
948 InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
949 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
951 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
952 assertNotNull(notification);
953 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
955 assertTrue(0 == manager.getControlLoopTimeout(null));
956 assertTrue(120 == manager.getControlLoopTimeout(120));
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);
972 ControlLoopEventManager manager = makeManager(onsetEvent);
974 InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
975 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
977 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
978 assertNotNull(notification);
979 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
981 assertTrue(0 == manager.getControlLoopTimeout(null));
982 assertTrue(120 == manager.getControlLoopTimeout(120));
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);
990 ControlLoopEventManager mgr = makeManager(onset);
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());
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");
1003 ControlLoopEventManager mgr = makeManager(onset);
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());
1012 public void testQueryAai_QueryVnfById() throws AaiException {
1013 ControlLoopEventManager mgr = null;
1015 mgr = makeManager(onset);
1016 mgr.queryAai(onset);
1018 assertNotNull(mgr.getVnfResponse());
1019 assertNull(mgr.getVserverResponse());
1021 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
1023 // should not re-query
1024 mgr.queryAai(onset);
1026 assertEquals(vnfresp, mgr.getVnfResponse());
1027 assertNull(mgr.getVserverResponse());
1031 public void testQueryAai_QueryVnfByName() throws AaiException {
1032 ControlLoopEventManager mgr = null;
1034 // vnf query by name
1035 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1036 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName");
1038 mgr = makeManager(onset);
1039 mgr.queryAai(onset);
1041 assertNotNull(mgr.getVnfResponse());
1042 assertNull(mgr.getVserverResponse());
1044 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
1046 // should not re-query
1047 mgr.queryAai(onset);
1049 assertEquals(vnfresp, mgr.getVnfResponse());
1050 assertNull(mgr.getVserverResponse());
1054 public void testQueryAai_QueryVnfById_Disabled() {
1055 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop");
1057 ControlLoopEventManager mgr = makeManager(onset);
1059 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
1060 .hasMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
1062 assertNotNull(mgr.getVnfResponse());
1063 assertNull(mgr.getVserverResponse());
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");
1071 ControlLoopEventManager mgr = makeManager(onset);
1072 mgr.queryAai(onset);
1074 assertNull(mgr.getVnfResponse());
1075 assertNotNull(mgr.getVserverResponse());
1077 AaiGetVserverResponse vsvresp = mgr.getVserverResponse();
1079 // should not re-query
1080 mgr.queryAai(onset);
1082 assertNull(mgr.getVnfResponse());
1083 assertEquals(vsvresp, mgr.getVserverResponse());
1087 public void testQueryAai_QueryVserver_Disabled() {
1088 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1089 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop");
1091 ControlLoopEventManager mgr = makeManager(onset);
1093 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
1094 .hasMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1096 assertNull(mgr.getVnfResponse());
1097 assertNotNull(mgr.getVserverResponse());
1100 @Test(expected = AaiException.class)
1101 public void testQueryAai_QueryException() throws AaiException {
1103 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, INVALID_URL);
1105 makeManager(onset).queryAai(onset);
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);
1117 public void testProcessVnfResponse_NullResponse() throws Exception {
1118 thrown.expect(AaiException.class);
1119 thrown.expectMessage("AAI Response is null (query by vnf-id)");
1121 AaiGetVnfResponse resp = null;
1122 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
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)");
1130 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1132 resp.setRequestError(new AaiNqRequestError());
1134 resp.setClosedLoopDisabled(false);
1135 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1136 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
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)");
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);
1151 public void testProcessVnfResponse_Inactive() throws Exception {
1152 thrown.expect(AaiException.class);
1153 thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)");
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);
1162 public void testProcessVserverResponse_Success() throws Exception {
1163 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1165 AaiNqVServer svr = new AaiNqVServer();
1166 resp.getVserver().add(svr);
1168 svr.setIsClosedLoopDisabled(false);
1169 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1170 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1174 public void testProcessVserverResponse_NullResponse() throws Exception {
1175 thrown.expect(AaiException.class);
1176 thrown.expectMessage("AAI Response is null (query by vserver-name)");
1178 AaiGetVserverResponse resp = null;
1179 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
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)");
1187 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1189 resp.setRequestError(new AaiNqRequestError());
1191 AaiNqVServer svr = new AaiNqVServer();
1192 resp.getVserver().add(svr);
1194 svr.setIsClosedLoopDisabled(false);
1195 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1197 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
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)");
1205 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1206 AaiNqVServer svr = new AaiNqVServer();
1207 resp.getVserver().add(svr);
1209 svr.setIsClosedLoopDisabled(true);
1210 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1211 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1215 public void testProcessVserverResponse_Inactive() throws Exception {
1216 thrown.expect(AaiException.class);
1217 thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)");
1219 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1220 AaiNqVServer svr = new AaiNqVServer();
1221 resp.getVserver().add(svr);
1223 svr.setIsClosedLoopDisabled(false);
1224 svr.setProvStatus("inactive1");
1225 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1229 public void testIsClosedLoopDisabled() {
1230 Map<String, String> aai = onset.getAai();
1233 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1234 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1235 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
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));
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));
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));
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));
1259 public void testIsProvStatusInactive() {
1260 Map<String, String> aai = onset.getAai();
1263 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1264 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1265 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1268 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1269 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1270 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1273 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1274 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1275 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1278 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1279 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
1280 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1283 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
1284 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1285 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
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"));
1297 assertFalse(ControlLoopEventManager.isAaiTrue("no"));
1298 assertFalse(ControlLoopEventManager.isAaiTrue(null));
1302 public void testGetNqVserverFromAai() {
1304 // empty vserver name
1305 ControlLoopEventManager manager = makeManager(onset);
1306 manager.activate(onset);
1307 assertNull(manager.getNqVserverFromAai());
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);
1315 AaiNqResponseWrapper resp = manager.getNqVserverFromAai();
1316 assertNotNull(resp);
1317 assertEquals(onset.getRequestId(), resp.getRequestId());
1318 assertNotNull(resp.getAaiNqResponse());
1319 assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty());
1321 // re-query should return the same object
1322 assertTrue(resp == manager.getNqVserverFromAai());
1326 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, INVALID_URL);
1328 // re-create manager
1329 manager = makeManager(onset);
1330 manager.activate(onset);
1331 assertNull(manager.getNqVserverFromAai());
1335 public void testGetCqResponseEmptyVserver() throws AaiException {
1336 ControlLoopEventManager mgr = makeManager(onset);
1337 mgr.queryAai(onset);
1339 assertThatThrownBy(() -> mgr.getCqResponse(onset)).isInstanceOf(AaiException.class)
1340 .hasMessage("Vserver name is missing");
1344 public void testGetCqResponse() throws AaiException {
1345 ControlLoopEventManager mgr = makeManager(onset);
1346 mgr.queryAai(onset);
1347 onset.getAai().put(VSERVER_NAME, "sample");
1349 AaiCqResponse aaiCqResponse = mgr.getCqResponse(onset);
1350 assertNotNull(aaiCqResponse);
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);
1368 private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
1369 return new MyManager(event.getClosedLoopControlName(), event.getRequestId());
1372 private static class MyManager extends ControlLoopEventManager implements Serializable {
1373 private static final long serialVersionUID = 1L;
1375 public MyManager(String closedLoopControlName, UUID requestId) {
1376 super(closedLoopControlName, requestId);
1380 protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
1381 return createPseudoLock(targetEntity, requestId, holdSec, callback);