decreased code smells
[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  * 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.bbobjects.VfModule;
28 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
29 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
30 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
31 import org.onap.so.client.exception.PayloadGenerationException;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.config.ConfigurableBeanFactory;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Component;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
40 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
41
42 @Component
43 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
44 public class VfModuleCDSRequestProvider implements CDSRequestProvider {
45     private String blueprintName;
46     private String blueprintVersion;
47     private BuildingBlockExecution execution;
48
49     @Autowired
50     private ExtractPojosForBB extractPojosForBB;
51
52     @Autowired
53     private ConfigureInstanceParamsForVfModule configureInstanceParamsForVfModule;
54
55     @Override
56     public String getBlueprintName() {
57         return blueprintName;
58     }
59
60     @Override
61     public String getBlueprintVersion() {
62         return blueprintVersion;
63     }
64
65     @Override
66     public <T> void setExecutionObject(T executionObject) {
67         execution = (BuildingBlockExecution) executionObject;
68     }
69
70     @Override
71     public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
72         JsonObject cdsPropertyObject = new JsonObject();
73         JsonObject vfModuleObject = new JsonObject();
74         String vfModuleName;
75
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 modelCustomizationUuidForVnf = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
82
83             blueprintName = genericVnf.getBlueprintName();
84             blueprintVersion = genericVnf.getBlueprintVersion();
85
86             VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID);
87             vfModuleName = vfModule.getVfModuleName();
88
89             final String modelCustomizationUuidForVfModule =
90                     vfModule.getModelInfoVfModule().getModelCustomizationUUID();
91
92             vfModuleObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
93             vfModuleObject.addProperty("service-model-uuid",
94                     serviceInstance.getModelInfoServiceInstance().getModelUuid());
95             vfModuleObject.addProperty("vnf-id", genericVnf.getVnfId());
96             vfModuleObject.addProperty("vnf-name", genericVnf.getVnfName());
97             vfModuleObject.addProperty("vf-module-id", vfModule.getVfModuleId());
98             vfModuleObject.addProperty("vf-module-name", vfModule.getVfModuleName());
99             vfModuleObject.addProperty("vf-module-customization-uuid",
100                     vfModule.getModelInfoVfModule().getModelCustomizationUUID());
101
102             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
103             List<Map<String, Object>> userParamsFromRequest =
104                     buildingBlock.getRequestContext().getRequestParameters().getUserParams();
105
106             configureInstanceParamsForVfModule.populateInstanceParams(vfModuleObject, userParamsFromRequest,
107                     modelCustomizationUuidForVnf, modelCustomizationUuidForVfModule);
108         } catch (Exception e) {
109             throw new PayloadGenerationException("Failed to buildPropertyObject for VF-Module", e);
110         }
111
112         // Not sure for resolutionKey should be same as vfModule name.
113         cdsPropertyObject.addProperty("resolution-key", vfModuleName);
114         cdsPropertyObject.addProperty("template-prefix", vfModuleName + action);
115         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vfModuleObject);
116
117         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
118     }
119
120 }