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.appc.Request;
34 import org.onap.policy.appc.Response;
35 import org.onap.policy.appc.ResponseCode;
36 import org.onap.policy.appc.ResponseValue;
37 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<String, String>();
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 Request);
95 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
96 assertFalse(manager.isOperationComplete());
97 assertTrue(manager.isOperationRunning());
101 Response response = new Response((Request) request);
102 response.Status.Code = ResponseCode.ACCEPT.getValue();
103 response.Status.Value = ResponseValue.ACCEPT.toString();
107 PolicyResult result = manager.onResponse(response);
108 logger.debug("{}",manager);
109 assertTrue(result == null);
110 assertFalse(manager.isOperationComplete());
111 assertTrue(manager.isOperationRunning());
113 // Now we are going to Fail it
115 response = new Response((Request) request);
116 response.Status.Code = ResponseCode.FAILURE.getValue();
117 response.Status.Value = ResponseValue.FAILURE.toString();
118 response.Status.Description = "AppC failed for some reason";
119 result = manager.onResponse(response);
120 logger.debug("{}",manager);
121 assertTrue(result.equals(PolicyResult.FAILURE));
122 assertFalse(manager.isOperationComplete());
123 assertFalse(manager.isOperationRunning());
127 request = manager.startOperation(onset);
128 logger.debug("{}",manager);
129 assertNotNull(request);
130 assertTrue(request instanceof Request);
131 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("2"));
132 assertFalse(manager.isOperationComplete());
133 assertTrue(manager.isOperationRunning());
137 response = new Response((Request) request);
138 logger.debug("{}",manager);
139 response.Status.Code = ResponseCode.ACCEPT.getValue();
140 response.Status.Value = ResponseValue.ACCEPT.toString();
144 result = manager.onResponse(response);
145 logger.debug("{}",manager);
146 assertTrue(result == null);
147 assertFalse(manager.isOperationComplete());
148 assertTrue(manager.isOperationRunning());
150 // Now we are going to Fail it
152 response = new Response((Request) request);
153 response.Status.Code = ResponseCode.FAILURE.getValue();
154 response.Status.Value = ResponseValue.FAILURE.toString();
155 response.Status.Description = "AppC failed for some reason";
156 result = manager.onResponse(response);
157 logger.debug("{}",manager);
158 assertTrue(result.equals(PolicyResult.FAILURE));
160 // Should be complete now
162 assertTrue(manager.isOperationComplete());
163 assertFalse(manager.isOperationRunning());
164 assertNotNull(manager.getOperationResult());
165 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
166 assertTrue(manager.getHistory().size() == 2);
167 } catch (ControlLoopException e) {
168 fail(e.getMessage());
173 public void testTimeout() {
175 // Load up the policy
177 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
178 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
181 // Create a processor
183 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
185 // create the manager
187 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
189 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
193 logger.debug("{}",manager);
194 assertFalse(manager.isOperationComplete());
195 assertFalse(manager.isOperationRunning());
199 Object request = manager.startOperation(onset);
200 logger.debug("{}",manager);
201 assertNotNull(request);
202 assertTrue(request instanceof Request);
203 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
204 assertFalse(manager.isOperationComplete());
205 assertTrue(manager.isOperationRunning());
209 Response response = new Response((Request) request);
210 response.Status.Code = ResponseCode.ACCEPT.getValue();
211 response.Status.Value = ResponseValue.ACCEPT.toString();
215 PolicyResult result = manager.onResponse(response);
216 logger.debug("{}",manager);
217 assertTrue(result == null);
218 assertFalse(manager.isOperationComplete());
219 assertTrue(manager.isOperationRunning());
221 // Now we are going to simulate Timeout
223 manager.setOperationHasTimedOut();
224 logger.debug("{}",manager);
225 assertTrue(manager.isOperationComplete());
226 assertFalse(manager.isOperationRunning());
227 assertTrue(manager.getHistory().size() == 1);
228 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
230 // Now we are going to Fail the previous request
232 response = new Response((Request) request);
233 response.Status.Code = ResponseCode.FAILURE.getValue();
234 response.Status.Value = ResponseValue.FAILURE.toString();
235 response.Status.Description = "AppC failed for some reason";
236 result = manager.onResponse(response);
237 logger.debug("{}",manager);
241 assertTrue(manager.isOperationComplete());
242 assertFalse(manager.isOperationRunning());
243 assertTrue(manager.getHistory().size() == 1);
244 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
245 } catch (ControlLoopException e) {
246 fail(e.getMessage());