Support instantiation of same model vnfs/vf-modules
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / client / cds / AbstractVnfCDSRequestProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.so.client.cds;
21
22 import com.google.gson.JsonParser;
23 import org.camunda.bpm.engine.delegate.DelegateExecution;
24 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
25 import org.junit.Before;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.common.DelegateExecutionImpl;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule;
34 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
36 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
38 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestParameters;
39 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
41 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoVfModule;
42 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
43 import org.onap.so.serviceinstancebeans.*;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.LinkedList;
48 import java.util.Map;
49
50 @RunWith(MockitoJUnitRunner.Silent.class)
51 public abstract class AbstractVnfCDSRequestProviderTest {
52
53     protected static final String GENERIC_VNF_ID = "vnfId_configVnfTest1";
54     protected static final String VF_MODULE_ID = "vf-module-id-1";
55     protected static final String VF_MODULE_NAME = "vf-module-name-1";
56     protected static final String VF_MODULE_CUSTOMIZATION_UUID = "23ce9ac4-e5dd-11e9-81b4-2a2ae2dbcce1";
57     protected static final String GENERIC_VNF_NAME = "vnf-name-1";
58     protected static final String SERVICE_INSTANCE_ID = "serviceInst_configTest";
59     protected static final String SERVICE_MODEL_UUID = "b45b5780-e5dd-11e9-81b4-2a2ae2dbcce4";
60     protected static final String SERVICE_INSTANCE_NAME = "test-service-instance";
61     protected static final String VNF_MODEL_CUSTOMIZATION_UUID = "23ce9ac4-e5dd-11e9-81b4-2a2ae2dbcce4";
62     protected static final String GENERAL_BLOCK_EXECUTION_MAP_KEY = "gBBInput";
63     protected static final String VNF_SCOPE = "vnf";
64     protected static final String SERVICE_SCOPE = "service";
65     protected static final String SERVICE_ACTION = "create";
66     protected static final String VF_SCOPE = "vfModule";
67     protected static final String ASSIGN_ACTION = "configAssign";
68     protected static final String DEPLOY_ACTION = "configDeploy";
69     protected static final String MSO_REQUEST_ID = "1234";
70     protected static final String BUILDING_BLOCK = "buildingBlock";
71     protected static final String PUBLIC_NET_ID = "public-net-id";
72     protected static final String CLOUD_REGION = "acl-cloud-region";
73
74     @Mock
75     protected ExtractPojosForBB extractPojosForBB;
76
77     protected BuildingBlockExecution buildingBlockExecution;
78
79     protected ExecuteBuildingBlock executeBuildingBlock;
80
81
82     @Before
83     public void setUp() {
84         buildingBlockExecution = createBuildingBlockExecution();
85         buildingBlockExecution.setVariable("lookupKeyMap", new HashMap<>());
86         executeBuildingBlock = new ExecuteBuildingBlock();
87     }
88
89     protected BuildingBlockExecution createBuildingBlockExecution() {
90         DelegateExecution execution = new DelegateExecutionFake();
91         execution.setVariable(GENERAL_BLOCK_EXECUTION_MAP_KEY, createGeneralBuildingBlock());
92         return new DelegateExecutionImpl(execution);
93     }
94
95     protected GeneralBuildingBlock createGeneralBuildingBlock() {
96         GeneralBuildingBlock generalBuildingBlock = new GeneralBuildingBlock();
97         RequestContext requestContext = new RequestContext();
98         RequestParameters requestParameters = new RequestParameters();
99         requestParameters.setUserParams(createRequestUserParams());
100         requestContext.setRequestParameters(requestParameters);
101         requestContext.setMsoRequestId(MSO_REQUEST_ID);
102         generalBuildingBlock.setRequestContext(requestContext);
103         return generalBuildingBlock;
104     }
105
106     protected ServiceInstance createServiceInstance() {
107         ServiceInstance serviceInstance = new ServiceInstance();
108         serviceInstance.setServiceInstanceName(SERVICE_INSTANCE_NAME);
109         serviceInstance.setServiceInstanceId(SERVICE_INSTANCE_ID);
110         ModelInfoServiceInstance modelInfoServiceInstance = new ModelInfoServiceInstance();
111         modelInfoServiceInstance.setModelUuid(SERVICE_MODEL_UUID);
112         serviceInstance.setModelInfoServiceInstance(modelInfoServiceInstance);
113         return serviceInstance;
114     }
115
116     protected GenericVnf createGenericVnf() {
117         GenericVnf genericVnf = new GenericVnf();
118         genericVnf.setVnfId(GENERIC_VNF_ID);
119         genericVnf.setVnfName(GENERIC_VNF_NAME);
120         genericVnf.setBlueprintName("test");
121         genericVnf.setBlueprintVersion("1.0.0");
122         ModelInfoGenericVnf modelInfoGenericVnf = new ModelInfoGenericVnf();
123         modelInfoGenericVnf.setModelCustomizationUuid(VNF_MODEL_CUSTOMIZATION_UUID);
124         genericVnf.setModelInfoGenericVnf(modelInfoGenericVnf);
125         return genericVnf;
126     }
127
128     protected VfModule createVfModule() {
129         VfModule vfModule = new VfModule();
130         vfModule.setVfModuleId(VF_MODULE_ID);
131         vfModule.setVfModuleName(VF_MODULE_NAME);
132         ModelInfoVfModule modelInfoVfModule = new ModelInfoVfModule();
133         modelInfoVfModule.setModelCustomizationUUID(VF_MODULE_CUSTOMIZATION_UUID);
134         vfModule.setModelInfoVfModule(modelInfoVfModule);
135         return vfModule;
136     }
137
138     protected List<Map<String, Object>> createRequestUserParams() {
139         List<Map<String, Object>> userParams = new ArrayList<>();
140         Map<String, Object> userParamMap = new HashMap<>();
141         userParamMap.put("service", getUserParams());
142         userParams.add(userParamMap);
143         return userParams;
144     }
145
146     protected Service getUserParams() {
147         Service service = new Service();
148         Resources resources = new Resources();
149         resources.setVnfs(createVnfList());
150         service.setResources(resources);
151         return service;
152     }
153
154     protected List<Vnfs> createVnfList() {
155         List<Map<String, String>> instanceParamsListSearchedVnf = new ArrayList<>();
156         Map<String, String> instanceParam = new HashMap<>();
157         instanceParam.put("public_net_id", PUBLIC_NET_ID);
158         instanceParam.put("acl-cloud-region", CLOUD_REGION);
159         instanceParamsListSearchedVnf.add(instanceParam);
160         Vnfs searchedVnf = createVnf(instanceParamsListSearchedVnf);
161         List<Vnfs> vnfList = new ArrayList<>();
162         vnfList.add(searchedVnf);
163         return vnfList;
164     }
165
166     protected Vnfs createVnf(List<Map<String, String>> instanceParamsList) {
167         Vnfs vnf = new Vnfs();
168         ModelInfo modelInfo = new ModelInfo();
169         modelInfo.setModelCustomizationId(VNF_MODEL_CUSTOMIZATION_UUID);
170         vnf.setModelInfo(modelInfo);
171         vnf.setInstanceParams(instanceParamsList);
172
173         // Set instance parameters and modelinfo for vf-module
174         VfModules vfModule = new VfModules();
175         ModelInfo modelInfoForVfModule = new ModelInfo();
176         modelInfoForVfModule.setModelCustomizationId(VF_MODULE_CUSTOMIZATION_UUID);
177         vfModule.setModelInfo(modelInfoForVfModule);
178
179         List<Map<String, String>> instanceParamsListSearchedVfModule = new ArrayList<>();
180         Map<String, String> instanceParams = new HashMap<>();
181         instanceParams.put("public-net-vf-module-id", PUBLIC_NET_ID);
182         instanceParams.put("aci-cloud-region-vf-module", CLOUD_REGION);
183
184         instanceParamsListSearchedVfModule.add(instanceParams);
185         vfModule.setInstanceParams(instanceParamsListSearchedVfModule);
186
187         List<VfModules> vfModules = new ArrayList<>();
188         vfModules.add(vfModule);
189
190         vnf.setVfModules(vfModules);
191
192         return vnf;
193     }
194
195     protected boolean verfiyJsonFromString(String payload) {
196         JsonParser parser = new JsonParser();
197         return parser.parse(payload).isJsonObject();
198     }
199
200     protected void setScopeAndAction(String scope, String action) {
201         BuildingBlock buildingBlock = new BuildingBlock();
202         buildingBlock.setBpmnScope(scope);
203         buildingBlock.setBpmnAction(action);
204         executeBuildingBlock.setBuildingBlock(buildingBlock);
205         buildingBlockExecution.setVariable(BUILDING_BLOCK, executeBuildingBlock);
206     }
207
208     protected void setScopeAndActionWithoutUserParams(String scope, String action) {
209         buildingBlockExecution.getGeneralBuildingBlock().getRequestContext().getRequestParameters()
210                 .setUserParams(new LinkedList<>());
211         setScopeAndAction(scope, action);
212     }
213 }