4a30a451a8bb3873322ad55be1536c40e9084ee4
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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
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.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;
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         @BeforeClass
98         public static void setUpSimulator() {
99                 try {
100                         org.onap.policy.simulators.Util.buildAaiSim();
101                 } catch (Exception e) {
102                         fail(e.getMessage());
103                 }
104         }
105
106         @AfterClass
107         public static void tearDownSimulator() {
108                 HttpServletServer.factory.destroy();
109         }
110
111         @Test
112         public void testRetriesFail() {
113                 //
114                 // Load up the policy
115                 //
116                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
117                 onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName());
118                 try {
119                         //
120                         // Create a processor
121                         //
122                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
123                         //
124                         // create the manager
125                         //
126                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
127                         VirtualControlLoopNotification notification = eventManager.activate(onset);
128
129                         assertNotNull(notification);
130                         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
131
132                         ControlLoopEventManager.NEW_EVENT_STATUS status = null;
133                         try {
134                                 status = eventManager.onNewEvent(onset);
135                         } catch (AAIException e) {
136                                 logger.warn(e.toString());
137                                 fail("A&AI Query Failed");
138                         }
139                         assertNotNull(status);
140                         assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
141
142                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
143                         logger.debug("{}",manager);
144                         //
145                         //
146                         //
147                         assertFalse(manager.isOperationComplete());
148                         assertFalse(manager.isOperationRunning());
149                         //
150                         // Start
151                         //
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());
161                         //
162                         // Accept
163                         //
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);
169                         //
170                         //
171                         //
172                         PolicyResult result = manager.onResponse(dmaapResponse);
173                         logger.debug("{}",manager);
174                         assertTrue(result == null);
175                         assertFalse(manager.isOperationComplete());
176                         assertTrue(manager.isOperationRunning());
177                         //
178                         // Now we are going to Fail it
179                         //
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());
189                         //
190                         // Retry it
191                         //
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());
201                         //
202                         // 
203                         //
204                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
205                         logger.debug("{}",manager);
206                         appcResponse.getStatus().setCode(100);
207                         appcResponse.getStatus().setMessage("ACCEPT");
208                         dmaapResponse.setBody(appcResponse);
209                         //
210                         //
211                         //
212                         result = manager.onResponse(dmaapResponse);
213                         logger.debug("{}",manager);
214                         assertTrue(result == null);
215                         assertFalse(manager.isOperationComplete());
216                         assertTrue(manager.isOperationRunning());
217                         //
218                         // Now we are going to Fail it
219                         //
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));
227                         //
228                         // Should be complete now
229                         //
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());
237                 }
238         }
239
240         @Test
241         public void testTimeout() {
242                 //
243                 // Load up the policy
244                 //
245                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
246                 onset.setClosedLoopControlName(pair.a.getControlLoop().getControlLoopName());
247                 try {
248                         //
249                         // Create a processor
250                         //
251                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
252                         //
253                         // create the manager
254                         //
255                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.getClosedLoopControlName(), onset.getRequestID());
256                         VirtualControlLoopNotification notification = eventManager.activate(onset);
257
258                         assertNotNull(notification);
259                         assertEquals(ControlLoopNotificationType.ACTIVE, notification.getNotification());
260
261                         ControlLoopEventManager.NEW_EVENT_STATUS status = null;
262                         try {
263                                 status = eventManager.onNewEvent(onset);
264                         } catch (AAIException e) {
265                                 logger.warn(e.toString());
266                                 fail("A&AI Query Failed");
267                         }
268                         assertNotNull(status);
269                         assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
270
271                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
272                         //
273                         //
274                         //
275                         logger.debug("{}",manager);
276                         assertFalse(manager.isOperationComplete());
277                         assertFalse(manager.isOperationRunning());
278                         //
279                         // Start
280                         //
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());
290                         //
291                         // Accept
292                         //
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");
298                         //
299                         //
300                         //
301                         PolicyResult result = manager.onResponse(dmaapResponse);
302                         logger.debug("{}",manager);
303                         assertTrue(result == null);
304                         assertFalse(manager.isOperationComplete());
305                         assertTrue(manager.isOperationRunning());
306                         //
307                         // Now we are going to simulate Timeout
308                         //
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));
315                         //
316                         // Now we are going to Fail the previous request
317                         //
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);
324                         //
325                         //
326                         //
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());
333                 }
334         }
335
336         @Test
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);
340
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");
350
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());
355
356                 ControlLoopOperationManager clom = manager.processControlLoop();
357                 assertNotNull(clom);
358                 assertNull(clom.getOperationResult());
359
360                 clom.setEventManager(manager);
361                 assertEquals(manager, clom.getEventManager());
362
363                 assertNull(clom.getTargetEntity());
364
365                 clom.setGuardApprovalStatus("WizardOKedIt");
366                 assertEquals("WizardOKedIt", clom.getGuardApprovalStatus());
367
368                 assertNull(clom.getOperationResult());
369
370                 Policy policy = manager.getProcessor().getCurrentPolicy();
371                 clom.getTarget(policy);
372
373                 Target savedTarget = policy.getTarget();
374                 policy.setTarget(null);
375                 try {
376                         clom.getTarget(policy);
377                         fail("test should throw an exception here");
378                 } catch (Exception e) {
379                         assertEquals("The target is null", e.getMessage());
380                 }
381
382                 policy.setTarget(new Target());
383                 try {
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());
388                 }
389
390                 policy.setTarget(savedTarget);
391
392                 policy.getTarget().setType(TargetType.PNF);
393                 try {
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());
398                 }
399
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");
404
405                 policy.getTarget().setType(TargetType.VNF);
406                 try {
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());
411                 }
412
413                 onsetEvent.setTarget("vserver.vserver-name");
414                 onsetEvent.getAAI().put("vserver.vserver-name", "OzVServer");
415                 assertEquals("OzVServer", clom.getTarget(policy));
416
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));
421
422                 onsetEvent.setTarget("generic-vnf.vnf-name");
423                 assertEquals("OzVNF", clom.getTarget(policy));
424
425                 manager.onNewEvent(onsetEvent);
426
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));
431
432                 policy.getTarget().setType(TargetType.VFC);
433                 try {
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());
438                 }
439
440                 assertEquals(Integer.valueOf(20), clom.getOperationTimeout());
441
442                 assertEquals("20s", clom.getOperationTimeoutString(100));
443
444                 assertEquals(null, clom.getOperationMessage());
445                 assertEquals(null, clom.getOperationMessage("The Wizard Escaped"));
446
447                 clom.startOperation(onsetEvent);
448
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"));
451
452                 assertEquals("actor=SO,operation=Restart,tar", clom.getOperationHistory().substring(0, 30));
453
454                 clom.setOperationHasException("The Wizard is gone");
455                 clom.setOperationHasGuardDeny();
456         }               
457
458         @Test
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);
462
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");
472
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());
477
478                 Policy policy = manager.getProcessor().getCurrentPolicy();
479                 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
480                 assertNotNull(clom);
481
482                 policy.setRecipe("ModifyConfig");
483                 policy.getTarget().setResourceID(UUID.randomUUID().toString());
484                 try {
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());
489                 }
490
491                 policy.getTarget().setResourceID("82194af1-3c2c-485a-8f44-420e22a9eaa4");
492                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
493                 assertNotNull(clom);
494
495                 policy.setActor("SO");
496                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
497                 assertNotNull(clom);
498
499                 policy.setActor("VFC");
500                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
501                 assertNotNull(clom);
502
503                 policy.setActor("Dorothy");
504                 try {
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());
509                 }
510         }
511
512         @Test
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);
516
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");
526
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());
531
532                 Policy policy = manager.getProcessor().getCurrentPolicy();
533                 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
534                 assertNotNull(clom);
535
536                 clom.startOperation(onsetEvent);
537
538                 try {
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());
543                 }
544
545                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
546                 assertNotNull(clom);
547                 String savedRecipe = policy.getRecipe();
548                 policy.setRecipe("ModifyConfig");
549                 policy.getTarget().setResourceID(UUID.randomUUID().toString());
550                 clom.startOperation(onsetEvent);
551                 policy.setRecipe(savedRecipe);
552
553                 policy.setRetry(null);
554                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
555                 assertNotNull(clom);
556                 clom.startOperation(onsetEvent);
557                 clom.setOperationHasTimedOut();
558                 assertTrue(clom.isOperationComplete());
559                 try {
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());
564                 }
565
566                 policy.setRetry(0);
567                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
568                 assertNotNull(clom);
569                 clom.startOperation(onsetEvent);
570                 clom.setOperationHasTimedOut();
571                 assertTrue(clom.isOperationComplete());
572                 try {
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());
577                 }
578
579                 policy.setRetry(1);
580                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
581                 assertNotNull(clom);
582                 clom.startOperation(onsetEvent);
583                 clom.setOperationHasTimedOut();
584                 clom.startOperation(onsetEvent);
585                 clom.setOperationHasTimedOut();
586                 assertTrue(clom.isOperationComplete());
587                 try {
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());
592                 }
593
594                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
595                 assertNotNull(clom);
596                 policy.setActor("SO");
597                 clom.startOperation(onsetEvent);
598
599                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
600                 assertNotNull(clom);
601                 policy.setActor("VFC");
602                 clom.startOperation(onsetEvent);
603
604                 clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
605                 assertNotNull(clom);
606                 policy.setActor("Oz");
607                 try {
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());
612                 }
613         }
614
615         @Test
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);
619
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");
629
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());
634
635                 Policy policy = manager.getProcessor().getCurrentPolicy();
636                 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
637                 assertNotNull(clom);
638
639                 assertNull(clom.onResponse(null));
640
641                 Response appcResponse = new Response();
642                 CommonHeader commonHeader = new CommonHeader();
643                 appcResponse.setCommonHeader(commonHeader );
644                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
645
646                 commonHeader.setSubRequestID("12345");
647                 appcResponse.setStatus(null);
648                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
649
650                 ResponseStatus responseStatus = new ResponseStatus();
651                 appcResponse.setStatus(responseStatus);
652                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
653
654                 responseStatus.setCode(0);
655                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
656
657                 responseStatus.setCode(ResponseCode.ACCEPT.getValue());
658                 assertEquals(null, clom.onResponse(appcResponse));
659
660                 responseStatus.setCode(ResponseCode.ERROR.getValue());
661                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
662
663                 responseStatus.setCode(ResponseCode.FAILURE.getValue());
664                 assertEquals(PolicyResult.FAILURE, clom.onResponse(appcResponse));
665
666                 responseStatus.setCode(ResponseCode.REJECT.getValue());
667                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(appcResponse));
668
669                 responseStatus.setCode(ResponseCode.SUCCESS.getValue());
670                 assertEquals(PolicyResult.SUCCESS, clom.onResponse(appcResponse));
671
672                 LCMResponseWrapper lrw = new LCMResponseWrapper();
673                 LCMResponse body = new LCMResponse();
674                 LCMCommonHeader lcmCH = new LCMCommonHeader();
675                 body.setCommonHeader(lcmCH );
676                 lrw.setBody(body );
677
678                 lcmCH.setSubRequestId("NotANumber");
679                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
680
681                 lcmCH.setSubRequestId("12345");
682                 assertEquals(PolicyResult.FAILURE_EXCEPTION, clom.onResponse(lrw));
683
684                 SOResponse soResponse = new SOResponse();
685                 SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null);
686
687                 soResponse.setHttpResponseCode(200);
688                 assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW));
689
690                 soResponse.setHttpResponseCode(202);
691                 assertEquals(PolicyResult.SUCCESS, clom.onResponse(soRW));
692
693                 soResponse.setHttpResponseCode(500);
694                 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));
695
696                 VFCResponse vfcResponse = new VFCResponse();
697                 VFCResponseDescriptor responseDescriptor = new VFCResponseDescriptor();
698                 vfcResponse.setResponseDescriptor(responseDescriptor );
699
700                 responseDescriptor.setStatus("finished");
701                 assertEquals(PolicyResult.SUCCESS, clom.onResponse(vfcResponse));
702
703                 responseDescriptor.setStatus("unfinished");
704                 assertEquals(PolicyResult.FAILURE, clom.onResponse(vfcResponse));
705         }
706
707         @Test
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);
711
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");
721
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());
726
727                 Policy policy = manager.getProcessor().getCurrentPolicy();
728                 ControlLoopOperationManager clom = new ControlLoopOperationManager(onsetEvent, policy, manager);
729                 assertNotNull(clom);
730
731                 clom.startOperation(onsetEvent);
732
733                 SOResponse soResponse = new SOResponse();
734                 SOResponseWrapper soRW = new SOResponseWrapper(soResponse , null);
735
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");
740
741                 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));
742                 
743                 System.setProperty("OperationsHistoryPU", "TestOperationsHistoryPU");
744                 assertEquals(PolicyResult.FAILURE, clom.onResponse(soRW));
745         }
746 }