d41d8bf57357edf4328c82dc909966cdce513424
[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.ControlLoopTargetType;
44 import org.onap.policy.controlloop.Util;
45 import org.onap.policy.controlloop.policy.ControlLoopPolicy;
46 import org.onap.policy.controlloop.policy.PolicyResult;
47 import org.onap.policy.controlloop.processor.ControlLoopProcessor;
48 import org.onap.policy.drools.http.server.HttpServletServer;
49 import org.onap.policy.drools.system.PolicyEngine;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class ControlLoopOperationManagerTest {
54         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
55         private static VirtualControlLoopEvent onset;
56         static {
57                 onset = new VirtualControlLoopEvent();
58                 onset.requestID = UUID.randomUUID();
59                 onset.target = "generic-vnf.vnf-name";
60                 onset.target_type = ControlLoopTargetType.VNF;
61                 onset.closedLoopAlarmStart = Instant.now();
62                 onset.AAI = new HashMap<>();
63                 onset.AAI.put("generic-vnf.vnf-name", "testTriggerSource");
64                 onset.closedLoopEventStatus = ControlLoopEventStatus.ONSET;
65                 
66                 /* Set environment properties */
67         PolicyEngine.manager.setEnvironmentProperty("aai.url", "http://localhost:6666");
68         PolicyEngine.manager.setEnvironmentProperty("aai.username", "AAI");
69         PolicyEngine.manager.setEnvironmentProperty("aai.password", "AAI");
70         }
71
72         @BeforeClass
73     public static void setUpSimulator() {
74         try {
75             org.onap.policy.simulators.Util.buildAaiSim();
76         } catch (Exception e) {
77             fail(e.getMessage());
78         }
79     }
80
81     @AfterClass
82     public static void tearDownSimulator() {
83         HttpServletServer.factory.destroy();
84     }
85         
86         @Test
87         public void testRetriesFail() {
88                 //
89                 // Load up the policy
90                 //
91                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
92                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
93                 try {
94                         //
95                         // Create a processor
96                         //
97                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
98                         //
99                         // create the manager
100                         //
101                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
102                         try {
103                 eventManager.checkEventSyntax(onset);
104             }
105             catch (ControlLoopException e) {
106                 logger.warn(e.toString());
107                 fail("The onset failed the syntax check");
108             }
109                         
110                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
111                         logger.debug("{}",manager);
112                         //
113                         //
114                         //
115                         assertFalse(manager.isOperationComplete());
116                         assertFalse(manager.isOperationRunning());
117                         //
118                         // Start
119                         //
120                         Object request = manager.startOperation(onset);
121                         logger.debug("{}",manager);
122                         assertNotNull(request);
123                         assertTrue(request instanceof LCMRequestWrapper);
124                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
125                         LCMRequest appcRequest = dmaapRequest.getBody();
126                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("1"));
127                         assertFalse(manager.isOperationComplete());
128                         assertTrue(manager.isOperationRunning());
129                         //
130                         // Accept
131                         //
132                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
133                         LCMResponse appcResponse = new LCMResponse((LCMRequest) appcRequest);
134                         appcResponse.getStatus().setCode(100);
135                         appcResponse.getStatus().setMessage("ACCEPT");
136                         dmaapResponse.setBody(appcResponse);
137                         //
138                         //
139                         //
140                         PolicyResult result = manager.onResponse(dmaapResponse);
141                         logger.debug("{}",manager);
142                         assertTrue(result == null);
143                         assertFalse(manager.isOperationComplete());
144                         assertTrue(manager.isOperationRunning());
145                         //
146                         // Now we are going to Fail it
147                         //
148                         appcResponse = new LCMResponse(appcRequest);
149                         appcResponse.getStatus().setCode(401);
150                         appcResponse.getStatus().setMessage("AppC failed for some reason");
151                         dmaapResponse.setBody(appcResponse);
152                         result = manager.onResponse(dmaapResponse);
153                         logger.debug("{}",manager);
154                         assertTrue(result.equals(PolicyResult.FAILURE));
155                         assertFalse(manager.isOperationComplete());
156                         assertFalse(manager.isOperationRunning());
157                         //
158                         // Retry it
159                         //
160                         request = manager.startOperation(onset);
161                         logger.debug("{}",manager);
162                         assertNotNull(request);
163                         assertTrue(request instanceof LCMRequestWrapper);
164                         dmaapRequest = (LCMRequestWrapper) request;
165                         appcRequest = dmaapRequest.getBody();
166                         assertTrue(appcRequest.getCommonHeader().getSubRequestId().contentEquals("2"));
167                         assertFalse(manager.isOperationComplete());
168                         assertTrue(manager.isOperationRunning());
169                         //
170                         // 
171                         //
172                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
173                         logger.debug("{}",manager);
174                         appcResponse.getStatus().setCode(100);
175                         appcResponse.getStatus().setMessage("ACCEPT");
176                         dmaapResponse.setBody(appcResponse);
177                         //
178                         //
179                         //
180                         result = manager.onResponse(dmaapResponse);
181                         logger.debug("{}",manager);
182                         assertTrue(result == null);
183                         assertFalse(manager.isOperationComplete());
184                         assertTrue(manager.isOperationRunning());
185                         //
186                         // Now we are going to Fail it
187                         //
188                         appcResponse = new LCMResponse((LCMRequest) appcRequest);
189                         appcResponse.getStatus().setCode(401);
190                         appcResponse.getStatus().setMessage("AppC failed for some reason");
191                         dmaapResponse.setBody(appcResponse);
192                         result = manager.onResponse(dmaapResponse);
193                         logger.debug("{}",manager);
194                         assertTrue(result.equals(PolicyResult.FAILURE));
195                         //
196                         // Should be complete now
197                         //
198                         assertTrue(manager.isOperationComplete());
199                         assertFalse(manager.isOperationRunning());
200                         assertNotNull(manager.getOperationResult());
201                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_RETRIES));
202                         assertTrue(manager.getHistory().size() == 2);
203                 } catch (ControlLoopException | AAIException e) {
204                         fail(e.getMessage());
205                 }
206         }
207
208         @Test
209         public void testTimeout() {
210                 //
211                 // Load up the policy
212                 //
213                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
214                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
215                 try {
216                         //
217                         // Create a processor
218                         //
219                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
220                         //
221                         // create the manager
222                         //
223                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
224                         try {
225                             eventManager.checkEventSyntax(onset);
226                         }
227                         catch (ControlLoopException e) {
228                             logger.warn(e.toString());
229                             fail("The onset failed the syntax check");
230                         }
231
232                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
233                         //
234                         //
235                         //
236                         logger.debug("{}",manager);
237                         assertFalse(manager.isOperationComplete());
238                         assertFalse(manager.isOperationRunning());
239                         //
240                         // Start
241                         //
242                         Object request = manager.startOperation(onset);
243                         logger.debug("{}",manager);
244                         assertNotNull(request);
245                         assertTrue((request) instanceof LCMRequestWrapper);
246                         LCMRequestWrapper dmaapRequest = (LCMRequestWrapper) request;
247                         LCMRequest appcRequest = dmaapRequest.getBody();
248                         assertTrue((appcRequest).getCommonHeader().getSubRequestId().contentEquals("1"));
249                         assertFalse(manager.isOperationComplete());
250                         assertTrue(manager.isOperationRunning());
251                         //
252                         // Accept
253                         //
254                         LCMResponseWrapper dmaapResponse = new LCMResponseWrapper();
255                         LCMResponse appcResponse = new LCMResponse(appcRequest);
256                 dmaapResponse.setBody(appcResponse);
257                         appcResponse.getStatus().setCode(100);
258                         appcResponse.getStatus().setMessage("ACCEPT");
259                         //
260                         //
261                         //
262                         PolicyResult result = manager.onResponse(dmaapResponse);
263                         logger.debug("{}",manager);
264                         assertTrue(result == null);
265                         assertFalse(manager.isOperationComplete());
266                         assertTrue(manager.isOperationRunning());
267                         //
268                         // Now we are going to simulate Timeout
269                         //
270                         manager.setOperationHasTimedOut();
271                         logger.debug("{}",manager);
272                         assertTrue(manager.isOperationComplete());
273                         assertFalse(manager.isOperationRunning());
274                         assertTrue(manager.getHistory().size() == 1);
275                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
276                         //
277                         // Now we are going to Fail the previous request
278                         //
279                         appcResponse = new LCMResponse(appcRequest);
280                         appcResponse.getStatus().setCode(401);
281                         appcResponse.getStatus().setMessage("AppC failed for some reason");
282                         dmaapResponse.setBody(appcResponse);
283                         result = manager.onResponse(dmaapResponse);
284                         logger.debug("{}",manager);
285                         //
286                         //
287                         //
288                         assertTrue(manager.isOperationComplete());
289                         assertFalse(manager.isOperationRunning());
290                         assertTrue(manager.getHistory().size() == 1);
291                         assertTrue(manager.getOperationResult().equals(PolicyResult.FAILURE_TIMEOUT));
292                 } catch (ControlLoopException | AAIException e) {
293                         fail(e.getMessage());
294                 }
295         }
296
297 }