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 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
111 logger.debug("{}",manager);
115 assertFalse(manager.isOperationComplete());
116 assertFalse(manager.isOperationRunning());
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());
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);
140 PolicyResult result = manager.onResponse(dmaapResponse);
141 logger.debug("{}",manager);
142 assertTrue(result == null);
143 assertFalse(manager.isOperationComplete());
144 assertTrue(manager.isOperationRunning());
146 // Now we are going to Fail it
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());
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());
172 appcResponse = new LCMResponse((LCMRequest) appcRequest);
173 logger.debug("{}",manager);
174 appcResponse.getStatus().setCode(100);
175 appcResponse.getStatus().setMessage("ACCEPT");
176 dmaapResponse.setBody(appcResponse);
180 result = manager.onResponse(dmaapResponse);
181 logger.debug("{}",manager);
182 assertTrue(result == null);
183 assertFalse(manager.isOperationComplete());
184 assertTrue(manager.isOperationRunning());
186 // Now we are going to Fail it
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));
196 // Should be complete now
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());
209 public void testTimeout() {
211 // Load up the policy
213 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
214 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
217 // Create a processor
219 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
221 // create the manager
223 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
224 VirtualControlLoopNotification notification = eventManager.activate(onset);
226 assertNotNull(notification);
227 assertEquals(ControlLoopNotificationType.ACTIVE, notification.notification);
229 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
233 logger.debug("{}",manager);
234 assertFalse(manager.isOperationComplete());
235 assertFalse(manager.isOperationRunning());
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());
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");
259 PolicyResult result = manager.onResponse(dmaapResponse);
260 logger.debug("{}",manager);
261 assertTrue(result == null);
262 assertFalse(manager.isOperationComplete());
263 assertTrue(manager.isOperationRunning());
265 // Now we are going to simulate Timeout
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));
274 // Now we are going to Fail the previous request
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);
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());