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.assertTrue;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.time.Instant;
36 import java.util.HashMap;
38 import java.util.UUID;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.ExpectedException;
46 import org.onap.policy.aai.AaiCqResponse;
47 import org.onap.policy.aai.AaiGetVnfResponse;
48 import org.onap.policy.aai.AaiGetVserverResponse;
49 import org.onap.policy.aai.AaiNqRequestError;
50 import org.onap.policy.aai.AaiNqResponseWrapper;
51 import org.onap.policy.aai.AaiNqVServer;
52 import org.onap.policy.aai.RelatedToProperty;
53 import org.onap.policy.aai.Relationship;
54 import org.onap.policy.aai.RelationshipData;
55 import org.onap.policy.aai.RelationshipList;
56 import org.onap.policy.aai.util.AaiException;
57 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
58 import org.onap.policy.common.utils.io.Serializer;
59 import org.onap.policy.controlloop.ControlLoopEventStatus;
60 import org.onap.policy.controlloop.ControlLoopException;
61 import org.onap.policy.controlloop.ControlLoopNotificationType;
62 import org.onap.policy.controlloop.SupportUtil;
63 import org.onap.policy.controlloop.VirtualControlLoopEvent;
64 import org.onap.policy.controlloop.VirtualControlLoopNotification;
65 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
66 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
67 import org.onap.policy.controlloop.policy.PolicyResult;
68 import org.onap.policy.drools.system.PolicyEngine;
69 import org.onap.policy.guard.GuardResult;
70 import org.onap.policy.guard.PolicyGuard;
71 import org.onap.policy.guard.PolicyGuard.LockResult;
72 import org.onap.policy.guard.TargetLock;
73 import org.powermock.reflect.Whitebox;
75 public class ControlLoopEventManagerTest {
76 private static final String PROCESS_VSERVER_RESPONSE = "processVServerResponse";
77 private static final String ONSET_ONE = "onsetOne";
78 private static final String VSERVER_NAME = "vserver.vserver-name";
79 private static final String TEST_YAML = "src/test/resources/test.yaml";
80 private static final String SERVICE_TYPE = "service-subscription.service-type";
81 private static final String SERVICE_INSTANCE_NAME = "service-instance.service-instance-name";
82 private static final String SERVICE_INSTANCE_ID = "service-instance.service-instance-id";
83 private static final String SERVICE_INSTANCE = "service-instance";
84 private static final String VNF_NAME_TEXT = "lll_vnf_010317";
85 private static final String SERVICE_INSTANCE_NAME_TEXT = "lll_svc_010317";
86 private static final String VNF_NAME = "generic-vnf.vnf-name";
87 private static final String VNF_ID = "generic-vnf.vnf-id";
88 private static final String SERVICE_INSTANCE_UUID = "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970";
89 private static final String MSO_CUSTOMER_ID = "customer.global-customer-id";
90 private static final String AAI_USERNAME = "aai.username";
91 private static final String AAI_URL = "aai.url";
92 private static final String AAI_PASS = "aai.password";
93 private static final String TWO_ONSET_TEST = "TwoOnsetTest";
94 private static final String MSO_1610_ST = "MSO_1610_ST";
95 private static final String MSO_DEV_SERVICE_TYPE = "MSO-dev-service-type";
96 private static final String VNF_UUID = "83f674e8-7555-44d7-9a39-bdc3770b0491";
97 private static final String AAI_SERVICE_SUBSCRIPTION_URI =
98 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription";
99 private static final String MSO_SERVICE_INSTANCE_URI = "/MSO-dev-service-type/service-instances/service-instance/";
101 private static final String PROCESS_VNF_RESPONSE_METHOD_NAME = "processVnfResponse";
103 private static final String INVALID_URL = "http://localhost:9999";
107 public ExpectedException thrown = ExpectedException.none();
109 private VirtualControlLoopEvent onset;
115 public static void setUpSimulator() throws Exception {
116 org.onap.policy.simulators.Util.buildAaiSim();
118 PolicyEngine.manager.setEnvironmentProperty(AAI_USERNAME, "AAI");
119 PolicyEngine.manager.setEnvironmentProperty(AAI_PASS, "AAI");
120 PolicyEngine.manager.setEnvironmentProperty(AAI_URL, "http://localhost:6666");
124 public static void tearDownSimulator() {
125 HttpServletServer.factory.destroy();
132 public void setUp() {
133 onset = new VirtualControlLoopEvent();
134 onset.setClosedLoopControlName("ControlLoop-vUSP");
135 onset.setRequestId(UUID.randomUUID());
136 onset.setTarget("VM_NAME");
137 onset.setClosedLoopAlarmStart(Instant.now());
138 onset.setAai(new HashMap<String, String>());
139 onset.getAai().put("cloud-region.identity-url", "foo");
140 onset.getAai().put("vserver.selflink", "bar");
141 onset.getAai().put(VNF_ID, VNF_UUID);
142 onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
144 PolicyEngine.manager.setEnvironmentProperty(AAI_URL, "http://localhost:6666");
148 public void testAaiVnfInfo() throws IOException {
149 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
150 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
151 AaiGetVnfResponse response = getQueryByVnfId2();
152 assertNotNull(response);
156 public void testAaiVnfInfo2() throws IOException {
157 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
158 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
159 AaiGetVnfResponse response = getQueryByVnfName2();
160 assertNotNull(response);
164 public void testAaiVserver() throws IOException {
165 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml(TEST_YAML);
166 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
167 AaiGetVserverResponse response = getQueryByVserverName2();
168 assertNotNull(response);
172 public void abatementCheckEventSyntaxTest() throws ControlLoopException {
173 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
174 event.setClosedLoopControlName("abatementAAI");
175 event.setRequestId(UUID.randomUUID());
176 event.setTarget(VNF_ID);
177 event.setClosedLoopAlarmStart(Instant.now());
178 event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
179 ControlLoopEventManager manager = makeManager(event);
180 assertNull(manager.getVnfResponse());
181 assertNull(manager.getVserverResponse());
182 manager.checkEventSyntax(event);
183 assertNull(manager.getVnfResponse());
184 assertNull(manager.getVserverResponse());
187 event.setAai(new HashMap<>());
188 event.getAai().put(VNF_NAME, "abatementTest");
189 manager.checkEventSyntax(event);
190 assertNull(manager.getVnfResponse());
191 assertNull(manager.getVserverResponse());
195 public void subsequentOnsetTest() throws Exception {
196 UUID requestId = UUID.randomUUID();
197 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
198 event.setClosedLoopControlName(TWO_ONSET_TEST);
199 event.setRequestId(requestId);
200 event.setTarget(VNF_ID);
201 event.setClosedLoopAlarmStart(Instant.now());
202 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
203 event.setAai(new HashMap<>());
204 event.getAai().put(VNF_NAME, ONSET_ONE);
206 ControlLoopEventManager manager = makeManager(event);
207 VirtualControlLoopNotification notification = manager.activate(event);
209 assertNotNull(notification);
210 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
212 ControlLoopEventManager.NewEventStatus status = null;
213 status = manager.onNewEvent(event);
214 assertNotNull(status);
215 assertEquals(ControlLoopEventManager.NewEventStatus.FIRST_ONSET, status);
217 AaiGetVnfResponse response = manager.getVnfResponse();
218 assertNotNull(response);
219 assertNull(manager.getVserverResponse());
221 VirtualControlLoopEvent event2 = new VirtualControlLoopEvent();
222 event2.setClosedLoopControlName(TWO_ONSET_TEST);
223 event2.setRequestId(requestId);
224 event2.setTarget(VNF_ID);
225 event2.setClosedLoopAlarmStart(Instant.now());
226 event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
227 event2.setAai(new HashMap<>());
228 event2.getAai().put(VNF_NAME, "onsetTwo");
231 status = manager.onNewEvent(event2);
232 assertEquals(ControlLoopEventManager.NewEventStatus.SUBSEQUENT_ONSET, status);
233 AaiGetVnfResponse response2 = manager.getVnfResponse();
234 assertNotNull(response2);
235 // We should not have queried AAI, so the stored response should be the same
236 assertEquals(response, response2);
237 assertNull(manager.getVserverResponse());
241 * Simulate a response.
243 public static AaiGetVnfResponse getQueryByVnfId2() {
244 AaiGetVnfResponse response = new AaiGetVnfResponse();
246 response.setVnfId(VNF_UUID);
247 response.setVnfName(VNF_NAME_TEXT);
248 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
249 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
250 response.setOrchestrationStatus("Created");
251 response.setInMaint(false);
252 response.setIsClosedLoopDisabled(false);
253 response.setResourceVersion("1494001988835");
254 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
256 final RelationshipList relationshipList = new RelationshipList();
257 final Relationship relationship = new Relationship();
258 RelationshipData relationshipDataItem = new RelationshipData();
260 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
261 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
262 relationship.getRelationshipData().add(relationshipDataItem);
264 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
265 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
266 relationship.getRelationshipData().add(relationshipDataItem);
268 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
269 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
270 relationship.getRelationshipData().add(relationshipDataItem);
272 RelatedToProperty item = new RelatedToProperty();
273 item.setPropertyKey(SERVICE_INSTANCE_NAME);
274 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
275 relationship.getRelatedToProperty().add(item);
277 relationship.setRelatedTo(SERVICE_INSTANCE);
278 relationship.setRelatedLink(
279 AAI_SERVICE_SUBSCRIPTION_URI
280 + MSO_SERVICE_INSTANCE_URI
281 + SERVICE_INSTANCE_UUID);
283 relationshipList.getRelationships().add(relationship);
284 response.setRelationshipList(relationshipList);
290 * Simulate a response.
292 public static AaiGetVnfResponse getQueryByVnfName2() {
293 AaiGetVnfResponse response = new AaiGetVnfResponse();
295 response.setVnfId(VNF_UUID);
296 response.setVnfName(VNF_NAME_TEXT);
297 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
298 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
299 response.setOrchestrationStatus("Created");
300 response.setInMaint(false);
301 response.setIsClosedLoopDisabled(false);
302 response.setResourceVersion("1494001988835");
303 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
305 final RelationshipList relationshipList = new RelationshipList();
306 final Relationship relationship = new Relationship();
307 RelationshipData relationshipDataItem = new RelationshipData();
309 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
310 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
311 relationship.getRelationshipData().add(relationshipDataItem);
313 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
314 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
315 relationship.getRelationshipData().add(relationshipDataItem);
317 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
318 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
319 relationship.getRelationshipData().add(relationshipDataItem);
321 RelatedToProperty item = new RelatedToProperty();
322 item.setPropertyKey(SERVICE_INSTANCE_NAME);
323 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
324 relationship.getRelatedToProperty().add(item);
326 relationship.setRelatedTo(SERVICE_INSTANCE);
327 relationship.setRelatedLink(
328 AAI_SERVICE_SUBSCRIPTION_URI
329 + MSO_SERVICE_INSTANCE_URI
330 + SERVICE_INSTANCE_UUID);
332 relationshipList.getRelationships().add(relationship);
333 response.setRelationshipList(relationshipList);
339 * Simulate a response.
341 public static AaiGetVserverResponse getQueryByVserverName2() {
342 final AaiGetVserverResponse response = new AaiGetVserverResponse();
344 AaiNqVServer svr = new AaiNqVServer();
346 svr.setVserverId("d0668d4f-c25e-4a1b-87c4-83845c01efd8");
347 svr.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0");
348 svr.setVserverName2("vjunos0");
349 svr.setVserverSelflink(
350 "https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8");
351 svr.setInMaint(false);
352 svr.setIsClosedLoopDisabled(false);
353 svr.setResourceVersion("1494001931513");
355 final RelationshipList relationshipList = new RelationshipList();
356 final Relationship relationship = new Relationship();
357 RelationshipData relationshipDataItem = new RelationshipData();
359 relationshipDataItem.setRelationshipKey(MSO_CUSTOMER_ID);
360 relationshipDataItem.setRelationshipValue(MSO_1610_ST);
361 relationship.getRelationshipData().add(relationshipDataItem);
363 relationshipDataItem.setRelationshipKey(SERVICE_TYPE);
364 relationshipDataItem.setRelationshipValue(MSO_DEV_SERVICE_TYPE);
365 relationship.getRelationshipData().add(relationshipDataItem);
367 relationshipDataItem.setRelationshipKey(SERVICE_INSTANCE_ID);
368 relationshipDataItem.setRelationshipValue(SERVICE_INSTANCE_UUID);
369 relationship.getRelationshipData().add(relationshipDataItem);
371 RelatedToProperty item = new RelatedToProperty();
372 item.setPropertyKey(SERVICE_INSTANCE_NAME);
373 item.setPropertyValue(SERVICE_INSTANCE_NAME_TEXT);
374 relationship.getRelatedToProperty().add(item);
376 relationship.setRelatedTo(SERVICE_INSTANCE);
377 relationship.setRelatedLink(
378 AAI_SERVICE_SUBSCRIPTION_URI
379 + MSO_SERVICE_INSTANCE_URI
380 + SERVICE_INSTANCE_UUID);
382 relationshipList.getRelationships().add(relationship);
383 svr.setRelationshipList(relationshipList);
385 response.getVserver().add(svr);
391 public void testMethods() {
392 UUID requestId = UUID.randomUUID();
393 ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
395 assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
396 assertEquals(requestId, clem.getRequestId());
398 clem.setActivated(true);
399 assertEquals(true, clem.isActivated());
401 clem.setControlLoopResult("SUCCESS");
402 assertEquals("SUCCESS", clem.getControlLoopResult());
404 clem.setControlLoopTimedOut();
405 assertEquals(true, clem.isControlLoopTimedOut());
407 clem.setNumAbatements(12345);
408 assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
410 clem.setNumOnsets(54321);
411 assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
413 assertNull(clem.getOnsetEvent());
414 assertNull(clem.getAbatementEvent());
415 assertNull(clem.getProcessor());
417 assertEquals(true, clem.isActive());
418 assertEquals(false, clem.releaseLock());
419 assertEquals(true, clem.isControlLoopTimedOut());
421 assertNull(clem.unlockCurrentOperation());
425 public void testAlreadyActivated() {
426 UUID requestId = UUID.randomUUID();
427 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
428 event.setClosedLoopControlName(TWO_ONSET_TEST);
429 event.setRequestId(requestId);
430 event.setTarget(VNF_ID);
431 event.setClosedLoopAlarmStart(Instant.now());
432 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
433 event.setAai(new HashMap<>());
434 event.getAai().put(VNF_NAME, ONSET_ONE);
436 ControlLoopEventManager manager = makeManager(event);
437 manager.setActivated(true);
438 VirtualControlLoopNotification notification = manager.activate(event);
439 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
443 public void testActivationYaml() throws IOException {
444 InputStream is = new FileInputStream(new File(TEST_YAML));
445 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
447 InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
448 final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
450 UUID requestId = UUID.randomUUID();
451 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
452 event.setClosedLoopControlName(TWO_ONSET_TEST);
453 event.setRequestId(requestId);
454 event.setTarget(VNF_ID);
455 event.setClosedLoopAlarmStart(Instant.now());
456 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
457 event.setAai(new HashMap<>());
458 event.getAai().put(VNF_NAME, ONSET_ONE);
460 ControlLoopEventManager manager = makeManager(event);
462 // Null YAML should fail
463 VirtualControlLoopNotification notificationNull = manager.activate(null, event);
464 assertNotNull(notificationNull);
465 assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
467 // Empty YAML should fail
468 VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
469 assertNotNull(notificationEmpty);
470 assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
472 // Bad YAML should fail
473 VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
474 assertNotNull(notificationBad);
475 assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
477 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
478 assertNotNull(notification);
479 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
481 // Another activate should fail
482 VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
483 assertNotNull(notificationActive);
484 assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
488 public void testControlLoopFinal() throws Exception {
489 InputStream is = new FileInputStream(new File(TEST_YAML));
490 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
492 UUID requestId = UUID.randomUUID();
493 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
494 event.setClosedLoopControlName(TWO_ONSET_TEST);
495 event.setRequestId(requestId);
496 event.setTarget(VNF_ID);
497 event.setClosedLoopAlarmStart(Instant.now());
498 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
499 event.setAai(new HashMap<>());
500 event.getAai().put(VNF_NAME, ONSET_ONE);
502 ControlLoopEventManager manager = makeManager(event);
503 ControlLoopEventManager manager2 = manager;
504 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
505 .hasMessage("ControlLoopEventManager MUST be activated first.");
507 manager.setActivated(true);
508 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
509 .hasMessage("No onset event for ControlLoopEventManager.");
511 manager.setActivated(false);
512 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
513 assertNotNull(notification);
514 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
516 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
517 assertNull(clfNotification);
519 // serialize and de-serialize manager
520 manager = Serializer.roundTrip(manager);
522 manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
523 clfNotification = manager.isControlLoopFinal();
524 assertNotNull(clfNotification);
525 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
527 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
528 notification = manager.activate(yamlString, event);
529 assertNotNull(notification);
530 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
532 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
533 clfNotification = manager.isControlLoopFinal();
534 assertNotNull(clfNotification);
535 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
537 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
538 notification = manager.activate(yamlString, event);
539 assertNotNull(notification);
540 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
542 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
543 clfNotification = manager.isControlLoopFinal();
544 assertNotNull(clfNotification);
545 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
547 manager.setControlLoopTimedOut();
548 clfNotification = manager.isControlLoopFinal();
549 assertNotNull(clfNotification);
550 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
554 public void testProcessControlLoop() throws Exception {
555 InputStream is = new FileInputStream(new File(TEST_YAML));
556 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
558 UUID requestId = UUID.randomUUID();
559 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
560 event.setClosedLoopControlName(TWO_ONSET_TEST);
561 event.setRequestId(requestId);
562 event.setTarget(VNF_ID);
563 event.setClosedLoopAlarmStart(Instant.now());
564 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
565 event.setAai(new HashMap<>());
566 event.getAai().put(VNF_NAME, ONSET_ONE);
568 ControlLoopEventManager manager = makeManager(event);
569 ControlLoopEventManager manager2 = manager;
570 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
571 .hasMessage("ControlLoopEventManager MUST be activated first.");
573 manager.setActivated(true);
574 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
575 .hasMessage("No onset event for ControlLoopEventManager.");
577 manager.setActivated(false);
578 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
579 assertNotNull(notification);
580 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
582 ControlLoopOperationManager clom = manager.processControlLoop();
584 assertNull(clom.getOperationResult());
586 // serialize and de-serialize manager
587 manager = Serializer.roundTrip(manager);
589 // Test operation in progress
590 ControlLoopEventManager manager3 = manager;
591 assertThatThrownBy(manager3::processControlLoop).isInstanceOf(ControlLoopException.class)
592 .hasMessage("Already working an Operation, do not call this method.");
594 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
595 notification = manager.activate(yamlString, event);
596 assertNotNull(notification);
597 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
599 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
600 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
601 assertNotNull(clfNotification);
602 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
604 // Test operation completed
605 ControlLoopEventManager manager4 = manager;
606 assertThatThrownBy(manager4::processControlLoop).isInstanceOf(ControlLoopException.class)
607 .hasMessage("Control Loop is in FINAL state, do not call this method.");
609 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
610 notification = manager.activate(yamlString, event);
611 assertNotNull(notification);
612 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
613 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE);
615 // Test operation with no next policy defined
616 ControlLoopEventManager manager5 = manager;
617 assertThatThrownBy(manager5::processControlLoop).isInstanceOf(ControlLoopException.class)
618 .hasMessage("The target type is null");
622 public void testFinishOperation() throws Exception {
623 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
624 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
626 InputStream isStd = new FileInputStream(new File(TEST_YAML));
627 final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
629 UUID requestId = UUID.randomUUID();
630 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
631 event.setClosedLoopControlName(TWO_ONSET_TEST);
632 event.setRequestId(requestId);
633 event.setTarget(VNF_ID);
634 event.setClosedLoopAlarmStart(Instant.now());
635 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
636 event.setAai(new HashMap<>());
637 event.getAai().put(VNF_ID, ONSET_ONE);
639 ControlLoopEventManager manager = makeManager(event);
640 ControlLoopEventManager manager2 = manager;
641 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
642 .hasMessage("No operation to finish.");
644 manager.setActivated(true);
645 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
646 .hasMessage("No operation to finish.");
648 manager.setActivated(false);
649 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
650 assertNotNull(notification);
651 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
653 assertThatThrownBy(manager2::lockCurrentOperation).isInstanceOf(ControlLoopException.class)
654 .hasMessage("Do not have a current operation.");
656 assertNull(manager.unlockCurrentOperation());
658 // serialize and de-serialize manager
659 manager = Serializer.roundTrip(manager);
661 ControlLoopOperationManager clom = manager.processControlLoop();
663 assertNull(clom.getOperationResult());
665 LockResult<GuardResult, TargetLock> lockLock = manager.lockCurrentOperation();
666 assertNotNull(lockLock);
667 assertEquals(GuardResult.LOCK_ACQUIRED, lockLock.getA());
669 LockResult<GuardResult, TargetLock> lockLockAgain = manager.lockCurrentOperation();
670 assertNotNull(lockLockAgain);
671 assertEquals(GuardResult.LOCK_ACQUIRED, lockLockAgain.getA());
672 assertEquals(lockLock.getB(), lockLockAgain.getB());
674 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
675 assertNull(manager.unlockCurrentOperation());
677 lockLock = manager.lockCurrentOperation();
678 assertNotNull(lockLock);
679 PolicyGuard.unlockTarget(lockLock.getB());
680 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
682 clom.startOperation(event);
684 // This call should be exception free
685 manager.finishOperation(clom);
687 ControlLoopEventManager otherManager = makeManager(event);
688 VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
689 assertNotNull(otherNotification);
690 assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
692 ControlLoopOperationManager otherClom = otherManager.processControlLoop();
693 assertNotNull(otherClom);
694 assertNull(otherClom.getOperationResult());
696 otherManager.finishOperation(clom);
700 public void testOnNewEvent() throws Exception {
701 InputStream is = new FileInputStream(new File(TEST_YAML));
702 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
704 UUID requestId = UUID.randomUUID();
705 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
706 onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
707 onsetEvent.setRequestId(requestId);
708 onsetEvent.setTarget(VNF_ID);
709 onsetEvent.setClosedLoopAlarmStart(Instant.now());
710 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
711 onsetEvent.setAai(new HashMap<>());
712 onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
714 VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent();
715 abatedEvent.setClosedLoopControlName(TWO_ONSET_TEST);
716 abatedEvent.setRequestId(requestId);
717 abatedEvent.setTarget(VNF_ID);
718 abatedEvent.setClosedLoopAlarmStart(Instant.now());
719 abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
720 abatedEvent.setAai(new HashMap<>());
721 abatedEvent.getAai().put(VNF_NAME, ONSET_ONE);
723 ControlLoopEventManager manager = makeManager(onsetEvent);
724 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
725 assertNotNull(notification);
726 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
728 assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
729 assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
730 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
732 VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent();
733 checkSyntaxEvent.setAai(null);
734 checkSyntaxEvent.setClosedLoopAlarmEnd(null);
735 checkSyntaxEvent.setClosedLoopAlarmStart(null);
736 checkSyntaxEvent.setClosedLoopControlName(null);
737 checkSyntaxEvent.setClosedLoopEventClient(null);
738 checkSyntaxEvent.setClosedLoopEventStatus(null);
739 checkSyntaxEvent.setFrom(null);
740 checkSyntaxEvent.setPolicyName(null);
741 checkSyntaxEvent.setPolicyScope(null);
742 checkSyntaxEvent.setPolicyVersion(null);
743 checkSyntaxEvent.setRequestId(null);
744 checkSyntaxEvent.setTarget(null);
745 checkSyntaxEvent.setTargetType(null);
746 checkSyntaxEvent.setVersion(null);
748 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
750 checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
751 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
753 checkSyntaxEvent.setClosedLoopControlName(null);
754 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
756 checkSyntaxEvent.setClosedLoopControlName("");
757 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
759 checkSyntaxEvent.setClosedLoopControlName(TWO_ONSET_TEST);
760 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
762 checkSyntaxEvent.setRequestId(null);
763 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
765 checkSyntaxEvent.setRequestId(requestId);
766 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
768 checkSyntaxEvent.setAai(null);
769 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
771 checkSyntaxEvent.setAai(new HashMap<>());
772 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
774 checkSyntaxEvent.setTarget("");
775 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
777 checkSyntaxEvent.setTarget(null);
778 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
780 checkSyntaxEvent.setTarget("");
781 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
783 checkSyntaxEvent.setTarget("OZ");
784 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
786 checkSyntaxEvent.setTarget("VM_NAME");
787 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
789 checkSyntaxEvent.setTarget("VNF_NAME");
790 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
792 checkSyntaxEvent.setTarget(VSERVER_NAME);
793 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
795 checkSyntaxEvent.setTarget(VNF_ID);
796 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
798 checkSyntaxEvent.setTarget(VNF_NAME);
799 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
801 checkSyntaxEvent.setAai(null);
802 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
804 checkSyntaxEvent.setAai(new HashMap<>());
805 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
807 checkSyntaxEvent.getAai().put(VNF_NAME, ONSET_ONE);
808 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
810 checkSyntaxEvent.getAai().put(VSERVER_NAME, ONSET_ONE);
811 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
813 checkSyntaxEvent.getAai().put(VNF_ID, ONSET_ONE);
814 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
818 public void testControlLoopTimeout() throws IOException {
819 InputStream is = new FileInputStream(new File(TEST_YAML));
820 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
822 UUID requestId = UUID.randomUUID();
823 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
824 onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
825 onsetEvent.setRequestId(requestId);
826 onsetEvent.setTarget(VNF_ID);
827 onsetEvent.setClosedLoopAlarmStart(Instant.now());
828 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
829 onsetEvent.setAai(new HashMap<>());
830 onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
832 ControlLoopEventManager manager = makeManager(onsetEvent);
833 assertTrue(0 == manager.getControlLoopTimeout(null));
834 assertTrue(120 == manager.getControlLoopTimeout(120));
836 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
837 assertNotNull(notification);
838 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
840 assertEquals(60, manager.getControlLoopTimeout(null));
844 public void testControlLoopTimeout_ZeroTimeout() throws IOException {
845 InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
846 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
848 UUID requestId = UUID.randomUUID();
849 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
850 onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
851 onsetEvent.setRequestId(requestId);
852 onsetEvent.setTarget(VNF_ID);
853 onsetEvent.setClosedLoopAlarmStart(Instant.now());
854 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
855 onsetEvent.setAai(new HashMap<>());
856 onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
858 ControlLoopEventManager manager = makeManager(onsetEvent);
860 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
861 assertNotNull(notification);
862 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
864 assertTrue(0 == manager.getControlLoopTimeout(null));
865 assertTrue(120 == manager.getControlLoopTimeout(120));
869 public void testControlLoopTimeout_NullTimeout() throws IOException {
870 InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
871 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
873 UUID requestId = UUID.randomUUID();
874 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
875 onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
876 onsetEvent.setRequestId(requestId);
877 onsetEvent.setTarget(VNF_ID);
878 onsetEvent.setClosedLoopAlarmStart(Instant.now());
879 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
880 onsetEvent.setAai(new HashMap<>());
881 onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
883 ControlLoopEventManager manager = makeManager(onsetEvent);
885 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
886 assertNotNull(notification);
887 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
889 assertTrue(0 == manager.getControlLoopTimeout(null));
890 assertTrue(120 == manager.getControlLoopTimeout(120));
894 public void testQueryAai_AlreadyDisabled() throws AaiException {
895 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
896 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
898 ControlLoopEventManager mgr = makeManager(onset);
900 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
901 .hasMessage("is-closed-loop-disabled is set to true on VServer or VNF");
902 assertNull(mgr.getVnfResponse());
903 assertNull(mgr.getVserverResponse());
907 public void testQueryAai_AlreadyInactive() throws AaiException {
908 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
909 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "not-active2");
911 ControlLoopEventManager mgr = makeManager(onset);
913 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
914 .hasMessage("prov-status is not ACTIVE on VServer or VNF");
915 assertNull(mgr.getVnfResponse());
916 assertNull(mgr.getVserverResponse());
920 public void testQueryAai_QueryVnfById() throws AaiException {
921 ControlLoopEventManager mgr = null;
923 mgr = makeManager(onset);
926 assertNotNull(mgr.getVnfResponse());
927 assertNull(mgr.getVserverResponse());
929 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
931 // should not re-query
934 assertEquals(vnfresp, mgr.getVnfResponse());
935 assertNull(mgr.getVserverResponse());
939 public void testQueryAai_QueryVnfByName() throws AaiException {
940 ControlLoopEventManager mgr = null;
943 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
944 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName");
946 mgr = makeManager(onset);
949 assertNotNull(mgr.getVnfResponse());
950 assertNull(mgr.getVserverResponse());
952 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
954 // should not re-query
957 assertEquals(vnfresp, mgr.getVnfResponse());
958 assertNull(mgr.getVserverResponse());
962 public void testQueryAai_QueryVnfById_Disabled() {
963 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop");
965 ControlLoopEventManager mgr = makeManager(onset);
967 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
968 .hasMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
970 assertNotNull(mgr.getVnfResponse());
971 assertNull(mgr.getVserverResponse());
975 public void testQueryAai_QueryVserver() throws AaiException {
976 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
977 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "AVserver");
979 ControlLoopEventManager mgr = makeManager(onset);
982 assertNull(mgr.getVnfResponse());
983 assertNotNull(mgr.getVserverResponse());
985 AaiGetVserverResponse vsvresp = mgr.getVserverResponse();
987 // should not re-query
990 assertNull(mgr.getVnfResponse());
991 assertEquals(vsvresp, mgr.getVserverResponse());
995 public void testQueryAai_QueryVserver_Disabled() {
996 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
997 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop");
999 ControlLoopEventManager mgr = makeManager(onset);
1001 assertThatThrownBy(() -> mgr.queryAai(onset)).isInstanceOf(AaiException.class)
1002 .hasMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1004 assertNull(mgr.getVnfResponse());
1005 assertNotNull(mgr.getVserverResponse());
1008 @Test(expected = AaiException.class)
1009 public void testQueryAai_QueryException() throws AaiException {
1011 PolicyEngine.manager.setEnvironmentProperty(AAI_URL, INVALID_URL);
1013 makeManager(onset).queryAai(onset);
1017 public void testProcessVnfResponse_Success() throws Exception {
1018 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1019 resp.setIsClosedLoopDisabled(false);
1020 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1021 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1025 public void testProcessVnfResponse_NullResponse() throws Exception {
1026 thrown.expect(AaiException.class);
1027 thrown.expectMessage("AAI Response is null (query by vnf-id)");
1029 AaiGetVnfResponse resp = null;
1030 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1034 public void testProcessVnfResponse_Error() throws Exception {
1035 thrown.expect(AaiException.class);
1036 thrown.expectMessage("AAI Responded with a request error (query by vnf-name)");
1038 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1040 resp.setRequestError(new AaiNqRequestError());
1042 resp.setIsClosedLoopDisabled(false);
1043 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1044 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1048 public void testProcessVnfResponse_Disabled() throws Exception {
1049 thrown.expect(AaiException.class);
1050 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
1052 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1053 resp.setIsClosedLoopDisabled(true);
1054 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1055 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1059 public void testProcessVnfResponse_Inactive() throws Exception {
1060 thrown.expect(AaiException.class);
1061 thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)");
1063 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1064 resp.setIsClosedLoopDisabled(false);
1065 resp.setProvStatus("inactive1");
1066 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1070 public void testProcessVserverResponse_Success() throws Exception {
1071 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1073 AaiNqVServer svr = new AaiNqVServer();
1074 resp.getVserver().add(svr);
1076 svr.setIsClosedLoopDisabled(false);
1077 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1078 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1082 public void testProcessVserverResponse_NullResponse() throws Exception {
1083 thrown.expect(AaiException.class);
1084 thrown.expectMessage("AAI Response is null (query by vserver-name)");
1086 AaiGetVserverResponse resp = null;
1087 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1091 public void testProcessVserverResponse_Error() throws Exception {
1092 thrown.expect(AaiException.class);
1093 thrown.expectMessage("AAI Responded with a request error (query by vserver-name)");
1095 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1097 resp.setRequestError(new AaiNqRequestError());
1099 AaiNqVServer svr = new AaiNqVServer();
1100 resp.getVserver().add(svr);
1102 svr.setIsClosedLoopDisabled(false);
1103 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1105 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1109 public void testProcessVserverResponse_Disabled() throws Exception {
1110 thrown.expect(AaiException.class);
1111 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1113 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1114 AaiNqVServer svr = new AaiNqVServer();
1115 resp.getVserver().add(svr);
1117 svr.setIsClosedLoopDisabled(true);
1118 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1119 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1123 public void testProcessVserverResponse_Inactive() throws Exception {
1124 thrown.expect(AaiException.class);
1125 thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)");
1127 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1128 AaiNqVServer svr = new AaiNqVServer();
1129 resp.getVserver().add(svr);
1131 svr.setIsClosedLoopDisabled(false);
1132 svr.setProvStatus("inactive1");
1133 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VSERVER_RESPONSE, resp);
1137 public void testIsClosedLoopDisabled() {
1138 Map<String, String> aai = onset.getAai();
1141 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1142 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1143 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1146 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1147 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1148 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1151 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1152 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1153 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1156 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1157 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1158 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1161 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1162 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1163 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1167 public void testIsProvStatusInactive() {
1168 Map<String, String> aai = onset.getAai();
1171 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1172 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1173 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1176 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1177 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1178 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1181 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1182 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1183 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1186 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1187 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
1188 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1191 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
1192 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1193 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1197 public void testIsAaiTrue() {
1198 assertTrue(ControlLoopEventManager.isAaiTrue("tRuE"));
1199 assertTrue(ControlLoopEventManager.isAaiTrue("T"));
1200 assertTrue(ControlLoopEventManager.isAaiTrue("t"));
1201 assertTrue(ControlLoopEventManager.isAaiTrue("yES"));
1202 assertTrue(ControlLoopEventManager.isAaiTrue("Y"));
1203 assertTrue(ControlLoopEventManager.isAaiTrue("y"));
1205 assertFalse(ControlLoopEventManager.isAaiTrue("no"));
1206 assertFalse(ControlLoopEventManager.isAaiTrue(null));
1210 public void testGetNqVserverFromAai() {
1212 // empty vserver name
1213 ControlLoopEventManager manager = makeManager(onset);
1214 manager.activate(onset);
1215 assertNull(manager.getNqVserverFromAai());
1218 // re-create manager with a vserver name in the onset
1219 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "my-name");
1220 manager = makeManager(onset);
1221 manager.activate(onset);
1223 AaiNqResponseWrapper resp = manager.getNqVserverFromAai();
1224 assertNotNull(resp);
1225 assertEquals(onset.getRequestId(), resp.getRequestId());
1226 assertNotNull(resp.getAaiNqResponse());
1227 assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty());
1229 // re-query should return the same object
1230 assertTrue(resp == manager.getNqVserverFromAai());
1234 PolicyEngine.manager.setEnvironmentProperty(AAI_URL, INVALID_URL);
1236 // re-create manager
1237 manager = makeManager(onset);
1238 manager.activate(onset);
1239 assertNull(manager.getNqVserverFromAai());
1243 public void testGetCqResponseEmptyVserver() throws AaiException {
1244 ControlLoopEventManager mgr = makeManager(onset);
1245 mgr.queryAai(onset);
1247 assertThatThrownBy(() -> mgr.getCqResponse(onset)).isInstanceOf(AaiException.class)
1248 .hasMessage("Vserver name is missing");
1252 public void testGetCqResponse() throws AaiException {
1253 ControlLoopEventManager mgr = makeManager(onset);
1254 mgr.queryAai(onset);
1255 onset.getAai().put(VSERVER_NAME, "sample");
1257 AaiCqResponse aaiCqResponse = mgr.getCqResponse(onset);
1258 assertNotNull(aaiCqResponse);
1262 private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
1263 return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());