Enable ControllerExecutionBB for service scope
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ServiceCDSRequestProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 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 java.util.List;
25 import java.util.Map;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
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.Optional;
37 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
38 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
39
40 @Component
41 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
42 public class ServiceCDSRequestProvider implements CDSRequestProvider {
43
44     private String resolutionKey;
45     private BuildingBlockExecution execution;
46     private String bluePrintName;
47     private String bluePrintVersion;
48
49     @Autowired
50     private ConfigureInstanceParamsForService configureInstanceParamsForService;
51
52     @Override
53     public String getBlueprintName() {
54         return bluePrintName;
55     }
56
57     @Override
58     public String getBlueprintVersion() {
59         return bluePrintVersion;
60     }
61
62     @Override
63     public <T> void setExecutionObject(T executionObject) {
64         execution = (BuildingBlockExecution) executionObject;
65     }
66
67     @Override
68     public Optional<String> buildRequestPayload(String action) throws PayloadGenerationException {
69         JsonObject cdsPropertyObject = new JsonObject();
70         JsonObject serviceObject = new JsonObject();
71         try {
72             ServiceInstance serviceInstance = execution.getGeneralBuildingBlock().getServiceInstance();
73             bluePrintName = serviceInstance.getModelInfoServiceInstance().getBlueprintName();
74             bluePrintVersion = serviceInstance.getModelInfoServiceInstance().getBlueprintVersion();
75             resolutionKey = serviceInstance.getServiceInstanceName();
76
77             serviceObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId());
78             serviceObject.addProperty("service-model-uuid",
79                     serviceInstance.getModelInfoServiceInstance().getModelUuid());
80
81             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
82             List<Map<String, Object>> userParamsFromRequest =
83                     buildingBlock.getRequestContext().getRequestParameters().getUserParams();
84             if (userParamsFromRequest != null && userParamsFromRequest.size() != 0) {
85                 configureInstanceParamsForService.populateInstanceParams(serviceObject, userParamsFromRequest);
86             }
87         } catch (Exception e) {
88             throw new PayloadGenerationException("Failed to buildPropertyObjectForService", e);
89         }
90
91         cdsPropertyObject.addProperty("resolution-key", resolutionKey);
92         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, serviceObject);
93
94         return Optional.of(buildRequestJsonObject(cdsPropertyObject, action));
95     }
96
97 }