2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.nio.charset.StandardCharsets;
35 import java.time.Instant;
36 import java.util.HashMap;
37 import java.util.UUID;
39 import org.apache.commons.io.IOUtils;
40 import org.junit.AfterClass;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.policy.aai.util.AAIException;
44 import org.onap.policy.appc.CommonHeader;
45 import org.onap.policy.appc.Response;
46 import org.onap.policy.appc.ResponseCode;
47 import org.onap.policy.appc.ResponseStatus;
48 import org.onap.policy.appclcm.LCMCommonHeader;
49 import org.onap.policy.appclcm.LCMRequest;
50 import org.onap.policy.appclcm.LCMRequestWrapper;
51 import org.onap.policy.appclcm.LCMResponse;
52 import org.onap.policy.appclcm.LCMResponseWrapper;
53 import org.onap.policy.controlloop.ControlLoopEventStatus;
54 import org.onap.policy.controlloop.ControlLoopNotificationType;
55 import org.onap.policy.controlloop.VirtualControlLoopEvent;
56 import org.onap.policy.controlloop.ControlLoopException;
57 import org.onap.policy.controlloop.ControlLoopTargetType;
58 import org.onap.policy.controlloop.Util;
59 import org.onap.policy.controlloop.VirtualControlLoopNotification;
60 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
61 import org.onap.policy.controlloop.policy.Policy;
62 import org.onap.policy.controlloop.policy.PolicyResult;
63 import org.onap.policy.controlloop.policy.Target;
64 import org.onap.policy.controlloop.policy.TargetType;
65 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
66 import org.onap.policy.drools.http.server.HttpServletServer;
67 import org.onap.policy.drools.system.PolicyEngine;
68 import org.onap.policy.so.SOResponse;
69 import org.onap.policy.so.SOResponseWrapper;
70 import org.onap.policy.vfc.VFCResponse;
71 import org.onap.policy.vfc.VFCResponseDescriptor;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
75 public class ControlLoopOperationManagerTest {
76 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
79 private static VirtualControlLoopEvent onset;
82 onset = new VirtualControlLoopEvent();
83 onset.setRequestID(UUID.randomUUID());
84 onset.setTarget("generic-vnf.vnf-name");
85 onset.setTargetType(ControlLoopTargetType.VNF);
86 onset.setClosedLoopAlarmStart(Instant.now());
87 onset.setAAI(new HashMap<>());
88 onset.getAAI().put("generic-vnf.vnf-name", "testTriggerSource");
89 onset.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
91 /* Set environment properties */
92 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
93 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
94 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
98 public static void setUpSimulator() {
100 org.onap.policy.simulators.Util.buildAaiSim();
101 } catch (Exception e) {
102 fail(e.getMessage());
107 public static void tearDownSimulator() {
108 HttpServletServer.factory.destroy();
112 public void testRetriesFail() {
114 // Load up the policy
116 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
117 onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName());
120 // Create a processor
122 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
124 // create the manager
126 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
127 VirtualControlLoopNotification notification = eventManager.activate(onset);
129 assertNotNull(notification);
130 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
132 ControlLoopEventManager.NEW_EVENT_STATUS status = null;
134 status = eventManager.onNewEvent(onset);
135 } catch (AAIException e) {
136 logger.warn(e.toString());
137 fail("A&AI Query Failed");
139 assertNotNull(status);
140 assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
142 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
143 logger.debug("{}",manager);
147 assertFalse(manager.isOperationComplete());
148 assertFalse(manager.isOperationRunning());
152 Object request = manager.startOperation(onset);
153 logger.debug("{}",manager);
154 assertNotNull(request);
155 assertTrue(request instanceof LCMRequestWrapper);
156 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
157 LCMRequest appcRequest = dmaapRequest.getBody();
158 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
159 assertFalse(manager.isOperationComplete());
160 assertTrue(manager.isOperationRunning());
164 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
165 LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
166 appcResponse.getStatus().setCode(100);
167 appcResponse.getStatus().setMessage("ACCEPT");
168 dmaapResponse.setBody(appcResponse);
172 PolicyResult result = manager.onResponse(dmaapResponse);
173 logger.debug("{}",manager);
174 assertTrue(result == null);
175 assertFalse(manager.isOperationComplete());
176 assertTrue(manager.isOperationRunning());
178 // Now we are going to Fail it
180 appcResponse = new LCMResponse(appcRequest);
181 appcResponse.getStatus().setCode(401);
182 appcResponse.getStatus().setMessage("AppC failed for some reason");
183 dmaapResponse.setBody(appcResponse);
184 result = manager.onResponse(dmaapResponse);
185 logger.debug("{}",manager);
186 assertTrue(result.equals(PolicyResult.FAILURE));
187 assertFalse(manager.isOperationComplete());
188 assertFalse(manager.isOperationRunning());
192 request = manager.startOperation(onset);
193 logger.debug("{}",manager);
194 assertNotNull(request);
195 assertTrue(request instanceof LCMRequestWrapper);
196 dmaapRequest = (LCMRequestWrapper) request;
197 appcRequest = dmaapRequest.getBody();
198 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
199 assertFalse(manager.isOperationComplete());
200 assertTrue(manager.isOperationRunning());
204 appcResponse = new LCMResponse((LCMRequest) appcRequest);
205 logger.debug("{}",manager);
206 appcResponse.getStatus().setCode(100);
207 appcResponse.getStatus().setMessage("ACCEPT");
208 dmaapResponse.setBody(appcResponse);
212 result = manager.onResponse(dmaapResponse);
213 logger.debug("{}",manager);
214 assertTrue(result == null);
215 assertFalse(manager.isOperationComplete());
216 assertTrue(manager.isOperationRunning());
218 // Now we are going to Fail it
220 appcResponse = new LCMResponse((LCMRequest) appcRequest);
221 appcResponse.getStatus().setCode(401);
222 appcResponse.getStatus().setMessage("AppC failed for some reason");
223 dmaapResponse.setBody(appcResponse);
224 result = manager.onResponse(dmaapResponse);
225 logger.debug("{}",manager);
226 assertTrue(result.equals(PolicyResult.FAILURE));
228 // Should be complete now
230 assertTrue(manager.isOperationComplete());
231 assertFalse(manager.isOperationRunning());
232 assertNotNull(manager.getOperationResult());
233 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
234 assertTrue(manager.getHistory().size() == 2);
235 } catch (ControlLoopException | AAIException e) {
236 fail(e.getMessage());
241 public void testTimeout() {
243 // Load up the policy
245 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
246 onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName());
249 // Create a processor
251 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
253 // create the manager
255 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
256 VirtualControlLoopNotification notification = eventManager.activate(onset);
258 assertNotNull(notification);
259 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
261 ControlLoopEventManager.NEW_EVENT_STATUS status = null;
263 status = eventManager.onNewEvent(onset);
264 } catch (AAIException e) {
265 logger.warn(e.toString());
266 fail("A&AI Query Failed");
268 assertNotNull(status);
269 assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
271 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
275 logger.debug("{}",manager);
276 assertFalse(manager.isOperationComplete());
277 assertFalse(manager.isOperationRunning());
281 Object request = manager.startOperation(onset);
282 logger.debug("{}",manager);
283 assertNotNull(request);
284 assertTrue((request) instanceof LCMRequestWrapper);
285 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
286 LCMRequest appcRequest = dmaapRequest.getBody();
287 assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
288 assertFalse(manager.isOperationComplete());
289 assertTrue(manager.isOperationRunning());
293 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
294 LCMResponse appcResponse = new LCMResponse(appcRequest);
295 dmaapResponse.setBody(appcResponse);
296 appcResponse.getStatus().setCode(100);
297 appcResponse.getStatus().setMessage("ACCEPT");
301 PolicyResult result = manager.onResponse(dmaapResponse);
302 logger.debug("{}",manager);
303 assertTrue(result == null);
304 assertFalse(manager.isOperationComplete());
305 assertTrue(manager.isOperationRunning());
307 // Now we are going to simulate Timeout
309 manager.setOperationHasTimedOut();
310 logger.debug("{}",manager);
311 assertTrue(manager.isOperationComplete());
312 assertFalse(manager.isOperationRunning());
313 assertTrue(manager.getHistory().size() == 1);
314 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
316 // Now we are going to Fail the previous request
318 appcResponse = new LCMResponse(appcRequest);
319 appcResponse.getStatus().setCode(401);
320 appcResponse.getStatus().setMessage("AppC failed for some reason");
321 dmaapResponse.setBody(appcResponse);
322 result = manager.onResponse(dmaapResponse);
323 logger.debug("{}",manager);
327 assertTrue(manager.isOperationComplete());
328 assertFalse(manager.isOperationRunning());
329 assertTrue(manager.getHistory().size() == 1);
330 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
331 } catch (ControlLoopException | AAIException e) {
332 fail(e.getMessage());
337 public void testMethods() throws IOException, ControlLoopException, AAIException {
338 InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
339 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
341 UUID requestId = UUID.randomUUID();
342 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
343 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
344 onsetEvent.setRequestID(requestId);
345 onsetEvent.setTarget("generic-vnf.vnf-id");
346 onsetEvent.setClosedLoopAlarmStart(Instant.now());
347 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
348 onsetEvent.setAAI(new HashMap<>());
349 onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
351 ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
352 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
353 assertNotNull(notification);
354 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
356 ControlLoopOperationManager clom = manager.processControlLoop();
358 assertNull(clom.getOperationResult());
360 clom.setEventManager(manager);
361 assertEquals(manager, clom.getEventManager());
363 assertNull(clom.getTargetEntity());
365 clom.setGuardApprovalStatus("WizardOKedIt");
366 assertEquals("WizardOKedIt", clom.getGuardApprovalStatus());
368 assertNull(clom.getOperationResult());
370 Policy policy = manager.getProcessor().getCurrentPolicy();
371 clom.getTarget(policy);
373 Target savedTarget = policy.getTarget();
374 policy.setTarget(null);
376 clom.getTarget(policy);
377 fail("test should throw an exception here");
378 } catch (Exception e) {
379 assertEquals("The target is null", e.getMessage());
382 policy.setTarget(new Target());
384 clom.getTarget(policy);
385 fail("test should throw an exception here");
386 } catch (Exception e) {
387 assertEquals("The target type is null", e.getMessage());
390 policy.setTarget(savedTarget);
392 policy.getTarget().setType(TargetType.PNF);
394 clom.getTarget(policy);
395 fail("test should throw an exception here");
396 } catch (Exception e) {
397 assertEquals("PNF target is not supported", e.getMessage());
400 onsetEvent.setTarget("Oz");
401 onsetEvent.getAAI().remove("generic-vnf.vnf-name");
402 onsetEvent.getAAI().remove("generic-vnf.vnf-id");
403 onsetEvent.getAAI().remove("vserver.vserver-name");
405 policy.getTarget().setType(TargetType.VNF);
407 clom.getTarget(policy);
408 fail("test should throw an exception here");
409 } catch (Exception e) {
410 assertEquals("Target does not match target type", e.getMessage());
413 onsetEvent.setTarget("vserver.vserver-name");
414 onsetEvent.getAAI().put("vserver.vserver-name", "OzVServer");
415 assertEquals("OzVServer", clom.getTarget(policy));
417 onsetEvent.getAAI().remove("vserver.vserver-name");
418 onsetEvent.setTarget("generic-vnf.vnf-id");
419 onsetEvent.getAAI().put("generic-vnf.vnf-id", "OzVNF");
420 assertEquals("OzVNF", clom.getTarget(policy));
422 onsetEvent.setTarget("generic-vnf.vnf-name");
423 assertEquals("OzVNF", clom.getTarget(policy));
425 manager.onNewEvent(onsetEvent);
427 onsetEvent.getAAI().remove("generic-vnf.vnf-id");
428 manager.getVnfResponse();
429 clom.getEventManager().getVnfResponse().setVnfID("generic-vnf.vnf-id");
430 assertEquals("generic-vnf.vnf-id", clom.getTarget(policy));
432 policy.getTarget().setType(TargetType.VFC);
434 clom.getTarget(policy);
435 fail("test should throw an exception here");
436 } catch (Exception e) {
437 assertEquals("The target type is not supported", e.getMessage());
440 assertEquals(Integer.valueOf(20), clom.getOperationTimeout());
442 assertEquals("20s", clom.getOperationTimeoutString(100));
444 assertEquals(null, clom.getOperationMessage());
445 assertEquals(null, clom.getOperationMessage("The Wizard Escaped"));
447 clom.startOperation(onsetEvent);
449 assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1", clom.getOperationMessage());
450 assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1, Guard result: The Wizard Escaped", clom.getOperationMessage("The Wizard Escaped"));
452 assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30));
454 clom.setOperationHasException("The Wizard is gone");
455 clom.setOperationHasGuardDeny();
459 public void testConstructor() throws IOException, ControlLoopException, AAIException {
460 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
461 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
463 UUID requestId = UUID.randomUUID();
464 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
465 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
466 onsetEvent.setRequestID(requestId);
467 onsetEvent.setTarget("generic-vnf.vnf-id");
468 onsetEvent.setClosedLoopAlarmStart(Instant.now());
469 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
470 onsetEvent.setAAI(new HashMap<>());
471 onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
473 ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
474 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
475 assertNotNull(notification);
476 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
478 Policy policy = manager.getProcessor().getCurrentPolicy();
479 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
482 policy.setRecipe("ModifyConfig");
483 policy.getTarget().setResourceID(UUID.randomUUID().toString());
485 new ControlLoopOperationManager(onsetEvent, policy, manager);
486 fail("test should throw an exception here");
487 } catch (Exception e) {
488 assertEquals("Target vnf-id could not be found", e.getMessage());
491 policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4");
492 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
495 policy.setActor("SO");
496 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
499 policy.setActor("VFC");
500 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
503 policy.setActor("Dorothy");
505 new ControlLoopOperationManager(onsetEvent, policy, manager);
506 fail("test should throw an exception here");
507 } catch (Exception e) {
508 assertEquals("ControlLoopEventManager: policy has an unknown actor.", e.getMessage());
513 public void testStartOperation() throws IOException, ControlLoopException, AAIException {
514 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
515 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
517 UUID requestId = UUID.randomUUID();
518 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
519 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
520 onsetEvent.setRequestID(requestId);
521 onsetEvent.setTarget("generic-vnf.vnf-id");
522 onsetEvent.setClosedLoopAlarmStart(Instant.now());
523 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
524 onsetEvent.setAAI(new HashMap<>());
525 onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
527 ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
528 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
529 assertNotNull(notification);
530 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
532 Policy policy = manager.getProcessor().getCurrentPolicy();
533 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
536 clom.startOperation(onsetEvent);
539 clom.startOperation(onsetEvent);
540 fail("test should throw an exception here");
541 } catch (Exception e) {
542 assertEquals("current operation is not null (an operation is already running)", e.getMessage());
545 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
547 String savedRecipe = policy.getRecipe();
548 policy.setRecipe("ModifyConfig");
549 policy.getTarget().setResourceID(UUID.randomUUID().toString());
550 clom.startOperation(onsetEvent);
551 policy.setRecipe(savedRecipe);
553 policy.setRetry(null);
554 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
556 clom.startOperation(onsetEvent);
557 clom.setOperationHasTimedOut();
558 assertTrue(clom.isOperationComplete());
560 clom.startOperation(onsetEvent);
561 fail("test should throw an exception here");
562 } catch (Exception e) {
563 assertEquals("current operation failed and retries are not allowed", e.getMessage());
567 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
569 clom.startOperation(onsetEvent);
570 clom.setOperationHasTimedOut();
571 assertTrue(clom.isOperationComplete());
573 clom.startOperation(onsetEvent);
574 fail("test should throw an exception here");
575 } catch (Exception e) {
576 assertEquals("current operation failed and retries are not allowed", e.getMessage());
580 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
582 clom.startOperation(onsetEvent);
583 clom.setOperationHasTimedOut();
584 clom.startOperation(onsetEvent);
585 clom.setOperationHasTimedOut();
586 assertTrue(clom.isOperationComplete());
588 clom.startOperation(onsetEvent);
589 fail("test should throw an exception here");
590 } catch (Exception e) {
591 assertEquals("current oepration has failed after 2 retries", e.getMessage());
594 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
596 policy.setActor("SO");
597 clom.startOperation(onsetEvent);
599 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
601 policy.setActor("VFC");
602 clom.startOperation(onsetEvent);
604 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
606 policy.setActor("Oz");
608 clom.startOperation(onsetEvent);
609 fail("test should throw an exception here");
610 } catch (Exception e) {
611 assertEquals("invalid actor Oz on policy", e.getMessage());
616 public void testOnResponse() throws IOException, ControlLoopException, AAIException {
617 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
618 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
620 UUID requestId = UUID.randomUUID();
621 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
622 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
623 onsetEvent.setRequestID(requestId);
624 onsetEvent.setTarget("generic-vnf.vnf-id");
625 onsetEvent.setClosedLoopAlarmStart(Instant.now());
626 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
627 onsetEvent.setAAI(new HashMap<>());
628 onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
630 ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
631 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
632 assertNotNull(notification);
633 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
635 Policy policy = manager.getProcessor().getCurrentPolicy();
636 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
639 assertNull(clom.onResponse(null));
641 Response appcResponse = new Response();
642 CommonHeader commonHeader = new CommonHeader();
643 appcResponse.setCommonHeader(commonHeader );
644 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
646 commonHeader.setSubRequestID("12345");
647 appcResponse.setStatus(null);
648 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
650 ResponseStatus responseStatus = new ResponseStatus();
651 appcResponse.setStatus(responseStatus);
652 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
654 responseStatus.setCode(0);
655 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
657 responseStatus.setCode(ResponseCode.ACCEPT.getValue());
658 assertEquals(null, clom.onResponse(appcResponse));
660 responseStatus.setCode(ResponseCode.ERROR.getValue());
661 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
663 responseStatus.setCode(ResponseCode.FAILURE.getValue());
664 assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse));
666 responseStatus.setCode(ResponseCode.REJECT.getValue());
667 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
669 responseStatus.setCode(ResponseCode.SUCCESS.getValue());
670 assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse));
672 LCMResponseWrapper lrw = new LCMResponseWrapper();
673 LCMResponse body = new LCMResponse();
674 LCMCommonHeader lcmCH = new LCMCommonHeader();
675 body.setCommonHeader(lcmCH );
678 lcmCH.setSubRequestId("NotANumber");
679 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
681 lcmCH.setSubRequestId("12345");
682 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
684 SOResponse soResponse = new SOResponse();
685 SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null);
687 soResponse.setHttpResponseCode(200);
688 assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW));
690 soResponse.setHttpResponseCode(202);
691 assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW));
693 soResponse.setHttpResponseCode(500);
694 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));
696 VFCResponse vfcResponse = new VFCResponse();
697 VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor();
698 vfcResponse.setResponseDescriptor(responseDescriptor );
700 responseDescriptor.setStatus("finished");
701 assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse));
703 responseDescriptor.setStatus("unfinished");
704 assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse));
708 public void testCompleteOperation() throws ControlLoopException, AAIException, IOException {
709 InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
710 String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
712 UUID requestId = UUID.randomUUID();
713 VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
714 onsetEvent.setClosedLoopControlName("TwoOnsetTest");
715 onsetEvent.setRequestID(requestId);
716 onsetEvent.setTarget("generic-vnf.vnf-id");
717 onsetEvent.setClosedLoopAlarmStart(Instant.now());
718 onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
719 onsetEvent.setAAI(new HashMap<>());
720 onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
722 ControlLoopEventManager manager = new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
723 VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
724 assertNotNull(notification);
725 assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
727 Policy policy = manager.getProcessor().getCurrentPolicy();
728 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
731 clom.startOperation(onsetEvent);
733 SOResponse soResponse = new SOResponse();
734 SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null);
736 PolicyEngine.manager.setEnvironmentProperty("guard.disabled", "false");
737 PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL, "http://somewhere.over.the.rainbow");
738 PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "Dorothy");
739 PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "Toto");
741 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));
743 System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
744 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));