2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.never;
33 import static org.mockito.Mockito.verify;
34 import static org.mockito.Mockito.when;
37 import java.io.FileInputStream;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.Serializable;
41 import java.nio.charset.StandardCharsets;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
44 import java.time.Instant;
45 import java.util.HashMap;
47 import java.util.UUID;
48 import org.apache.commons.io.IOUtils;
49 import org.jetbrains.annotations.NotNull;
50 import org.junit.AfterClass;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
55 import org.onap.policy.common.utils.coder.CoderException;
56 import org.onap.policy.common.utils.coder.StandardCoder;
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.ControlLoopTargetType;
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.PolicyResult;
66 import org.onap.policy.drools.core.lock.Lock;
67 import org.onap.policy.drools.core.lock.LockCallback;
68 import org.onap.policy.drools.system.PolicyEngineConstants;
69 import org.onap.policy.drools.utils.Pair;
70 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
71 import org.powermock.reflect.Whitebox;
73 public class ControlLoopEventManagerTest {
74 private static final String TARGET_LOCK_FIELD = "targetLock";
75 private static final String ONSET_ONE = "onsetOne";
76 private static final String VSERVER_NAME = "vserver.vserver-name";
77 private static final String TEST_YAML = "src/test/resources/test.yaml";
78 private static final String VNF_NAME = "generic-vnf.vnf-name";
79 private static final String VNF_ID = "generic-vnf.vnf-id";
80 private static final String AAI_USERNAME = "aai.username";
81 private static final String AAI_URL = "aai.url";
82 private static final String AAI_PASS = "aai.password";
83 private static final String TWO_ONSET_TEST = "TwoOnsetTest";
84 private static final String VNF_UUID = "83f674e8-7555-44d7-9a39-bdc3770b0491";
86 private VirtualControlLoopEvent onset;
87 private LockCallback callback;
93 public static void setUpSimulator() throws Exception {
94 org.onap.policy.simulators.Util.buildAaiSim();
96 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_USERNAME, "AAI");
97 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_PASS, "AAI");
98 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, "http://localhost:6666");
99 PolicyEngineConstants.getManager().setEnvironmentProperty("aai.customQuery", "false");
103 public static void tearDownSimulator() {
104 HttpServletServerFactoryInstance.getServerFactory().destroy();
111 public void setUp() {
112 callback = mock(LockCallback.class);
114 onset = new VirtualControlLoopEvent();
115 onset.setClosedLoopControlName("ControlLoop-vUSP");
116 onset.setRequestId(UUID.randomUUID());
117 onset.setTarget("VM_NAME");
118 onset.setClosedLoopAlarmStart(Instant.now());
119 onset.setAai(new HashMap<>());
120 onset.getAai().put("cloud-region.identity-url", "foo");
121 onset.getAai().put("vserver.selflink", "bar");
122 onset.getAai().put(VNF_ID, VNF_UUID);
123 onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
124 onset.setTargetType(ControlLoopTargetType.VNF);
126 PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, "http://localhost:6666");
130 public void testMethods() {
131 UUID requestId = UUID.randomUUID();
132 ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
134 assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
135 assertEquals(requestId, clem.getRequestId());
137 clem.setActivated(true);
138 assertEquals(true, clem.isActivated());
140 clem.setControlLoopResult("SUCCESS");
141 assertEquals("SUCCESS", clem.getControlLoopResult());
143 clem.setControlLoopTimedOut();
144 assertEquals(true, clem.isControlLoopTimedOut());
146 clem.setNumAbatements(12345);
147 assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
149 clem.setNumOnsets(54321);
150 assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
152 assertNull(clem.getOnsetEvent());
153 assertNull(clem.getAbatementEvent());
154 assertNull(clem.getProcessor());
156 assertEquals(true, clem.isControlLoopTimedOut());
158 assertNull(clem.unlockCurrentOperation());
162 public void testAlreadyActivated() {
163 VirtualControlLoopEvent event = getOnsetEvent();
165 ControlLoopEventManager manager = makeManager(event);
166 manager.setActivated(true);
167 VirtualControlLoopNotification notification = manager.activate(event);
168 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
172 public void testActivationYaml() throws IOException, CoderException {
174 VirtualControlLoopEvent event = getOnsetEvent();
175 ControlLoopEventManager manager = makeManager(event);
177 // Null YAML should fail
178 VirtualControlLoopNotification notificationNull = manager.activate((String) null, event);
179 assertNotNull(notificationNull);
180 assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
182 // Empty YAML should fail
183 VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
184 assertNotNull(notificationEmpty);
185 assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
187 // Bad YAML should fail
188 InputStream isBad = new FileInputStream(new File("src/test/resources/notutf8.yaml"));
189 final String yamlStringBad = IOUtils.toString(isBad, StandardCharsets.UTF_8);
191 VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
192 assertNotNull(notificationBad);
193 assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
196 InputStream is = new FileInputStream(new File(TEST_YAML));
197 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
199 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
200 assertNotNull(notification);
201 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
203 // Another activate should fail
204 VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
205 assertNotNull(notificationActive);
206 assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
210 public void testActivateToscaLegacy() throws IOException, CoderException {
212 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
213 ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
215 VirtualControlLoopEvent event = getOnsetEvent();
216 ControlLoopEventManager manager = makeManager(event);
218 // trigger a reject by passing the wrong policy type
219 toscaPolicy.setType("onap.policies.controlloop.operational.common.Drools");
220 VirtualControlLoopNotification notification = manager.activate(toscaPolicy, event);
221 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
223 // place back correct policy type
224 toscaPolicy.setType("onap.policies.controlloop.Operational");
225 notification = manager.activate(toscaPolicy, event);
226 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
228 // another activate should fail
229 notification = manager.activate(toscaPolicy, event);
230 assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
234 public void testControlLoopFinal() throws Exception {
235 VirtualControlLoopEvent event = getOnsetEvent();
237 ControlLoopEventManager manager = makeManager(event);
238 ControlLoopEventManager manager2 = manager;
239 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
240 .hasMessage("ControlLoopEventManager MUST be activated first.");
242 manager.setActivated(true);
243 assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
244 .hasMessage("No onset event for ControlLoopEventManager.");
246 manager.setActivated(false);
248 InputStream is = new FileInputStream(new File(TEST_YAML));
249 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
251 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
252 assertNotNull(notification);
253 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
255 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
256 assertNull(clfNotification);
258 // serialize and de-serialize manager
259 manager = Serializer.roundTrip(manager);
261 manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
262 clfNotification = manager.isControlLoopFinal();
263 assertNotNull(clfNotification);
264 assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
266 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
267 notification = manager.activate(yamlString, event);
268 assertNotNull(notification);
269 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
271 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
272 clfNotification = manager.isControlLoopFinal();
273 assertNotNull(clfNotification);
274 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
276 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
277 notification = manager.activate(yamlString, event);
278 assertNotNull(notification);
279 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
281 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
282 clfNotification = manager.isControlLoopFinal();
283 assertNotNull(clfNotification);
284 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
286 manager.setControlLoopTimedOut();
287 clfNotification = manager.isControlLoopFinal();
288 assertNotNull(clfNotification);
289 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
293 private VirtualControlLoopEvent getOnsetEvent() {
294 UUID requestId = UUID.randomUUID();
295 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
296 event.setClosedLoopControlName(TWO_ONSET_TEST);
297 event.setRequestId(requestId);
298 event.setTarget(VNF_ID);
299 event.setClosedLoopAlarmStart(Instant.now());
300 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
301 event.setAai(new HashMap<>());
302 event.getAai().put(VNF_NAME, ONSET_ONE);
303 event.setTargetType(ControlLoopTargetType.VNF);
308 public void testProcessControlLoop() throws Exception {
309 UUID requestId = UUID.randomUUID();
310 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
311 event.setClosedLoopControlName(TWO_ONSET_TEST);
312 event.setRequestId(requestId);
313 event.setTarget(VNF_ID);
314 event.setClosedLoopAlarmStart(Instant.now());
315 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
316 event.setAai(new HashMap<>());
317 event.getAai().put(VNF_NAME, ONSET_ONE);
318 event.getAai().put(VSERVER_NAME, "testVserverName");
319 event.setTargetType(ControlLoopTargetType.VNF);
321 ControlLoopEventManager manager = makeManager(event);
322 ControlLoopEventManager manager2 = manager;
323 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
324 .hasMessage("ControlLoopEventManager MUST be activated first.");
326 manager.setActivated(true);
327 assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
328 .hasMessage("No onset event for ControlLoopEventManager.");
330 manager.setActivated(false);
332 InputStream is = new FileInputStream(new File(TEST_YAML));
333 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
335 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
336 assertNotNull(notification);
337 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
339 ControlLoopOperationManager clom = manager.processControlLoop();
341 assertNull(clom.getOperationResult());
343 // serialize and de-serialize manager
344 manager = Serializer.roundTrip(manager);
346 // Test operation in progress
347 ControlLoopEventManager manager3 = manager;
348 assertThatThrownBy(manager3::processControlLoop).isInstanceOf(ControlLoopException.class)
349 .hasMessage("Already working an Operation, do not call this method.");
351 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
352 notification = manager.activate(yamlString, event);
353 assertNotNull(notification);
354 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
356 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
357 VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
358 assertNotNull(clfNotification);
359 assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
361 // Test operation completed
362 ControlLoopEventManager manager4 = manager;
363 assertThatThrownBy(manager4::processControlLoop).isInstanceOf(ControlLoopException.class)
364 .hasMessage("Control Loop is in FINAL state, do not call this method.");
366 manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
367 notification = manager.activate(yamlString, event);
368 assertNotNull(notification);
369 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
370 manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE);
372 // Test operation with no next policy defined
373 ControlLoopEventManager manager5 = manager;
374 assertThatThrownBy(manager5::processControlLoop).isInstanceOf(ControlLoopException.class)
375 .hasMessage("The target type is null");
379 public void testFinishOperation() throws Exception {
380 InputStream isStd = new FileInputStream(new File(TEST_YAML));
381 final String yamlStringStd = IOUtils.toString(isStd, StandardCharsets.UTF_8);
383 VirtualControlLoopEvent event = makeEvent();
385 ControlLoopEventManager manager = makeManager(event);
386 ControlLoopEventManager manager2 = manager;
387 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
388 .hasMessage("No operation to finish.");
390 manager.setActivated(true);
391 assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
392 .hasMessage("No operation to finish.");
394 manager.setActivated(false);
396 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
397 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
399 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
400 assertNotNull(notification);
401 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
403 event.getAai().put(VSERVER_NAME, "testVserverName");
405 // serialize and de-serialize manager
406 manager = Serializer.roundTrip(manager);
408 ControlLoopOperationManager clom = manager.processControlLoop();
410 assertNull(clom.getOperationResult());
412 clom.startOperation(event);
414 // This call should be exception free
415 manager.finishOperation(clom);
417 ControlLoopEventManager otherManager = makeManager(event);
418 VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
419 assertNotNull(otherNotification);
420 assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
422 ControlLoopOperationManager otherClom = otherManager.processControlLoop();
423 assertNotNull(otherClom);
424 assertNull(otherClom.getOperationResult());
426 otherManager.finishOperation(clom);
430 public void testLockCurrentOperation_testUnlockCurrentOperation() throws Exception {
431 VirtualControlLoopEvent event = makeEvent();
433 ControlLoopEventManager manager = makeManager(event);
435 manager.setActivated(false);
437 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
438 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
440 VirtualControlLoopNotification notification = manager.activate(yamlString, event);
441 assertNotNull(notification);
442 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
444 ControlLoopEventManager manager2 = manager;
445 assertThatThrownBy(() -> manager2.lockCurrentOperation(callback)).isInstanceOf(ControlLoopException.class)
446 .hasMessage("Do not have a current operation.");
448 assertNull(manager.unlockCurrentOperation());
450 event.getAai().put(VSERVER_NAME, "testVserverName");
452 ControlLoopOperationManager clom = manager.processControlLoop();
454 assertNull(clom.getOperationResult());
456 Pair<Lock, Lock> lockPair = manager.lockCurrentOperation(callback);
457 assertNull(lockPair.first());
458 assertNotNull(lockPair.second());
460 // pseudo lock - session should NOT have been notified of the change
461 verify(callback, never()).lockAvailable(any());
462 verify(callback, never()).lockUnavailable(any());
464 // repeat - should cause an extension
465 Lock lock = lockPair.second();
466 lockPair = manager.lockCurrentOperation(callback);
469 * even with a pseudo lock, the session should have been notified that it was
473 verify(callback).lockAvailable(lock);
475 assertSame(lock, manager.unlockCurrentOperation());
477 assertNull(lockPair.first());
478 assertNull(lockPair.second());
480 // force it to use a pseudo lock
481 manager.setUseTargetLock(false);
482 lockPair = manager.lockCurrentOperation(callback);
483 assertNull(lockPair.first());
484 assertNotNull(lockPair.second());
486 lock = lockPair.second();
488 lockPair = manager.lockCurrentOperation(callback);
489 assertNull(lockPair.first());
490 assertNull(lockPair.second());
492 // first lock uses a pseudo lock, so it will only update when extended
493 verify(callback).lockAvailable(lock);
495 // force it to re-create the lock due to change in resource ID
496 lock = mock(Lock.class);
497 when(lock.getResourceId()).thenReturn("different");
498 Whitebox.setInternalState(manager, TARGET_LOCK_FIELD, lock);
500 lockPair = manager.lockCurrentOperation(callback);
501 assertSame(lock, lockPair.first());
502 assertNotNull(lockPair.second());
504 lock = lockPair.second();
506 lockPair = manager.lockCurrentOperation(callback);
507 assertNull(lockPair.first());
508 assertNull(lockPair.second());
510 // first lock uses a pseudo lock, so it won't do an update
511 verify(callback).lockAvailable(lock);
513 assertSame(lock, manager.unlockCurrentOperation());
514 assertNull(manager.unlockCurrentOperation());
516 // try again - this time don't return the fact handle- no change in count
517 lockPair = manager.lockCurrentOperation(callback);
518 assertNull(lockPair.first());
519 assertNotNull(lockPair.second());
523 public void testOnNewEvent() throws Exception {
524 UUID requestId = UUID.randomUUID();
525 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
526 onsetEvent.setClosedLoopControlName(TWO_ONSET_TEST);
527 onsetEvent.setRequestId(requestId);
528 onsetEvent.setTarget(VNF_ID);
529 onsetEvent.setClosedLoopAlarmStart(Instant.now());
530 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
531 onsetEvent.setAai(new HashMap<>());
532 onsetEvent.getAai().put(VNF_NAME, ONSET_ONE);
533 onsetEvent.setTargetType(ControlLoopTargetType.VNF);
535 VirtualControlLoopEvent abatedEvent = new VirtualControlLoopEvent();
536 abatedEvent.setClosedLoopControlName(TWO_ONSET_TEST);
537 abatedEvent.setRequestId(requestId);
538 abatedEvent.setTarget(VNF_ID);
539 abatedEvent.setClosedLoopAlarmStart(Instant.now());
540 abatedEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ABATED);
541 abatedEvent.setAai(new HashMap<>());
542 abatedEvent.getAai().put(VNF_NAME, ONSET_ONE);
544 ControlLoopEventManager manager = makeManager(onsetEvent);
546 InputStream is = new FileInputStream(new File(TEST_YAML));
547 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
549 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
550 assertNotNull(notification);
551 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
553 assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
554 assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
555 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
557 VirtualControlLoopEvent checkSyntaxEvent = new VirtualControlLoopEvent();
558 checkSyntaxEvent.setAai(null);
559 checkSyntaxEvent.setClosedLoopAlarmEnd(null);
560 checkSyntaxEvent.setClosedLoopAlarmStart(null);
561 checkSyntaxEvent.setClosedLoopControlName(null);
562 checkSyntaxEvent.setClosedLoopEventClient(null);
563 checkSyntaxEvent.setClosedLoopEventStatus(null);
564 checkSyntaxEvent.setFrom(null);
565 checkSyntaxEvent.setPolicyName(null);
566 checkSyntaxEvent.setPolicyScope(null);
567 checkSyntaxEvent.setPolicyVersion(null);
568 checkSyntaxEvent.setRequestId(null);
569 checkSyntaxEvent.setTarget(null);
570 checkSyntaxEvent.setTargetType(null);
571 checkSyntaxEvent.setVersion(null);
573 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
575 checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
576 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
578 checkSyntaxEvent.setClosedLoopControlName(null);
579 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
581 checkSyntaxEvent.setClosedLoopControlName("");
582 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
584 checkSyntaxEvent.setClosedLoopControlName(TWO_ONSET_TEST);
585 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
587 checkSyntaxEvent.setRequestId(null);
588 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
590 checkSyntaxEvent.setRequestId(requestId);
591 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
593 checkSyntaxEvent.setAai(null);
594 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
596 checkSyntaxEvent.setAai(new HashMap<>());
597 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
599 checkSyntaxEvent.setTargetType("");
600 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
602 checkSyntaxEvent.setTarget("");
603 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
605 checkSyntaxEvent.setTarget(null);
606 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
608 checkSyntaxEvent.setTarget("");
609 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
611 checkSyntaxEvent.setTarget("OZ");
612 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
614 checkSyntaxEvent.setTarget("VM_NAME");
615 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
617 checkSyntaxEvent.setTarget("VNF_NAME");
618 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
620 checkSyntaxEvent.setTarget(VSERVER_NAME);
621 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
623 checkSyntaxEvent.setTarget(VNF_ID);
624 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
626 checkSyntaxEvent.setTarget(VNF_NAME);
627 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
629 checkSyntaxEvent.setAai(null);
630 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
632 checkSyntaxEvent.setAai(new HashMap<>());
633 assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
635 checkSyntaxEvent.getAai().put(VNF_NAME, ONSET_ONE);
636 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
638 checkSyntaxEvent.getAai().put(VSERVER_NAME, ONSET_ONE);
639 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
641 checkSyntaxEvent.getAai().put(VNF_ID, ONSET_ONE);
642 assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
646 public void testControlLoopTimeout() throws IOException {
647 VirtualControlLoopEvent onsetEvent = getOnsetEvent();
649 ControlLoopEventManager manager = makeManager(onsetEvent);
650 assertEquals(0, manager.getControlLoopTimeout(null));
651 assertEquals(120, manager.getControlLoopTimeout(120));
653 InputStream is = new FileInputStream(new File(TEST_YAML));
654 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
656 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
657 assertNotNull(notification);
658 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
660 assertEquals(60, manager.getControlLoopTimeout(null));
664 public void testControlLoopTimeout_ZeroTimeout() throws IOException {
665 VirtualControlLoopEvent onsetEvent = getOnsetEvent();
667 ControlLoopEventManager manager = makeManager(onsetEvent);
669 InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
670 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
672 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
673 assertNotNull(notification);
674 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
676 assertEquals(0, manager.getControlLoopTimeout(null));
677 assertEquals(120, manager.getControlLoopTimeout(120));
681 public void testControlLoopTimeout_NullTimeout() throws IOException {
682 VirtualControlLoopEvent onsetEvent = getOnsetEvent();
684 ControlLoopEventManager manager = makeManager(onsetEvent);
686 InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
687 final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
689 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
690 assertNotNull(notification);
691 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
693 assertEquals(0, manager.getControlLoopTimeout(null));
694 assertEquals(120, manager.getControlLoopTimeout(120));
698 public void testIsClosedLoopDisabled() {
699 Map<String, String> aai = onset.getAai();
702 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
703 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
704 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
707 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
708 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
709 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
712 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.FALSE.toString());
713 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
714 assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
717 aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
718 aai.put(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
719 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
722 aai.put(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED, Boolean.TRUE.toString());
723 aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
724 assertTrue(ControlLoopEventManager.isClosedLoopDisabled(onset));
728 public void testIsProvStatusInactive() {
729 Map<String, String> aai = onset.getAai();
732 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
733 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
734 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
737 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
738 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
739 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
742 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
743 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
744 assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
747 aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
748 aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
749 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
752 aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
753 aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
754 assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
758 public void testIsAaiTrue() {
759 assertTrue(ControlLoopEventManager.isAaiTrue("tRuE"));
760 assertTrue(ControlLoopEventManager.isAaiTrue("T"));
761 assertTrue(ControlLoopEventManager.isAaiTrue("t"));
762 assertTrue(ControlLoopEventManager.isAaiTrue("yES"));
763 assertTrue(ControlLoopEventManager.isAaiTrue("Y"));
764 assertTrue(ControlLoopEventManager.isAaiTrue("y"));
766 assertFalse(ControlLoopEventManager.isAaiTrue("no"));
767 assertFalse(ControlLoopEventManager.isAaiTrue(null));
771 private VirtualControlLoopEvent makeEvent() {
772 UUID requestId = UUID.randomUUID();
773 VirtualControlLoopEvent event = new VirtualControlLoopEvent();
774 event.setClosedLoopControlName(TWO_ONSET_TEST);
775 event.setRequestId(requestId);
776 event.setTarget(VNF_ID);
777 event.setClosedLoopAlarmStart(Instant.now());
778 event.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
779 event.setAai(new HashMap<>());
780 event.getAai().put(VNF_ID, ONSET_ONE);
781 event.getAai().put(VSERVER_NAME, "test-vserver");
782 event.setTargetType(ControlLoopTargetType.VNF);
786 private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
787 return new MyManager(event.getClosedLoopControlName(), event.getRequestId());
790 private static class MyManager extends ControlLoopEventManager implements Serializable {
791 private static final long serialVersionUID = 1L;
793 public MyManager(String closedLoopControlName, UUID requestId) {
794 super(closedLoopControlName, requestId);
798 protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
799 return createPseudoLock(targetEntity, requestId, holdSec, callback);