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.AaiGetVnfResponse;
47 import org.onap.policy.aai.AaiGetVserverResponse;
48 import org.onap.policy.aai.AaiNqRequestError;
49 import org.onap.policy.aai.AaiNqResponseWrapper;
50 import org.onap.policy.aai.AaiNqVServer;
51 import org.onap.policy.aai.RelatedToProperty;
52 import org.onap.policy.aai.Relationship;
53 import org.onap.policy.aai.RelationshipData;
54 import org.onap.policy.aai.RelationshipList;
55 import org.onap.policy.aai.util.AaiException;
56 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
57 import org.onap.policy.common.utils.io.Serializer;
58 import org.onap.policy.controlloop.ControlLoopEventStatus;
59 import org.onap.policy.controlloop.ControlLoopException;
60 import org.onap.policy.controlloop.ControlLoopNotificationType;
61 import org.onap.policy.controlloop.SupportUtil;
62 import org.onap.policy.controlloop.VirtualControlLoopEvent;
63 import org.onap.policy.controlloop.VirtualControlLoopNotification;
64 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager.NewEventStatus;
65 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
66 import org.onap.policy.controlloop.policy.PolicyResult;
67 import org.onap.policy.drools.system.PolicyEngine;
68 import org.onap.policy.guard.GuardResult;
69 import org.onap.policy.guard.PolicyGuard;
70 import org.onap.policy.guard.PolicyGuard.LockResult;
71 import org.onap.policy.guard.TargetLock;
72 import org.powermock.reflect.Whitebox;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
76 public class ControlLoopEventManagerTest {
77 private static final String PROCESS_VNF_RESPONSE_METHOD_NAME = "processVnfResponse";
79 private static final String INVALID_URL = "http://localhost:9999";
81 private static final Logger logger = LoggerFactory.getLogger(ControlLoopEventManagerTest.class);
84 public ExpectedException thrown = ExpectedException.none();
86 private VirtualControlLoopEvent onset;
92 public static void setUpSimulator() {
94 org.onap.policy.simulators.Util.buildAaiSim();
95 } catch (Exception e) {
98 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
99 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
100 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
104 public static void tearDownSimulator() {
105 HttpServletServer.factory.destroy();
112 public void setUp() {
113 onset = new VirtualControlLoopEvent();
114 onset.setClosedLoopControlName("ControlLoop-vUSP");
115 onset.setRequestId(UUID.randomUUID());
116 onset.setTarget("VM_NAME");
117 onset.setClosedLoopAlarmStart(Instant.now());
118 onset.setAai(new HashMap<String, String>());
119 onset.getAai().put("cloud-region.identity-url", "foo");
120 onset.getAai().put("vserver.selflink", "bar");
121 onset.getAai().put("generic-vnf.vnf-id", "83f674e8-7555-44d7-9a39-bdc3770b0491");
122 onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
124 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
128 public void testAaiVnfInfo() {
129 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
130 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
132 AaiGetVnfResponse response = getQueryByVnfId2(
133 PolicyEngine.manager.getEnvironmentProperty("aai.url")
134 + "/aai/v11/network/generic-vnfs/generic-vnf/",
135 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
136 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(),
137 "5e49ca06-2972-4532-9ed4-6d071588d792");
138 assertNotNull(response);
139 logger.info("testAAIVnfInfo test result is " + (response == null ? "null" : "not null"));
140 } catch (Exception e) {
141 logger.error("testAAIVnfInfo Exception: ", e);
142 fail(e.getMessage());
147 public void testAaiVnfInfo2() {
148 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
149 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
151 AaiGetVnfResponse response = getQueryByVnfName2(
152 PolicyEngine.manager.getEnvironmentProperty("aai.url")
153 + "/aai/v11/network/generic-vnfs/generic-vnf?vnf-name=",
154 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
155 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(), "lll_vnf_010317");
156 assertNotNull(response);
157 logger.info("testAAIVnfInfo2 test result is " + (response == null ? "null" : "not null"));
158 } catch (Exception e) {
159 logger.error("testAAIVnfInfo2 Exception: ", e);
160 fail(e.getMessage());
165 public void testAaiVserver() {
166 final SupportUtil.Pair<ControlLoopPolicy, String> pair = SupportUtil.loadYaml("src/test/resources/test.yaml");
167 onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
169 AaiGetVserverResponse response = getQueryByVserverName2(
170 PolicyEngine.manager.getEnvironmentProperty("aai.url") + "/aai/v11/nodes/vservers?vserver-name=",
171 PolicyEngine.manager.getEnvironmentProperty("aai.username"),
172 PolicyEngine.manager.getEnvironmentProperty("aai.password"), UUID.randomUUID(),
173 "USMSO1SX7NJ0103UJZZ01-vjunos0");
174 assertNotNull(response);
175 logger.info("testAAIVserver test result is " + (response == null ? "null" : "not null"));
176 } catch (Exception e) {
177 logger.error("testAAIVserver Exception: ", e);
178 fail(e.getMessage());
183 public void abatementCheckEventSyntaxTest() {
184 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
185 event.setClosedLoopControlName("abatementAAI");
186 event.setRequestId(UUID.randomUUID());
187 event.setTarget("generic-vnf.vnf-id");
188 event.setClosedLoopAlarmStart(Instant.now());
189 event.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
190 ControlLoopEventManager manager = makeManager(event);
191 assertNull(manager.getVnfResponse());
192 assertNull(manager.getVserverResponse());
194 manager.checkEventSyntax(event);
195 } catch (ControlLoopException e) {
196 logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage());
198 fail("Exception in check event syntax");
200 assertNull(manager.getVnfResponse());
201 assertNull(manager.getVserverResponse());
204 event.setAai(new HashMap<>());
205 event.getAai().put("generic-vnf.vnf-name", "abatementTest");
207 manager.checkEventSyntax(event);
208 } catch (ControlLoopException e) {
209 logger.debug("ControlLoopException in abatemetCheckEventSyntaxTest: " + e.getMessage());
211 fail("Exception in check event syntax");
213 assertNull(manager.getVnfResponse());
214 assertNull(manager.getVserverResponse());
218 public void subsequentOnsetTest() throws IOException {
219 UUID requestId = UUID.randomUUID();
220 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
221 event.setClosedLoopControlName("TwoOnsetTest");
222 event.setRequestId(requestId);
223 event.setTarget("generic-vnf.vnf-id");
224 event.setClosedLoopAlarmStart(Instant.now());
225 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
226 event.setAai(new HashMap<>());
227 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
229 ControlLoopEventManager manager = makeManager(event);
230 VirtualControlLoopNotification notification = manager.activate(event);
232 assertNotNull(notification);
233 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
235 ControlLoopEventManager.NewEventStatus status = null;
237 status = manager.onNewEvent(event);
238 } catch (AaiException e) {
239 logger.warn(e.toString());
240 fail("A&AI Query Failed");
242 assertNotNull(status);
243 assertEquals(ControlLoopEventManager.NewEventStatus.FIRST_ONSET, status);
245 AaiGetVnfResponse response = manager.getVnfResponse();
246 assertNotNull(response);
247 assertNull(manager.getVserverResponse());
249 VirtualControlLoopEvent event2 = new VirtualControlLoopEvent();
250 event2.setClosedLoopControlName("TwoOnsetTest");
251 event2.setRequestId(requestId);
252 event2.setTarget("generic-vnf.vnf-id");
253 event2.setClosedLoopAlarmStart(Instant.now());
254 event2.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
255 event2.setAai(new HashMap<>());
256 event2.getAai().put("generic-vnf.vnf-name", "onsetTwo");
260 status = manager.onNewEvent(event2);
261 } catch (AaiException e) {
262 logger.warn(e.toString());
263 fail("A&AI Query Failed");
265 assertEquals(ControlLoopEventManager.NewEventStatus.SUBSEQUENT_ONSET, status);
266 AaiGetVnfResponse response2 = manager.getVnfResponse();
267 assertNotNull(response2);
268 // We should not have queried AAI, so the stored response should be the same
269 assertEquals(response, response2);
270 assertNull(manager.getVserverResponse());
274 * Simulate a response.
276 public static AaiGetVnfResponse getQueryByVnfId2(String urlGet, String username, String password, UUID requestId,
278 AaiGetVnfResponse response = new AaiGetVnfResponse();
280 response.setVnfId("83f674e8-7555-44d7-9a39-bdc3770b0491");
281 response.setVnfName("lll_vnf_010317");
282 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
283 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
284 response.setOrchestrationStatus("Created");
285 response.setInMaint(false);
286 response.setIsClosedLoopDisabled(false);
287 response.setResourceVersion("1494001988835");
288 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
290 final RelationshipList relationshipList = new RelationshipList();
291 final Relationship relationship = new Relationship();
292 RelationshipData relationshipDataItem = new RelationshipData();
294 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
295 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
296 relationship.getRelationshipData().add(relationshipDataItem);
298 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
299 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
300 relationship.getRelationshipData().add(relationshipDataItem);
302 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
303 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
304 relationship.getRelationshipData().add(relationshipDataItem);
306 RelatedToProperty item = new RelatedToProperty();
307 item.setPropertyKey("service-instance.service-instance-name");
308 item.setPropertyValue("lll_svc_010317");
309 relationship.getRelatedToProperty().add(item);
311 relationship.setRelatedTo("service-instance");
312 relationship.setRelatedLink(
313 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
314 + "/MSO-dev-service-type/service-instances/service-instance/"
315 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
317 relationshipList.getRelationships().add(relationship);
318 response.setRelationshipList(relationshipList);
324 * Simulate a response.
326 public static AaiGetVnfResponse getQueryByVnfName2(String urlGet, String username, String password, UUID requestId,
328 AaiGetVnfResponse response = new AaiGetVnfResponse();
330 response.setVnfId("83f674e8-7555-44d7-9a39-bdc3770b0491");
331 response.setVnfName("lll_vnf_010317");
332 response.setVnfType("Basa-122216-Service/VidVsamp12BaseVolume 1");
333 response.setServiceId("a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb");
334 response.setOrchestrationStatus("Created");
335 response.setInMaint(false);
336 response.setIsClosedLoopDisabled(false);
337 response.setResourceVersion("1494001988835");
338 response.setModelInvariantId("f18be3cd-d446-456e-9109-121d9b62feaa");
340 final RelationshipList relationshipList = new RelationshipList();
341 final Relationship relationship = new Relationship();
342 RelationshipData relationshipDataItem = new RelationshipData();
344 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
345 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
346 relationship.getRelationshipData().add(relationshipDataItem);
348 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
349 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
350 relationship.getRelationshipData().add(relationshipDataItem);
352 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
353 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
354 relationship.getRelationshipData().add(relationshipDataItem);
356 RelatedToProperty item = new RelatedToProperty();
357 item.setPropertyKey("service-instance.service-instance-name");
358 item.setPropertyValue("lll_svc_010317");
359 relationship.getRelatedToProperty().add(item);
361 relationship.setRelatedTo("service-instance");
362 relationship.setRelatedLink(
363 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
364 + "/MSO-dev-service-type/service-instances/service-instance/"
365 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
367 relationshipList.getRelationships().add(relationship);
368 response.setRelationshipList(relationshipList);
374 * Simulate a response.
376 public static AaiGetVserverResponse getQueryByVserverName2(String urlGet, String username, String password,
377 UUID requestId, String key) {
378 final AaiGetVserverResponse response = new AaiGetVserverResponse();
380 AaiNqVServer svr = new AaiNqVServer();
382 svr.setVserverId("d0668d4f-c25e-4a1b-87c4-83845c01efd8");
383 svr.setVserverName("USMSO1SX7NJ0103UJZZ01-vjunos0");
384 svr.setVserverName2("vjunos0");
385 svr.setVserverSelflink(
386 "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");
387 svr.setInMaint(false);
388 svr.setIsClosedLoopDisabled(false);
389 svr.setResourceVersion("1494001931513");
391 final RelationshipList relationshipList = new RelationshipList();
392 final Relationship relationship = new Relationship();
393 RelationshipData relationshipDataItem = new RelationshipData();
395 relationshipDataItem.setRelationshipKey("customer.global-customer-id");
396 relationshipDataItem.setRelationshipValue("MSO_1610_ST");
397 relationship.getRelationshipData().add(relationshipDataItem);
399 relationshipDataItem.setRelationshipKey("service-subscription.service-type");
400 relationshipDataItem.setRelationshipValue("MSO-dev-service-type");
401 relationship.getRelationshipData().add(relationshipDataItem);
403 relationshipDataItem.setRelationshipKey("service-instance.service-instance-id");
404 relationshipDataItem.setRelationshipValue("e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
405 relationship.getRelationshipData().add(relationshipDataItem);
407 RelatedToProperty item = new RelatedToProperty();
408 item.setPropertyKey("service-instance.service-instance-name");
409 item.setPropertyValue("lll_svc_010317");
410 relationship.getRelatedToProperty().add(item);
412 relationship.setRelatedTo("service-instance");
413 relationship.setRelatedLink(
414 "/aai/v11/business/customers/customer/MSO_1610_ST/service-subscriptions/service-subscription"
415 + "/MSO-dev-service-type/service-instances/service-instance/"
416 + "e1e9c97c-02c0-4919-9b4c-eb5d5ef68970");
418 relationshipList.getRelationships().add(relationship);
419 svr.setRelationshipList(relationshipList);
421 response.getVserver().add(svr);
427 public void testMethods() {
428 UUID requestId = UUID.randomUUID();
429 ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
431 assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
432 assertEquals(requestId, clem.getRequestId());
434 clem.setActivated(true);
435 assertEquals(true, clem.isActivated());
437 clem.setControlLoopResult("SUCCESS");
438 assertEquals("SUCCESS", clem.getControlLoopResult());
440 clem.setControlLoopTimedOut();
441 assertEquals(true, clem.isControlLoopTimedOut());
443 clem.setNumAbatements(12345);
444 assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
446 clem.setNumOnsets(54321);
447 assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
449 assertNull(clem.getOnsetEvent());
450 assertNull(clem.getAbatementEvent());
451 assertNull(clem.getProcessor());
453 assertEquals(true, clem.isActive());
454 assertEquals(false, clem.releaseLock());
455 assertEquals(true, clem.isControlLoopTimedOut());
457 assertNull(clem.unlockCurrentOperation());
461 public void testAlreadyActivated() {
462 UUID requestId = UUID.randomUUID();
463 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
464 event.setClosedLoopControlName("TwoOnsetTest");
465 event.setRequestId(requestId);
466 event.setTarget("generic-vnf.vnf-id");
467 event.setClosedLoopAlarmStart(Instant.now());
468 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
469 event.setAai(new HashMap<>());
470 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
472 ControlLoopEventManager manager = makeManager(event);
473 manager.setActivated(true);
474 VirtualControlLoopNotification notification = manager.activate(event);
475 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
479 public void testActivationYaml() throws IOException {
480 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
481 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
483 InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
484 final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
486 UUID requestId = UUID.randomUUID();
487 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
488 event.setClosedLoopControlName("TwoOnsetTest");
489 event.setRequestId(requestId);
490 event.setTarget("generic-vnf.vnf-id");
491 event.setClosedLoopAlarmStart(Instant.now());
492 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
493 event.setAai(new HashMap<>());
494 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
496 ControlLoopEventManager manager = makeManager(event);
498 // Null YAML should fail
499 VirtualControlLoopNotification notificationNull = manager.activate(null, event);
500 assertNotNull(notificationNull);
501 assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
503 // Empty YAML should fail
504 VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
505 assertNotNull(notificationEmpty);
506 assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
508 // Bad YAML should fail
509 VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
510 assertNotNull(notificationBad);
511 assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
513 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
514 assertNotNull(notification);
515 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
517 // Another activate should fail
518 VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
519 assertNotNull(notificationActive);
520 assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
524 public void testControlLoopFinal() throws ControlLoopException, IOException {
525 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
526 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
528 UUID requestId = UUID.randomUUID();
529 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
530 event.setClosedLoopControlName("TwoOnsetTest");
531 event.setRequestId(requestId);
532 event.setTarget("generic-vnf.vnf-id");
533 event.setClosedLoopAlarmStart(Instant.now());
534 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
535 event.setAai(new HashMap<>());
536 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
538 ControlLoopEventManager manager = makeManager(event);
540 manager.isControlLoopFinal();
541 fail("test should throw an exception here");
542 } catch (ControlLoopException e) {
543 assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage());
546 manager.setActivated(true);
548 manager.isControlLoopFinal();
549 fail("test should throw an exception here");
550 } catch (ControlLoopException e) {
551 assertEquals("No onset event for ControlLoopEventManager.", e.getMessage());
554 manager.setActivated(false);
555 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
556 assertNotNull(notification);
557 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
559 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
560 assertNull(clfNotification);
562 // serialize and de-serialize manager
563 manager = Serializer.roundTrip(manager);
565 manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
566 clfNotification = manager.isControlLoopFinal();
567 assertNotNull(clfNotification);
568 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
570 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
571 notification = manager.activate(yamlString, event);
572 assertNotNull(notification);
573 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
575 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
576 clfNotification = manager.isControlLoopFinal();
577 assertNotNull(clfNotification);
578 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
580 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
581 notification = manager.activate(yamlString, event);
582 assertNotNull(notification);
583 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
585 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
586 clfNotification = manager.isControlLoopFinal();
587 assertNotNull(clfNotification);
588 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
590 manager.setControlLoopTimedOut();
591 clfNotification = manager.isControlLoopFinal();
592 assertNotNull(clfNotification);
593 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
597 public void testProcessControlLoop() throws ControlLoopException, IOException, AaiException {
598 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
599 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
601 UUID requestId = UUID.randomUUID();
602 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
603 event.setClosedLoopControlName("TwoOnsetTest");
604 event.setRequestId(requestId);
605 event.setTarget("generic-vnf.vnf-id");
606 event.setClosedLoopAlarmStart(Instant.now());
607 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
608 event.setAai(new HashMap<>());
609 event.getAai().put("generic-vnf.vnf-name", "onsetOne");
611 ControlLoopEventManager manager = makeManager(event);
613 manager.processControlLoop();
614 fail("test should throw an exception here");
615 } catch (Exception e) {
616 assertEquals("ControlLoopEventManager MUST be activated first.", e.getMessage());
619 manager.setActivated(true);
621 manager.processControlLoop();
622 fail("test should throw an exception here");
623 } catch (Exception e) {
624 assertEquals("No onset event for ControlLoopEventManager.", e.getMessage());
627 manager.setActivated(false);
628 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
629 assertNotNull(notification);
630 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
632 ControlLoopOperationManager clom = manager.processControlLoop();
634 assertNull(clom.getOperationResult());
636 // serialize and de-serialize manager
637 manager = Serializer.roundTrip(manager);
639 // Test operation in progress
641 manager.processControlLoop();
642 fail("test should throw an exception here");
643 } catch (Exception e) {
644 assertEquals("Already working an Operation, do not call this method.", e.getMessage());
647 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
648 notification = manager.activate(yamlString, event);
649 assertNotNull(notification);
650 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
652 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
653 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
654 assertNotNull(clfNotification);
655 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
657 // Test operation completed
659 manager.processControlLoop();
660 fail("test should throw an exception here");
661 } catch (Exception e) {
662 assertEquals("Control Loop is in FINAL state, do not call this method.", e.getMessage());
665 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
666 notification = manager.activate(yamlString, event);
667 assertNotNull(notification);
668 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
669 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE);
671 // Test operation with no next policy defined
673 manager.processControlLoop();
674 fail("test should throw an exception here");
675 } catch (Exception e) {
676 assertEquals("The target type is null", e.getMessage());
681 public void testFinishOperation() throws IOException, ControlLoopException, AaiException {
682 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
683 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
685 InputStream isStd = new FileInputStream(new File("src/test/resources/test.yaml"));
686 final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
688 UUID requestId = UUID.randomUUID();
689 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
690 event.setClosedLoopControlName("TwoOnsetTest");
691 event.setRequestId(requestId);
692 event.setTarget("generic-vnf.vnf-id");
693 event.setClosedLoopAlarmStart(Instant.now());
694 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
695 event.setAai(new HashMap<>());
696 event.getAai().put("generic-vnf.vnf-id", "onsetOne");
698 ControlLoopEventManager manager = makeManager(event);
700 manager.finishOperation(null);
701 fail("test should throw an exception here");
702 } catch (Exception e) {
703 assertEquals("No operation to finish.", e.getMessage());
706 manager.setActivated(true);
708 manager.finishOperation(null);
709 fail("test should throw an exception here");
710 } catch (Exception e) {
711 assertEquals("No operation to finish.", e.getMessage());
714 manager.setActivated(false);
715 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
716 assertNotNull(notification);
717 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
720 manager.lockCurrentOperation();
721 fail("test should throw an exception here");
722 } catch (Exception e) {
723 assertEquals("Do not have a current operation.", e.getMessage());
726 assertNull(manager.unlockCurrentOperation());
728 // serialize and de-serialize manager
729 manager = Serializer.roundTrip(manager);
731 ControlLoopOperationManager clom = manager.processControlLoop();
733 assertNull(clom.getOperationResult());
735 LockResult<GuardResult, TargetLock> lockLock = manager.lockCurrentOperation();
736 assertNotNull(lockLock);
737 assertEquals(GuardResult.LOCK_ACQUIRED, lockLock.getA());
739 LockResult<GuardResult, TargetLock> lockLockAgain = manager.lockCurrentOperation();
740 assertNotNull(lockLockAgain);
741 assertEquals(GuardResult.LOCK_ACQUIRED, lockLockAgain.getA());
742 assertEquals(lockLock.getB(), lockLockAgain.getB());
744 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
745 assertNull(manager.unlockCurrentOperation());
747 lockLock = manager.lockCurrentOperation();
748 assertNotNull(lockLock);
749 PolicyGuard.unlockTarget(lockLock.getB());
750 assertEquals(lockLock.getB(), manager.unlockCurrentOperation());
752 clom.startOperation(event);
754 // This call should be exception free
755 manager.finishOperation(clom);
757 ControlLoopEventManager otherManager = makeManager(event);
758 VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
759 assertNotNull(otherNotification);
760 assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
762 ControlLoopOperationManager otherClom = otherManager.processControlLoop();
763 assertNotNull(otherClom);
764 assertNull(otherClom.getOperationResult());
766 otherManager.finishOperation(clom);
770 public void testOnNewEvent() throws IOException, AaiException {
771 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
772 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
774 UUID requestId = UUID.randomUUID();
775 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
776 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
777 onsetEvent.setRequestId(requestId);
778 onsetEvent.setTarget("generic-vnf.vnf-id");
779 onsetEvent.setClosedLoopAlarmStart(Instant.now());
780 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
781 onsetEvent.setAai(new HashMap<>());
782 onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
784 VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent();
785 abatedEvent.setClosedLoopControlName("TwoOnsetTest");
786 abatedEvent.setRequestId(requestId);
787 abatedEvent.setTarget("generic-vnf.vnf-id");
788 abatedEvent.setClosedLoopAlarmStart(Instant.now());
789 abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
790 abatedEvent.setAai(new HashMap<>());
791 abatedEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
793 ControlLoopEventManager manager = makeManager(onsetEvent);
794 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
795 assertNotNull(notification);
796 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
798 assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
799 assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
800 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
802 VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent();
803 checkSyntaxEvent.setAai(null);
804 checkSyntaxEvent.setClosedLoopAlarmEnd(null);
805 checkSyntaxEvent.setClosedLoopAlarmStart(null);
806 checkSyntaxEvent.setClosedLoopControlName(null);
807 checkSyntaxEvent.setClosedLoopEventClient(null);
808 checkSyntaxEvent.setClosedLoopEventStatus(null);
809 checkSyntaxEvent.setFrom(null);
810 checkSyntaxEvent.setPolicyName(null);
811 checkSyntaxEvent.setPolicyScope(null);
812 checkSyntaxEvent.setPolicyVersion(null);
813 checkSyntaxEvent.setRequestId(null);
814 checkSyntaxEvent.setTarget(null);
815 checkSyntaxEvent.setTargetType(null);
816 checkSyntaxEvent.setVersion(null);
818 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
820 checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
821 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
823 checkSyntaxEvent.setClosedLoopControlName(null);
824 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
826 checkSyntaxEvent.setClosedLoopControlName("");
827 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
829 checkSyntaxEvent.setClosedLoopControlName("TwoOnsetTest");
830 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
832 checkSyntaxEvent.setRequestId(null);
833 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
835 checkSyntaxEvent.setRequestId(requestId);
836 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
838 checkSyntaxEvent.setAai(null);
839 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
841 checkSyntaxEvent.setAai(new HashMap<>());
842 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
844 checkSyntaxEvent.setTarget("");
845 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
847 checkSyntaxEvent.setTarget(null);
848 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
850 checkSyntaxEvent.setTarget("");
851 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
853 checkSyntaxEvent.setTarget("OZ");
854 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
856 checkSyntaxEvent.setTarget("VM_NAME");
857 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
859 checkSyntaxEvent.setTarget("VNF_NAME");
860 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
862 checkSyntaxEvent.setTarget("vserver.vserver-name");
863 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
865 checkSyntaxEvent.setTarget("generic-vnf.vnf-id");
866 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
868 checkSyntaxEvent.setTarget("generic-vnf.vnf-name");
869 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
871 checkSyntaxEvent.setAai(null);
872 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
874 checkSyntaxEvent.setAai(new HashMap<>());
875 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
877 checkSyntaxEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
878 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
880 checkSyntaxEvent.getAai().put("vserver.vserver-name", "onsetOne");
881 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
883 checkSyntaxEvent.getAai().put("generic-vnf.vnf-id", "onsetOne");
884 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
888 public void testControlLoopTimeout() throws IOException {
889 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
890 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
892 UUID requestId = UUID.randomUUID();
893 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
894 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
895 onsetEvent.setRequestId(requestId);
896 onsetEvent.setTarget("generic-vnf.vnf-id");
897 onsetEvent.setClosedLoopAlarmStart(Instant.now());
898 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
899 onsetEvent.setAai(new HashMap<>());
900 onsetEvent.getAai().put("generic-vnf.vnf-name", "onsetOne");
902 ControlLoopEventManager manager = makeManager(onsetEvent);
903 assertTrue(0 == manager.getControlLoopTimeout(null));
904 assertTrue(120 == manager.getControlLoopTimeout(120));
906 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
907 assertNotNull(notification);
908 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
910 assertTrue(60 == manager.getControlLoopTimeout(null));
914 public void testQueryAai_AlreadyDisabled() throws AaiException {
915 ControlLoopEventManager mgr = null;
918 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
919 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS,
920 ControlLoopEventManager.PROV_STATUS_ACTIVE);
922 mgr = makeManager(onset);
925 fail("missing exception");
927 } catch (AaiException expected) {
928 assertEquals("is-closed-loop-disabled is set to true on VServer or VNF", expected.getMessage());
929 assertNull(mgr.getVnfResponse());
930 assertNull(mgr.getVserverResponse());
935 public void testQueryAai_AlreadyInactive() throws AaiException {
936 ControlLoopEventManager mgr = null;
939 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
940 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "not-active2");
942 mgr = makeManager(onset);
945 fail("missing exception");
947 } catch (AaiException expected) {
948 assertEquals("prov-status is not ACTIVE on VServer or VNF", expected.getMessage());
949 assertNull(mgr.getVnfResponse());
950 assertNull(mgr.getVserverResponse());
955 public void testQueryAai_QueryVnfById() throws AaiException {
956 ControlLoopEventManager mgr = null;
958 mgr = makeManager(onset);
961 assertNotNull(mgr.getVnfResponse());
962 assertNull(mgr.getVserverResponse());
964 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
966 // should not re-query
969 assertEquals(vnfresp, mgr.getVnfResponse());
970 assertNull(mgr.getVserverResponse());
974 public void testQueryAai_QueryVnfByName() throws AaiException {
975 ControlLoopEventManager mgr = null;
978 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
979 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_NAME, "AVNFName");
981 mgr = makeManager(onset);
984 assertNotNull(mgr.getVnfResponse());
985 assertNull(mgr.getVserverResponse());
987 AaiGetVnfResponse vnfresp = mgr.getVnfResponse();
989 // should not re-query
992 assertEquals(vnfresp, mgr.getVnfResponse());
993 assertNull(mgr.getVserverResponse());
997 public void testQueryAai_QueryVnfById_Disabled() throws AaiException {
998 ControlLoopEventManager mgr = null;
1001 onset.getAai().put(ControlLoopEventManager.GENERIC_VNF_VNF_ID, "disableClosedLoop");
1003 mgr = makeManager(onset);
1004 mgr.queryAai(onset);
1006 fail("missing exception");
1008 } catch (AaiException expected) {
1009 assertEquals("is-closed-loop-disabled is set to true (query by vnf-id)", expected.getMessage());
1011 assertNotNull(mgr.getVnfResponse());
1012 assertNull(mgr.getVserverResponse());
1017 public void testQueryAai_QueryVserver() throws AaiException {
1018 ControlLoopEventManager mgr = null;
1020 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1021 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "AVserver");
1023 mgr = makeManager(onset);
1024 mgr.queryAai(onset);
1026 assertNull(mgr.getVnfResponse());
1027 assertNotNull(mgr.getVserverResponse());
1029 AaiGetVserverResponse vsvresp = mgr.getVserverResponse();
1031 // should not re-query
1032 mgr.queryAai(onset);
1034 assertNull(mgr.getVnfResponse());
1035 assertEquals(vsvresp, mgr.getVserverResponse());
1039 public void testQueryAai_QueryVserver_Disabled() throws AaiException {
1040 ControlLoopEventManager mgr = null;
1043 onset.getAai().remove(ControlLoopEventManager.GENERIC_VNF_VNF_ID);
1044 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "disableClosedLoop");
1046 mgr = makeManager(onset);
1047 mgr.queryAai(onset);
1049 fail("missing exception");
1051 } catch (AaiException expected) {
1052 assertEquals("is-closed-loop-disabled is set to true (query by vserver-name)", expected.getMessage());
1054 assertNull(mgr.getVnfResponse());
1055 assertNotNull(mgr.getVserverResponse());
1059 @Test(expected = AaiException.class)
1060 public void testQueryAai_QueryException() throws AaiException {
1062 PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL);
1064 makeManager(onset).queryAai(onset);
1068 public void testProcessVnfResponse_Success() throws Exception {
1069 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1070 resp.setIsClosedLoopDisabled(false);
1071 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1072 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1076 public void testProcessVnfResponse_NullResponse() throws Exception {
1077 thrown.expect(AaiException.class);
1078 thrown.expectMessage("AAI Response is null (query by vnf-id)");
1080 AaiGetVnfResponse resp = null;
1081 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1085 public void testProcessVnfResponse_Error() throws Exception {
1086 thrown.expect(AaiException.class);
1087 thrown.expectMessage("AAI Responded with a request error (query by vnf-name)");
1089 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1091 resp.setRequestError(new AaiNqRequestError());
1093 resp.setIsClosedLoopDisabled(false);
1094 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1095 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1099 public void testProcessVnfResponse_Disabled() throws Exception {
1100 thrown.expect(AaiException.class);
1101 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vnf-id)");
1103 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1104 resp.setIsClosedLoopDisabled(true);
1105 resp.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1106 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, true);
1110 public void testProcessVnfResponse_Inactive() throws Exception {
1111 thrown.expect(AaiException.class);
1112 thrown.expectMessage("prov-status is not ACTIVE (query by vnf-name)");
1114 AaiGetVnfResponse resp = new AaiGetVnfResponse();
1115 resp.setIsClosedLoopDisabled(false);
1116 resp.setProvStatus("inactive1");
1117 Whitebox.invokeMethod(ControlLoopEventManager.class, PROCESS_VNF_RESPONSE_METHOD_NAME, resp, false);
1121 public void testProcessVserverResponse_Success() throws Exception {
1122 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1124 AaiNqVServer svr = new AaiNqVServer();
1125 resp.getVserver().add(svr);
1127 svr.setIsClosedLoopDisabled(false);
1128 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1129 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1133 public void testProcessVserverResponse_NullResponse() throws Exception {
1134 thrown.expect(AaiException.class);
1135 thrown.expectMessage("AAI Response is null (query by vserver-name)");
1137 AaiGetVserverResponse resp = null;
1138 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1142 public void testProcessVserverResponse_Error() throws Exception {
1143 thrown.expect(AaiException.class);
1144 thrown.expectMessage("AAI Responded with a request error (query by vserver-name)");
1146 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1148 resp.setRequestError(new AaiNqRequestError());
1150 AaiNqVServer svr = new AaiNqVServer();
1151 resp.getVserver().add(svr);
1153 svr.setIsClosedLoopDisabled(false);
1154 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1156 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1160 public void testProcessVserverResponse_Disabled() throws Exception {
1161 thrown.expect(AaiException.class);
1162 thrown.expectMessage("is-closed-loop-disabled is set to true (query by vserver-name)");
1164 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1165 AaiNqVServer svr = new AaiNqVServer();
1166 resp.getVserver().add(svr);
1168 svr.setIsClosedLoopDisabled(true);
1169 svr.setProvStatus(ControlLoopEventManager.PROV_STATUS_ACTIVE);
1170 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1174 public void testProcessVserverResponse_Inactive() throws Exception {
1175 thrown.expect(AaiException.class);
1176 thrown.expectMessage("prov-status is not ACTIVE (query by vserver-name)");
1178 AaiGetVserverResponse resp = new AaiGetVserverResponse();
1179 AaiNqVServer svr = new AaiNqVServer();
1180 resp.getVserver().add(svr);
1182 svr.setIsClosedLoopDisabled(false);
1183 svr.setProvStatus("inactive1");
1184 Whitebox.invokeMethod(ControlLoopEventManager.class, "processVServerResponse", resp);
1188 public void testIsClosedLoopDisabled() {
1189 Map<String, String> aai = onset.getAai();
1192 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1193 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1194 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1197 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1198 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1199 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1202 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
1203 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1204 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
1207 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
1208 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1209 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1212 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
1213 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
1214 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
1218 public void testIsProvStatusInactive() {
1219 Map<String, String> aai = onset.getAai();
1222 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1223 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1224 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1227 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1228 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1229 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1232 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
1233 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1234 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
1237 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
1238 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
1239 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1242 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
1243 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
1244 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
1248 public void testIsAaiTrue() {
1249 assertTrue(ControlLoopEventManager.isAaiTrue("tRuE"));
1250 assertTrue(ControlLoopEventManager.isAaiTrue("T"));
1251 assertTrue(ControlLoopEventManager.isAaiTrue("t"));
1252 assertTrue(ControlLoopEventManager.isAaiTrue("yES"));
1253 assertTrue(ControlLoopEventManager.isAaiTrue("Y"));
1254 assertTrue(ControlLoopEventManager.isAaiTrue("y"));
1256 assertFalse(ControlLoopEventManager.isAaiTrue("no"));
1257 assertFalse(ControlLoopEventManager.isAaiTrue(null));
1261 public void testGetNqVserverFromAai() throws Exception {
1263 // empty vserver name
1264 ControlLoopEventManager manager = makeManager(onset);
1265 manager.activate(onset);
1266 assertNull(manager.getNqVserverFromAai());
1269 // re-create manager with a vserver name in the onset
1270 onset.getAai().put(ControlLoopEventManager.VSERVER_VSERVER_NAME, "my-name");
1271 manager = makeManager(onset);
1272 manager.activate(onset);
1274 AaiNqResponseWrapper resp = manager.getNqVserverFromAai();
1275 assertNotNull(resp);
1276 assertEquals(onset.getRequestId(), resp.getRequestId());
1277 assertNotNull(resp.getAaiNqResponse());
1278 assertFalse(resp.getAaiNqResponse().getInventoryResponseItems().isEmpty());
1280 // re-query should return the same object
1281 assertTrue(resp == manager.getNqVserverFromAai());
1285 PolicyEngine.manager.setEnvironmentProperty("aai.url", INVALID_URL);
1287 // re-create manager
1288 manager = makeManager(onset);
1289 manager.activate(onset);
1290 assertNull(manager.getNqVserverFromAai());
1293 private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
1294 return new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());