2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.eventmanager;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.time.Instant;
30 import java.util.HashMap;
31 import java.util.UUID;
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;
56 public class ControlLoopOperationManagerTest {
57 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
58 private static VirtualControlLoopEvent onset;
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;
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");
76 public static void setUpSimulator() {
78 org.onap.policy.simulators.Util.buildAaiSim();
79 } catch (Exception e) {
85 public static void tearDownSimulator() {
86 HttpServletServer.factory.destroy();
90 public void testRetriesFail() {
94 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
95 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
100 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
102 // create the manager
104 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
105 VirtualControlLoopNotification notification = eventManager.activate(onset);
107 assertNotNull(notification);
108 assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
110 ControlLoopEventManager.NEW_EVENT_STATUS status = null;
112 status = eventManager.onNewEvent(onset);
113 } catch (AAIException e) {
114 logger.warn(e.toString());
115 fail("A&AI Query Failed");
117 assertNotNull(status);
118 assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
120 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
121 logger.debug("{}",manager);
125 assertFalse(manager.isOperationComplete());
126 assertFalse(manager.isOperationRunning());
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());
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);
150 PolicyResult result = manager.onResponse(dmaapResponse);
151 logger.debug("{}",manager);
152 assertTrue(result == null);
153 assertFalse(manager.isOperationComplete());
154 assertTrue(manager.isOperationRunning());
156 // Now we are going to Fail it
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());
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());
182 appcResponse = new LCMResponse((LCMRequest) appcRequest);
183 logger.debug("{}",manager);
184 appcResponse.getStatus().setCode(100);
185 appcResponse.getStatus().setMessage("ACCEPT");
186 dmaapResponse.setBody(appcResponse);
190 result = manager.onResponse(dmaapResponse);
191 logger.debug("{}",manager);
192 assertTrue(result == null);
193 assertFalse(manager.isOperationComplete());
194 assertTrue(manager.isOperationRunning());
196 // Now we are going to Fail it
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));
206 // Should be complete now
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());
219 public void testTimeout() {
221 // Load up the policy
223 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
224 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
227 // Create a processor
229 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
231 // create the manager
233 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
234 VirtualControlLoopNotification notification = eventManager.activate(onset);
236 assertNotNull(notification);
237 assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
239 ControlLoopEventManager.NEW_EVENT_STATUS status = null;
241 status = eventManager.onNewEvent(onset);
242 } catch (AAIException e) {
243 logger.warn(e.toString());
244 fail("A&AI Query Failed");
246 assertNotNull(status);
247 assertEquals(ControlLoopEventManager.NEW_EVENT_STATUS.FIRST_ONSET, status);
249 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
253 logger.debug("{}",manager);
254 assertFalse(manager.isOperationComplete());
255 assertFalse(manager.isOperationRunning());
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());
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");
279 PolicyResult result = manager.onResponse(dmaapResponse);
280 logger.debug("{}",manager);
281 assertTrue(result == null);
282 assertFalse(manager.isOperationComplete());
283 assertTrue(manager.isOperationRunning());
285 // Now we are going to simulate Timeout
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));
294 // Now we are going to Fail the previous request
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);
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());