3b81d527030ba5d76b74ff3f65df8c9a5edcd653
[so.git] /
1 /*
2  * ============LICENSE_START======================================================= Copyright (C) 2020 Nokia. All rights
3  * reserved. ================================================================================ Licensed under the Apache
4  * License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain
5  * a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  * ============LICENSE_END=========================================================
13  */
14
15 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
16
17 import com.google.gson.JsonObject;
18 import org.onap.logging.filter.base.ONAPComponents;
19 import org.onap.so.bpmn.common.BuildingBlockExecution;
20 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
21 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerRunnable;
22 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
23 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
24 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
25 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
26 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.cds.AbstractCDSProcessingBBUtils;
29 import org.onap.so.client.cds.ConfigureInstanceParamsForPnf;
30 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
31 import org.onap.so.client.cds.PayloadConstants;
32 import org.onap.so.client.exception.BBObjectNotFoundException;
33 import org.onap.so.client.exception.ExceptionBuilder;
34 import org.onap.so.client.exception.PayloadGenerationException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.UUID;
42 import static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_NAME;
43 import static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_VERSION;
44 import static org.onap.so.client.cds.PayloadConstants.RESOLUTION_KEY;
45 import static org.onap.so.client.cds.PayloadConstants.REQUEST;
46 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
47 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
48
49 @Component
50 public class GenericPnfCDSControllerRunnableBB implements ControllerRunnable<BuildingBlockExecution> {
51
52     private static final Logger logger = LoggerFactory.getLogger(GenericPnfCDSControllerRunnableBB.class);
53     private static final String EXECUTION_OBJECT = "executionObject";
54     private static final String ORIGINATOR_ID = "SO";
55     private static final String BUILDING_BLOCK = "buildingBlock";
56     private static final String DEFAULT_SYNC_MODE = "sync";
57     private static final String MSO_REQUEST_ID = "msoRequestId";
58
59     private AbstractCDSProcessingBBUtils abstractCDSProcessingBBUtils;
60     private ExtractPojosForBB extractPojosForBB;
61     private ExceptionBuilder exceptionBuilder;
62     private ConfigureInstanceParamsForPnf configureInstanceParamsForPnf;
63
64     @Autowired
65     public GenericPnfCDSControllerRunnableBB(AbstractCDSProcessingBBUtils abstractCDSProcessingBBUtils,
66             ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder,
67             ConfigureInstanceParamsForPnf configureInstanceParamsForPnf) {
68         this.abstractCDSProcessingBBUtils = abstractCDSProcessingBBUtils;
69         this.extractPojosForBB = extractPojosForBB;
70         this.exceptionBuilder = exceptionBuilder;
71         this.configureInstanceParamsForPnf = configureInstanceParamsForPnf;
72     }
73
74     @Override
75     public Boolean understand(ControllerContext<BuildingBlockExecution> controllerContext) {
76         return PayloadConstants.CDS_ACTOR.equalsIgnoreCase(controllerContext.getControllerActor())
77                 && PayloadConstants.PNF_SCOPE.equalsIgnoreCase(controllerContext.getControllerScope());
78     }
79
80     @Override
81     public Boolean ready(ControllerContext<BuildingBlockExecution> controllerContext) {
82         return true;
83     }
84
85     @Override
86     public void prepare(ControllerContext<BuildingBlockExecution> controllerContext) {
87         BuildingBlockExecution buildingBlockExecution = controllerContext.getExecution();
88         final AbstractCDSPropertiesBean abstractCDSPropertiesBean =
89                 prepareAndSetCdsPropertyBean(buildingBlockExecution);
90
91         buildingBlockExecution.setVariable(EXECUTION_OBJECT, abstractCDSPropertiesBean);
92     }
93
94     @Override
95     public void run(ControllerContext<BuildingBlockExecution> controllerContext) {
96         BuildingBlockExecution buildingBlockExecution = controllerContext.getExecution();
97         abstractCDSProcessingBBUtils.constructExecutionServiceInputObject(buildingBlockExecution);
98         abstractCDSProcessingBBUtils.sendRequestToCDSClient(buildingBlockExecution);
99     }
100
101     private AbstractCDSPropertiesBean prepareAndSetCdsPropertyBean(BuildingBlockExecution buildingBlockExecution) {
102         final AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
103         final String action = getAction(buildingBlockExecution);
104
105         if (action == null) {
106             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 7000, "Action is null!",
107                     ONAPComponents.SO);
108         }
109
110         abstractCDSPropertiesBean.setRequestObject(this.buildRequestPayload(action, buildingBlockExecution));
111         abstractCDSPropertiesBean.setBlueprintName(buildingBlockExecution.getVariable(PRC_BLUEPRINT_NAME));
112         abstractCDSPropertiesBean.setBlueprintVersion(buildingBlockExecution.getVariable(PRC_BLUEPRINT_VERSION));
113         abstractCDSPropertiesBean.setRequestId(buildingBlockExecution.getVariable(MSO_REQUEST_ID));
114         abstractCDSPropertiesBean.setOriginatorId(ORIGINATOR_ID);
115         abstractCDSPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
116         abstractCDSPropertiesBean.setActionName(action);
117         abstractCDSPropertiesBean.setMode(DEFAULT_SYNC_MODE);
118         return abstractCDSPropertiesBean;
119     }
120
121     private String buildRequestPayload(String action, BuildingBlockExecution execution) {
122         final JsonObject pnfObject = new JsonObject();
123         String resolutionKey = null;
124         try {
125             final Pnf pnf = getPnf(execution);
126             final String modelCustomizationUuid = pnf.getModelInfoPnf().getModelCustomizationUuid();
127             final ServiceInstance serviceInstance = getServiceInstance(execution);
128             resolutionKey = pnf.getPnfName();
129
130             setExecutionVariable("service-instance-id", serviceInstance.getServiceInstanceId(), pnfObject);
131             setExecutionVariable("service-model-uuid", serviceInstance.getModelInfoServiceInstance().getModelUuid(),
132                     pnfObject);
133             setExecutionVariable("pnf-id", pnf.getPnfId(), pnfObject);
134             setExecutionVariable("pnf-name", resolutionKey, pnfObject);
135             setExecutionVariable("pnf-customization-uuid", modelCustomizationUuid, pnfObject);
136
137             final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
138
139             List<Map<String, Object>> userParamsFromRequest =
140                     generalBuildingBlock.getRequestContext().getRequestParameters().getUserParams();
141             if (userParamsFromRequest != null && userParamsFromRequest.size() != 0) {
142                 configureInstanceParamsForPnf.populateInstanceParams(pnfObject, userParamsFromRequest,
143                         modelCustomizationUuid);
144             }
145         } catch (BBObjectNotFoundException | PayloadGenerationException exception) {
146             logger.error("An exception occurred when creating payload for CDS request", exception);
147             exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, exception);
148         }
149         final JsonObject cdsPropertyObject = new JsonObject();
150         cdsPropertyObject.addProperty(RESOLUTION_KEY, resolutionKey);
151         cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, pnfObject);
152
153         return buildRequestJsonObject(cdsPropertyObject, action);
154     }
155
156     private String buildRequestJsonObject(JsonObject cdsPropertyObject, String action) {
157         String requestBasedOnAction = action.concat(SEPARATOR).concat(REQUEST);
158         JsonObject requestObject = new JsonObject();
159         requestObject.add(requestBasedOnAction, cdsPropertyObject);
160         return requestObject.toString();
161     }
162
163     private void setExecutionVariable(String jsonProperty, String executionProperty, JsonObject pnfObject) {
164         if (executionProperty != null) {
165             pnfObject.addProperty(jsonProperty, executionProperty);
166         }
167     }
168
169     private String getAction(BuildingBlockExecution buildingBlockExecution) {
170         ExecuteBuildingBlock executeBuildingBlock = buildingBlockExecution.getVariable(BUILDING_BLOCK);
171         return executeBuildingBlock.getBuildingBlock().getBpmnAction();
172     }
173
174     private Pnf getPnf(BuildingBlockExecution buildingBlockExecution) throws BBObjectNotFoundException {
175         return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.PNF);
176     }
177
178     private ServiceInstance getServiceInstance(BuildingBlockExecution buildingBlockExecution)
179             throws BBObjectNotFoundException {
180         return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID);
181     }
182 }