Supports new aai changes.
[policy/models.git] / models-interactions / model-actors / actor.so / src / test / java / org / onap / policy / controlloop / actor / so / SoActorServiceProviderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * TestSOActorServiceProvider
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2018-2019 AT&T. All rights reserved.
8  * Modifications Copyright (C) 2019 Nordix Foundation.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.controlloop.actor.so;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.fail;
30
31 import java.io.IOException;
32 import java.nio.charset.StandardCharsets;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.TreeMap;
37 import java.util.UUID;
38 import org.apache.commons.io.IOUtils;
39 import org.eclipse.persistence.exceptions.JAXBException;
40 import org.junit.Test;
41 import org.onap.policy.aai.AaiCqResponse;
42 import org.onap.policy.aai.AaiNqResponse;
43 import org.onap.policy.aai.AaiNqResponseWrapper;
44 import org.onap.policy.controlloop.ControlLoopOperation;
45 import org.onap.policy.controlloop.VirtualControlLoopEvent;
46 import org.onap.policy.controlloop.policy.Policy;
47 import org.onap.policy.controlloop.policy.Target;
48 import org.onap.policy.so.SoOperationType;
49 import org.onap.policy.so.SoRequest;
50 import org.onap.policy.so.SoRequestParameters;
51 import org.onap.policy.so.util.Serialization;
52
53 public class SoActorServiceProviderTest {
54
55     private static final String VF_MODULE_CREATE = "VF Module Create";
56     private static final String VF_MODULE_DELETE = "VF Module Delete";
57
58     @Test
59     public void testConstructRequest() throws Exception {
60         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
61         final ControlLoopOperation operation = new ControlLoopOperation();
62         final AaiNqResponseWrapper aaiNqResp = loadAaiResponse(onset, "aai/AaiNqResponse-Full.json");
63
64         final UUID requestId = UUID.randomUUID();
65         onset.setRequestId(requestId);
66
67         Policy policy = new Policy();
68         policy.setActor("Dorothy");
69         policy.setRecipe("GoToOz");
70
71         instantiateTarget(policy);
72
73         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
74
75         policy.setActor("SO");
76         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
77
78         policy.setRecipe(VF_MODULE_CREATE);
79
80         // empty policy payload
81         SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
82         assertNotNull(request);
83
84         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
85         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
86         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
87
88         // non-empty policy payload
89         policy.setPayload(makePayload());
90         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
91         assertNotNull(request);
92         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
93         assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
94         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
95         assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
96
97         // payload with config, but no request params
98         policy.setPayload(makePayload());
99         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
100         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
101         assertNotNull(request);
102         assertNull(request.getRequestDetails().getRequestParameters());
103         assertNotNull(request.getRequestDetails().getConfigurationParameters());
104
105         // payload with request, but no config params
106         policy.setPayload(makePayload());
107         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
108         request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
109         assertNotNull(request);
110         assertNotNull(request.getRequestDetails().getRequestParameters());
111         assertNull(request.getRequestDetails().getConfigurationParameters());
112
113         // null response
114         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
115
116         // response has no base VF module
117         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
118                 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
119
120         policy.setTarget(null);
121
122         // response has no non-base VF modules (other than the "dummy")
123         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
124                 loadAaiResponse(onset, "aai/AaiNqResponse-NoNonBase.json")));
125
126         instantiateTarget(policy);
127         policy.setRecipe(VF_MODULE_DELETE);
128         SoRequest deleteRequest = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
129         assertNotNull(deleteRequest);
130         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
131
132         /*
133          * NOTE: The remaining tests must be done in order
134          */
135
136         policy.setRecipe(VF_MODULE_CREATE);
137
138         // null tenant
139         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
140                 .remove(1);
141         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
142
143         // null service item
144         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
145                 .setItems(null);
146         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
147
148         // null response
149         aaiNqResp.setAaiNqResponse(null);
150         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
151     }
152
153     private void instantiateTarget(Policy policy) {
154
155         Target target = new Target();
156         target.setModelCustomizationId("3e2d67ad-3495-4732-82f6-b0b872791fff");
157         target.setModelInvariantId("90b793b5-b8ae-4c36-b10b-4b6372859d3a");
158         target.setModelName("SproutScalingVf..scaling_sprout..module-1");
159         target.setModelVersion("1");
160         target.setModelVersionId("2210154d-e61a-4d7f-8fb9-0face1aee3f8");
161
162         policy.setTarget(target);
163     }
164
165     private void instantiateTargetCq(Policy policy) {
166
167         Target target = new Target();
168         target.setModelCustomizationId("47958575-138f-452a-8c8d-d89b595f8164");
169         target.setModelInvariantId("e6130d03-56f1-4b0a-9a1d-e1b2ebc30e0e");
170         target.setModelName("VfwclVfwsnkBbefb8ce2bde..base_vfw..module-0");
171         target.setModelVersion("1");
172         target.setModelVersionId("94b18b1d-cc91-4f43-911a-e6348665f292");
173
174         policy.setTarget(target);
175     }
176
177     @Test
178     public void testSendRequest() {
179         try {
180             SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
181         } catch (Exception e) {
182             fail("Test should not throw an exception");
183         }
184     }
185
186     @Test
187     public void testMethods() {
188         SoActorServiceProvider sp = new SoActorServiceProvider();
189
190         assertEquals("SO", sp.actor());
191         assertEquals(2, sp.recipes().size());
192         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
193         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
194         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
195         assertEquals(0, sp.recipeTargets("unknown recipe").size());
196         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
197     }
198
199     @Test
200     public void testConstructRequestCq() throws Exception {
201         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
202         final ControlLoopOperation operation = new ControlLoopOperation();
203         final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
204         final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
205         final UUID requestId = UUID.randomUUID();
206         onset.setRequestId(requestId);
207
208         Policy policy = new Policy();
209         policy.setActor("Dorothy");
210         policy.setRecipe("GoToOz");
211
212         instantiateTargetCq(policy);
213
214         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
215
216         policy.setActor("SO");
217
218         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
219
220         policy.setRecipe(VF_MODULE_CREATE);
221
222         // empty policy payload
223         SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
224         assertNotNull(request);
225
226         assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
227         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
228         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
229
230         // non-empty policy payload
231         policy.setPayload(makePayload());
232         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
233         assertNotNull(request);
234         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
235         assertEquals("avalue", request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
236         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
237         assertEquals("cvalue", request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
238
239         // payload with config, but no request params
240         policy.setPayload(makePayload());
241         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
242         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
243         assertNotNull(request);
244         assertNull(request.getRequestDetails().getRequestParameters());
245         assertNotNull(request.getRequestDetails().getConfigurationParameters());
246
247         // payload with request, but no config params
248         policy.setPayload(makePayload());
249         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
250         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
251         assertNotNull(request);
252         assertNotNull(request.getRequestDetails().getRequestParameters());
253         assertNull(request.getRequestDetails().getConfigurationParameters());
254
255         // null response
256         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
257
258         instantiateTargetCq(policy);
259         policy.setRecipe(VF_MODULE_DELETE);
260         SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
261         assertNotNull(deleteRequest);
262         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
263
264         /*
265          * NOTE: The remaining tests must be done in order
266          */
267
268         policy.setRecipe(VF_MODULE_CREATE);
269
270         // null tenant
271         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
272
273         // null service item
274         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
275
276         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
277     }
278
279     /**
280      * Reads an AAI vserver named-query response from a file.
281      *
282      * @param fileName name of the file containing the JSON response
283      * @return output from the AAI vserver named-query
284      * @throws IOException if the file cannot be read
285      * @throws JAXBException throws JAXBException
286      */
287     private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException, JAXBException {
288         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
289         return new AaiCqResponse(resp);
290     }
291
292
293
294     /**
295      * Creates a policy payload containing request & configuration parameters.
296      *
297      * @return the payload
298      */
299     private Map<String, String> makePayload() {
300         Map<String, String> payload = new TreeMap<>();
301
302         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
303         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
304
305         return payload;
306     }
307
308     /**
309      * Creates request parameters.
310      *
311      * @return request parameters, encoded as JSON
312      */
313     private String makeReqParams() {
314         SoRequestParameters params = new SoRequestParameters();
315
316         params.setUsePreload(true);
317
318         Map<String, String> map = new TreeMap<>();
319         map.put("akey", "avalue");
320
321         List<Map<String, String>> lst = new LinkedList<>();
322         lst.add(map);
323
324         params.setUserParams(lst);
325
326         return Serialization.gsonPretty.toJson(params);
327     }
328
329     /**
330      * Creates configuration parameters.
331      *
332      * @return configuration parameters, encoded as JSON
333      */
334     private String makeConfigParams() {
335         Map<String, String> map = new TreeMap<>();
336         map.put("ckey", "cvalue");
337
338         List<Map<String, String>> lst = new LinkedList<>();
339         lst.add(map);
340
341         return Serialization.gsonPretty.toJson(lst);
342     }
343
344     /**
345      * Reads an AAI vserver named-query response from a file.
346      *
347      * @param onset the ONSET event
348      * @param fileName name of the file containing the JSON response
349      * @return output from the AAI vserver named-query
350      * @throws IOException if the file cannot be read
351      */
352     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
353         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
354         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
355
356         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);
357     }
358 }