Merge "Fix Final Failure on AAI Queries"
[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 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.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.aai.util.AAIException;
37 import org.onap.policy.appclcm.LCMRequest;
38 import org.onap.policy.appclcm.LCMRequestWrapper;
39 import org.onap.policy.appclcm.LCMResponse;
40 import org.onap.policy.appclcm.LCMResponseWrapper;
41 import org.onap.policy.controlloop.ControlLoopEventStatus;
42 import org.onap.policy.controlloop.ControlLoopNotificationType;
43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
44 import org.onap.policy.controlloop.ControlLoopException;
45 import org.onap.policy.controlloop.ControlLoopTargetType;
46 import org.onap.policy.controlloop.Util;
47 import org.onap.policy.controlloop.VirtualControlLoopNotification;
48 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
49 import org.onap.policy.controlloop.policy.PolicyResult;
50 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
51 import org.onap.policy.drools.http.server.HttpServletServer;
52 import org.onap.policy.drools.system.PolicyEngine;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class ControlLoopOperationManagerTest {
57         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
58         private static VirtualControlLoopEvent onset;
59         static {
60                 onset = new VirtualControlLoopEvent();
61                 onset.requestID = UUID.randomUUID();
62                 onset.target = "generic-vnf.vnf-name";
63                 onset.target_type = ControlLoopTargetType.VNF;
64                 onset.closedLoopAlarmStart = Instant.now();
65                 onset.AAI = new HashMap<>();
66                 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
67                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
68                 
69                 /* Set environment properties */
70         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
71         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
72         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
73         }
74
75         @BeforeClass
76     public static void setUpSimulator() {
77         try {
78             org.onap.policy.simulators.Util.buildAaiSim();
79         } catch (Exception e) {
80             fail(e.getMessage());
81         }
82     }
83
84     @AfterClass
85     public static void tearDownSimulator() {
86         HttpServletServer.factory.destroy();
87     }
88         
89         @Test
90         public void testRetriesFail() {
91                 //
92                 // Load up the policy
93                 //
94                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
95                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
96                 try {
97                         //
98                         // Create a processor
99                         //
100                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
101                         //
102                         // create the manager
103                         //
104                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
105             VirtualControlLoopNotification notification = eventManager.activate(onset);
106                         
107                         assertNotNull(notification);
108                         assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
109                         
110                         ControlLoopEventManager.NEW_EVENT_STATUS status = null;
111                 try {
112                     status = eventManager.onNewEvent(onset);
113                 } catch (AAIException e) {
114                     logger.warn(e.toString());
115                     fail("A&AI Query Failed");
116                 }
117                 assertNotNull(status);
118                 assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
119                         
120                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
121                         logger.debug("{}",manager);
122                         //
123                         //
124                         //
125                         assertFalse(manager.isOperationComplete());
126                         assertFalse(manager.isOperationRunning());
127                         //
128                         // Start
129                         //
130                         Object request = manager.startOperation(onset);
131                         logger.debug("{}",manager);
132                         assertNotNull(request);
133                         assertTrue(request instanceof LCMRequestWrapper);
134                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
135                         LCMRequest appcRequest = dmaapRequest.getBody();
136                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
137                         assertFalse(manager.isOperationComplete());
138                         assertTrue(manager.isOperationRunning());
139                         //
140                         // Accept
141                         //
142                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
143                         LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
144                         appcResponse.getStatus().setCode(100);
145                         appcResponse.getStatus().setMessage("ACCEPT");
146                         dmaapResponse.setBody(appcResponse);
147                         //
148                         //
149                         //
150                         PolicyResult result = manager.onResponse(dmaapResponse);
151                         logger.debug("{}",manager);
152                         assertTrue(result == null);
153                         assertFalse(manager.isOperationComplete());
154                         assertTrue(manager.isOperationRunning());
155                         //
156                         // Now we are going to Fail it
157                         //
158                         appcResponse = new LCMResponse(appcRequest);
159                         appcResponse.getStatus().setCode(401);
160                         appcResponse.getStatus().setMessage("AppC failed for some reason");
161                         dmaapResponse.setBody(appcResponse);
162                         result = manager.onResponse(dmaapResponse);
163                         logger.debug("{}",manager);
164                         assertTrue(result.equals(PolicyResult.FAILURE));
165                         assertFalse(manager.isOperationComplete());
166                         assertFalse(manager.isOperationRunning());
167                         //
168                         // Retry it
169                         //
170                         request = manager.startOperation(onset);
171                         logger.debug("{}",manager);
172                         assertNotNull(request);
173                         assertTrue(request instanceof LCMRequestWrapper);
174                         dmaapRequest = (LCMRequestWrapper) request;
175                         appcRequest = dmaapRequest.getBody();
176                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
177                         assertFalse(manager.isOperationComplete());
178                         assertTrue(manager.isOperationRunning());
179                         //
180                         // 
181                         //
182                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
183                         logger.debug("{}",manager);
184                         appcResponse.getStatus().setCode(100);
185                         appcResponse.getStatus().setMessage("ACCEPT");
186                         dmaapResponse.setBody(appcResponse);
187                         //
188                         //
189                         //
190                         result = manager.onResponse(dmaapResponse);
191                         logger.debug("{}",manager);
192                         assertTrue(result == null);
193                         assertFalse(manager.isOperationComplete());
194                         assertTrue(manager.isOperationRunning());
195                         //
196                         // Now we are going to Fail it
197                         //
198                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
199                         appcResponse.getStatus().setCode(401);
200                         appcResponse.getStatus().setMessage("AppC failed for some reason");
201                         dmaapResponse.setBody(appcResponse);
202                         result = manager.onResponse(dmaapResponse);
203                         logger.debug("{}",manager);
204                         assertTrue(result.equals(PolicyResult.FAILURE));
205                         //
206                         // Should be complete now
207                         //
208                         assertTrue(manager.isOperationComplete());
209                         assertFalse(manager.isOperationRunning());
210                         assertNotNull(manager.getOperationResult());
211                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
212                         assertTrue(manager.getHistory().size() == 2);
213                 } catch (ControlLoopException | AAIException e) {
214                         fail(e.getMessage());
215                 }
216         }
217
218         @Test
219         public void testTimeout() {
220                 //
221                 // Load up the policy
222                 //
223                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
224                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
225                 try {
226                         //
227                         // Create a processor
228                         //
229                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
230                         //
231                         // create the manager
232                         //
233                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
234                         VirtualControlLoopNotification notification = eventManager.activate(onset);
235                         
236                         assertNotNull(notification);
237                         assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
238
239                         ControlLoopEventManager.NEW_EVENT_STATUS status = null;
240             try {
241                 status = eventManager.onNewEvent(onset);
242             } catch (AAIException e) {
243                 logger.warn(e.toString());
244                 fail("A&AI Query Failed");
245             }
246             assertNotNull(status);
247             assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
248                         
249                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
250                         //
251                         //
252                         //
253                         logger.debug("{}",manager);
254                         assertFalse(manager.isOperationComplete());
255                         assertFalse(manager.isOperationRunning());
256                         //
257                         // Start
258                         //
259                         Object request = manager.startOperation(onset);
260                         logger.debug("{}",manager);
261                         assertNotNull(request);
262                         assertTrue((request) instanceof LCMRequestWrapper);
263                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
264                         LCMRequest appcRequest = dmaapRequest.getBody();
265                         assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
266                         assertFalse(manager.isOperationComplete());
267                         assertTrue(manager.isOperationRunning());
268                         //
269                         // Accept
270                         //
271                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
272                         LCMResponse appcResponse = new LCMResponse(appcRequest);
273                 dmaapResponse.setBody(appcResponse);
274                         appcResponse.getStatus().setCode(100);
275                         appcResponse.getStatus().setMessage("ACCEPT");
276                         //
277                         //
278                         //
279                         PolicyResult result = manager.onResponse(dmaapResponse);
280                         logger.debug("{}",manager);
281                         assertTrue(result == null);
282                         assertFalse(manager.isOperationComplete());
283                         assertTrue(manager.isOperationRunning());
284                         //
285                         // Now we are going to simulate Timeout
286                         //
287                         manager.setOperationHasTimedOut();
288                         logger.debug("{}",manager);
289                         assertTrue(manager.isOperationComplete());
290                         assertFalse(manager.isOperationRunning());
291                         assertTrue(manager.getHistory().size() == 1);
292                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
293                         //
294                         // Now we are going to Fail the previous request
295                         //
296                         appcResponse = new LCMResponse(appcRequest);
297                         appcResponse.getStatus().setCode(401);
298                         appcResponse.getStatus().setMessage("AppC failed for some reason");
299                         dmaapResponse.setBody(appcResponse);
300                         result = manager.onResponse(dmaapResponse);
301                         logger.debug("{}",manager);
302                         //
303                         //
304                         //
305                         assertTrue(manager.isOperationComplete());
306                         assertFalse(manager.isOperationRunning());
307                         assertTrue(manager.getHistory().size() == 1);
308                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
309                 } catch (ControlLoopException | AAIException e) {
310                         fail(e.getMessage());
311                 }
312         }
313
314 }