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
 
  14  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  24 package org.onap.policy.controlloop.actor.so;
 
  26 import static org.junit.Assert.assertEquals;
 
  27 import static org.junit.Assert.assertNotNull;
 
  28 import static org.junit.Assert.assertNull;
 
  30 import java.io.IOException;
 
  31 import java.nio.charset.StandardCharsets;
 
  32 import java.util.LinkedList;
 
  33 import java.util.List;
 
  35 import java.util.TreeMap;
 
  36 import java.util.UUID;
 
  37 import org.apache.commons.io.IOUtils;
 
  38 import org.junit.Test;
 
  39 import org.onap.policy.aai.AaiCqResponse;
 
  40 import org.onap.policy.aai.AaiNqResponse;
 
  41 import org.onap.policy.aai.AaiNqResponseWrapper;
 
  42 import org.onap.policy.controlloop.ControlLoopOperation;
 
  43 import org.onap.policy.controlloop.VirtualControlLoopEvent;
 
  44 import org.onap.policy.controlloop.policy.Policy;
 
  45 import org.onap.policy.controlloop.policy.Target;
 
  46 import org.onap.policy.so.SoOperationType;
 
  47 import org.onap.policy.so.SoRequest;
 
  48 import org.onap.policy.so.SoRequestParameters;
 
  49 import org.onap.policy.so.util.Serialization;
 
  51 public class SoActorServiceProviderTest {
 
  53     private static final String C_VALUE = "cvalue";
 
  54     private static final String A_VALUE = "avalue";
 
  55     private static final String VF_MODULE_CREATE = "VF Module Create";
 
  56     private static final String VF_MODULE_DELETE = "VF Module Delete";
 
  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");
 
  64         final UUID requestId = UUID.randomUUID();
 
  65         onset.setRequestId(requestId);
 
  67         Policy policy = new Policy();
 
  68         policy.setActor("Dorothy");
 
  69         policy.setRecipe("GoToOz");
 
  71         instantiateTarget(policy);
 
  73         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
 
  75         policy.setActor("SO");
 
  76         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
 
  78         policy.setRecipe(VF_MODULE_CREATE);
 
  80         // empty policy payload
 
  81         SoRequest request = new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp);
 
  82         assertNotNull(request);
 
  84         assertEquals("my_module_3", request.getRequestDetails().getRequestInfo().getInstanceName());
 
  85         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
 
  86         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
 
  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(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
 
  94         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
 
  95         assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
 
  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());
 
 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());
 
 114         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, null));
 
 116         // response has no base VF module
 
 117         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy,
 
 118                 loadAaiResponse(onset, "aai/AaiNqResponse-NoBase.json")));
 
 120         policy.setTarget(null);
 
 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")));
 
 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());
 
 133          * NOTE: The remaining tests must be done in order
 
 136         policy.setRecipe(VF_MODULE_CREATE);
 
 139         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems()
 
 141         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
 
 144         aaiNqResp.getAaiNqResponse().getInventoryResponseItems().get(0).getItems().getInventoryResponseItems().get(0)
 
 146         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
 
 149         aaiNqResp.setAaiNqResponse(null);
 
 150         assertNull(new SoActorServiceProvider().constructRequest(onset, operation, policy, aaiNqResp));
 
 153     private void instantiateTarget(Policy policy) {
 
 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");
 
 162         policy.setTarget(target);
 
 165     private void instantiateTargetCq(Policy policy) {
 
 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");
 
 174         policy.setTarget(target);
 
 178     public void testSendRequest() {
 
 179         SoActorServiceProvider.sendRequest(UUID.randomUUID().toString(), null, null, null, null, null);
 
 183     public void testMethods() {
 
 184         SoActorServiceProvider sp = new SoActorServiceProvider();
 
 186         assertEquals("SO", sp.actor());
 
 187         assertEquals(2, sp.recipes().size());
 
 188         assertEquals(VF_MODULE_CREATE, sp.recipes().get(0));
 
 189         assertEquals(VF_MODULE_DELETE, sp.recipes().get(1));
 
 190         assertEquals(0, sp.recipePayloads(VF_MODULE_CREATE).size());
 
 191         assertEquals(0, sp.recipeTargets("unknown recipe").size());
 
 192         assertEquals(1, sp.recipeTargets(VF_MODULE_CREATE).size());
 
 196     public void testConstructRequestCq() throws Exception {
 
 197         VirtualControlLoopEvent onset = new VirtualControlLoopEvent();
 
 198         final ControlLoopOperation operation = new ControlLoopOperation();
 
 199         final AaiCqResponse aaiCqResp = loadAaiResponseCq("aai/AaiCqResponseFull.json");
 
 200         final AaiCqResponse aaiCqRespMissing = loadAaiResponseCq("aai/AaiCqResponseMissing.json");
 
 201         final UUID requestId = UUID.randomUUID();
 
 202         onset.setRequestId(requestId);
 
 204         Policy policy = new Policy();
 
 205         policy.setActor("Dorothy");
 
 206         policy.setRecipe("GoToOz");
 
 208         instantiateTargetCq(policy);
 
 210         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp));
 
 212         policy.setActor("SO");
 
 214         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
 
 216         policy.setRecipe(VF_MODULE_CREATE);
 
 218         // empty policy payload
 
 219         SoRequest request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
 
 220         assertNotNull(request);
 
 222         assertEquals("vfModuleName", request.getRequestDetails().getRequestInfo().getInstanceName());
 
 223         assertEquals("policy", request.getRequestDetails().getRequestInfo().getRequestorId());
 
 224         assertEquals("RegionOne", request.getRequestDetails().getCloudConfiguration().getLcpCloudRegionId());
 
 226         // non-empty policy payload
 
 227         policy.setPayload(makePayload());
 
 228         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
 
 229         assertNotNull(request);
 
 230         assertEquals(true, request.getRequestDetails().getRequestParameters().isUsePreload());
 
 231         assertEquals(A_VALUE, request.getRequestDetails().getRequestParameters().getUserParams().get(0).get("akey"));
 
 232         assertEquals(1, request.getRequestDetails().getConfigurationParameters().size());
 
 233         assertEquals(C_VALUE, request.getRequestDetails().getConfigurationParameters().get(0).get("ckey"));
 
 235         // payload with config, but no request params
 
 236         policy.setPayload(makePayload());
 
 237         policy.getPayload().remove(SoActorServiceProvider.REQ_PARAM_NM);
 
 238         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
 
 239         assertNotNull(request);
 
 240         assertNull(request.getRequestDetails().getRequestParameters());
 
 241         assertNotNull(request.getRequestDetails().getConfigurationParameters());
 
 243         // payload with request, but no config params
 
 244         policy.setPayload(makePayload());
 
 245         policy.getPayload().remove(SoActorServiceProvider.CONFIG_PARAM_NM);
 
 246         request = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
 
 247         assertNotNull(request);
 
 248         assertNotNull(request.getRequestDetails().getRequestParameters());
 
 249         assertNull(request.getRequestDetails().getConfigurationParameters());
 
 252         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
 
 254         instantiateTargetCq(policy);
 
 255         policy.setRecipe(VF_MODULE_DELETE);
 
 256         SoRequest deleteRequest = new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqResp);
 
 257         assertNotNull(deleteRequest);
 
 258         assertEquals(SoOperationType.DELETE_VF_MODULE, deleteRequest.getOperationType());
 
 261          * NOTE: The remaining tests must be done in order
 
 264         policy.setRecipe(VF_MODULE_CREATE);
 
 267         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
 
 270         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, aaiCqRespMissing));
 
 272         assertNull(new SoActorServiceProvider().constructRequestCq(onset, operation, policy, null));
 
 276      * Reads an AAI vserver named-query response from a file.
 
 278      * @param fileName name of the file containing the JSON response
 
 279      * @return output from the AAI vserver named-query
 
 280      * @throws IOException if the file cannot be read
 
 282     private AaiCqResponse loadAaiResponseCq(String fileName) throws IOException {
 
 283         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
 
 284         return new AaiCqResponse(resp);
 
 290      * Creates a policy payload containing request & configuration parameters.
 
 292      * @return the payload
 
 294     private Map<String, String> makePayload() {
 
 295         Map<String, String> payload = new TreeMap<>();
 
 297         payload.put(SoActorServiceProvider.REQ_PARAM_NM, makeReqParams());
 
 298         payload.put(SoActorServiceProvider.CONFIG_PARAM_NM, makeConfigParams());
 
 304      * Creates request parameters.
 
 306      * @return request parameters, encoded as JSON
 
 308     private String makeReqParams() {
 
 309         SoRequestParameters params = new SoRequestParameters();
 
 311         params.setUsePreload(true);
 
 313         Map<String, String> map = new TreeMap<>();
 
 314         map.put("akey", A_VALUE);
 
 316         List<Map<String, String>> lst = new LinkedList<>();
 
 319         params.setUserParams(lst);
 
 321         return Serialization.gsonPretty.toJson(params);
 
 325      * Creates configuration parameters.
 
 327      * @return configuration parameters, encoded as JSON
 
 329     private String makeConfigParams() {
 
 330         Map<String, String> map = new TreeMap<>();
 
 331         map.put("ckey", C_VALUE);
 
 333         List<Map<String, String>> lst = new LinkedList<>();
 
 336         return Serialization.gsonPretty.toJson(lst);
 
 340      * Reads an AAI vserver named-query response from a file.
 
 342      * @param onset the ONSET event
 
 343      * @param fileName name of the file containing the JSON response
 
 344      * @return output from the AAI vserver named-query
 
 345      * @throws IOException if the file cannot be read
 
 347     private AaiNqResponseWrapper loadAaiResponse(VirtualControlLoopEvent onset, String fileName) throws IOException {
 
 348         String resp = IOUtils.toString(getClass().getResource(fileName), StandardCharsets.UTF_8);
 
 349         AaiNqResponse aaiNqResponse = Serialization.gsonPretty.fromJson(resp, AaiNqResponse.class);
 
 351         return new AaiNqResponseWrapper(onset.getRequestId(), aaiNqResponse);