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.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.aai.util.AAIException;
36 import org.onap.policy.appclcm.LCMRequest;
37 import org.onap.policy.appclcm.LCMRequestWrapper;
38 import org.onap.policy.appclcm.LCMResponse;
39 import org.onap.policy.appclcm.LCMResponseWrapper;
40 import org.onap.policy.controlloop.ControlLoopEventStatus;
41 import org.onap.policy.controlloop.VirtualControlLoopEvent;
42 import org.onap.policy.controlloop.ControlLoopException;
43 import org.onap.policy.controlloop.Util;
44 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
45 import org.onap.policy.controlloop.policy.PolicyResult;
46 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
47 import org.onap.policy.drools.http.server.HttpServletServer;
48 import org.onap.policy.drools.system.PolicyEngine;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class ControlLoopOperationManagerTest {
53 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
54 private static VirtualControlLoopEvent onset;
56 onset = new VirtualControlLoopEvent();
57 onset.requestID = UUID.randomUUID();
58 onset.target = "generic-vnf.vnf-name";
59 onset.closedLoopAlarmStart = Instant.now();
60 onset.AAI = new HashMap<>();
61 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
62 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
64 /* Set environment properties */
65 PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
66 PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
67 PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
71 public static void setUpSimulator() {
73 org.onap.policy.simulators.Util.buildAaiSim();
74 } catch (Exception e) {
80 public static void tearDownSimulator() {
81 HttpServletServer.factory.destroy();
85 public void testRetriesFail() {
89 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
90 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
95 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
99 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
101 eventManager.checkEventSyntax(onset);
103 catch (ControlLoopException e) {
104 logger.warn(e.toString());
105 fail("The onset failed the syntax check");
108 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
109 logger.debug("{}",manager);
113 assertFalse(manager.isOperationComplete());
114 assertFalse(manager.isOperationRunning());
118 Object request = manager.startOperation(onset);
119 logger.debug("{}",manager);
120 assertNotNull(request);
121 assertTrue(request instanceof LCMRequestWrapper);
122 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
123 LCMRequest appcRequest = dmaapRequest.getBody();
124 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
125 assertFalse(manager.isOperationComplete());
126 assertTrue(manager.isOperationRunning());
130 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
131 LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
132 appcResponse.getStatus().setCode(100);
133 appcResponse.getStatus().setMessage("ACCEPT");
134 dmaapResponse.setBody(appcResponse);
138 PolicyResult result = manager.onResponse(dmaapResponse);
139 logger.debug("{}",manager);
140 assertTrue(result == null);
141 assertFalse(manager.isOperationComplete());
142 assertTrue(manager.isOperationRunning());
144 // Now we are going to Fail it
146 appcResponse = new LCMResponse(appcRequest);
147 appcResponse.getStatus().setCode(401);
148 appcResponse.getStatus().setMessage("AppC failed for some reason");
149 dmaapResponse.setBody(appcResponse);
150 result = manager.onResponse(dmaapResponse);
151 logger.debug("{}",manager);
152 assertTrue(result.equals(PolicyResult.FAILURE));
153 assertFalse(manager.isOperationComplete());
154 assertFalse(manager.isOperationRunning());
158 request = manager.startOperation(onset);
159 logger.debug("{}",manager);
160 assertNotNull(request);
161 assertTrue(request instanceof LCMRequestWrapper);
162 dmaapRequest = (LCMRequestWrapper) request;
163 appcRequest = dmaapRequest.getBody();
164 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
165 assertFalse(manager.isOperationComplete());
166 assertTrue(manager.isOperationRunning());
170 appcResponse = new LCMResponse((LCMRequest) appcRequest);
171 logger.debug("{}",manager);
172 appcResponse.getStatus().setCode(100);
173 appcResponse.getStatus().setMessage("ACCEPT");
174 dmaapResponse.setBody(appcResponse);
178 result = manager.onResponse(dmaapResponse);
179 logger.debug("{}",manager);
180 assertTrue(result == null);
181 assertFalse(manager.isOperationComplete());
182 assertTrue(manager.isOperationRunning());
184 // Now we are going to Fail it
186 appcResponse = new LCMResponse((LCMRequest) appcRequest);
187 appcResponse.getStatus().setCode(401);
188 appcResponse.getStatus().setMessage("AppC failed for some reason");
189 dmaapResponse.setBody(appcResponse);
190 result = manager.onResponse(dmaapResponse);
191 logger.debug("{}",manager);
192 assertTrue(result.equals(PolicyResult.FAILURE));
194 // Should be complete now
196 assertTrue(manager.isOperationComplete());
197 assertFalse(manager.isOperationRunning());
198 assertNotNull(manager.getOperationResult());
199 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
200 assertTrue(manager.getHistory().size() == 2);
201 } catch (ControlLoopException | AAIException e) {
202 fail(e.getMessage());
207 public void testTimeout() {
209 // Load up the policy
211 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
212 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
215 // Create a processor
217 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
219 // create the manager
221 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
223 eventManager.checkEventSyntax(onset);
225 catch (ControlLoopException e) {
226 logger.warn(e.toString());
227 fail("The onset failed the syntax check");
230 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
234 logger.debug("{}",manager);
235 assertFalse(manager.isOperationComplete());
236 assertFalse(manager.isOperationRunning());
240 Object request = manager.startOperation(onset);
241 logger.debug("{}",manager);
242 assertNotNull(request);
243 assertTrue((request) instanceof LCMRequestWrapper);
244 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
245 LCMRequest appcRequest = dmaapRequest.getBody();
246 assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
247 assertFalse(manager.isOperationComplete());
248 assertTrue(manager.isOperationRunning());
252 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
253 LCMResponse appcResponse = new LCMResponse(appcRequest);
254 dmaapResponse.setBody(appcResponse);
255 appcResponse.getStatus().setCode(100);
256 appcResponse.getStatus().setMessage("ACCEPT");
260 PolicyResult result = manager.onResponse(dmaapResponse);
261 logger.debug("{}",manager);
262 assertTrue(result == null);
263 assertFalse(manager.isOperationComplete());
264 assertTrue(manager.isOperationRunning());
266 // Now we are going to simulate Timeout
268 manager.setOperationHasTimedOut();
269 logger.debug("{}",manager);
270 assertTrue(manager.isOperationComplete());
271 assertFalse(manager.isOperationRunning());
272 assertTrue(manager.getHistory().size() == 1);
273 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
275 // Now we are going to Fail the previous request
277 appcResponse = new LCMResponse(appcRequest);
278 appcResponse.getStatus().setCode(401);
279 appcResponse.getStatus().setMessage("AppC failed for some reason");
280 dmaapResponse.setBody(appcResponse);
281 result = manager.onResponse(dmaapResponse);
282 logger.debug("{}",manager);
286 assertTrue(manager.isOperationComplete());
287 assertFalse(manager.isOperationRunning());
288 assertTrue(manager.getHistory().size() == 1);
289 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
290 } catch (ControlLoopException | AAIException e) {
291 fail(e.getMessage());