478b65c349cbfb0831efd4c803ec7e903f6359c4
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / VfModuleCDSRequestProvider.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.bbobjects.VfModule;
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 VfModuleCDSRequestProvider 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 ConfigureInstanceParamsForVfModule configureInstanceParamsForVfModule;
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 vfModuleObject = new JsonObject();
76         String vfModuleName;
77
78         try {
79             ServiceInstance serviceInstance =
80                     extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
81             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
82
83             final String modelCustomizationUuidForVnf = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
84
85             blueprintName = genericVnf.getModelInfoGenericVnf().getBlueprintName();
86             blueprintVersion = genericVnf.getModelInfoGenericVnf().getBlueprintVersion();
87
88             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
89             vfModuleName = vfModule.getVfModuleName();
90
91             final String modelCustomizationUuidForVfModule =
92                     vfModule.getModelInfoVfModule().getModelCustomizationUUID();
93
94             vfModuleObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
95             vfModuleObject.addProperty("service-model-uuid",
96                     serviceInstance.getModelInfoServiceInstance().getModelUuid());
97             vfModuleObject.addProperty("vnf-id", genericVnf.getVnfId());
98             vfModuleObject.addProperty("vnf-name", genericVnf.getVnfName());
99             vfModuleObject.addProperty("vf-module-id", vfModule.getVfModuleId());
100             vfModuleObject.addProperty("vf-module-name", vfModule.getVfModuleName());
101             vfModuleObject.addProperty("vf-module-customization-uuid",
102                     vfModule.getModelInfoVfModule().getModelCustomizationUUID());
103
104             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
105             List<Map<String, Object>> userParamsFromRequest =
106                     buildingBlock.getRequestContext().getRequestParameters().getUserParams();
107             if (userParamsFromRequest != null && userParamsFromRequest.size() != 0) {
108                 configureInstanceParamsForVfModule.populateInstanceParams(vfModuleObject, userParamsFromRequest,
109                         modelCustomizationUuidForVnf, modelCustomizationUuidForVfModule);
110             }
111         } catch (Exception e) {
112             throw new PayloadGenerationException("Failed to buildPropertyObject for VF-Module", e);
113         }
114
115         // Not sure for resolutionKey should be same as vfModule name.
116         cdsPropertyObject.addProperty("resolution-key", vfModuleName);
117         cdsPropertyObject.addProperty("template-prefix", vfModuleName + action);
118         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vfModuleObject);
119
120         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
121     }
122
123 }