Support instantiation of same model vnfs/vf-modules
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / VnfCDSRequestProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Bell Canada
6  * ================================================================================
7  * Modifications Copyright (c) 2020 Tech Mahindra
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.cds;
24
25 import com.google.gson.JsonObject;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
30 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
32 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
33 import org.onap.so.client.exception.PayloadGenerationException;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Component;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
42 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
43
44 @Component
45 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46 public class VnfCDSRequestProvider implements CDSRequestProvider {
47     private String blueprintName;
48     private String blueprintVersion;
49     private BuildingBlockExecution execution;
50
51     @Autowired
52     private ExtractPojosForBB extractPojosForBB;
53
54     @Autowired
55     private ConfigureInstanceParamsForVnf configureInstanceParamsForVnf;
56
57     @Override
58     public String getBlueprintName() {
59         return blueprintName;
60     }
61
62     @Override
63     public String getBlueprintVersion() {
64         return blueprintVersion;
65     }
66
67     @Override
68     public <T> void setExecutionObject(T executionObject) {
69         execution = (BuildingBlockExecution) executionObject;
70     }
71
72     @Override
73     public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
74         JsonObject cdsPropertyObject = new JsonObject();
75         JsonObject vnfObject = new JsonObject();
76         String resolutionKey;
77         try {
78             ServiceInstance serviceInstance =
79                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
80             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
81
82             final String modelCustomizationUuid = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
83
84             resolutionKey = genericVnf.getVnfName();
85             blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
86             blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
87
88             vnfObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
89             vnfObject.addProperty("service-model-uuid", serviceInstance.getModelInfoServiceInstance().getModelUuid());
90             vnfObject.addProperty("vnf-id", genericVnf.getVnfId());
91             vnfObject.addProperty("vnf-name", genericVnf.getVnfName());
92             vnfObject.addProperty("vnf-customization-uuid", modelCustomizationUuid);
93
94             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
95             List<Map<String, Object>> userParamsFromRequest =
96                     buildingBlock.getRequestContext().getRequestParameters().getUserParams();
97             String vnfInstanceName = execution.getLookupMap().getOrDefault(ResourceKey.VNF_INSTANCE_NAME, "");
98             if (userParamsFromRequest != null && userParamsFromRequest.size() != 0) {
99                 configureInstanceParamsForVnf.populateInstanceParams(vnfObject, userParamsFromRequest,
100                         modelCustomizationUuid, vnfInstanceName);
101             }
102         } catch (Exception e) {
103             throw new PayloadGenerationException("Failed to buildPropertyObjectForVnf", e);
104         }
105
106         cdsPropertyObject.addProperty("resolution-key", resolutionKey);
107         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vnfObject);
108
109         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
110     }
111 }