Ensure no AAI lookup on subsequent onset
[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                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
111                         logger.debug("{}",manager);
112                         //
113                         //
114                         //
115                         assertFalse(manager.isOperationComplete());
116                         assertFalse(manager.isOperationRunning());
117                         //
118                         // Start
119                         //
120                         Object request = manager.startOperation(onset);
121                         logger.debug("{}",manager);
122                         assertNotNull(request);
123                         assertTrue(request instanceof LCMRequestWrapper);
124                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
125                         LCMRequest appcRequest = dmaapRequest.getBody();
126                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
127                         assertFalse(manager.isOperationComplete());
128                         assertTrue(manager.isOperationRunning());
129                         //
130                         // Accept
131                         //
132                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
133                         LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
134                         appcResponse.getStatus().setCode(100);
135                         appcResponse.getStatus().setMessage("ACCEPT");
136                         dmaapResponse.setBody(appcResponse);
137                         //
138                         //
139                         //
140                         PolicyResult result = manager.onResponse(dmaapResponse);
141                         logger.debug("{}",manager);
142                         assertTrue(result == null);
143                         assertFalse(manager.isOperationComplete());
144                         assertTrue(manager.isOperationRunning());
145                         //
146                         // Now we are going to Fail it
147                         //
148                         appcResponse = new LCMResponse(appcRequest);
149                         appcResponse.getStatus().setCode(401);
150                         appcResponse.getStatus().setMessage("AppC failed for some reason");
151                         dmaapResponse.setBody(appcResponse);
152                         result = manager.onResponse(dmaapResponse);
153                         logger.debug("{}",manager);
154                         assertTrue(result.equals(PolicyResult.FAILURE));
155                         assertFalse(manager.isOperationComplete());
156                         assertFalse(manager.isOperationRunning());
157                         //
158                         // Retry it
159                         //
160                         request = manager.startOperation(onset);
161                         logger.debug("{}",manager);
162                         assertNotNull(request);
163                         assertTrue(request instanceof LCMRequestWrapper);
164                         dmaapRequest = (LCMRequestWrapper) request;
165                         appcRequest = dmaapRequest.getBody();
166                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
167                         assertFalse(manager.isOperationComplete());
168                         assertTrue(manager.isOperationRunning());
169                         //
170                         // 
171                         //
172                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
173                         logger.debug("{}",manager);
174                         appcResponse.getStatus().setCode(100);
175                         appcResponse.getStatus().setMessage("ACCEPT");
176                         dmaapResponse.setBody(appcResponse);
177                         //
178                         //
179                         //
180                         result = manager.onResponse(dmaapResponse);
181                         logger.debug("{}",manager);
182                         assertTrue(result == null);
183                         assertFalse(manager.isOperationComplete());
184                         assertTrue(manager.isOperationRunning());
185                         //
186                         // Now we are going to Fail it
187                         //
188                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
189                         appcResponse.getStatus().setCode(401);
190                         appcResponse.getStatus().setMessage("AppC failed for some reason");
191                         dmaapResponse.setBody(appcResponse);
192                         result = manager.onResponse(dmaapResponse);
193                         logger.debug("{}",manager);
194                         assertTrue(result.equals(PolicyResult.FAILURE));
195                         //
196                         // Should be complete now
197                         //
198                         assertTrue(manager.isOperationComplete());
199                         assertFalse(manager.isOperationRunning());
200                         assertNotNull(manager.getOperationResult());
201                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
202                         assertTrue(manager.getHistory().size() == 2);
203                 } catch (ControlLoopException | AAIException e) {
204                         fail(e.getMessage());
205                 }
206         }
207
208         @Test
209         public void testTimeout() {
210                 //
211                 // Load up the policy
212                 //
213                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
214                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
215                 try {
216                         //
217                         // Create a processor
218                         //
219                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
220                         //
221                         // create the manager
222                         //
223                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
224                         VirtualControlLoopNotification notification = eventManager.activate(onset);
225                         
226                         assertNotNull(notification);
227                         assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
228
229                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
230                         //
231                         //
232                         //
233                         logger.debug("{}",manager);
234                         assertFalse(manager.isOperationComplete());
235                         assertFalse(manager.isOperationRunning());
236                         //
237                         // Start
238                         //
239                         Object request = manager.startOperation(onset);
240                         logger.debug("{}",manager);
241                         assertNotNull(request);
242                         assertTrue((request) instanceof LCMRequestWrapper);
243                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
244                         LCMRequest appcRequest = dmaapRequest.getBody();
245                         assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
246                         assertFalse(manager.isOperationComplete());
247                         assertTrue(manager.isOperationRunning());
248                         //
249                         // Accept
250                         //
251                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
252                         LCMResponse appcResponse = new LCMResponse(appcRequest);
253                 dmaapResponse.setBody(appcResponse);
254                         appcResponse.getStatus().setCode(100);
255                         appcResponse.getStatus().setMessage("ACCEPT");
256                         //
257                         //
258                         //
259                         PolicyResult result = manager.onResponse(dmaapResponse);
260                         logger.debug("{}",manager);
261                         assertTrue(result == null);
262                         assertFalse(manager.isOperationComplete());
263                         assertTrue(manager.isOperationRunning());
264                         //
265                         // Now we are going to simulate Timeout
266                         //
267                         manager.setOperationHasTimedOut();
268                         logger.debug("{}",manager);
269                         assertTrue(manager.isOperationComplete());
270                         assertFalse(manager.isOperationRunning());
271                         assertTrue(manager.getHistory().size() == 1);
272                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
273                         //
274                         // Now we are going to Fail the previous request
275                         //
276                         appcResponse = new LCMResponse(appcRequest);
277                         appcResponse.getStatus().setCode(401);
278                         appcResponse.getStatus().setMessage("AppC failed for some reason");
279                         dmaapResponse.setBody(appcResponse);
280                         result = manager.onResponse(dmaapResponse);
281                         logger.debug("{}",manager);
282                         //
283                         //
284                         //
285                         assertTrue(manager.isOperationComplete());
286                         assertFalse(manager.isOperationRunning());
287                         assertTrue(manager.getHistory().size() == 1);
288                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
289                 } catch (ControlLoopException | AAIException e) {
290                         fail(e.getMessage());
291                 }
292         }
293
294 }