Removed checkstyle warnings
[policy/drools-applications.git] / controlloop / common / eventmanager / src / test / java / org / onap / policy / controlloop / eventmanager / ControlLoopOperationManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.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;
29
30 import java.io.File;
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;
38
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.ControlLoopException;
55 import org.onap.policy.controlloop.ControlLoopNotificationType;
56 import org.onap.policy.controlloop.ControlLoopTargetType;
57 import org.onap.policy.controlloop.Util;
58 import org.onap.policy.controlloop.VirtualControlLoopEvent;
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;
74
75 public class ControlLoopOperationManagerTest {
76     private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
77
78
79     private static VirtualControlLoopEvent onset;
80
81     static {
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);
90
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");
95     }
96
97     /**
98      * Set up test class.
99      */
100     @BeforeClass
101     public static void setUpSimulator() {
102         try {
103             org.onap.policy.simulators.Util.buildAaiSim();
104         } catch (Exception e) {
105             fail(e.getMessage());
106         }
107     }
108
109     @AfterClass
110     public static void tearDownSimulator() {
111         HttpServletServer.factory.destroy();
112     }
113
114     @Test
115     public void testRetriesFail() {
116         //
117         // Load up the policy
118         //
119         final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
120         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
121         try {
122             //
123             // Create a processor
124             //
125             final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value);
126             //
127             // create the manager
128             //
129             ControlLoopEventManager eventManager =
130                     new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
131             VirtualControlLoopNotification notification = eventManager.activate(onset);
132
133             assertNotNull(notification);
134             assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
135
136             ControlLoopEventManager.NEW_EVENT_STATUS status = null;
137             try {
138                 status = eventManager.onNewEvent(onset);
139             } catch (AaiException e) {
140                 logger.warn(e.toString());
141                 fail("A&AI Query Failed");
142             }
143             assertNotNull(status);
144             assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
145
146             ControlLoopOperationManager manager =
147                     new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
148             logger.debug("{}", manager);
149             //
150             //
151             //
152             assertFalse(manager.isOperationComplete());
153             assertFalse(manager.isOperationRunning());
154             //
155             // Start
156             //
157             Object request = manager.startOperation(onset);
158             logger.debug("{}", manager);
159             assertNotNull(request);
160             assertTrue(request instanceof LCMRequestWrapper);
161             LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
162             LCMRequest appcRequest = dmaapRequest.getBody();
163             assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
164             assertFalse(manager.isOperationComplete());
165             assertTrue(manager.isOperationRunning());
166             //
167             // Accept
168             //
169             LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
170             LCMResponse appcResponse = new LCMResponse(appcRequest);
171             appcResponse.getStatus().setCode(100);
172             appcResponse.getStatus().setMessage("ACCEPT");
173             dmaapResponse.setBody(appcResponse);
174             //
175             //
176             //
177             PolicyResult result = manager.onResponse(dmaapResponse);
178             logger.debug("{}", manager);
179             assertTrue(result == null);
180             assertFalse(manager.isOperationComplete());
181             assertTrue(manager.isOperationRunning());
182             //
183             // Now we are going to Fail it
184             //
185             appcResponse = new LCMResponse(appcRequest);
186             appcResponse.getStatus().setCode(401);
187             appcResponse.getStatus().setMessage("AppC failed for some reason");
188             dmaapResponse.setBody(appcResponse);
189             result = manager.onResponse(dmaapResponse);
190             logger.debug("{}", manager);
191             assertTrue(result.equals(PolicyResult.FAILURE));
192             assertFalse(manager.isOperationComplete());
193             assertFalse(manager.isOperationRunning());
194             //
195             // Retry it
196             //
197             request = manager.startOperation(onset);
198             logger.debug("{}", manager);
199             assertNotNull(request);
200             assertTrue(request instanceof LCMRequestWrapper);
201             dmaapRequest = (LCMRequestWrapper) request;
202             appcRequest = dmaapRequest.getBody();
203             assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
204             assertFalse(manager.isOperationComplete());
205             assertTrue(manager.isOperationRunning());
206             //
207             //
208             //
209             appcResponse = new LCMResponse(appcRequest);
210             logger.debug("{}", manager);
211             appcResponse.getStatus().setCode(100);
212             appcResponse.getStatus().setMessage("ACCEPT");
213             dmaapResponse.setBody(appcResponse);
214             //
215             //
216             //
217             result = manager.onResponse(dmaapResponse);
218             logger.debug("{}", manager);
219             assertTrue(result == null);
220             assertFalse(manager.isOperationComplete());
221             assertTrue(manager.isOperationRunning());
222             //
223             // Now we are going to Fail it
224             //
225             appcResponse = new LCMResponse(appcRequest);
226             appcResponse.getStatus().setCode(401);
227             appcResponse.getStatus().setMessage("AppC failed for some reason");
228             dmaapResponse.setBody(appcResponse);
229             result = manager.onResponse(dmaapResponse);
230             logger.debug("{}", manager);
231             assertTrue(result.equals(PolicyResult.FAILURE));
232             //
233             // Should be complete now
234             //
235             assertTrue(manager.isOperationComplete());
236             assertFalse(manager.isOperationRunning());
237             assertNotNull(manager.getOperationResult());
238             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
239             assertTrue(manager.getHistory().size() == 2);
240         } catch (ControlLoopException | AaiException e) {
241             fail(e.getMessage());
242         }
243     }
244
245     @Test
246     public void testTimeout() {
247         //
248         // Load up the policy
249         //
250         final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
251         onset.setClosedLoopControlName(pair.key.getControlLoop().getControlLoopName());
252         try {
253             //
254             // Create a processor
255             //
256             final ControlLoopProcessor processor = new ControlLoopProcessor(pair.value);
257             //
258             // create the manager
259             //
260             ControlLoopEventManager eventManager =
261                     new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
262             VirtualControlLoopNotification notification = eventManager.activate(onset);
263
264             assertNotNull(notification);
265             assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
266
267             ControlLoopEventManager.NEW_EVENT_STATUS status = null;
268             try {
269                 status = eventManager.onNewEvent(onset);
270             } catch (AaiException e) {
271                 logger.warn(e.toString());
272                 fail("A&AI Query Failed");
273             }
274             assertNotNull(status);
275             assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
276
277             ControlLoopOperationManager manager =
278                     new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
279             //
280             //
281             //
282             logger.debug("{}", manager);
283             assertFalse(manager.isOperationComplete());
284             assertFalse(manager.isOperationRunning());
285             //
286             // Start
287             //
288             Object request = manager.startOperation(onset);
289             logger.debug("{}", manager);
290             assertNotNull(request);
291             assertTrue((request) instanceof LCMRequestWrapper);
292             LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
293             LCMRequest appcRequest = dmaapRequest.getBody();
294             assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
295             assertFalse(manager.isOperationComplete());
296             assertTrue(manager.isOperationRunning());
297             //
298             // Accept
299             //
300             LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
301             LCMResponse appcResponse = new LCMResponse(appcRequest);
302             dmaapResponse.setBody(appcResponse);
303             appcResponse.getStatus().setCode(100);
304             appcResponse.getStatus().setMessage("ACCEPT");
305             //
306             //
307             //
308             PolicyResult result = manager.onResponse(dmaapResponse);
309             logger.debug("{}", manager);
310             assertTrue(result == null);
311             assertFalse(manager.isOperationComplete());
312             assertTrue(manager.isOperationRunning());
313             //
314             // Now we are going to simulate Timeout
315             //
316             manager.setOperationHasTimedOut();
317             logger.debug("{}", manager);
318             assertTrue(manager.isOperationComplete());
319             assertFalse(manager.isOperationRunning());
320             assertTrue(manager.getHistory().size() == 1);
321             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
322             //
323             // Now we are going to Fail the previous request
324             //
325             appcResponse = new LCMResponse(appcRequest);
326             appcResponse.getStatus().setCode(401);
327             appcResponse.getStatus().setMessage("AppC failed for some reason");
328             dmaapResponse.setBody(appcResponse);
329             result = manager.onResponse(dmaapResponse);
330             logger.debug("{}", manager);
331             //
332             //
333             //
334             assertTrue(manager.isOperationComplete());
335             assertFalse(manager.isOperationRunning());
336             assertTrue(manager.getHistory().size() == 1);
337             assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
338         } catch (ControlLoopException | AaiException e) {
339             fail(e.getMessage());
340         }
341     }
342
343     @Test
344     public void testMethods() throws IOException, ControlLoopException, AaiException {
345         InputStream is = new FileInputStream(new File("src/test/resources/testSOactor.yaml"));
346         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
347
348         UUID requestId = UUID.randomUUID();
349         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
350         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
351         onsetEvent.setRequestID(requestId);
352         onsetEvent.setTarget("generic-vnf.vnf-id");
353         onsetEvent.setClosedLoopAlarmStart(Instant.now());
354         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
355         onsetEvent.setAAI(new HashMap<>());
356         onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
357
358         ControlLoopEventManager manager =
359                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
360         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
361         assertNotNull(notification);
362         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
363
364         ControlLoopOperationManager clom = manager.processControlLoop();
365         assertNotNull(clom);
366         assertNull(clom.getOperationResult());
367
368         clom.setEventManager(manager);
369         assertEquals(manager, clom.getEventManager());
370
371         assertNull(clom.getTargetEntity());
372
373         clom.setGuardApprovalStatus("WizardOKedIt");
374         assertEquals("WizardOKedIt", clom.getGuardApprovalStatus());
375
376         assertNull(clom.getOperationResult());
377
378         Policy policy = manager.getProcessor().getCurrentPolicy();
379         clom.getTarget(policy);
380
381         final Target savedTarget = policy.getTarget();
382         policy.setTarget(null);
383         try {
384             clom.getTarget(policy);
385             fail("test should throw an exception here");
386         } catch (Exception e) {
387             assertEquals("The target is null", e.getMessage());
388         }
389
390         policy.setTarget(new Target());
391         try {
392             clom.getTarget(policy);
393             fail("test should throw an exception here");
394         } catch (Exception e) {
395             assertEquals("The target type is null", e.getMessage());
396         }
397
398         policy.setTarget(savedTarget);
399
400         policy.getTarget().setType(TargetType.PNF);
401         try {
402             clom.getTarget(policy);
403             fail("test should throw an exception here");
404         } catch (Exception e) {
405             assertEquals("PNF target is not supported", e.getMessage());
406         }
407
408         onsetEvent.setTarget("Oz");
409         onsetEvent.getAAI().remove("generic-vnf.vnf-name");
410         onsetEvent.getAAI().remove("generic-vnf.vnf-id");
411         onsetEvent.getAAI().remove("vserver.vserver-name");
412
413         policy.getTarget().setType(TargetType.VNF);
414         try {
415             clom.getTarget(policy);
416             fail("test should throw an exception here");
417         } catch (Exception e) {
418             assertEquals("Target does not match target type", e.getMessage());
419         }
420
421         onsetEvent.setTarget("vserver.vserver-name");
422         onsetEvent.getAAI().put("vserver.vserver-name", "OzVServer");
423         assertEquals("OzVServer", clom.getTarget(policy));
424
425         onsetEvent.getAAI().remove("vserver.vserver-name");
426         onsetEvent.setTarget("generic-vnf.vnf-id");
427         onsetEvent.getAAI().put("generic-vnf.vnf-id", "OzVNF");
428         assertEquals("OzVNF", clom.getTarget(policy));
429
430         onsetEvent.setTarget("generic-vnf.vnf-name");
431         assertEquals("OzVNF", clom.getTarget(policy));
432
433         manager.onNewEvent(onsetEvent);
434
435         onsetEvent.getAAI().remove("generic-vnf.vnf-id");
436         manager.getVnfResponse();
437         clom.getEventManager().getVnfResponse().setVnfId("generic-vnf.vnf-id");
438         assertEquals("generic-vnf.vnf-id", clom.getTarget(policy));
439
440         policy.getTarget().setType(TargetType.VFC);
441         try {
442             clom.getTarget(policy);
443             fail("test should throw an exception here");
444         } catch (Exception e) {
445             assertEquals("The target type is not supported", e.getMessage());
446         }
447
448         assertEquals(Integer.valueOf(20), clom.getOperationTimeout());
449
450         assertEquals("20s", clom.getOperationTimeoutString(100));
451
452         assertEquals(null, clom.getOperationMessage());
453         assertEquals(null, clom.getOperationMessage("The Wizard Escaped"));
454
455         clom.startOperation(onsetEvent);
456
457         assertEquals("actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1",
458                 clom.getOperationMessage());
459         assertEquals(
460                 "actor=SO,operation=Restart,target=Target [type=VFC, resourceID=null],subRequestId=1, Guard result: "
461                         + "The Wizard Escaped",
462                 clom.getOperationMessage("The Wizard Escaped"));
463
464         assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30));
465
466         clom.setOperationHasException("The Wizard is gone");
467         clom.setOperationHasGuardDeny();
468     }
469
470     @Test
471     public void testConstructor() throws IOException, ControlLoopException, AaiException {
472         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
473         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
474
475         UUID requestId = UUID.randomUUID();
476         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
477         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
478         onsetEvent.setRequestID(requestId);
479         onsetEvent.setTarget("generic-vnf.vnf-id");
480         onsetEvent.setClosedLoopAlarmStart(Instant.now());
481         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
482         onsetEvent.setAAI(new HashMap<>());
483         onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
484
485         ControlLoopEventManager manager =
486                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
487         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
488         assertNotNull(notification);
489         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
490
491         Policy policy = manager.getProcessor().getCurrentPolicy();
492         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
493         assertNotNull(clom);
494
495         policy.setRecipe("ModifyConfig");
496         policy.getTarget().setResourceID(UUID.randomUUID().toString());
497         try {
498             new ControlLoopOperationManager(onsetEvent, policy, manager);
499             fail("test should throw an exception here");
500         } catch (Exception e) {
501             assertEquals("Target vnf-id could not be found", e.getMessage());
502         }
503
504         policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4");
505         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
506         assertNotNull(clom);
507
508         policy.setActor("SO");
509         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
510         assertNotNull(clom);
511
512         policy.setActor("VFC");
513         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
514         assertNotNull(clom);
515
516         policy.setActor("Dorothy");
517         try {
518             new ControlLoopOperationManager(onsetEvent, policy, manager);
519             fail("test should throw an exception here");
520         } catch (Exception e) {
521             assertEquals("ControlLoopEventManager: policy has an unknown actor.", e.getMessage());
522         }
523     }
524
525     @Test
526     public void testStartOperation() throws IOException, ControlLoopException, AaiException {
527         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
528         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
529
530         UUID requestId = UUID.randomUUID();
531         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
532         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
533         onsetEvent.setRequestID(requestId);
534         onsetEvent.setTarget("generic-vnf.vnf-id");
535         onsetEvent.setClosedLoopAlarmStart(Instant.now());
536         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
537         onsetEvent.setAAI(new HashMap<>());
538         onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
539
540         ControlLoopEventManager manager =
541                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
542         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
543         assertNotNull(notification);
544         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
545
546         Policy policy = manager.getProcessor().getCurrentPolicy();
547         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
548         assertNotNull(clom);
549
550         clom.startOperation(onsetEvent);
551
552         try {
553             clom.startOperation(onsetEvent);
554             fail("test should throw an exception here");
555         } catch (Exception e) {
556             assertEquals("current operation is not null (an operation is already running)", e.getMessage());
557         }
558
559         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
560         assertNotNull(clom);
561         final String savedRecipe = policy.getRecipe();
562         policy.setRecipe("ModifyConfig");
563         policy.getTarget().setResourceID(UUID.randomUUID().toString());
564         clom.startOperation(onsetEvent);
565         policy.setRecipe(savedRecipe);
566
567         policy.setRetry(null);
568         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
569         assertNotNull(clom);
570         clom.startOperation(onsetEvent);
571         clom.setOperationHasTimedOut();
572         assertTrue(clom.isOperationComplete());
573         try {
574             clom.startOperation(onsetEvent);
575             fail("test should throw an exception here");
576         } catch (Exception e) {
577             assertEquals("current operation failed and retries are not allowed", e.getMessage());
578         }
579
580         policy.setRetry(0);
581         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
582         assertNotNull(clom);
583         clom.startOperation(onsetEvent);
584         clom.setOperationHasTimedOut();
585         assertTrue(clom.isOperationComplete());
586         try {
587             clom.startOperation(onsetEvent);
588             fail("test should throw an exception here");
589         } catch (Exception e) {
590             assertEquals("current operation failed and retries are not allowed", e.getMessage());
591         }
592
593         policy.setRetry(1);
594         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
595         assertNotNull(clom);
596         clom.startOperation(onsetEvent);
597         clom.setOperationHasTimedOut();
598         clom.startOperation(onsetEvent);
599         clom.setOperationHasTimedOut();
600         assertTrue(clom.isOperationComplete());
601         try {
602             clom.startOperation(onsetEvent);
603             fail("test should throw an exception here");
604         } catch (Exception e) {
605             assertEquals("current oepration has failed after 2 retries", e.getMessage());
606         }
607
608         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
609         assertNotNull(clom);
610         policy.setActor("SO");
611         clom.startOperation(onsetEvent);
612
613         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
614         assertNotNull(clom);
615         policy.setActor("VFC");
616         clom.startOperation(onsetEvent);
617
618         clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
619         assertNotNull(clom);
620         policy.setActor("Oz");
621         try {
622             clom.startOperation(onsetEvent);
623             fail("test should throw an exception here");
624         } catch (Exception e) {
625             assertEquals("invalid actor Oz on policy", e.getMessage());
626         }
627     }
628
629     @Test
630     public void testOnResponse() throws IOException, ControlLoopException, AaiException {
631         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
632         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
633
634         UUID requestId = UUID.randomUUID();
635         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
636         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
637         onsetEvent.setRequestID(requestId);
638         onsetEvent.setTarget("generic-vnf.vnf-id");
639         onsetEvent.setClosedLoopAlarmStart(Instant.now());
640         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
641         onsetEvent.setAAI(new HashMap<>());
642         onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
643
644         ControlLoopEventManager manager =
645                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
646         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
647         assertNotNull(notification);
648         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
649
650         Policy policy = manager.getProcessor().getCurrentPolicy();
651         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
652         assertNotNull(clom);
653
654         assertNull(clom.onResponse(null));
655
656         Response appcResponse = new Response();
657         CommonHeader commonHeader = new CommonHeader();
658         appcResponse.setCommonHeader(commonHeader);
659         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
660
661         commonHeader.setSubRequestID("12345");
662         appcResponse.setStatus(null);
663         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
664
665         ResponseStatus responseStatus = new ResponseStatus();
666         appcResponse.setStatus(responseStatus);
667         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
668
669         responseStatus.setCode(0);
670         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
671
672         responseStatus.setCode(ResponseCode.ACCEPT.getValue());
673         assertEquals(null, clom.onResponse(appcResponse));
674
675         responseStatus.setCode(ResponseCode.ERROR.getValue());
676         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
677
678         responseStatus.setCode(ResponseCode.FAILURE.getValue());
679         assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse));
680
681         responseStatus.setCode(ResponseCode.REJECT.getValue());
682         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
683
684         responseStatus.setCode(ResponseCode.SUCCESS.getValue());
685         assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse));
686
687         LCMResponseWrapper lrw = new LCMResponseWrapper();
688         LCMResponse body = new LCMResponse();
689         LCMCommonHeader lcmCh = new LCMCommonHeader();
690         body.setCommonHeader(lcmCh);
691         lrw.setBody(body);
692
693         lcmCh.setSubRequestId("NotANumber");
694         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
695
696         lcmCh.setSubRequestId("12345");
697         assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
698
699         SOResponse soResponse = new SOResponse();
700         SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null);
701
702         soResponse.setHttpResponseCode(200);
703         assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw));
704
705         soResponse.setHttpResponseCode(202);
706         assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRw));
707
708         soResponse.setHttpResponseCode(500);
709         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
710
711         VFCResponse vfcResponse = new VFCResponse();
712         VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor();
713         vfcResponse.setResponseDescriptor(responseDescriptor);
714
715         responseDescriptor.setStatus("finished");
716         assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse));
717
718         responseDescriptor.setStatus("unfinished");
719         assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse));
720     }
721
722     @Test
723     public void testCompleteOperation() throws ControlLoopException, AaiException, IOException {
724         InputStream is = new FileInputStream(new File("src/test/resources/test.yaml"));
725         final String yamlString = IOUtils.toString(is, StandardCharsets.UTF_8);
726
727         UUID requestId = UUID.randomUUID();
728         VirtualControlLoopEvent onsetEvent = new VirtualControlLoopEvent();
729         onsetEvent.setClosedLoopControlName("TwoOnsetTest");
730         onsetEvent.setRequestID(requestId);
731         onsetEvent.setTarget("generic-vnf.vnf-id");
732         onsetEvent.setClosedLoopAlarmStart(Instant.now());
733         onsetEvent.setClosedLoopEventStatus(ControlLoopEventStatus.ONSET);
734         onsetEvent.setAAI(new HashMap<>());
735         onsetEvent.getAAI().put("generic-vnf.vnf-name", "onsetOne");
736
737         ControlLoopEventManager manager =
738                 new ControlLoopEventManager(onsetEvent.getClosedLoopControlName(), onsetEvent.getRequestID());
739         VirtualControlLoopNotification notification = manager.activate(yamlString, onsetEvent);
740         assertNotNull(notification);
741         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
742
743         Policy policy = manager.getProcessor().getCurrentPolicy();
744         ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
745         assertNotNull(clom);
746
747         clom.startOperation(onsetEvent);
748
749         SOResponse soResponse = new SOResponse();
750         final SOResponseWrapper soRw = new SOResponseWrapper(soResponse, null);
751
752         PolicyEngine.manager.setEnvironmentProperty("guard.disabled", "false");
753         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_URL,
754                 "http://somewhere.over.the.rainbow");
755         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_USER, "Dorothy");
756         PolicyEngine.manager.setEnvironmentProperty(org.onap.policy.guard.Util.ONAP_KEY_PASS, "Toto");
757
758         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
759
760         System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
761         assertEquals(PolicyResult.FAILURE, clom.onResponse(soRw));
762     }
763 }