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.Test;
33 import org.onap.policy.aai.util.AAIException;
34 import org.onap.policy.appclcm.LCMRequest;
35 import org.onap.policy.appclcm.LCMRequestWrapper;
36 import org.onap.policy.appclcm.LCMResponse;
37 import org.onap.policy.appclcm.LCMResponseWrapper;
38 import org.onap.policy.controlloop.ControlLoopEventStatus;
39 import org.onap.policy.controlloop.VirtualControlLoopEvent;
40 import org.onap.policy.controlloop.ControlLoopException;
41 import org.onap.policy.controlloop.Util;
42 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
43 import org.onap.policy.controlloop.policy.PolicyResult;
44 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class ControlLoopOperationManagerTest {
49 private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
50 private static VirtualControlLoopEvent onset;
52 onset = new VirtualControlLoopEvent();
53 onset.requestID = UUID.randomUUID();
54 onset.target = "vserver.selflink";
55 onset.closedLoopAlarmStart = Instant.now();
56 onset.AAI = new HashMap<>();
57 onset.AAI.put("cloud-region.identity-url", "foo");
58 onset.AAI.put("vserver.selflink", "bar");
59 onset.AAI.put("vserver.is-closed-loop-disabled", "false");
60 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
61 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
65 public void testRetriesFail() {
69 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
70 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
75 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
79 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
81 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
82 logger.debug("{}",manager);
86 assertFalse(manager.isOperationComplete());
87 assertFalse(manager.isOperationRunning());
91 Object request = manager.startOperation(onset);
92 logger.debug("{}",manager);
93 assertNotNull(request);
94 assertTrue(request instanceof LCMRequestWrapper);
95 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
96 LCMRequest appcRequest = dmaapRequest.getBody();
97 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
98 assertFalse(manager.isOperationComplete());
99 assertTrue(manager.isOperationRunning());
103 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
104 LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
105 appcResponse.getStatus().setCode(100);
106 appcResponse.getStatus().setMessage("ACCEPT");
107 dmaapResponse.setBody(appcResponse);
111 PolicyResult result = manager.onResponse(dmaapResponse);
112 logger.debug("{}",manager);
113 assertTrue(result == null);
114 assertFalse(manager.isOperationComplete());
115 assertTrue(manager.isOperationRunning());
117 // Now we are going to Fail it
119 appcResponse = new LCMResponse(appcRequest);
120 appcResponse.getStatus().setCode(401);
121 appcResponse.getStatus().setMessage("AppC failed for some reason");
122 dmaapResponse.setBody(appcResponse);
123 result = manager.onResponse(dmaapResponse);
124 logger.debug("{}",manager);
125 assertTrue(result.equals(PolicyResult.FAILURE));
126 assertFalse(manager.isOperationComplete());
127 assertFalse(manager.isOperationRunning());
131 request = manager.startOperation(onset);
132 logger.debug("{}",manager);
133 assertNotNull(request);
134 assertTrue(request instanceof LCMRequestWrapper);
135 dmaapRequest = (LCMRequestWrapper) request;
136 appcRequest = dmaapRequest.getBody();
137 assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
138 assertFalse(manager.isOperationComplete());
139 assertTrue(manager.isOperationRunning());
143 appcResponse = new LCMResponse((LCMRequest) appcRequest);
144 logger.debug("{}",manager);
145 appcResponse.getStatus().setCode(100);
146 appcResponse.getStatus().setMessage("ACCEPT");
147 dmaapResponse.setBody(appcResponse);
151 result = manager.onResponse(dmaapResponse);
152 logger.debug("{}",manager);
153 assertTrue(result == null);
154 assertFalse(manager.isOperationComplete());
155 assertTrue(manager.isOperationRunning());
157 // Now we are going to Fail it
159 appcResponse = new LCMResponse((LCMRequest) appcRequest);
160 appcResponse.getStatus().setCode(401);
161 appcResponse.getStatus().setMessage("AppC failed for some reason");
162 dmaapResponse.setBody(appcResponse);
163 result = manager.onResponse(dmaapResponse);
164 logger.debug("{}",manager);
165 assertTrue(result.equals(PolicyResult.FAILURE));
167 // Should be complete now
169 assertTrue(manager.isOperationComplete());
170 assertFalse(manager.isOperationRunning());
171 assertNotNull(manager.getOperationResult());
172 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
173 assertTrue(manager.getHistory().size() == 2);
174 } catch (ControlLoopException | AAIException e) {
175 fail(e.getMessage());
180 public void testTimeout() {
182 // Load up the policy
184 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
185 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
188 // Create a processor
190 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
192 // create the manager
194 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
196 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
200 logger.debug("{}",manager);
201 assertFalse(manager.isOperationComplete());
202 assertFalse(manager.isOperationRunning());
206 Object request = manager.startOperation(onset);
207 logger.debug("{}",manager);
208 assertNotNull(request);
209 assertTrue((request) instanceof LCMRequestWrapper);
210 LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
211 LCMRequest appcRequest = dmaapRequest.getBody();
212 assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
213 assertFalse(manager.isOperationComplete());
214 assertTrue(manager.isOperationRunning());
218 LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
219 LCMResponse appcResponse = new LCMResponse(appcRequest);
220 dmaapResponse.setBody(appcResponse);
221 appcResponse.getStatus().setCode(100);
222 appcResponse.getStatus().setMessage("ACCEPT");
226 PolicyResult result = manager.onResponse(dmaapResponse);
227 logger.debug("{}",manager);
228 assertTrue(result == null);
229 assertFalse(manager.isOperationComplete());
230 assertTrue(manager.isOperationRunning());
232 // Now we are going to simulate Timeout
234 manager.setOperationHasTimedOut();
235 logger.debug("{}",manager);
236 assertTrue(manager.isOperationComplete());
237 assertFalse(manager.isOperationRunning());
238 assertTrue(manager.getHistory().size() == 1);
239 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
241 // Now we are going to Fail the previous request
243 appcResponse = new LCMResponse(appcRequest);
244 appcResponse.getStatus().setCode(401);
245 appcResponse.getStatus().setMessage("AppC failed for some reason");
246 dmaapResponse.setBody(appcResponse);
247 result = manager.onResponse(dmaapResponse);
248 logger.debug("{}",manager);
252 assertTrue(manager.isOperationComplete());
253 assertFalse(manager.isOperationRunning());
254 assertTrue(manager.getHistory().size() == 1);
255 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
256 } catch (ControlLoopException | AAIException e) {
257 fail(e.getMessage());