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.VirtualControlLoopEvent;
58 import org.onap.policy.controlloop.actor.guard.GuardActorServiceProvider;
59 import org.onap.policy.controlloop.actor.guard.GuardOperation;
60 import org.onap.policy.controlloop.actorserviceprovider.ActorService;
61 import org.onap.policy.controlloop.actorserviceprovider.Operation;
62 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
63 import org.onap.policy.controlloop.actorserviceprovider.Operator;
64 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
65 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
66 import org.onap.policy.controlloop.actorserviceprovider.spi.Actor;
67 import org.onap.policy.controlloop.ophistory.OperationHistoryDataManager;
68 import org.onap.policy.controlloop.policy.Policy;
69 import org.onap.policy.controlloop.policy.PolicyResult;
70 import org.onap.policy.controlloop.policy.Target;
71 import org.onap.policy.controlloop.policy.TargetType;
73 public class ControlLoopOperationManager2Test {
74 private static final UUID REQ_ID = UUID.randomUUID();
75 private static final String MISMATCH = "mismatch";
76 private static final String POLICY_ID = "my-policy";
77 private static final String POLICY_ACTOR = "my-actor";
78 private static final String POLICY_OPERATION = "my-operation";
79 private static final String OTHER_ACTOR = "another-actor";
80 private static final String MY_TARGET = "my-target";
81 private static final String MY_VNF_ID = "my-vnf-id";
82 private static final String PAYLOAD_KEY = "payload-key";
83 private static final String PAYLOAD_VALUE = "payload-value";
84 private static final long REMAINING_MS = 5000;
85 private static final int MAX_RUN = 100;
86 private static final Integer POLICY_RETRY = 3;
87 private static final Integer POLICY_TIMEOUT = 20;
88 private static final IllegalArgumentException EXPECTED_EXCEPTION =
89 new IllegalArgumentException("expected exception");
92 private ArgumentCaptor<Consumer<OperationOutcome>> lockCallback;
95 private OperationHistoryDataManager dataMgr;
97 private ManagerContext mgrctx;
99 private Operator policyOperator;
101 private Operation policyOperation;
103 private Actor policyActor;
105 private ActorService actors;
107 private AaiCqResponse cqdata;
109 private GenericVnf vnf;
111 private CompletableFuture<OperationOutcome> lockFuture;
112 private CompletableFuture<OperationOutcome> policyFuture;
113 private Target target;
114 private Map<String, String> payload;
115 private Policy policy;
116 private VirtualControlLoopEvent event;
117 private ControlLoopEventContext context;
118 private PseudoExecutor executor;
119 private ControlLoopOperationManager2 mgr;
125 public void setUp() {
126 MockitoAnnotations.initMocks(this);
128 lockFuture = new CompletableFuture<>();
129 policyFuture = new CompletableFuture<>();
131 when(mgrctx.getActorService()).thenReturn(actors);
132 when(mgrctx.getDataManager()).thenReturn(dataMgr);
133 when(mgrctx.requestLock(any(), any())).thenReturn(lockFuture);
135 // configure policy operation
136 when(actors.getActor(POLICY_ACTOR)).thenReturn(policyActor);
137 when(policyActor.getOperator(POLICY_OPERATION)).thenReturn(policyOperator);
138 when(policyOperator.buildOperation(any())).thenReturn(policyOperation);
139 when(policyOperation.start()).thenReturn(policyFuture);
141 when(vnf.getVnfId()).thenReturn(MY_VNF_ID);
142 when(cqdata.getDefaultGenericVnf()).thenReturn(vnf);
144 target = new Target();
145 target.setType(TargetType.VM);
147 payload = Map.of(PAYLOAD_KEY, PAYLOAD_VALUE);
149 policy = new Policy();
150 policy.setId(POLICY_ID);
151 policy.setActor(POLICY_ACTOR);
152 policy.setRecipe(POLICY_OPERATION);
153 policy.setTarget(target);
154 policy.setPayload(payload);
155 policy.setRetry(POLICY_RETRY);
156 policy.setTimeout(POLICY_TIMEOUT);
158 event = new VirtualControlLoopEvent();
159 event.setRequestId(REQ_ID);
160 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
161 event.setAai(new TreeMap<>(Map.of(ControlLoopOperationManager2.VSERVER_VSERVER_NAME, MY_TARGET)));
163 context = new ControlLoopEventContext(event);
164 context.setProperty(AaiCqResponse.CONTEXT_KEY, cqdata);
166 executor = new PseudoExecutor();
168 mgr = new ControlLoopOperationManager2(mgrctx, context, policy, executor);
172 public void testStart() {
173 mgr.start(REMAINING_MS);
175 // should have determined the target entity by now
176 assertEquals(MY_TARGET, mgr.getTargetEntity());
178 verify(mgrctx).requestLock(eq(MY_TARGET), any());
180 lockFuture.complete(new OperationOutcome());
182 policyFuture.complete(genOpOutcome());
185 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
187 assertTrue(mgr.nextStep());
188 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
190 assertTrue(mgr.nextStep());
191 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
193 assertTrue(mgr.nextStep());
194 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
196 assertFalse(mgr.nextStep());
198 OperationOutcome outcome = mgr.getOutcomes().peek();
199 assertEquals(PolicyResult.SUCCESS, outcome.getResult());
200 assertTrue(outcome.isFinalOutcome());
202 verify(mgrctx, times(4)).updated(mgr);
206 * Tests start() when detmTarget() (i.e., the first task) throws an exception.
209 public void testStartDetmTargetException() {
210 policy.setTarget(new Target());
211 mgr.start(REMAINING_MS);
215 assertFalse(mgr.nextStep());
216 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
218 // should have called update() for operation-start, but not for any nextStep()
219 verify(mgrctx).updated(mgr);
223 * Tests start() when a subsequent task throws an exception.
226 public void testStartException() {
227 when(policyOperation.start()).thenThrow(EXPECTED_EXCEPTION);
229 mgr.start(REMAINING_MS);
231 lockFuture.complete(new OperationOutcome());
234 assertFalse(mgr.nextStep());
235 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
237 // should have called update() for operation-start, but not for any nextStep()
238 verify(mgrctx).updated(mgr);
242 * Tests start() when the control loop times out.
245 public void testStartClTimeout_testHandleTimeout() throws InterruptedException {
246 // catch the callback when it times out
247 CountDownLatch updatedLatch = new CountDownLatch(1);
249 updatedLatch.countDown();
251 }).when(mgrctx).updated(any());
253 long tstart = System.currentTimeMillis();
255 // give it a short timeout
258 assertTrue(updatedLatch.await(5, TimeUnit.SECONDS));
259 assertTrue(System.currentTimeMillis() - tstart >= 100);
261 // don't generate any responses
264 // wait for the future to be canceled, via a background thread
265 CountDownLatch futureLatch = new CountDownLatch(1);
266 mgr.getFuture().whenComplete((unused, thrown) -> futureLatch.countDown());
267 assertTrue(futureLatch.await(5, TimeUnit.SECONDS));
269 // lock should have been canceled
270 assertTrue(mgr.getFuture().isCancelled());
272 assertFalse(mgr.nextStep());
273 assertEquals(ControlLoopOperationManager2.State.CONTROL_LOOP_TIMEOUT, mgr.getState());
275 // should have called update() for operation-start, but not for any nextStep()
276 verify(mgrctx).updated(mgr);
278 // should not have tried to store anything in the DB
279 verify(dataMgr, never()).store(any(), any(), any());
283 public void testStartOperation() {
284 mgr.start(REMAINING_MS);
286 lockFuture.complete(new OperationOutcome());
290 verify(policyOperation).start();
292 ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
293 verify(policyOperator).buildOperation(captor.capture());
295 ControlLoopOperationParams params = captor.getValue();
297 assertNotNull(params);
298 assertEquals(POLICY_ACTOR, params.getActor());
299 assertSame(actors, params.getActorService());
300 assertNotNull(params.getCompleteCallback());
301 assertSame(context, params.getContext());
302 assertSame(executor, params.getExecutor());
303 assertEquals(POLICY_OPERATION, params.getOperation());
304 assertEquals(payload, params.getPayload());
305 assertSame(REQ_ID, params.getRequestId());
306 assertSame(POLICY_RETRY, params.getRetry());
307 assertNotNull(params.getStartCallback());
308 assertSame(target, params.getTarget());
309 assertEquals(MY_TARGET, params.getTargetEntity());
310 assertSame(POLICY_TIMEOUT, params.getTimeoutSec());
314 public void testStartOperationNullPayload() {
315 policy.setPayload(null);
316 mgr.start(REMAINING_MS);
318 lockFuture.complete(new OperationOutcome());
322 verify(policyOperation).start();
324 ArgumentCaptor<ControlLoopOperationParams> captor = ArgumentCaptor.forClass(ControlLoopOperationParams.class);
325 verify(policyOperator).buildOperation(captor.capture());
327 ControlLoopOperationParams params = captor.getValue();
329 assertNotNull(params);
330 assertEquals(POLICY_ACTOR, params.getActor());
331 assertSame(actors, params.getActorService());
332 assertNotNull(params.getCompleteCallback());
333 assertSame(context, params.getContext());
334 assertSame(executor, params.getExecutor());
335 assertEquals(POLICY_OPERATION, params.getOperation());
336 assertTrue(params.getPayload().isEmpty());
337 assertSame(REQ_ID, params.getRequestId());
338 assertSame(POLICY_RETRY, params.getRetry());
339 assertNotNull(params.getStartCallback());
340 assertSame(target, params.getTarget());
341 assertEquals(MY_TARGET, params.getTargetEntity());
342 assertSame(POLICY_TIMEOUT, params.getTimeoutSec());
346 public void testGetOperationMessage() {
348 assertNull(mgr.getOperationMessage());
351 assertThat(mgr.getOperationMessage()).contains("actor=my-actor").contains("operation=my-operation");
355 public void testGetOperationResult() {
357 assertNotNull(mgr.getOperationResult());
360 assertEquals(PolicyResult.SUCCESS, mgr.getOperationResult());
364 * Tests getOperationResult() when it ends in a failure.
367 public void testGetOperationResultFailure() {
368 mgr.start(REMAINING_MS);
373 assertEquals(PolicyResult.FAILURE_GUARD, mgr.getOperationResult());
377 * Tests handleException() when the exception is a "cancel".
380 public void testHandleExceptionCanceled() {
381 lockFuture.cancel(false);
383 mgr.start(REMAINING_MS);
387 assertTrue(mgr.nextStep());
388 assertEquals(ControlLoopOperationManager2.State.ACTIVE, mgr.getState());
392 public void testCancel() {
393 mgr.start(REMAINING_MS);
396 assertTrue(mgr.getFuture().isCancelled());
400 * Tests cancel() when the operation hasn't been started.
403 public void testCancelNotStarted() {
404 assertNull(mgr.getFuture());
407 assertNull(mgr.getFuture());
411 public void testLockUnavailable() {
412 mgr.start(REMAINING_MS);
416 // lock failure outcome
417 final OperationOutcome outcome = genLockFailure();
421 assertFalse(mgr.nextStep());
422 assertEquals(ControlLoopOperationManager2.State.LOCK_DENIED, mgr.getState());
424 assertEquals(outcome, mgr.getOutcomes().peek());
426 // should have called update() for operation-start, but not for any nextStep()
427 verify(mgrctx).updated(mgr);
431 * Tests onStart() and onComplete() with other actors.
434 public void testOnStart_testOnComplete() {
435 mgr.start(REMAINING_MS);
437 lockFuture.complete(new OperationOutcome());
440 // generate failure outcome for ANOTHER actor - should be ignored
441 OperationOutcome outcome = mgr.getParams().makeOutcome();
442 outcome.setActor(OTHER_ACTOR);
443 outcome.setResult(PolicyResult.FAILURE);
444 outcome.setStart(Instant.now());
445 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
447 outcome.setEnd(Instant.now());
448 mgr.getParams().callbackCompleted(outcome);
450 policyFuture.complete(genOpOutcome());
453 // should not include the other actor's outcome
454 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
456 assertTrue(mgr.nextStep());
457 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
459 assertTrue(mgr.nextStep());
460 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
462 assertTrue(mgr.nextStep());
463 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
465 assertFalse(mgr.nextStep());
467 assertEquals(PolicyResult.SUCCESS, mgr.getOutcomes().peek().getResult());
469 verify(mgrctx, times(4)).updated(mgr);
473 public void testNextStep() {
474 mgr.start(REMAINING_MS);
476 // only do the lock and the guard
477 lockFuture.complete(new OperationOutcome());
481 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
483 assertTrue(mgr.nextStep());
484 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
486 assertTrue(mgr.nextStep());
487 assertTrue(mgr.nextStep());
489 verify(mgrctx, times(2)).updated(mgr);
493 * Tests processOutcome() when the lock is denied.
496 public void testProcessOutcomeLockDenied() {
497 mgr.start(REMAINING_MS);
499 // unavailable from the start => "denied"
504 assertEquals(ControlLoopOperationManager2.State.LOCK_DENIED, mgr.getState());
506 assertFalse(mgr.nextStep());
507 verify(mgrctx).updated(mgr);
509 verifyDb(1, PolicyResult.FAILURE_GUARD, "Operation denied by Lock");
513 * Tests processOutcome() when the lock is lost.
516 public void testProcessOutcomeLockLost() {
517 mgr.start(REMAINING_MS);
519 // indicate lock success initially
520 lockFuture.complete(new OperationOutcome());
525 // now generate a lock failure => "lost"
530 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
532 assertTrue(mgr.nextStep());
533 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
535 assertTrue(mgr.nextStep());
536 assertEquals(ControlLoopOperationManager2.State.LOCK_LOST, mgr.getState());
538 assertFalse(mgr.nextStep());
539 verify(mgrctx, times(3)).updated(mgr);
541 verifyDb(1, PolicyResult.FAILURE, "Operation aborted by Lock");
545 * Tests processOutcome() when the guard is permitted.
548 public void testProcessOutcomeGuardPermit() {
549 mgr.start(REMAINING_MS);
551 lockFuture.complete(new OperationOutcome());
556 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
558 assertTrue(mgr.nextStep());
559 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
561 assertTrue(mgr.nextStep());
562 verify(mgrctx, times(2)).updated(mgr);
564 verify(dataMgr, never()).store(any(), any(), any());
568 * Tests processOutcome() when the guard is permitted.
571 public void testProcessOutcomeGuardDenied() {
572 mgr.start(REMAINING_MS);
574 lockFuture.complete(new OperationOutcome());
575 genGuardOutcome(false);
579 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
581 assertTrue(mgr.nextStep());
582 assertEquals(ControlLoopOperationManager2.State.GUARD_DENIED, mgr.getState());
584 assertFalse(mgr.nextStep());
585 verify(mgrctx, times(2)).updated(mgr);
587 verifyDb(1, PolicyResult.FAILURE_GUARD, "Operation denied by Guard");
591 * Tests processOutcome() when the operation is a success.
594 public void testProcessOutcomeOperSuccess() {
595 mgr.start(REMAINING_MS);
597 lockFuture.complete(new OperationOutcome());
603 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
605 assertTrue(mgr.nextStep());
606 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
608 assertTrue(mgr.nextStep());
609 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
611 assertTrue(mgr.nextStep());
612 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
614 assertFalse(mgr.nextStep());
615 verify(mgrctx, times(4)).updated(mgr);
617 verifyDb(1, PolicyResult.SUCCESS, null);
621 * Tests processOutcome() when the operation is a failure.
624 public void testProcessOutcomeOperFailure() {
625 mgr.start(REMAINING_MS);
627 lockFuture.complete(new OperationOutcome());
633 assertEquals(ControlLoopOperationManager2.State.GUARD_STARTED, mgr.getState());
635 assertTrue(mgr.nextStep());
636 assertEquals(ControlLoopOperationManager2.State.GUARD_PERMITTED, mgr.getState());
638 assertTrue(mgr.nextStep());
639 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
641 assertTrue(mgr.nextStep());
642 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
643 verifyDb(1, PolicyResult.FAILURE, null);
645 assertThat(mgr.toString()).contains("attempts=1");
651 assertTrue(mgr.nextStep());
652 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
654 assertTrue(mgr.nextStep());
655 assertEquals(ControlLoopOperationManager2.State.OPERATION_FAILURE, mgr.getState());
656 verifyDb(2, PolicyResult.FAILURE, null);
658 assertThat(mgr.toString()).contains("attempts=2");
660 // and finally a success
663 assertTrue(mgr.nextStep());
664 assertEquals(ControlLoopOperationManager2.State.OPERATION_STARTED, mgr.getState());
666 assertTrue(mgr.nextStep());
667 assertEquals(ControlLoopOperationManager2.State.OPERATION_SUCCESS, mgr.getState());
668 verifyDb(3, PolicyResult.SUCCESS, null);
670 assertThat(mgr.toString()).contains("attempts=3");
672 assertFalse(mgr.nextStep());
673 verify(mgrctx, times(8)).updated(mgr);
677 public void testGetOperationHistory() {
679 assertNull(mgr.getOperationHistory());
682 assertThat(mgr.getOperationHistory()).contains("actor=my-actor").contains("operation=my-operation")
683 .contains("outcome=Success");
687 public void testGetHistory() {
689 assertEquals(0, mgr.getHistory().size());
692 assertEquals(1, mgr.getHistory().size());
696 public void testDetmTargetVm() {
697 target.setType(TargetType.VM);
698 assertNull(mgr.detmTarget());
699 assertEquals(MY_TARGET, mgr.getTargetEntity());
701 target.setType(TargetType.VNF);
702 assertNull(mgr.detmTarget());
703 assertEquals(MY_TARGET, mgr.getTargetEntity());
705 target.setType(TargetType.VFMODULE);
706 assertNull(mgr.detmTarget());
707 assertEquals(MY_TARGET, mgr.getTargetEntity());
710 target.setType(TargetType.VFC);
711 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
712 .withMessage("The target type is not supported");
715 target.setType(null);
716 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("The target type is null");
719 policy.setTarget(null);
720 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("The target is null");
724 public void testDetmPnfTarget() {
726 assertNull(mgr.detmTarget());
727 assertEquals(MY_TARGET, mgr.getTargetEntity());
729 // missing enrichment data
730 event.getAai().clear();
731 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
732 .withMessage("AAI section is missing " + ControlLoopOperationManager2.PNF_NAME);
735 event.setTarget(MISMATCH);
736 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
737 .withMessage("Target does not match target type");
741 public void testDetmVfModuleTarget() {
743 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
744 event.getAai().clear();
745 event.getAai().putAll(Map.of(ControlLoopOperationManager2.VSERVER_VSERVER_NAME, MY_TARGET));
746 assertNull(mgr.detmTarget());
747 assertEquals(MY_TARGET, mgr.getTargetEntity());
750 event.setTarget(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID);
751 event.getAai().clear();
752 event.getAai().putAll(Map.of(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID, MY_TARGET));
753 assertNull(mgr.detmTarget());
754 assertEquals(MY_TARGET, mgr.getTargetEntity());
757 event.setTarget(MISMATCH);
758 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
759 .withMessage("Target does not match target type");
761 // missing enrichment data
762 event.setTarget(ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
763 event.getAai().clear();
764 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget())
765 .withMessage("Enrichment data is missing " + ControlLoopOperationManager2.VSERVER_VSERVER_NAME);
768 event.setTarget(null);
769 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("Target is null");
773 public void testDetmVnfName() {
775 assertNull(mgr.detmTarget());
776 assertEquals(MY_TARGET, mgr.getTargetEntity());
778 // force it to be gotten from the CQ data
779 event.getAai().clear();
780 assertNull(mgr.detmTarget());
781 assertEquals(MY_VNF_ID, mgr.getTargetEntity());
785 public void testExtractVnfFromCq() {
786 // force it to be gotten from the CQ data
788 event.getAai().clear();
790 // missing vnf id in CQ data
791 when(vnf.getVnfId()).thenReturn(null);
792 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("No vnf-id found");
794 // missing default vnf in CQ data
795 when(cqdata.getDefaultGenericVnf()).thenReturn(null);
796 assertThatIllegalArgumentException().isThrownBy(() -> mgr.detmTarget()).withMessage("No vnf-id found");
800 public void testGetState_testGetActor_testGetOperation() {
801 assertEquals(ControlLoopOperationManager2.State.ACTIVE, mgr.getState());
802 assertEquals(POLICY_ACTOR, mgr.getActor());
803 assertEquals(POLICY_OPERATION, mgr.getOperation());
807 public void testToString() {
808 assertThat(mgr.toString()).contains("state").contains("requestId").contains("policyId").contains("attempts");
812 * Runs a cycle, from start to completion.
814 private void runCyle() {
815 mgr.start(REMAINING_MS);
817 lockFuture.complete(new OperationOutcome());
824 assertTrue(mgr.nextStep());
827 assertTrue(mgr.nextStep());
830 assertTrue(mgr.nextStep());
833 assertFalse(mgr.nextStep());
837 * Runs everything until the executor queue is empty.
839 private void runToCompletion() {
840 assertTrue(executor.runAll(MAX_RUN));
844 * Generates a failure outcome for the lock, and invokes the callbacks.
846 * @return the generated outcome
848 private OperationOutcome genLockFailure() {
849 OperationOutcome outcome = new OperationOutcome();
850 outcome.setActor(ControlLoopOperationManager2.LOCK_ACTOR);
851 outcome.setOperation(ControlLoopOperationManager2.LOCK_OPERATION);
852 outcome.setResult(PolicyResult.FAILURE);
853 outcome.setStart(Instant.now());
854 outcome.setEnd(Instant.now());
855 outcome.setFinalOutcome(true);
857 verify(mgrctx).requestLock(eq(MY_TARGET), lockCallback.capture());
858 lockCallback.getValue().accept(outcome);
860 lockFuture.complete(outcome);
866 * Generates an outcome for the guard, and invokes the callbacks.
868 * @return the generated outcome
870 private OperationOutcome genGuardOutcome() {
871 return genGuardOutcome(true);
875 * Generates an outcome for the guard, and invokes the callbacks.
877 * @param permit {@code true} if the guard should be permitted, {@code false} if
879 * @return the generated outcome
881 private OperationOutcome genGuardOutcome(boolean permit) {
882 OperationOutcome outcome = mgr.getParams().makeOutcome();
883 outcome.setActor(GuardActorServiceProvider.NAME);
884 outcome.setOperation(GuardOperation.NAME);
885 outcome.setStart(Instant.now());
886 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
889 outcome.setResult(PolicyResult.FAILURE);
892 outcome.setEnd(Instant.now());
893 mgr.getParams().callbackCompleted(outcome);
899 * Generates an outcome for the operation, itself, and invokes the callbacks.
901 * @return the generated outcome
903 private OperationOutcome genOpOutcome() {
904 return genOpOutcome(true);
908 * Generates an outcome for the operation, itself, and invokes the callbacks.
910 * @param success {@code true} if the outcome should be a success, {@code false} if a
912 * @return the generated outcome
914 private OperationOutcome genOpOutcome(boolean success) {
915 OperationOutcome outcome = mgr.getParams().makeOutcome();
916 outcome.setStart(Instant.now());
917 mgr.getParams().callbackStarted(new OperationOutcome(outcome));
920 outcome.setFinalOutcome(true);
922 outcome.setResult(PolicyResult.FAILURE);
925 outcome.setEnd(Instant.now());
926 mgr.getParams().callbackCompleted(outcome);
932 * Configures the data for a PNF target.
934 private void setTargetPnf() {
935 event.setTarget(ControlLoopOperationManager2.PNF_NAME);
936 event.getAai().clear();
937 event.getAai().putAll(Map.of(ControlLoopOperationManager2.PNF_NAME, MY_TARGET));
939 target.setType(TargetType.PNF);
943 * Configures the data for a VNF-NAME target.
945 private void setTargetVnfName() {
946 event.setTarget(ControlLoopOperationManager2.GENERIC_VNF_VNF_NAME);
947 event.getAai().clear();
948 event.getAai().putAll(Map.of(ControlLoopOperationManager2.GENERIC_VNF_VNF_ID, MY_TARGET));
950 target.setType(TargetType.VNF);
953 private void verifyDb(int nrecords, PolicyResult expectedResult, String expectedMsg) {
954 ArgumentCaptor<ControlLoopOperation> captor = ArgumentCaptor.forClass(ControlLoopOperation.class);
955 verify(dataMgr, times(nrecords)).store(any(), any(), captor.capture());
957 ControlLoopOperation oper = captor.getValue();
959 assertEquals(expectedResult.toString(), oper.getOutcome());
960 assertEquals(expectedMsg, oper.getMessage());