2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertSame;
30 import static org.junit.Assert.assertTrue;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.ArgumentMatchers.eq;
33 import static org.mockito.Mockito.doAnswer;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.times;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
39 import java.time.Instant;
41 import java.util.TreeMap;
42 import java.util.UUID;
43 import java.util.concurrent.CompletableFuture;
44 import java.util.concurrent.CountDownLatch;
45 import java.util.concurrent.TimeUnit;
46 import java.util.function.Consumer;
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.ArgumentCaptor;
50 import org.mockito.Captor;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.onap.aai.domain.yang.GenericVnf;
54 import org.onap.policy.aai.AaiCqResponse;
55 import org.onap.policy.common.utils.time.PseudoExecutor;
56 import org.onap.policy.controlloop.ControlLoopOperation;
57 import org.onap.policy.controlloop.ControlLoopResponse;
58 import org.onap.policy.controlloop.VirtualControlLoopEvent;
59 import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider;
60 import org.onap.policy.controlloop.actor.guard.GuardOperation;
61 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
62 import org.onap.policy.controlloop.actorserviceprovider.Operation;
63 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
64 import org.onap.policy.controlloop.actorserviceprovider.Operator;
65 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
66 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
67 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
68 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
69 import org.onap.policy.controlloop.policy.Policy;
70 import org.onap.policy.controlloop.policy.PolicyResult;
71 import org.onap.policy.controlloop.policy.Target;
72 import org.onap.policy.controlloop.policy.TargetType;
73 import org.onap.policy.sdnr.PciBody;
74 import org.onap.policy.sdnr.PciMessage;
75 import org.onap.policy.sdnr.PciResponse;
77 public class ControlLoopOperationManager2Test {
78 private static final UUID REQ_ID = UUID.randomUUID();
79 private static final String MISMATCH = "mismatch";
80 private static final String POLICY_ID = "my-policy";
81 private static final String POLICY_ACTOR = "my-actor";
82 private static final String POLICY_OPERATION = "my-operation";
83 private static final String OTHER_ACTOR = "another-actor";
84 private static final String MY_TARGET = "my-target";
85 private static final String MY_VNF_ID = "my-vnf-id";
86 private static final String PAYLOAD_KEY = "payload-key";
87 private static final String PAYLOAD_VALUE = "payload-value";
88 private static final long REMAINING_MS = 5000;
89 private static final int MAX_RUN = 100;
90 private static final Integer POLICY_RETRY = 3;
91 private static final Integer POLICY_TIMEOUT = 20;
92 private static final IllegalArgumentException EXPECTED_EXCEPTION =
93 new IllegalArgumentException("expected exception");
96 private ArgumentCaptor<Consumer<OperationOutcome>> lockCallback;
99 private OperationHistoryDataManager dataMgr;
101 private ManagerContext mgrctx;
103 private Operator policyOperator;
105 private Operation policyOperation;
107 private Actor policyActor;
109 private ActorService actors;
111 private AaiCqResponse cqdata;
113 private GenericVnf vnf;
115 private CompletableFuture<OperationOutcome> lockFuture;
116 private CompletableFuture<OperationOutcome> policyFuture;
117 private Target target;
118 private Map<String, String> payload;
119 private Policy policy;
120 private VirtualControlLoopEvent event;
121 private ControlLoopEventContext context;
122 private PseudoExecutor executor;
123 private ControlLoopOperationManager2 mgr;
129 public void setUp() {
130 MockitoAnnotations.initMocks(this);
132 lockFuture = new CompletableFuture<>();
133 policyFuture = new CompletableFuture<>();
135 when(mgrctx.getActorService()).thenReturn(actors);
136 when(mgrctx.getDataManager()).thenReturn(dataMgr);
137 when(mgrctx.requestLock(any(), any())).thenReturn(lockFuture);
139 // configure policy operation
140 when(actors.getActor(POLICY_ACTOR)).thenReturn(policyActor);
141 when(policyActor.getOperator(POLICY_OPERATION)).thenReturn(policyOperator);
142 when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
143 when(policyOperation.start()).thenReturn(policyFuture);
145 when(vnf.getVnfId()).thenReturn(MY_VNF_ID);
146 when(cqdata.getDefaultGenericVnf()).thenReturn(vnf);
148 target = new Target();
149 target.setType(TargetType.VM);
151 payload = Map.of(PAYLOAD_KEY, PAYLOAD_VALUE);
153 policy = new Policy();
154 policy.setId(POLICY_ID);
155 policy.setActor(POLICY_ACTOR);
156 policy.setRecipe(POLICY_OPERATION);
157 policy.setTarget(target);
158 policy.setPayload(payload);
159 policy.setRetry(POLICY_RETRY);
160 policy.setTimeout(POLICY_TIMEOUT);
162 event = new VirtualControlLoopEvent();
163 event.setRequestId(REQ_ID);
164 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
165 event.setAai(new TreeMap<>(Map.of(ControlLoopOperationManager2.VSERVER_VSERVER_NAME, MY_TARGET)));
167 context = new ControlLoopEventContext(event);
168 context.setProperty(AaiCqResponse.CONTEXT_KEY, cqdata);
170 executor = new PseudoExecutor();
172 mgr = new ControlLoopOperationManager2(mgrctx, context, policy, executor);
176 public void testStart() {
177 mgr.start(REMAINING_MS);
179 // should have determined the target entity by now
180 assertEquals(MY_TARGET, mgr.getTargetEntity());
182 verify(mgrctx).requestLock(eq(MY_TARGET), any());
184 lockFuture.complete(new OperationOutcome());
186 policyFuture.complete(genOpOutcome());
189 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
191 assertTrue(mgr.nextStep());
192 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
194 assertTrue(mgr.nextStep());
195 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
197 assertTrue(mgr.nextStep());
198 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
200 assertFalse(mgr.nextStep());
202 OperationOutcome outcome = mgr.getOutcomes().peek();
203 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
204 assertTrue(outcome.isFinalOutcome());
206 verify(mgrctx, times(4)).updated(mgr);
210 * Tests start() when detmTarget() (i.e., the first task) throws an exception.
213 public void testStartDetmTargetException() {
214 policy.setTarget(new Target());
215 mgr.start(REMAINING_MS);
219 assertFalse(mgr.nextStep());
220 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
222 // should have called update() for operation-start, but not for any nextStep()
223 verify(mgrctx).updated(mgr);
227 * Tests start() when a subsequent task throws an exception.
230 public void testStartException() {
231 when(policyOperation.start()).thenThrow(EXPECTED_EXCEPTION);
233 mgr.start(REMAINING_MS);
235 lockFuture.complete(new OperationOutcome());
238 assertFalse(mgr.nextStep());
239 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
241 // should have called update() for operation-start, but not for any nextStep()
242 verify(mgrctx).updated(mgr);
246 * Tests start() when the control loop times out before the operation starts.
249 public void testStartClTimeout_testHandleTimeout() throws InterruptedException {
250 // catch the callback when it times out
251 CountDownLatch updatedLatch = new CountDownLatch(1);
253 updatedLatch.countDown();
255 }).when(mgrctx).updated(any());
257 long tstart = System.currentTimeMillis();
259 // give it a short timeout
262 assertTrue(updatedLatch.await(5, TimeUnit.SECONDS));
263 assertTrue(System.currentTimeMillis() - tstart >= 100);
265 // don't generate any responses
268 // wait for the future to be canceled, via a background thread
269 CountDownLatch futureLatch = new CountDownLatch(1);
270 mgr.getFuture().whenComplete((unused, thrown) -> futureLatch.countDown());
271 assertTrue(futureLatch.await(5, TimeUnit.SECONDS));
273 // lock should have been canceled
274 assertTrue(mgr.getFuture().isCancelled());
276 assertFalse(mgr.nextStep());
277 assertEquals(ControlLoopOperationManager2.State.CONTROL_LOOP_TIMEOUT, mgr.getState());
279 // should have called update() for operation-start, but not for any nextStep()
280 verify(mgrctx).updated(mgr);
282 // should have added a record to the DB
283 verify(dataMgr).store(any(), any(), any(), any());
287 public void testStartOperation() {
288 mgr.start(REMAINING_MS);
290 lockFuture.complete(new OperationOutcome());
294 verify(policyOperation).start();
296 ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
297 verify(policyOperator).buildOperation(captor.capture());
299 ControlLoopOperationParams params = captor.getValue();
301 assertNotNull(params);
302 assertEquals(POLICY_ACTOR, params.getActor());
303 assertSame(actors, params.getActorService());
304 assertNotNull(params.getCompleteCallback());
305 assertSame(context, params.getContext());
306 assertSame(executor, params.getExecutor());
307 assertEquals(POLICY_OPERATION, params.getOperation());
308 assertEquals(payload, params.getPayload());
309 assertSame(REQ_ID, params.getRequestId());
310 assertSame(POLICY_RETRY, params.getRetry());
311 assertNotNull(params.getStartCallback());
312 assertSame(target, params.getTarget());
313 assertEquals(MY_TARGET, params.getTargetEntity());
314 assertSame(POLICY_TIMEOUT, params.getTimeoutSec());
318 public void testStartOperationNullPayload() {
319 policy.setPayload(null);
320 mgr.start(REMAINING_MS);
322 lockFuture.complete(new OperationOutcome());
326 verify(policyOperation).start();
328 ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
329 verify(policyOperator).buildOperation(captor.capture());
331 ControlLoopOperationParams params = captor.getValue();
333 assertNotNull(params);
334 assertEquals(POLICY_ACTOR, params.getActor());
335 assertSame(actors, params.getActorService());
336 assertNotNull(params.getCompleteCallback());
337 assertSame(context, params.getContext());
338 assertSame(executor, params.getExecutor());
339 assertEquals(POLICY_OPERATION, params.getOperation());
340 assertTrue(params.getPayload().isEmpty());
341 assertSame(REQ_ID, params.getRequestId());
342 assertSame(POLICY_RETRY, params.getRetry());
343 assertNotNull(params.getStartCallback());
344 assertSame(target, params.getTarget());
345 assertEquals(MY_TARGET, params.getTargetEntity());
346 assertSame(POLICY_TIMEOUT, params.getTimeoutSec());
350 public void testMakeControlLoopResponse() {
351 final OperationOutcome outcome = new OperationOutcome();
352 PciMessage msg = new PciMessage();
353 outcome.setResponse(msg);
355 PciBody body = new PciBody();
358 PciResponse output = new PciResponse();
359 body.setOutput(output);
361 output.setPayload("my-payload");
364 // not an SDNR action - should return null
365 assertNull(mgr.makeControlLoopResponse(outcome));
368 * now work with SDNR actor
370 policy.setActor("SDNR");
371 mgr = new ControlLoopOperationManager2(mgrctx, context, policy, executor);
373 // should return null for a null input
374 assertNull(mgr.makeControlLoopResponse(null));
376 // should generate a response, with a payload
377 checkResp(outcome, "my-payload");
380 * these should generate a response, with null payload
382 output.setPayload(null);
383 checkResp(outcome, null);
385 body.setOutput(null);
386 checkResp(outcome, null);
389 checkResp(outcome, null);
391 outcome.setResponse(null);
392 checkResp(outcome, null);
396 public void testGetOperationMessage() {
398 assertNull(mgr.getOperationMessage());
401 assertThat(mgr.getOperationMessage()).contains("actor=my-actor").contains("operation=my-operation");
405 public void testGetOperationResult() {
407 assertNotNull(mgr.getOperationResult());
410 assertEquals(PolicyResult.SUCCESS, mgr.getOperationResult());
414 * Tests getOperationResult() when it ends in a failure.
417 public void testGetOperationResultFailure() {
418 mgr.start(REMAINING_MS);
423 assertEquals(PolicyResult.FAILURE_GUARD, mgr.getOperationResult());
427 * Tests handleException() when the exception is a "cancel".
430 public void testHandleExceptionCanceled() {
431 lockFuture.cancel(false);
433 mgr.start(REMAINING_MS);
437 assertTrue(mgr.nextStep());
438 assertEquals(ControlLoopOperationManager2.State.ACTIVE, mgr.getState());
442 public void testCancel() {
443 mgr.start(REMAINING_MS);
446 assertTrue(mgr.getFuture().isCancelled());
450 * Tests cancel() when the operation hasn't been started.
453 public void testCancelNotStarted() {
454 assertNull(mgr.getFuture());
457 assertNull(mgr.getFuture());
461 public void testLockUnavailable() {
462 mgr.start(REMAINING_MS);
466 // lock failure outcome
467 final OperationOutcome outcome = genLockFailure();
471 assertFalse(mgr.nextStep());
472 assertEquals(ControlLoopOperationManager2.State.LOCK_DENIED, mgr.getState());
474 assertEquals(outcome, mgr.getOutcomes().peek());
476 // should have called update() for operation-start, but not for any nextStep()
477 verify(mgrctx).updated(mgr);
481 * Tests onStart() and onComplete() with other actors.
484 public void testOnStart_testOnComplete() {
485 mgr.start(REMAINING_MS);
487 lockFuture.complete(new OperationOutcome());
490 // generate failure outcome for ANOTHER actor - should be ignored
491 OperationOutcome outcome = mgr.getParams().makeOutcome();
492 outcome.setActor(OTHER_ACTOR);
493 outcome.setResult(PolicyResult.FAILURE);
494 outcome.setStart(Instant.now());
495 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
497 outcome.setEnd(Instant.now());
498 mgr.getParams().callbackCompleted(outcome);
500 policyFuture.complete(genOpOutcome());
503 // should not include the other actor's outcome
504 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
506 assertTrue(mgr.nextStep());
507 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
509 assertTrue(mgr.nextStep());
510 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
512 assertTrue(mgr.nextStep());
513 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
515 assertFalse(mgr.nextStep());
517 assertEquals(PolicyResult.SUCCESS, mgr.getOutcomes().peek().getResult());
519 verify(mgrctx, times(4)).updated(mgr);
523 public void testNextStep() {
524 mgr.start(REMAINING_MS);
526 // only do the lock and the guard
527 lockFuture.complete(new OperationOutcome());
531 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
533 assertTrue(mgr.nextStep());
534 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
536 assertTrue(mgr.nextStep());
537 assertTrue(mgr.nextStep());
539 verify(mgrctx, times(2)).updated(mgr);
543 * Tests processOutcome() when the lock is denied.
546 public void testProcessOutcomeLockDenied() {
547 mgr.start(REMAINING_MS);
549 // unavailable from the start => "denied"
554 assertEquals(ControlLoopOperationManager2.State.LOCK_DENIED, mgr.getState());
556 assertFalse(mgr.nextStep());
557 verify(mgrctx).updated(mgr);
559 verifyDb(1, PolicyResult.FAILURE_GUARD, "Operation denied by Lock");
563 * Tests processOutcome() when the lock is lost.
566 public void testProcessOutcomeLockLost() {
567 mgr.start(REMAINING_MS);
569 // indicate lock success initially
570 lockFuture.complete(new OperationOutcome());
575 // now generate a lock failure => "lost"
580 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
582 assertTrue(mgr.nextStep());
583 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
585 assertTrue(mgr.nextStep());
586 assertEquals(ControlLoopOperationManager2.State.LOCK_LOST, mgr.getState());
588 assertFalse(mgr.nextStep());
589 verify(mgrctx, times(3)).updated(mgr);
591 verifyDb(1, PolicyResult.FAILURE, "Operation aborted by Lock");
595 * Tests processOutcome() when the guard is permitted.
598 public void testProcessOutcomeGuardPermit() {
599 mgr.start(REMAINING_MS);
601 lockFuture.complete(new OperationOutcome());
606 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
608 assertTrue(mgr.nextStep());
609 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
611 assertTrue(mgr.nextStep());
612 verify(mgrctx, times(2)).updated(mgr);
614 verify(dataMgr, never()).store(any(), any(), any(), any());
618 * Tests processOutcome() when the guard is permitted.
621 public void testProcessOutcomeGuardDenied() {
622 mgr.start(REMAINING_MS);
624 lockFuture.complete(new OperationOutcome());
625 genGuardOutcome(false);
629 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
631 assertTrue(mgr.nextStep());
632 assertEquals(ControlLoopOperationManager2.State.GUARD_DENIED, mgr.getState());
634 assertFalse(mgr.nextStep());
635 verify(mgrctx, times(2)).updated(mgr);
637 verifyDb(1, PolicyResult.FAILURE_GUARD, "Operation denied by Guard");
641 * Tests processOutcome() when the operation is a success.
644 public void testProcessOutcomeOperSuccess() {
645 mgr.start(REMAINING_MS);
647 lockFuture.complete(new OperationOutcome());
653 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
655 assertTrue(mgr.nextStep());
656 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
658 assertTrue(mgr.nextStep());
659 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
661 assertTrue(mgr.nextStep());
662 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
664 assertFalse(mgr.nextStep());
665 verify(mgrctx, times(4)).updated(mgr);
667 verifyDb(2, PolicyResult.SUCCESS, null);
671 * Tests processOutcome() when the operation is a failure.
674 public void testProcessOutcomeOperFailure() {
675 mgr.start(REMAINING_MS);
677 lockFuture.complete(new OperationOutcome());
683 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
685 assertTrue(mgr.nextStep());
686 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
688 assertTrue(mgr.nextStep());
689 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
691 assertTrue(mgr.nextStep());
692 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
693 verifyDb(2, PolicyResult.FAILURE, null);
695 assertThat(mgr.toString()).contains("attempts=1");
701 assertTrue(mgr.nextStep());
702 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
704 assertTrue(mgr.nextStep());
705 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
706 verifyDb(4, PolicyResult.FAILURE, null);
708 assertThat(mgr.toString()).contains("attempts=2");
710 // and finally a success
713 assertTrue(mgr.nextStep());
714 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
716 assertTrue(mgr.nextStep());
717 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
718 verifyDb(6, PolicyResult.SUCCESS, null);
720 assertThat(mgr.toString()).contains("attempts=3");
722 assertFalse(mgr.nextStep());
723 verify(mgrctx, times(8)).updated(mgr);
727 public void testGetOperationHistory() {
729 assertNull(mgr.getOperationHistory());
732 assertThat(mgr.getOperationHistory()).contains("actor=my-actor").contains("operation=my-operation")
733 .contains("outcome=Success");
737 public void testGetHistory() {
739 assertEquals(0, mgr.getHistory().size());
742 assertEquals(1, mgr.getHistory().size());
746 public void testDetmTargetVm() {
747 target.setType(TargetType.VM);
748 assertNull(mgr.detmTarget());
749 assertEquals(MY_TARGET, mgr.getTargetEntity());
751 target.setType(TargetType.VNF);
752 assertNull(mgr.detmTarget());
753 assertEquals(MY_TARGET, mgr.getTargetEntity());
755 target.setType(TargetType.VFMODULE);
756 assertNull(mgr.detmTarget());
757 assertEquals(MY_TARGET, mgr.getTargetEntity());
760 target.setType(TargetType.VFC);
761 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
762 .withMessage("The target type is not supported");
765 target.setType(null);
766 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("The target type is null");
769 policy.setTarget(null);
770 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("The target is null");
774 public void testDetmPnfTarget() {
776 assertNull(mgr.detmTarget());
777 assertEquals(MY_TARGET, mgr.getTargetEntity());
779 // missing enrichment data
780 event.getAai().clear();
781 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
782 .withMessage("AAI section is missing " + ControlLoopOperationManager2.PNF_NAME);
785 event.setTarget(MISMATCH);
786 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
787 .withMessage("Target does not match target type");
791 public void testDetmVfModuleTarget() {
793 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
794 event.getAai().clear();
795 event.getAai().putAll(Map.of(ControlLoopOperationManager2.VSERVER_VSERVER_NAME, MY_TARGET));
796 assertNull(mgr.detmTarget());
797 assertEquals(MY_TARGET, mgr.getTargetEntity());
800 event.setTarget(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID);
801 event.getAai().clear();
802 event.getAai().putAll(Map.of(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID, MY_TARGET));
803 assertNull(mgr.detmTarget());
804 assertEquals(MY_TARGET, mgr.getTargetEntity());
807 event.setTarget(MISMATCH);
808 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
809 .withMessage("Target does not match target type");
811 // missing enrichment data
812 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
813 event.getAai().clear();
814 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
815 .withMessage("Enrichment data is missing " + ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
818 event.setTarget(null);
819 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("Target is null");
823 public void testDetmVnfName() {
825 assertNull(mgr.detmTarget());
826 assertEquals(MY_TARGET, mgr.getTargetEntity());
828 // force it to be gotten from the CQ data
829 event.getAai().clear();
830 assertNull(mgr.detmTarget());
831 assertEquals(MY_VNF_ID, mgr.getTargetEntity());
835 public void testExtractVnfFromCq() {
836 // force it to be gotten from the CQ data
838 event.getAai().clear();
840 // missing vnf id in CQ data
841 when(vnf.getVnfId()).thenReturn(null);
842 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("No vnf-id found");
844 // missing default vnf in CQ data
845 when(cqdata.getDefaultGenericVnf()).thenReturn(null);
846 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("No vnf-id found");
850 public void testGetState_testGetActor_testGetOperation() {
851 assertEquals(ControlLoopOperationManager2.State.ACTIVE, mgr.getState());
852 assertEquals(POLICY_ACTOR, mgr.getActor());
853 assertEquals(POLICY_OPERATION, mgr.getOperation());
857 public void testToString() {
858 assertThat(mgr.toString()).contains("state").contains("requestId").contains("policyId").contains("attempts");
862 * Runs a cycle, from start to completion.
864 private void runCyle() {
865 mgr.start(REMAINING_MS);
867 lockFuture.complete(new OperationOutcome());
874 assertTrue(mgr.nextStep());
877 assertTrue(mgr.nextStep());
880 assertTrue(mgr.nextStep());
883 assertFalse(mgr.nextStep());
887 * Runs everything until the executor queue is empty.
889 private void runToCompletion() {
890 assertTrue(executor.runAll(MAX_RUN));
894 * Generates a failure outcome for the lock, and invokes the callbacks.
896 * @return the generated outcome
898 private OperationOutcome genLockFailure() {
899 OperationOutcome outcome = new OperationOutcome();
900 outcome.setActor(ControlLoopOperationManager2.LOCK_ACTOR);
901 outcome.setOperation(ControlLoopOperationManager2.LOCK_OPERATION);
902 outcome.setResult(PolicyResult.FAILURE);
903 outcome.setStart(Instant.now());
904 outcome.setEnd(Instant.now());
905 outcome.setFinalOutcome(true);
907 verify(mgrctx).requestLock(eq(MY_TARGET), lockCallback.capture());
908 lockCallback.getValue().accept(outcome);
910 lockFuture.complete(outcome);
916 * Generates an outcome for the guard, and invokes the callbacks.
918 * @return the generated outcome
920 private OperationOutcome genGuardOutcome() {
921 return genGuardOutcome(true);
925 * Generates an outcome for the guard, and invokes the callbacks.
927 * @param permit {@code true} if the guard should be permitted, {@code false} if
929 * @return the generated outcome
931 private OperationOutcome genGuardOutcome(boolean permit) {
932 OperationOutcome outcome = mgr.getParams().makeOutcome();
933 outcome.setActor(GuardActorServiceProvider.NAME);
934 outcome.setOperation(GuardOperation.NAME);
935 outcome.setStart(Instant.now());
936 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
939 outcome.setResult(PolicyResult.FAILURE);
942 outcome.setEnd(Instant.now());
943 mgr.getParams().callbackCompleted(outcome);
949 * Generates an outcome for the operation, itself, and invokes the callbacks.
951 * @return the generated outcome
953 private OperationOutcome genOpOutcome() {
954 return genOpOutcome(true);
958 * Generates an outcome for the operation, itself, and invokes the callbacks.
960 * @param success {@code true} if the outcome should be a success, {@code false} if a
962 * @return the generated outcome
964 private OperationOutcome genOpOutcome(boolean success) {
965 OperationOutcome outcome = mgr.getParams().makeOutcome();
966 outcome.setStart(Instant.now());
967 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
970 outcome.setFinalOutcome(true);
972 outcome.setResult(PolicyResult.FAILURE);
975 outcome.setEnd(Instant.now());
976 mgr.getParams().callbackCompleted(outcome);
982 * Configures the data for a PNF target.
984 private void setTargetPnf() {
985 event.setTarget(ControlLoopOperationManager2.PNF_NAME);
986 event.getAai().clear();
987 event.getAai().putAll(Map.of(ControlLoopOperationManager2.PNF_NAME, MY_TARGET));
989 target.setType(TargetType.PNF);
993 * Configures the data for a VNF-NAME target.
995 private void setTargetVnfName() {
996 event.setTarget(ControlLoopOperationManager2.GENERIC_VNF_VNF_NAME);
997 event.getAai().clear();
998 event.getAai().putAll(Map.of(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID, MY_TARGET));
1000 target.setType(TargetType.VNF);
1003 private void checkResp(OperationOutcome outcome, String expectedPayload) {
1004 ControlLoopResponse resp = mgr.makeControlLoopResponse(outcome);
1005 assertNotNull(resp);
1006 assertEquals(REQ_ID, resp.getRequestId());
1007 assertEquals(expectedPayload, resp.getPayload());
1010 private void verifyDb(int nrecords, PolicyResult expectedResult, String expectedMsg) {
1011 ArgumentCaptor<String> entityCaptor = ArgumentCaptor.forClass(String.class);
1012 ArgumentCaptor<ControlLoopOperation> opCaptor = ArgumentCaptor.forClass(ControlLoopOperation.class);
1013 verify(dataMgr, times(nrecords)).store(any(), any(), entityCaptor.capture(), opCaptor.capture());
1015 assertEquals(MY_TARGET, entityCaptor.getValue());
1017 ControlLoopOperation oper = opCaptor.getValue();
1019 assertEquals(expectedResult.toString(), oper.getOutcome());
1020 assertEquals(expectedMsg, oper.getMessage());