6116d987ec495a94a583f2268637ff1ec87eeee2
[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.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;
38
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;
47
48 public class ControlLoopOperationManagerTest {
49         private static final Logger logger = LoggerFactory.getLogger(ControlLoopOperationManagerTest.class);
50         private static VirtualControlLoopEvent onset;
51         static {
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;
62         }
63
64         @Test
65         public void testRetriesFail() {
66                 //
67                 // Load up the policy
68                 //
69                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
70                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
71                 try {
72                         //
73                         // Create a processor
74                         //
75                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
76                         //
77                         // create the manager
78                         //
79                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
80
81                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
82                         logger.debug("{}",manager);
83                         //
84                         //
85                         //
86                         assertFalse(manager.isOperationComplete());
87                         assertFalse(manager.isOperationRunning());
88                         //
89                         // Start
90                         //
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());
98                         //
99                         // Accept
100                         //
101                         Response response = new Response((Request) request);
102                         response.Status.Code = ResponseCode.ACCEPT.getValue();
103                         response.Status.Value = ResponseValue.ACCEPT.toString();
104                         //
105                         //
106                         //
107                         PolicyResult result = manager.onResponse(response);
108                         logger.debug("{}",manager);
109                         assertTrue(result == null);
110                         assertFalse(manager.isOperationComplete());
111                         assertTrue(manager.isOperationRunning());
112                         //
113                         // Now we are going to Fail it
114                         //
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());
124                         //
125                         // Retry it
126                         //
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());
134                         //
135                         // 
136                         //
137                         response = new Response((Request) request);
138                         logger.debug("{}",manager);
139                         response.Status.Code = ResponseCode.ACCEPT.getValue();
140                         response.Status.Value = ResponseValue.ACCEPT.toString();
141                         //
142                         //
143                         //
144                         result = manager.onResponse(response);
145                         logger.debug("{}",manager);
146                         assertTrue(result == null);
147                         assertFalse(manager.isOperationComplete());
148                         assertTrue(manager.isOperationRunning());
149                         //
150                         // Now we are going to Fail it
151                         //
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));
159                         //
160                         // Should be complete now
161                         //
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());
169                 }
170         }
171
172         @Test
173         public void testTimeout() {
174                 //
175                 // Load up the policy
176                 //
177                 final Util.Pair<ControlLoopPolicy, String> pair = Util.loadYaml("src/test/resources/test.yaml");
178                 onset.closedLoopControlName = pair.a.getControlLoop().getControlLoopName();
179                 try {
180                         //
181                         // Create a processor
182                         //
183                         ControlLoopProcessor processor = new ControlLoopProcessor(pair.b);
184                         //
185                         // create the manager
186                         //
187                         ControlLoopEventManager eventManager = new ControlLoopEventManager(onset.closedLoopControlName, onset.requestID);
188
189                         ControlLoopOperationManager manager = new ControlLoopOperationManager(onset, processor.getCurrentPolicy(), eventManager);
190                         //
191                         //
192                         //
193                         logger.debug("{}",manager);
194                         assertFalse(manager.isOperationComplete());
195                         assertFalse(manager.isOperationRunning());
196                         //
197                         // Start
198                         //
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());
206                         //
207                         // Accept
208                         //
209                         Response response = new Response((Request) request);
210                         response.Status.Code = ResponseCode.ACCEPT.getValue();
211                         response.Status.Value = ResponseValue.ACCEPT.toString();
212                         //
213                         //
214                         //
215                         PolicyResult result = manager.onResponse(response);
216                         logger.debug("{}",manager);
217                         assertTrue(result == null);
218                         assertFalse(manager.isOperationComplete());
219                         assertTrue(manager.isOperationRunning());
220                         //
221                         // Now we are going to simulate Timeout
222                         //
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));
229                         //
230                         // Now we are going to Fail the previous request
231                         //
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);
238                         //
239                         //
240                         //
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());
247                 }
248         }
249
250 }