caa17ff381bf43a6d976353c8054a8bf1366e23c
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * unit test
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
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;
27
28 import java.time.Instant;
29 import java.util.HashMap;
30 import java.util.UUID;
31
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;
51
52 public class ControlLoopOperationManagerTest {
53         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
54         private static VirtualControlLoopEvent onset;
55         static {
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;
63                 
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");
68         }
69
70         @BeforeClass
71     public static void setUpSimulator() {
72         try {
73             org.onap.policy.simulators.Util.buildAaiSim();
74         } catch (Exception e) {
75             fail(e.getMessage());
76         }
77     }
78
79     @AfterClass
80     public static void tearDownSimulator() {
81         HttpServletServer.factory.destroy();
82     }
83         
84         @Test
85         public void testRetriesFail() {
86                 //
87                 // Load up the policy
88                 //
89                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
90                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
91                 try {
92                         //
93                         // Create a processor
94                         //
95                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
96                         //
97                         // create the manager
98                         //
99                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
100                         try {
101                 eventManager.checkEventSyntax(onset);
102             }
103             catch (ControlLoopException e) {
104                 logger.warn(e.toString());
105                 fail("The onset failed the syntax check");
106             }
107                         
108                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
109                         logger.debug("{}",manager);
110                         //
111                         //
112                         //
113                         assertFalse(manager.isOperationComplete());
114                         assertFalse(manager.isOperationRunning());
115                         //
116                         // Start
117                         //
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());
127                         //
128                         // Accept
129                         //
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);
135                         //
136                         //
137                         //
138                         PolicyResult result = manager.onResponse(dmaapResponse);
139                         logger.debug("{}",manager);
140                         assertTrue(result == null);
141                         assertFalse(manager.isOperationComplete());
142                         assertTrue(manager.isOperationRunning());
143                         //
144                         // Now we are going to Fail it
145                         //
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());
155                         //
156                         // Retry it
157                         //
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());
167                         //
168                         // 
169                         //
170                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
171                         logger.debug("{}",manager);
172                         appcResponse.getStatus().setCode(100);
173                         appcResponse.getStatus().setMessage("ACCEPT");
174                         dmaapResponse.setBody(appcResponse);
175                         //
176                         //
177                         //
178                         result = manager.onResponse(dmaapResponse);
179                         logger.debug("{}",manager);
180                         assertTrue(result == null);
181                         assertFalse(manager.isOperationComplete());
182                         assertTrue(manager.isOperationRunning());
183                         //
184                         // Now we are going to Fail it
185                         //
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));
193                         //
194                         // Should be complete now
195                         //
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());
203                 }
204         }
205
206         @Test
207         public void testTimeout() {
208                 //
209                 // Load up the policy
210                 //
211                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
212                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
213                 try {
214                         //
215                         // Create a processor
216                         //
217                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
218                         //
219                         // create the manager
220                         //
221                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
222                         try {
223                             eventManager.checkEventSyntax(onset);
224                         }
225                         catch (ControlLoopException e) {
226                             logger.warn(e.toString());
227                             fail("The onset failed the syntax check");
228                         }
229
230                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
231                         //
232                         //
233                         //
234                         logger.debug("{}",manager);
235                         assertFalse(manager.isOperationComplete());
236                         assertFalse(manager.isOperationRunning());
237                         //
238                         // Start
239                         //
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());
249                         //
250                         // Accept
251                         //
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");
257                         //
258                         //
259                         //
260                         PolicyResult result = manager.onResponse(dmaapResponse);
261                         logger.debug("{}",manager);
262                         assertTrue(result == null);
263                         assertFalse(manager.isOperationComplete());
264                         assertTrue(manager.isOperationRunning());
265                         //
266                         // Now we are going to simulate Timeout
267                         //
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));
274                         //
275                         // Now we are going to Fail the previous request
276                         //
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);
283                         //
284                         //
285                         //
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());
292                 }
293         }
294
295 }