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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 public class ControlLoopEventManagerTest {
78 private static final String PROCESS_VNF_RESPONSE_METHOD_NAME = "processVnfResponse";
80 private static final String INVALID_URL = "http://localhost:9999";
82 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class);
86 public ExpectedException thrown = ExpectedException.none();
88 private VirtualControlLoopEvent onset;
94 public static void setUpSimulator() {
96 org.onap.policy.simulators.Util.buildAaiSim();
97 } catch (Exception e) {
100 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
101 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
102 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
106 public static void tearDownSimulator() {
107 HttpServletServer.factory.destroy();
114 public void setUp() {
115 onset = new VirtualControlLoopEvent();
116 onset.setClosedLoopControlName("ControlLoop-vUSP");
117 onset.setRequestId(UUID.randomUUID());
118 onset.setTarget("VM_NAME");
119 onset.setClosedLoopAlarmStart(Instant.now());
120 onset.setAai(new HashMap<String, String>());
121 onset.getAai().put("cloud-region.identity-url", "foo");
122 onset.getAai().put("vserver.selflink", "bar");
123 onset.getAai().put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491");
124 onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
126 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
130 public void testAaiVnfInfo() {
131 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
132 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
134 AaiGetVnfResponse response = getQueryByVnfId2(
135 PolicyEngine.manager.getEnvironmentProperty("aai.url")
136 + "/aai/v11/network/generic-vnfs/generic-vnf/",
137 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
138 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(),
139 "5e49ca06-2972-4532-9ed4-6d071588d792");
140 assertNotNull(response);
141 logger.info("testAAIVnfInfo test result is " + (response == null ? "null" : "not null"));
142 } catch (Exception e) {
143 logger.error("testAAIVnfInfo Exception: ", e);
144 fail(e.getMessage());
149 public void testAaiVnfInfo2() {
150 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
151 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
153 AaiGetVnfResponse response = getQueryByVnfName2(
154 PolicyEngine.manager.getEnvironmentProperty("aai.url")
155 + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=",
156 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
157 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), "lll_vnf_010317");
158 assertNotNull(response);
159 logger.info("testAAIVnfInfo2 test result is " + (response == null ? "null" : "not null"));
160 } catch (Exception e) {
161 logger.error("testAAIVnfInfo2 Exception: ", e);
162 fail(e.getMessage());
167 public void testAaiVserver() {
168 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
169 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
171 AaiGetVserverResponse response = getQueryByVserverName2(
172 PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=",
173 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
174 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(),
175 "USMSO1SX7NJ0103UJZZ01-vjunos0");
176 assertNotNull(response);
177 logger.info("testAAIVserver test result is " + (response == null ? "null" : "not null"));
178 } catch (Exception e) {
179 logger.error("testAAIVserver Exception: ", e);
180 fail(e.getMessage());
185 public void abatementCheckEventSyntaxTest() {
186 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
187 event.setClosedLoopControlName("abatementAAI");
188 event.setRequestId(UUID.randomUUID());
189 event.setTarget("generic-vnf.vnf-id");
190 event.setClosedLoopAlarmStart(Instant.now());
191 event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
192 ControlLoopEventManager manager = makeManager(event);
193 assertNull(manager.getVnfResponse());
194 assertNull(manager.getVserverResponse());
196 manager.checkEventSyntax(event);
197 } catch (ControlLoopException e) {
198 logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage());
200 fail("Exception in check event syntax");
202 assertNull(manager.getVnfResponse());
203 assertNull(manager.getVserverResponse());
206 event.setAai(new HashMap<>());
207 event.getAai().put("generic-vnf.vnf-name", "abatementTest");
209 manager.checkEventSyntax(event);
210 } catch (ControlLoopException e) {
211 logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage());
213 fail("Exception in check event syntax");
215 assertNull(manager.getVnfResponse());
216 assertNull(manager.getVserverResponse());
220 public void subsequentOnsetTest() throws IOException {
221 UUID requestId = UUID.randomUUID();
222 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
223 event.setClosedLoopControlName("TwoOnsetTest");
224 event.setRequestId(requestId);
225 event.setTarget("generic-vnf.vnf-id");
226 event.setClosedLoopAlarmStart(Instant.now());
227 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
228 event.setAai(new HashMap<>());
229 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
231 ControlLoopEventManager manager = makeManager(event);
232 VirtualControlLoopNotification notification = manager.activate(event);
234 assertNotNull(notification);
235 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
237 ControlLoopEventManager.NewEventStatus status = null;
239 status = manager.onNewEvent(event);
240 } catch (AaiException e) {
241 logger.warn(e.toString());
242 fail("A&AI Query Failed");
244 assertNotNull(status);
245 assertEquals(ControlLoopEventManager.NewEventStatus.FIRST_ONSET, status);
247 AaiGetVnfResponse response = manager.getVnfResponse();
248 assertNotNull(response);
249 assertNull(manager.getVserverResponse());
251 VirtualControlLoopEvent event2 = new VirtualControlLoopEvent();
252 event2.setClosedLoopControlName("TwoOnsetTest");
253 event2.setRequestId(requestId);
254 event2.setTarget("generic-vnf.vnf-id");
255 event2.setClosedLoopAlarmStart(Instant.now());
256 event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
257 event2.setAai(new HashMap<>());
258 event2.getAai().put("generic-vnf.vnf-name", "onsetTwo");
262 status = manager.onNewEvent(event2);
263 } catch (AaiException e) {
264 logger.warn(e.toString());
265 fail("A&AI Query Failed");
267 assertEquals(ControlLoopEventManager.NewEventStatus.SUBSEQUENT_ONSET, status);
268 AaiGetVnfResponse response2 = manager.getVnfResponse();
269 assertNotNull(response2);
270 // We should not have queried AAI, so the stored response should be the same
271 assertEquals(response, response2);
272 assertNull(manager.getVserverResponse());
276 * Simulate a response.
278 public static AaiGetVnfResponse getQueryByVnfId2(String urlGet, String username, String password, UUID requestId,
280 AaiGetVnfResponse response = new AaiGetVnfResponse();
282 response.setVnfId("83f674e8-7555-44d7-9a39-bdc3770b0491");
283 response.setVnfName("lll_vnf_010317");
284 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
285 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
286 response.setOrchestrationStatus("Created");
287 response.setInMaint(false);
288 response.setIsClosedLoopDisabled(false);
289 response.setResourceVersion("1494001988835");
290 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
292 final RelationshipList relationshipList = new RelationshipList();
293 final Relationship relationship = new Relationship();
294 RelationshipData relationshipDataItem = new RelationshipData();
296 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
297 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
298 relationship.getRelationshipData().add(relationshipDataItem);
300 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
301 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
302 relationship.getRelationshipData().add(relationshipDataItem);
304 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
305 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
306 relationship.getRelationshipData().add(relationshipDataItem);
308 RelatedToProperty item = new RelatedToProperty();
309 item.setPropertyKey("service-instance.service-instance-name");
310 item.setPropertyValue("lll_svc_010317");
311 relationship.getRelatedToProperty().add(item);
313 relationship.setRelatedTo("service-instance");
314 relationship.setRelatedLink(
315 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
316 + "/MSO-dev-service-type/service-instances/service-instance/"
317 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
319 relationshipList.getRelationships().add(relationship);
320 response.setRelationshipList(relationshipList);
326 * Simulate a response.
328 public static AaiGetVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestId,
330 AaiGetVnfResponse response = new AaiGetVnfResponse();
332 response.setVnfId("83f674e8-7555-44d7-9a39-bdc3770b0491");
333 response.setVnfName("lll_vnf_010317");
334 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
335 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
336 response.setOrchestrationStatus("Created");
337 response.setInMaint(false);
338 response.setIsClosedLoopDisabled(false);
339 response.setResourceVersion("1494001988835");
340 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
342 final RelationshipList relationshipList = new RelationshipList();
343 final Relationship relationship = new Relationship();
344 RelationshipData relationshipDataItem = new RelationshipData();
346 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
347 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
348 relationship.getRelationshipData().add(relationshipDataItem);
350 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
351 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
352 relationship.getRelationshipData().add(relationshipDataItem);
354 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
355 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
356 relationship.getRelationshipData().add(relationshipDataItem);
358 RelatedToProperty item = new RelatedToProperty();
359 item.setPropertyKey("service-instance.service-instance-name");
360 item.setPropertyValue("lll_svc_010317");
361 relationship.getRelatedToProperty().add(item);
363 relationship.setRelatedTo("service-instance");
364 relationship.setRelatedLink(
365 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
366 + "/MSO-dev-service-type/service-instances/service-instance/"
367 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
369 relationshipList.getRelationships().add(relationship);
370 response.setRelationshipList(relationshipList);
376 * Simulate a response.
378 public static AaiGetVserverResponse getQueryByVserverName2(String urlGet, String username, String password,
379 UUID requestId, String key) {
380 final AaiGetVserverResponse response = new AaiGetVserverResponse();
382 AaiNqVServer svr = new AaiNqVServer();
384 svr.setVserverId("d0668d4f-c25e-4a1b-87c4-83845c01efd8");
385 svr.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0");
386 svr.setVserverName2("vjunos0");
387 svr.setVserverSelflink(
388 "https://aai-ext1.test.att.com:8443/aai/v7/cloud-infrastructure/cloud-regions/cloud-region/att-aic/AAIAIC25/tenants/tenant/USMSO1SX7NJ0103UJZZ01%3A%3AuCPE-VMS/vservers/vserver/d0668d4f-c25e-4a1b-87c4-83845c01efd8");
389 svr.setInMaint(false);
390 svr.setIsClosedLoopDisabled(false);
391 svr.setResourceVersion("1494001931513");
393 final RelationshipList relationshipList = new RelationshipList();
394 final Relationship relationship = new Relationship();
395 RelationshipData relationshipDataItem = new RelationshipData();
397 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
398 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
399 relationship.getRelationshipData().add(relationshipDataItem);
401 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
402 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
403 relationship.getRelationshipData().add(relationshipDataItem);
405 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
406 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
407 relationship.getRelationshipData().add(relationshipDataItem);
409 RelatedToProperty item = new RelatedToProperty();
410 item.setPropertyKey("service-instance.service-instance-name");
411 item.setPropertyValue("lll_svc_010317");
412 relationship.getRelatedToProperty().add(item);
414 relationship.setRelatedTo("service-instance");
415 relationship.setRelatedLink(
416 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
417 + "/MSO-dev-service-type/service-instances/service-instance/"
418 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
420 relationshipList.getRelationships().add(relationship);
421 svr.setRelationshipList(relationshipList);
423 response.getVserver().add(svr);
429 public void testMethods() {
430 UUID requestId = UUID.randomUUID();
431 ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
433 assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
434 assertEquals(requestId, clem.getRequestId());
436 clem.setActivated(true);
437 assertEquals(true, clem.isActivated());
439 clem.setControlLoopResult("SUCCESS");
440 assertEquals("SUCCESS", clem.getControlLoopResult());
442 clem.setControlLoopTimedOut();
443 assertEquals(true, clem.isControlLoopTimedOut());
445 clem.setNumAbatements(12345);
446 assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
448 clem.setNumOnsets(54321);
449 assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
451 assertNull(clem.getOnsetEvent());
452 assertNull(clem.getAbatementEvent());
453 assertNull(clem.getProcessor());
455 assertEquals(true, clem.isActive());
456 assertEquals(false, clem.releaseLock());
457 assertEquals(true, clem.isControlLoopTimedOut());
459 assertNull(clem.unlockCurrentOperation());
463 public void testAlreadyActivated() {
464 UUID requestId = UUID.randomUUID();
465 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
466 event.setClosedLoopControlName("TwoOnsetTest");
467 event.setRequestId(requestId);
468 event.setTarget("generic-vnf.vnf-id");
469 event.setClosedLoopAlarmStart(Instant.now());
470 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
471 event.setAai(new HashMap<>());
472 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
474 ControlLoopEventManager manager = makeManager(event);
475 manager.setActivated(true);
476 VirtualControlLoopNotification notification = manager.activate(event);
477 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
481 public void testActivationYaml() throws IOException {
482 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
483 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
485 InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
486 final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
488 UUID requestId = UUID.randomUUID();
489 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
490 event.setClosedLoopControlName("TwoOnsetTest");
491 event.setRequestId(requestId);
492 event.setTarget("generic-vnf.vnf-id");
493 event.setClosedLoopAlarmStart(Instant.now());
494 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
495 event.setAai(new HashMap<>());
496 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
498 ControlLoopEventManager manager = makeManager(event);
500 // Null YAML should fail
501 VirtualControlLoopNotification notificationNull = manager.activate(null, event);
502 assertNotNull(notificationNull);
503 assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
505 // Empty YAML should fail
506 VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
507 assertNotNull(notificationEmpty);
508 assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
510 // Bad YAML should fail
511 VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
512 assertNotNull(notificationBad);
513 assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
515 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
516 assertNotNull(notification);
517 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
519 // Another activate should fail
520 VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
521 assertNotNull(notificationActive);
522 assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
526 public void testControlLoopFinal() throws ControlLoopException, IOException {
527 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
528 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
530 UUID requestId = UUID.randomUUID();
531 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
532 event.setClosedLoopControlName("TwoOnsetTest");
533 event.setRequestId(requestId);
534 event.setTarget("generic-vnf.vnf-id");
535 event.setClosedLoopAlarmStart(Instant.now());
536 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
537 event.setAai(new HashMap<>());
538 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
540 ControlLoopEventManager manager = makeManager(event);
542 manager.isControlLoopFinal();
543 fail("test should throw an exception here");
544 } catch (ControlLoopException e) {
545 assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage());
548 manager.setActivated(true);
550 manager.isControlLoopFinal();
551 fail("test should throw an exception here");
552 } catch (ControlLoopException e) {
553 assertEquals("No onset event for ControlLoopEventManager.", e.getMessage());
556 manager.setActivated(false);
557 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
558 assertNotNull(notification);
559 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
561 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
562 assertNull(clfNotification);
564 // serialize and de-serialize manager
565 manager = Serializer.roundTrip(manager);
567 manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
568 clfNotification = manager.isControlLoopFinal();
569 assertNotNull(clfNotification);
570 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
572 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
573 notification = manager.activate(yamlString, event);
574 assertNotNull(notification);
575 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
577 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
578 clfNotification = manager.isControlLoopFinal();
579 assertNotNull(clfNotification);
580 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
582 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
583 notification = manager.activate(yamlString, event);
584 assertNotNull(notification);
585 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
587 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
588 clfNotification = manager.isControlLoopFinal();
589 assertNotNull(clfNotification);
590 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
592 manager.setControlLoopTimedOut();
593 clfNotification = manager.isControlLoopFinal();
594 assertNotNull(clfNotification);
595 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
599 public void testProcessControlLoop() throws ControlLoopException, IOException, AaiException {
600 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
601 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
603 UUID requestId = UUID.randomUUID();
604 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
605 event.setClosedLoopControlName("TwoOnsetTest");
606 event.setRequestId(requestId);
607 event.setTarget("generic-vnf.vnf-id");
608 event.setClosedLoopAlarmStart(Instant.now());
609 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
610 event.setAai(new HashMap<>());
611 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
613 ControlLoopEventManager manager = makeManager(event);
615 manager.processControlLoop();
616 fail("test should throw an exception here");
617 } catch (Exception e) {
618 assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage());
621 manager.setActivated(true);
623 manager.processControlLoop();
624 fail("test should throw an exception here");
625 } catch (Exception e) {
626 assertEquals("No onset event for ControlLoopEventManager.", e.getMessage());
629 manager.setActivated(false);
630 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
631 assertNotNull(notification);
632 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
634 ControlLoopOperationManager clom = manager.processControlLoop();
636 assertNull(clom.getOperationResult());
638 // serialize and de-serialize manager
639 manager = Serializer.roundTrip(manager);
641 // Test operation in progress
643 manager.processControlLoop();
644 fail("test should throw an exception here");
645 } catch (Exception e) {
646 assertEquals("Already working an Operation, do not call this method.", e.getMessage());
649 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
650 notification = manager.activate(yamlString, event);
651 assertNotNull(notification);
652 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
654 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
655 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
656 assertNotNull(clfNotification);
657 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
659 // Test operation completed
661 manager.processControlLoop();
662 fail("test should throw an exception here");
663 } catch (Exception e) {
664 assertEquals("Control Loop is in FINAL state, do not call this method.", e.getMessage());
667 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
668 notification = manager.activate(yamlString, event);
669 assertNotNull(notification);
670 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
671 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE);
673 // Test operation with no next policy defined
675 manager.processControlLoop();
676 fail("test should throw an exception here");
677 } catch (Exception e) {
678 assertEquals("The target type is null", e.getMessage());
683 public void testFinishOperation() throws IOException, ControlLoopException, AaiException {
684 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
685 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
687 InputStream isStd = new FileInputStream(new File("src/test/resources/test.yaml"));
688 final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
690 UUID requestId = UUID.randomUUID();
691 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
692 event.setClosedLoopControlName("TwoOnsetTest");
693 event.setRequestId(requestId);
694 event.setTarget("generic-vnf.vnf-id");
695 event.setClosedLoopAlarmStart(Instant.now());
696 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
697 event.setAai(new HashMap<>());
698 event.getAai().put("generic-vnf.vnf-id", "onsetOne");
700 ControlLoopEventManager manager = makeManager(event);
702 manager.finishOperation(null);
703 fail("test should throw an exception here");
704 } catch (Exception e) {
705 assertEquals("No operation to finish.", e.getMessage());
708 manager.setActivated(true);
710 manager.finishOperation(null);
711 fail("test should throw an exception here");
712 } catch (Exception e) {
713 assertEquals("No operation to finish.", e.getMessage());
716 manager.setActivated(false);
717 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
718 assertNotNull(notification);
719 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
722 manager.lockCurrentOperation();
723 fail("test should throw an exception here");
724 } catch (Exception e) {
725 assertEquals("Do not have a current operation.", e.getMessage());
728 assertNull(manager.unlockCurrentOperation());
730 // serialize and de-serialize manager
731 manager = Serializer.roundTrip(manager);
733 ControlLoopOperationManager clom = manager.processControlLoop();
735 assertNull(clom.getOperationResult());
737 LockResult<GuardResult, TargetLock> lockLock = manager.lockCurrentOperation();
738 assertNotNull(lockLock);
739 assertEquals(GuardResult.LOCK_ACQUIRED, lockLock.getA());
741 LockResult<GuardResult, TargetLock> lockLockAgain = manager.lockCurrentOperation();
742 assertNotNull(lockLockAgain);
743 assertEquals(GuardResult.LOCK_ACQUIRED, lockLockAgain.getA());
744 assertEquals(lockLock.getB(), lockLockAgain.getB());
746 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
747 assertNull(manager.unlockCurrentOperation());
749 lockLock = manager.lockCurrentOperation();
750 assertNotNull(lockLock);
751 PolicyGuard.unlockTarget(lockLock.getB());
752 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
754 clom.startOperation(event);
756 // This call should be exception free
757 manager.finishOperation(clom);
759 ControlLoopEventManager otherManager = makeManager(event);
760 VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
761 assertNotNull(otherNotification);
762 assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
764 ControlLoopOperationManager otherClom = otherManager.processControlLoop();
765 assertNotNull(otherClom);
766 assertNull(otherClom.getOperationResult());
768 otherManager.finishOperation(clom);
772 public void testOnNewEvent() throws IOException, AaiException {
773 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
774 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
776 UUID requestId = UUID.randomUUID();
777 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
778 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
779 onsetEvent.setRequestId(requestId);
780 onsetEvent.setTarget("generic-vnf.vnf-id");
781 onsetEvent.setClosedLoopAlarmStart(Instant.now());
782 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
783 onsetEvent.setAai(new HashMap<>());
784 onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
786 VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent();
787 abatedEvent.setClosedLoopControlName("TwoOnsetTest");
788 abatedEvent.setRequestId(requestId);
789 abatedEvent.setTarget("generic-vnf.vnf-id");
790 abatedEvent.setClosedLoopAlarmStart(Instant.now());
791 abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
792 abatedEvent.setAai(new HashMap<>());
793 abatedEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
795 ControlLoopEventManager manager = makeManager(onsetEvent);
796 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
797 assertNotNull(notification);
798 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
800 assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
801 assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
802 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
804 VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent();
805 checkSyntaxEvent.setAai(null);
806 checkSyntaxEvent.setClosedLoopAlarmEnd(null);
807 checkSyntaxEvent.setClosedLoopAlarmStart(null);
808 checkSyntaxEvent.setClosedLoopControlName(null);
809 checkSyntaxEvent.setClosedLoopEventClient(null);
810 checkSyntaxEvent.setClosedLoopEventStatus(null);
811 checkSyntaxEvent.setFrom(null);
812 checkSyntaxEvent.setPolicyName(null);
813 checkSyntaxEvent.setPolicyScope(null);
814 checkSyntaxEvent.setPolicyVersion(null);
815 checkSyntaxEvent.setRequestId(null);
816 checkSyntaxEvent.setTarget(null);
817 checkSyntaxEvent.setTargetType(null);
818 checkSyntaxEvent.setVersion(null);
820 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
822 checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
823 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
825 checkSyntaxEvent.setClosedLoopControlName(null);
826 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
828 checkSyntaxEvent.setClosedLoopControlName("");
829 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
831 checkSyntaxEvent.setClosedLoopControlName("TwoOnsetTest");
832 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
834 checkSyntaxEvent.setRequestId(null);
835 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
837 checkSyntaxEvent.setRequestId(requestId);
838 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
840 checkSyntaxEvent.setAai(null);
841 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
843 checkSyntaxEvent.setAai(new HashMap<>());
844 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
846 checkSyntaxEvent.setTarget("");
847 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
849 checkSyntaxEvent.setTarget(null);
850 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
852 checkSyntaxEvent.setTarget("");
853 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
855 checkSyntaxEvent.setTarget("OZ");
856 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
858 checkSyntaxEvent.setTarget("VM_NAME");
859 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
861 checkSyntaxEvent.setTarget("VNF_NAME");
862 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
864 checkSyntaxEvent.setTarget("vserver.vserver-name");
865 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
867 checkSyntaxEvent.setTarget("generic-vnf.vnf-id");
868 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
870 checkSyntaxEvent.setTarget("generic-vnf.vnf-name");
871 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
873 checkSyntaxEvent.setAai(null);
874 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
876 checkSyntaxEvent.setAai(new HashMap<>());
877 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
879 checkSyntaxEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
880 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
882 checkSyntaxEvent.getAai().put("vserver.vserver-name", "onsetOne");
883 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
885 checkSyntaxEvent.getAai().put("generic-vnf.vnf-id", "onsetOne");
886 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
890 public void testControlLoopTimeout() throws IOException {
891 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
892 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
894 UUID requestId = UUID.randomUUID();
895 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
896 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
897 onsetEvent.setRequestId(requestId);
898 onsetEvent.setTarget("generic-vnf.vnf-id");
899 onsetEvent.setClosedLoopAlarmStart(Instant.now());
900 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
901 onsetEvent.setAai(new HashMap<>());
902 onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
904 ControlLoopEventManager manager = makeManager(onsetEvent);
905 assertTrue(0 == manager.getControlLoopTimeout(null));
906 assertTrue(120 == manager.getControlLoopTimeout(120));
908 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
909 assertNotNull(notification);
910 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
912 assertTrue(60 == manager.getControlLoopTimeout(null));
916 public void testQueryAai_AlreadyDisabled() throws AaiException {
917 ControlLoopEventManager mgr = null;
920 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
921 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS,
922 ControlLoopEventManager.PROV_STATUS_ACTIVE);
924 mgr = makeManager(onset);
927 fail("missing exception");
929 } catch (AaiException expected) {
930 assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", expected.getMessage());
931 assertNull(mgr.getVnfResponse());
932 assertNull(mgr.getVserverResponse());
937 public void testQueryAai_AlreadyInactive() throws AaiException {
938 ControlLoopEventManager mgr = null;
941 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
942 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "not-active2");
944 mgr = makeManager(onset);
947 fail("missing exception");
949 } catch (AaiException expected) {
950 assertEquals("prov-status is not ACTIVE on VServer or VNF", expected.getMessage());
951 assertNull(mgr.getVnfResponse());
952 assertNull(mgr.getVserverResponse());
957 public void testQueryAai_QueryVnfById() throws AaiException {
958 ControlLoopEventManager mgr = null;
960 mgr = makeManager(onset);
963 assertNotNull(mgr.getVnfResponse());
964 assertNull(mgr.getVserverResponse());
966 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
968 // should not re-query
971 assertEquals(vnfresp, mgr.getVnfResponse());
972 assertNull(mgr.getVserverResponse());
976 public void testQueryAai_QueryVnfByName() throws AaiException {
977 ControlLoopEventManager mgr = null;
980 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
981 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName");
983 mgr = makeManager(onset);
986 assertNotNull(mgr.getVnfResponse());
987 assertNull(mgr.getVserverResponse());
989 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
991 // should not re-query
994 assertEquals(vnfresp, mgr.getVnfResponse());
995 assertNull(mgr.getVserverResponse());
999 public void testQueryAai_QueryVnfById_Disabled() throws AaiException {
1000 ControlLoopEventManager mgr = null;
1003 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop");
1005 mgr = makeManager(onset);
1006 mgr.queryAai(onset);
1008 fail("missing exception");
1010 } catch (AaiException expected) {
1011 assertEquals("is-closed-loop-disabled is set to true (query by vnf-id)", expected.getMessage());
1013 assertNotNull(mgr.getVnfResponse());
1014 assertNull(mgr.getVserverResponse());
1019 public void testQueryAai_QueryVserver() throws AaiException {
1020 ControlLoopEventManager mgr = null;
1022 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1023 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "AVserver");
1025 mgr = makeManager(onset);
1026 mgr.queryAai(onset);
1028 assertNull(mgr.getVnfResponse());
1029 assertNotNull(mgr.getVserverResponse());
1031 AaiGetVserverResponse vsvresp = mgr.getVserverResponse();
1033 // should not re-query
1034 mgr.queryAai(onset);
1036 assertNull(mgr.getVnfResponse());
1037 assertEquals(vsvresp, mgr.getVserverResponse());
1041 public void testQueryAai_QueryVserver_Disabled() throws AaiException {
1042 ControlLoopEventManager mgr = null;
1045 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1046 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop");
1048 mgr = makeManager(onset);
1049 mgr.queryAai(onset);
1051 fail("missing exception");
1053 } catch (AaiException expected) {
1054 assertEquals("is-closed-loop-disabled is set to true (query by vserver-name)", expected.getMessage());
1056 assertNull(mgr.getVnfResponse());
1057 assertNotNull(mgr.getVserverResponse());
1061 @Test(expected = AaiException.class)
1062 public void testQueryAai_QueryException() throws AaiException {
1064 PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL);
1066 makeManager(onset).queryAai(onset);
1070 public void testProcessVnfResponse_Success() throws Exception {
1071 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1072 resp.setIsClosedLoopDisabled(false);
1073 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1074 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1078 public void testProcessVnfResponse_NullResponse() throws Exception {
1079 thrown.expect(AaiException.class);
1080 thrown.expectMessage("AAI Response is null (query by vnf-id)");
1082 AaiGetVnfResponse resp = null;
1083 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1087 public void testProcessVnfResponse_Error() throws Exception {
1088 thrown.expect(AaiException.class);
1089 thrown.expectMessage("AAI Responded with a request error (query by vnf-name)");
1091 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1093 resp.setRequestError(new AaiNqRequestError());
1095 resp.setIsClosedLoopDisabled(false);
1096 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1097 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1101 public void testProcessVnfResponse_Disabled() throws Exception {
1102 thrown.expect(AaiException.class);
1103 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
1105 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1106 resp.setIsClosedLoopDisabled(true);
1107 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1108 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1112 public void testProcessVnfResponse_Inactive() throws Exception {
1113 thrown.expect(AaiException.class);
1114 thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)");
1116 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1117 resp.setIsClosedLoopDisabled(false);
1118 resp.setProvStatus("inactive1");
1119 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1123 public void testProcessVserverResponse_Success() throws Exception {
1124 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1126 AaiNqVServer svr = new AaiNqVServer();
1127 resp.getVserver().add(svr);
1129 svr.setIsClosedLoopDisabled(false);
1130 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1131 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1135 public void testProcessVserverResponse_NullResponse() throws Exception {
1136 thrown.expect(AaiException.class);
1137 thrown.expectMessage("AAI Response is null (query by vserver-name)");
1139 AaiGetVserverResponse resp = null;
1140 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1144 public void testProcessVserverResponse_Error() throws Exception {
1145 thrown.expect(AaiException.class);
1146 thrown.expectMessage("AAI Responded with a request error (query by vserver-name)");
1148 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1150 resp.setRequestError(new AaiNqRequestError());
1152 AaiNqVServer svr = new AaiNqVServer();
1153 resp.getVserver().add(svr);
1155 svr.setIsClosedLoopDisabled(false);
1156 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1158 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1162 public void testProcessVserverResponse_Disabled() throws Exception {
1163 thrown.expect(AaiException.class);
1164 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1166 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1167 AaiNqVServer svr = new AaiNqVServer();
1168 resp.getVserver().add(svr);
1170 svr.setIsClosedLoopDisabled(true);
1171 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1172 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1176 public void testProcessVserverResponse_Inactive() throws Exception {
1177 thrown.expect(AaiException.class);
1178 thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)");
1180 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1181 AaiNqVServer svr = new AaiNqVServer();
1182 resp.getVserver().add(svr);
1184 svr.setIsClosedLoopDisabled(false);
1185 svr.setProvStatus("inactive1");
1186 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1190 public void testIsClosedLoopDisabled() {
1191 Map<String, String> aai = onset.getAai();
1194 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1195 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1196 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1199 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1200 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1201 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1204 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1205 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1206 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1209 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1210 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1211 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1214 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1215 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1216 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1220 public void testIsProvStatusInactive() {
1221 Map<String, String> aai = onset.getAai();
1224 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1225 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1226 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1229 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1230 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1231 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1234 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1235 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1236 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1239 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1240 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
1241 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1244 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
1245 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1246 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1250 public void testIsAaiTrue() {
1251 assertTrue(ControlLoopEventManager.isAaiTrue("tRuE"));
1252 assertTrue(ControlLoopEventManager.isAaiTrue("T"));
1253 assertTrue(ControlLoopEventManager.isAaiTrue("t"));
1254 assertTrue(ControlLoopEventManager.isAaiTrue("yES"));
1255 assertTrue(ControlLoopEventManager.isAaiTrue("Y"));
1256 assertTrue(ControlLoopEventManager.isAaiTrue("y"));
1258 assertFalse(ControlLoopEventManager.isAaiTrue("no"));
1259 assertFalse(ControlLoopEventManager.isAaiTrue(null));
1263 public void testGetNqVserverFromAai() throws Exception {
1265 // empty vserver name
1266 ControlLoopEventManager manager = makeManager(onset);
1267 manager.activate(onset);
1268 assertNull(manager.getNqVserverFromAai());
1271 // re-create manager with a vserver name in the onset
1272 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "my-name");
1273 manager = makeManager(onset);
1274 manager.activate(onset);
1276 AaiNqResponseWrapper resp = manager.getNqVserverFromAai();
1277 assertNotNull(resp);
1278 assertEquals(onset.getRequestId(), resp.getRequestId());
1279 assertNotNull(resp.getAaiNqResponse());
1280 assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty());
1282 // re-query should return the same object
1283 assertTrue(resp == manager.getNqVserverFromAai());
1287 PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL);
1289 // re-create manager
1290 manager = makeManager(onset);
1291 manager.activate(onset);
1292 assertNull(manager.getNqVserverFromAai());
1296 public void testGetCqResponse() {
1298 ControlLoopEventManager mgr = null;
1299 mgr = makeManager(onset);
1300 mgr.queryAai(onset);
1301 AaiCqResponse aaiCqResponse = mgr.getCqResponse(onset);
1302 assertNotNull(aaiCqResponse);
1305 } catch (Exception e) {
1306 logger.error("testGetCqResponse Exception: ", e);
1307 fail(e.getMessage());
1312 private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
1313 return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());