7479f66b1952faa15aebcd7614ca11863cb04fa2
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
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;
35
36 import java.io.File;
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;
46 import java.util.Map;
47 import java.util.UUID;
48 import org.apache.commons.io.IOUtils;
49 import org.apache.commons.lang3.tuple.Pair;
50 import org.jetbrains.annotations.NotNull;
51 import org.junit.AfterClass;
52 import org.junit.Before;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
56 import org.onap.policy.common.utils.coder.CoderException;
57 import org.onap.policy.common.utils.coder.StandardCoder;
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.ControlLoopTargetType;
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.PolicyResult;
67 import org.onap.policy.drools.core.lock.Lock;
68 import org.onap.policy.drools.core.lock.LockCallback;
69 import org.onap.policy.drools.system.PolicyEngineConstants;
70 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
71 import org.powermock.reflect.Whitebox;
72
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";
85
86     private VirtualControlLoopEvent onset;
87     private LockCallback callback;
88
89     /**
90      * Set up test class.
91      */
92     @BeforeClass
93     public static void setUpSimulator() throws Exception {
94         org.onap.policy.simulators.Util.buildAaiSim();
95
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");
100     }
101
102     @AfterClass
103     public static void tearDownSimulator() {
104         HttpServletServerFactoryInstance.getServerFactory().destroy();
105     }
106
107     /**
108      * Setup.
109      */
110     @Before
111     public void setUp() {
112         callback = mock(LockCallback.class);
113
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);
125
126         PolicyEngineConstants.getManager().setEnvironmentProperty(AAI_URL, "http://localhost:6666");
127     }
128
129     @Test
130     public void testMethods() {
131         UUID requestId = UUID.randomUUID();
132         ControlLoopEventManager clem = new ControlLoopEventManager("MyClosedLoopName", requestId);
133
134         assertEquals("MyClosedLoopName", clem.getClosedLoopControlName());
135         assertEquals(requestId, clem.getRequestId());
136
137         clem.setActivated(true);
138         assertEquals(true, clem.isActivated());
139
140         clem.setControlLoopResult("SUCCESS");
141         assertEquals("SUCCESS", clem.getControlLoopResult());
142
143         clem.setControlLoopTimedOut();
144         assertEquals(true, clem.isControlLoopTimedOut());
145
146         clem.setNumAbatements(12345);
147         assertEquals(Integer.valueOf(12345), clem.getNumAbatements());
148
149         clem.setNumOnsets(54321);
150         assertEquals(Integer.valueOf(54321), clem.getNumOnsets());
151
152         assertNull(clem.getOnsetEvent());
153         assertNull(clem.getAbatementEvent());
154         assertNull(clem.getProcessor());
155
156         assertEquals(true, clem.isControlLoopTimedOut());
157
158         assertNull(clem.unlockCurrentOperation());
159     }
160
161     @Test
162     public void testAlreadyActivated() {
163         VirtualControlLoopEvent event = getOnsetEvent();
164
165         ControlLoopEventManager manager = makeManager(event);
166         manager.setActivated(true);
167         VirtualControlLoopNotification notification = manager.activate(event);
168         assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
169     }
170
171     @Test
172     public void testActivationYaml() throws IOException, CoderException {
173
174         VirtualControlLoopEvent event = getOnsetEvent();
175         ControlLoopEventManager manager = makeManager(event);
176
177         // Null YAML should fail
178         VirtualControlLoopNotification notificationNull = manager.activate((String) null, event);
179         assertNotNull(notificationNull);
180         assertEquals(ControlLoopNotificationType.REJECTED, notificationNull.getNotification());
181
182         // Empty YAML should fail
183         VirtualControlLoopNotification notificationEmpty = manager.activate("", event);
184         assertNotNull(notificationEmpty);
185         assertEquals(ControlLoopNotificationType.REJECTED, notificationEmpty.getNotification());
186
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);
190
191         VirtualControlLoopNotification notificationBad = manager.activate(yamlStringBad, event);
192         assertNotNull(notificationBad);
193         assertEquals(ControlLoopNotificationType.REJECTED, notificationBad.getNotification());
194
195
196         InputStream is = new FileInputStream(new File(TEST_YAML));
197         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
198
199         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
200         assertNotNull(notification);
201         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
202
203         // Another activate should fail
204         VirtualControlLoopNotification notificationActive = manager.activate(yamlString, event);
205         assertNotNull(notificationActive);
206         assertEquals(ControlLoopNotificationType.REJECTED, notificationActive.getNotification());
207     }
208
209     @Test
210     public void testActivateToscaLegacy() throws IOException, CoderException {
211         String policy =
212                 new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-legacy-vcpe.json")));
213         ToscaPolicy toscaPolicy = new StandardCoder().decode(policy, ToscaPolicy.class);
214
215         VirtualControlLoopEvent event = getOnsetEvent();
216         ControlLoopEventManager manager = makeManager(event);
217
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());
222
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());
227
228         // another activate should fail
229         notification = manager.activate(toscaPolicy, event);
230         assertEquals(ControlLoopNotificationType.REJECTED, notification.getNotification());
231     }
232
233     @Test
234     public void testControlLoopFinal() throws Exception {
235         VirtualControlLoopEvent event = getOnsetEvent();
236
237         ControlLoopEventManager manager = makeManager(event);
238         ControlLoopEventManager manager2 = manager;
239         assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
240                         .hasMessage("ControlLoopEventManager MUST be activated first.");
241
242         manager.setActivated(true);
243         assertThatThrownBy(manager2::isControlLoopFinal).isInstanceOf(ControlLoopException.class)
244                         .hasMessage("No onset event for ControlLoopEventManager.");
245
246         manager.setActivated(false);
247
248         InputStream is = new FileInputStream(new File(TEST_YAML));
249         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
250
251         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
252         assertNotNull(notification);
253         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
254
255         VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
256         assertNull(clfNotification);
257
258         // serialize and de-serialize manager
259         manager = Serializer.roundTrip(manager);
260
261         manager.getProcessor().nextPolicyForResult(PolicyResult.SUCCESS);
262         clfNotification = manager.isControlLoopFinal();
263         assertNotNull(clfNotification);
264         assertEquals(ControlLoopNotificationType.FINAL_SUCCESS, clfNotification.getNotification());
265
266         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
267         notification = manager.activate(yamlString, event);
268         assertNotNull(notification);
269         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
270
271         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_EXCEPTION);
272         clfNotification = manager.isControlLoopFinal();
273         assertNotNull(clfNotification);
274         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
275
276         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
277         notification = manager.activate(yamlString, event);
278         assertNotNull(notification);
279         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
280
281         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
282         clfNotification = manager.isControlLoopFinal();
283         assertNotNull(clfNotification);
284         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
285
286         manager.setControlLoopTimedOut();
287         clfNotification = manager.isControlLoopFinal();
288         assertNotNull(clfNotification);
289         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
290     }
291
292     @NotNull
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);
304         return event;
305     }
306
307     @Test
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);
320
321         ControlLoopEventManager manager = makeManager(event);
322         ControlLoopEventManager manager2 = manager;
323         assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
324                         .hasMessage("ControlLoopEventManager MUST be activated first.");
325
326         manager.setActivated(true);
327         assertThatThrownBy(manager2::processControlLoop).isInstanceOf(ControlLoopException.class)
328                         .hasMessage("No onset event for ControlLoopEventManager.");
329
330         manager.setActivated(false);
331
332         InputStream is = new FileInputStream(new File(TEST_YAML));
333         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
334
335         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
336         assertNotNull(notification);
337         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
338
339         ControlLoopOperationManager clom = manager.processControlLoop();
340         assertNotNull(clom);
341         assertNull(clom.getOperationResult());
342
343         // serialize and de-serialize manager
344         manager = Serializer.roundTrip(manager);
345
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.");
350
351         manager = new ControlLoopEventManager(event.getClosedLoopControlName(), event.getRequestId());
352         notification = manager.activate(yamlString, event);
353         assertNotNull(notification);
354         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
355
356         manager.getProcessor().nextPolicyForResult(PolicyResult.FAILURE_GUARD);
357         VirtualControlLoopNotification clfNotification = manager.isControlLoopFinal();
358         assertNotNull(clfNotification);
359         assertEquals(ControlLoopNotificationType.FINAL_FAILURE, clfNotification.getNotification());
360
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.");
365
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);
371
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");
376     }
377
378     @Test
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);
382
383         VirtualControlLoopEvent event = makeEvent();
384
385         ControlLoopEventManager manager = makeManager(event);
386         ControlLoopEventManager manager2 = manager;
387         assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
388                         .hasMessage("No operation to finish.");
389
390         manager.setActivated(true);
391         assertThatThrownBy(() -> manager2.finishOperation(null)).isInstanceOf(ControlLoopException.class)
392                         .hasMessage("No operation to finish.");
393
394         manager.setActivated(false);
395
396         InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
397         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
398
399         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
400         assertNotNull(notification);
401         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
402
403         event.getAai().put(VSERVER_NAME, "testVserverName");
404
405         // serialize and de-serialize manager
406         manager = Serializer.roundTrip(manager);
407
408         ControlLoopOperationManager clom = manager.processControlLoop();
409         assertNotNull(clom);
410         assertNull(clom.getOperationResult());
411
412         clom.startOperation(event);
413
414         // This call should be exception free
415         manager.finishOperation(clom);
416
417         ControlLoopEventManager otherManager = makeManager(event);
418         VirtualControlLoopNotification otherNotification = otherManager.activate(yamlStringStd, event);
419         assertNotNull(otherNotification);
420         assertEquals(ControlLoopNotificationType.ACTIVE, otherNotification.getNotification());
421
422         ControlLoopOperationManager otherClom = otherManager.processControlLoop();
423         assertNotNull(otherClom);
424         assertNull(otherClom.getOperationResult());
425
426         otherManager.finishOperation(clom);
427     }
428
429     @Test
430     public void testLockCurrentOperation_testUnlockCurrentOperation() throws Exception {
431         VirtualControlLoopEvent event = makeEvent();
432
433         ControlLoopEventManager manager = makeManager(event);
434
435         manager.setActivated(false);
436
437         InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
438         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
439
440         VirtualControlLoopNotification notification = manager.activate(yamlString, event);
441         assertNotNull(notification);
442         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
443
444         ControlLoopEventManager manager2 = manager;
445         assertThatThrownBy(() -> manager2.lockCurrentOperation(callback)).isInstanceOf(ControlLoopException.class)
446                         .hasMessage("Do not have a current operation.");
447
448         assertNull(manager.unlockCurrentOperation());
449
450         event.getAai().put(VSERVER_NAME, "testVserverName");
451
452         ControlLoopOperationManager clom = manager.processControlLoop();
453         assertNotNull(clom);
454         assertNull(clom.getOperationResult());
455
456         Pair<Lock, Lock> lockPair = manager.lockCurrentOperation(callback);
457         assertNull(lockPair.getLeft());
458         assertNotNull(lockPair.getRight());
459
460         // pseudo lock - session should NOT have been notified of the change
461         verify(callback, never()).lockAvailable(any());
462         verify(callback, never()).lockUnavailable(any());
463
464         // repeat - should cause an extension
465         Lock lock = lockPair.getRight();
466         lockPair = manager.lockCurrentOperation(callback);
467
468         /*
469          * even with a pseudo lock, the session should have been notified that it was
470          * extended
471          */
472
473         verify(callback).lockAvailable(lock);
474
475         assertSame(lock, manager.unlockCurrentOperation());
476
477         assertNull(lockPair.getLeft());
478         assertNull(lockPair.getRight());
479
480         // force it to use a pseudo lock
481         manager.setUseTargetLock(false);
482         lockPair = manager.lockCurrentOperation(callback);
483         assertNull(lockPair.getLeft());
484         assertNotNull(lockPair.getRight());
485
486         lock = lockPair.getRight();
487
488         lockPair = manager.lockCurrentOperation(callback);
489         assertNull(lockPair.getLeft());
490         assertNull(lockPair.getRight());
491
492         // first lock uses a pseudo lock, so it will only update when extended
493         verify(callback).lockAvailable(lock);
494
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);
499
500         lockPair = manager.lockCurrentOperation(callback);
501         assertSame(lock, lockPair.getLeft());
502         assertNotNull(lockPair.getRight());
503
504         lock = lockPair.getRight();
505
506         lockPair = manager.lockCurrentOperation(callback);
507         assertNull(lockPair.getLeft());
508         assertNull(lockPair.getRight());
509
510         // first lock uses a pseudo lock, so it won't do an update
511         verify(callback).lockAvailable(lock);
512
513         assertSame(lock, manager.unlockCurrentOperation());
514         assertNull(manager.unlockCurrentOperation());
515
516         // try again - this time don't return the fact handle- no change in count
517         lockPair = manager.lockCurrentOperation(callback);
518         assertNull(lockPair.getLeft());
519         assertNotNull(lockPair.getRight());
520     }
521
522     @Test
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);
534
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);
543
544         ControlLoopEventManager manager = makeManager(onsetEvent);
545
546         InputStream is = new FileInputStream(new File(TEST_YAML));
547         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
548
549         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
550         assertNotNull(notification);
551         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
552
553         assertEquals(NewEventStatus.FIRST_ONSET, manager.onNewEvent(onsetEvent));
554         assertEquals(NewEventStatus.FIRST_ABATEMENT, manager.onNewEvent(abatedEvent));
555         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
556
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);
572
573         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
574
575         checkSyntaxEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
576         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
577
578         checkSyntaxEvent.setClosedLoopControlName(null);
579         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
580
581         checkSyntaxEvent.setClosedLoopControlName("");
582         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
583
584         checkSyntaxEvent.setClosedLoopControlName(TWO_ONSET_TEST);
585         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
586
587         checkSyntaxEvent.setRequestId(null);
588         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
589
590         checkSyntaxEvent.setRequestId(requestId);
591         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
592
593         checkSyntaxEvent.setAai(null);
594         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
595
596         checkSyntaxEvent.setAai(new HashMap<>());
597         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
598
599         checkSyntaxEvent.setTargetType("");
600         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
601
602         checkSyntaxEvent.setTarget("");
603         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
604
605         checkSyntaxEvent.setTarget(null);
606         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
607
608         checkSyntaxEvent.setTarget("");
609         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
610
611         checkSyntaxEvent.setTarget("OZ");
612         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
613
614         checkSyntaxEvent.setTarget("VM_NAME");
615         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
616
617         checkSyntaxEvent.setTarget("VNF_NAME");
618         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
619
620         checkSyntaxEvent.setTarget(VSERVER_NAME);
621         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
622
623         checkSyntaxEvent.setTarget(VNF_ID);
624         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
625
626         checkSyntaxEvent.setTarget(VNF_NAME);
627         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
628
629         checkSyntaxEvent.setAai(null);
630         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
631
632         checkSyntaxEvent.setAai(new HashMap<>());
633         assertEquals(NewEventStatus.SYNTAX_ERROR, manager.onNewEvent(checkSyntaxEvent));
634
635         checkSyntaxEvent.getAai().put(VNF_NAME, ONSET_ONE);
636         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
637
638         checkSyntaxEvent.getAai().put(VSERVER_NAME, ONSET_ONE);
639         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
640
641         checkSyntaxEvent.getAai().put(VNF_ID, ONSET_ONE);
642         assertEquals(NewEventStatus.SUBSEQUENT_ABATEMENT, manager.onNewEvent(abatedEvent));
643     }
644
645     @Test
646     public void testControlLoopTimeout() throws IOException {
647         VirtualControlLoopEvent onsetEvent = getOnsetEvent();
648
649         ControlLoopEventManager manager = makeManager(onsetEvent);
650         assertEquals(0, manager.getControlLoopTimeout(null));
651         assertEquals(120, manager.getControlLoopTimeout(120));
652
653         InputStream is = new FileInputStream(new File(TEST_YAML));
654         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
655
656         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
657         assertNotNull(notification);
658         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
659
660         assertEquals(60, manager.getControlLoopTimeout(null));
661     }
662
663     @Test
664     public void testControlLoopTimeout_ZeroTimeout() throws IOException {
665         VirtualControlLoopEvent onsetEvent = getOnsetEvent();
666
667         ControlLoopEventManager manager = makeManager(onsetEvent);
668
669         InputStream is = new FileInputStream(new File("src/test/resources/test-zero-timeout.yaml"));
670         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
671
672         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
673         assertNotNull(notification);
674         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
675
676         assertEquals(0, manager.getControlLoopTimeout(null));
677         assertEquals(120, manager.getControlLoopTimeout(120));
678     }
679
680     @Test
681     public void testControlLoopTimeout_NullTimeout() throws IOException {
682         VirtualControlLoopEvent onsetEvent = getOnsetEvent();
683
684         ControlLoopEventManager manager = makeManager(onsetEvent);
685
686         InputStream is = new FileInputStream(new File("src/test/resources/test-null-timeout.yaml"));
687         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
688
689         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
690         assertNotNull(notification);
691         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
692
693         assertEquals(0, manager.getControlLoopTimeout(null));
694         assertEquals(120, manager.getControlLoopTimeout(120));
695     }
696
697     @Test
698     public void testIsClosedLoopDisabled() {
699         Map<String, String> aai = onset.getAai();
700
701         // null, null
702         aai.remove(ControlLoopEventManager.GENERIC_VNF_IS_CLOSED_LOOP_DISABLED);
703         aai.remove(ControlLoopEventManager.VSERVER_IS_CLOSED_LOOP_DISABLED);
704         assertFalse(ControlLoopEventManager.isClosedLoopDisabled(onset));
705
706         // null, false
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));
710
711         // false, null
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));
715
716         // null, true
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));
720
721         // true, null
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));
725     }
726
727     @Test
728     public void testIsProvStatusInactive() {
729         Map<String, String> aai = onset.getAai();
730
731         // null, null
732         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
733         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
734         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
735
736         // null, active
737         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
738         aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
739         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
740
741         // active, null
742         aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, ControlLoopEventManager.PROV_STATUS_ACTIVE);
743         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
744         assertFalse(ControlLoopEventManager.isProvStatusInactive(onset));
745
746         // null, inactive
747         aai.remove(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS);
748         aai.put(ControlLoopEventManager.VSERVER_PROV_STATUS, "other1");
749         assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
750
751         // inactive, null
752         aai.put(ControlLoopEventManager.GENERIC_VNF_PROV_STATUS, "other2");
753         aai.remove(ControlLoopEventManager.VSERVER_PROV_STATUS);
754         assertTrue(ControlLoopEventManager.isProvStatusInactive(onset));
755     }
756
757     @Test
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"));
765
766         assertFalse(ControlLoopEventManager.isAaiTrue("no"));
767         assertFalse(ControlLoopEventManager.isAaiTrue(null));
768     }
769
770
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);
783         return event;
784     }
785
786     private ControlLoopEventManager makeManager(VirtualControlLoopEvent event) {
787         return new MyManager(event.getClosedLoopControlName(), event.getRequestId());
788     }
789
790     private static class MyManager extends ControlLoopEventManager implements Serializable {
791         private static final long serialVersionUID = 1L;
792
793         public MyManager(String closedLoopControlName, UUID requestId) {
794             super(closedLoopControlName, requestId);
795         }
796
797         @Override
798         protected Lock createRealLock(String targetEntity, UUID requestId, int holdSec, LockCallback callback) {
799             return createPseudoLock(targetEntity, requestId, holdSec, callback);
800         }
801     }
802 }