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