Merge "Fix Critical & Major issues in adapters"
[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  * 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.so.client.cds;
22
23 import com.google.gson.JsonObject;
24 import org.onap.so.bpmn.common.BuildingBlockExecution;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
27 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
28 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
29 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
30 import org.onap.so.client.exception.PayloadGenerationException;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
33 import org.springframework.context.annotation.Scope;
34 import org.springframework.stereotype.Component;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Optional;
38 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
39 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
40
41 @Component
42 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
43 public class VnfCDSRequestProvider implements CDSRequestProvider {
44     private String blueprintName;
45     private String blueprintVersion;
46     private BuildingBlockExecution execution;
47
48     @Autowired
49     private ExtractPojosForBB extractPojosForBB;
50
51     @Autowired
52     private ConfigureInstanceParamsForVnf configureInstanceParamsForVnf;
53
54     @Override
55     public String getBlueprintName() {
56         return blueprintName;
57     }
58
59     @Override
60     public String getBlueprintVersion() {
61         return blueprintVersion;
62     }
63
64     @Override
65     public <T> void setExecutionObject(T executionObject) {
66         execution = (BuildingBlockExecution) executionObject;
67     }
68
69     @Override
70     public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
71         JsonObject cdsPropertyObject = new JsonObject();
72         JsonObject vnfObject = new JsonObject();
73         String resolutionKey;
74         try {
75             ServiceInstance serviceInstance =
76                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
77             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
78
79             final String modelCustomizationUuid = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
80
81             resolutionKey = genericVnf.getVnfName();
82             blueprintName = genericVnf.getBlueprintName();
83             blueprintVersion = genericVnf.getBlueprintVersion();
84
85             vnfObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
86             vnfObject.addProperty("service-model-uuid", serviceInstance.getModelInfoServiceInstance().getModelUuid());
87             vnfObject.addProperty("vnf-id", genericVnf.getVnfId());
88             vnfObject.addProperty("vnf-name", genericVnf.getVnfName());
89             vnfObject.addProperty("vnf-customization-uuid", modelCustomizationUuid);
90
91             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
92             List<Map<String, Object>> userParamsFromRequest =
93                     buildingBlock.getRequestContext().getRequestParameters().getUserParams();
94
95             configureInstanceParamsForVnf.populateInstanceParams(vnfObject, userParamsFromRequest,
96                     modelCustomizationUuid);
97         } catch (Exception e) {
98             throw new PayloadGenerationException("Failed to buildPropertyObjectForVnf", e);
99         }
100
101         cdsPropertyObject.addProperty("resolution-key", resolutionKey);
102         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vnfObject);
103
104         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
105     }
106 }