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;
46 public class ControlLoopOperationManagerTest {
48 private static VirtualControlLoopEvent onset;
50 onset = new VirtualControlLoopEvent();
51 onset.requestID = UUID.randomUUID();
52 onset.target = "vserver.selflink";
53 onset.closedLoopAlarmStart = Instant.now();
54 onset.AAI = new HashMap<String, String>();
55 onset.AAI.put("cloud-region.identity-url", "foo");
56 onset.AAI.put("vserver.selflink", "bar");
57 onset.AAI.put("vserver.is-closed-loop-disabled", "false");
58 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
59 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
63 public void testRetriesFail() {
67 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
68 onset.closedLoopControlName = pair.a.controlLoop.controlLoopName;
73 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
77 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
79 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
80 System.out.println(manager);
84 assertFalse(manager.isOperationComplete());
85 assertFalse(manager.isOperationRunning());
89 Object request = manager.startOperation(onset);
90 System.out.println(manager);
91 assertNotNull(request);
92 assertTrue(request instanceof Request);
93 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
94 assertFalse(manager.isOperationComplete());
95 assertTrue(manager.isOperationRunning());
99 Response response = new Response((Request) request);
100 response.Status.Code = ResponseCode.ACCEPT.getValue();
101 response.Status.Value = ResponseValue.ACCEPT.toString();
105 PolicyResult result = manager.onResponse(response);
106 System.out.println(manager);
107 assertTrue(result == null);
108 assertFalse(manager.isOperationComplete());
109 assertTrue(manager.isOperationRunning());
111 // Now we are going to Fail it
113 response = new Response((Request) request);
114 response.Status.Code = ResponseCode.FAILURE.getValue();
115 response.Status.Value = ResponseValue.FAILURE.toString();
116 response.Status.Description = "AppC failed for some reason";
117 result = manager.onResponse(response);
118 System.out.println(manager);
119 assertTrue(result.equals(PolicyResult.FAILURE));
120 assertFalse(manager.isOperationComplete());
121 assertFalse(manager.isOperationRunning());
125 request = manager.startOperation(onset);
126 System.out.println(manager);
127 assertNotNull(request);
128 assertTrue(request instanceof Request);
129 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("2"));
130 assertFalse(manager.isOperationComplete());
131 assertTrue(manager.isOperationRunning());
135 response = new Response((Request) request);
136 System.out.println(manager);
137 response.Status.Code = ResponseCode.ACCEPT.getValue();
138 response.Status.Value = ResponseValue.ACCEPT.toString();
142 result = manager.onResponse(response);
143 System.out.println(manager);
144 assertTrue(result == null);
145 assertFalse(manager.isOperationComplete());
146 assertTrue(manager.isOperationRunning());
148 // Now we are going to Fail it
150 response = new Response((Request) request);
151 response.Status.Code = ResponseCode.FAILURE.getValue();
152 response.Status.Value = ResponseValue.FAILURE.toString();
153 response.Status.Description = "AppC failed for some reason";
154 result = manager.onResponse(response);
155 System.out.println(manager);
156 assertTrue(result.equals(PolicyResult.FAILURE));
158 // Should be complete now
160 assertTrue(manager.isOperationComplete());
161 assertFalse(manager.isOperationRunning());
162 assertNotNull(manager.getOperationResult());
163 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
164 assertTrue(manager.getHistory().size() == 2);
165 } catch (ControlLoopException e) {
166 fail(e.getMessage());
171 public void testTimeout() {
173 // Load up the policy
175 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
176 onset.closedLoopControlName = pair.a.controlLoop.controlLoopName;
179 // Create a processor
181 ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
183 // create the manager
185 ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
187 ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
191 System.out.println(manager);
192 assertFalse(manager.isOperationComplete());
193 assertFalse(manager.isOperationRunning());
197 Object request = manager.startOperation(onset);
198 System.out.println(manager);
199 assertNotNull(request);
200 assertTrue(request instanceof Request);
201 assertTrue(((Request)request).CommonHeader.SubRequestID.contentEquals("1"));
202 assertFalse(manager.isOperationComplete());
203 assertTrue(manager.isOperationRunning());
207 Response response = new Response((Request) request);
208 response.Status.Code = ResponseCode.ACCEPT.getValue();
209 response.Status.Value = ResponseValue.ACCEPT.toString();
213 PolicyResult result = manager.onResponse(response);
214 System.out.println(manager);
215 assertTrue(result == null);
216 assertFalse(manager.isOperationComplete());
217 assertTrue(manager.isOperationRunning());
219 // Now we are going to simulate Timeout
221 manager.setOperationHasTimedOut();
222 System.out.println(manager);
223 assertTrue(manager.isOperationComplete());
224 assertFalse(manager.isOperationRunning());
225 assertTrue(manager.getHistory().size() == 1);
226 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
228 // Now we are going to Fail the previous request
230 response = new Response((Request) request);
231 response.Status.Code = ResponseCode.FAILURE.getValue();
232 response.Status.Value = ResponseValue.FAILURE.toString();
233 response.Status.Description = "AppC failed for some reason";
234 result = manager.onResponse(response);
235 System.out.println(manager);
239 assertTrue(manager.isOperationComplete());
240 assertFalse(manager.isOperationRunning());
241 assertTrue(manager.getHistory().size() == 1);
242 assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
243 } catch (ControlLoopException e) {
244 fail(e.getMessage());