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
7 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
15 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
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.ResourceKey;
26 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
27 import org.onap.so.client.cds.AbstractCDSProcessingBBUtils;
28 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
29 import org.onap.so.client.cds.PayloadConstants;
30 import org.onap.so.client.exception.BBObjectNotFoundException;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36 import java.util.UUID;
37 import static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_NAME;
38 import static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_VERSION;
39 import static org.onap.so.client.cds.PayloadConstants.RESOLUTION_KEY;
40 import static org.onap.so.client.cds.PayloadConstants.REQUEST;
41 import static org.onap.so.client.cds.PayloadConstants.SEPARATOR;
42 import static org.onap.so.client.cds.PayloadConstants.PROPERTIES;
45 public class GenericPnfCDSControllerRunnableBB implements ControllerRunnable<BuildingBlockExecution> {
47 private static final Logger logger = LoggerFactory.getLogger(GenericPnfCDSControllerRunnableBB.class);
48 private static final String EXECUTION_OBJECT = "executionObject";
49 private static final String ORIGINATOR_ID = "SO";
50 private static final String BUILDING_BLOCK = "buildingBlock";
51 private static final String DEFAULT_SYNC_MODE = "sync";
52 private static final String MSO_REQUEST_ID = "msoRequestId";
54 private AbstractCDSProcessingBBUtils abstractCDSProcessingBBUtils;
55 private ExtractPojosForBB extractPojosForBB;
56 private ExceptionBuilder exceptionBuilder;
59 public GenericPnfCDSControllerRunnableBB(AbstractCDSProcessingBBUtils abstractCDSProcessingBBUtils,
60 ExtractPojosForBB extractPojosForBB, ExceptionBuilder exceptionBuilder) {
61 this.abstractCDSProcessingBBUtils = abstractCDSProcessingBBUtils;
62 this.extractPojosForBB = extractPojosForBB;
63 this.exceptionBuilder = exceptionBuilder;
67 public Boolean understand(ControllerContext<BuildingBlockExecution> controllerContext) {
68 return PayloadConstants.CDS_ACTOR.equalsIgnoreCase(controllerContext.getControllerActor())
69 && PayloadConstants.PNF_SCOPE.equalsIgnoreCase(controllerContext.getControllerScope());
73 public Boolean ready(ControllerContext<BuildingBlockExecution> controllerContext) {
78 public void prepare(ControllerContext<BuildingBlockExecution> controllerContext) {
79 BuildingBlockExecution buildingBlockExecution = controllerContext.getExecution();
80 final AbstractCDSPropertiesBean abstractCDSPropertiesBean =
81 prepareAndSetCdsPropertyBean(buildingBlockExecution);
83 buildingBlockExecution.setVariable(EXECUTION_OBJECT, abstractCDSPropertiesBean);
87 public void run(ControllerContext<BuildingBlockExecution> controllerContext) {
88 BuildingBlockExecution buildingBlockExecution = controllerContext.getExecution();
89 abstractCDSProcessingBBUtils.constructExecutionServiceInputObject(buildingBlockExecution);
90 abstractCDSProcessingBBUtils.sendRequestToCDSClient(buildingBlockExecution);
93 private AbstractCDSPropertiesBean prepareAndSetCdsPropertyBean(BuildingBlockExecution buildingBlockExecution) {
94 final AbstractCDSPropertiesBean abstractCDSPropertiesBean = new AbstractCDSPropertiesBean();
95 final String action = getAction(buildingBlockExecution);
98 exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 7000, "Action is null!",
102 abstractCDSPropertiesBean.setRequestObject(this.buildRequestPayload(action, buildingBlockExecution));
103 abstractCDSPropertiesBean.setBlueprintName(buildingBlockExecution.getVariable(PRC_BLUEPRINT_NAME));
104 abstractCDSPropertiesBean.setBlueprintVersion(buildingBlockExecution.getVariable(PRC_BLUEPRINT_VERSION));
105 abstractCDSPropertiesBean.setRequestId(buildingBlockExecution.getVariable(MSO_REQUEST_ID));
106 abstractCDSPropertiesBean.setOriginatorId(ORIGINATOR_ID);
107 abstractCDSPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
108 abstractCDSPropertiesBean.setActionName(action);
109 abstractCDSPropertiesBean.setMode(DEFAULT_SYNC_MODE);
110 return abstractCDSPropertiesBean;
113 private String buildRequestPayload(String action, BuildingBlockExecution execution) {
114 final JsonObject pnfObject = new JsonObject();
115 String resolutionKey = null;
117 final Pnf pnf = getPnf(execution);
118 final ServiceInstance serviceInstance = getServiceInstance(execution);
119 resolutionKey = pnf.getPnfName();
121 setExecutionVariable("service-instance-id", serviceInstance.getServiceInstanceId(), pnfObject);
122 setExecutionVariable("service-model-uuid", serviceInstance.getModelInfoServiceInstance().getModelUuid(),
124 setExecutionVariable("pnf-id", pnf.getPnfId(), pnfObject);
125 setExecutionVariable("pnf-name", resolutionKey, pnfObject);
126 setExecutionVariable("pnf-customization-uuid", pnf.getModelInfoPnf().getModelCustomizationUuid(),
129 } catch (BBObjectNotFoundException exception) {
130 logger.error("An exception occurred when creating payload for CDS request", exception);
131 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, exception);
133 final JsonObject cdsPropertyObject = new JsonObject();
134 cdsPropertyObject.addProperty(RESOLUTION_KEY, resolutionKey);
135 cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, pnfObject);
137 return buildRequestJsonObject(cdsPropertyObject, action);
140 private String buildRequestJsonObject(JsonObject cdsPropertyObject, String action) {
141 String requestBasedOnAction = action.concat(SEPARATOR).concat(REQUEST);
142 JsonObject requestObject = new JsonObject();
143 requestObject.add(requestBasedOnAction, cdsPropertyObject);
144 return requestObject.toString();
147 private void setExecutionVariable(String jsonProperty, String executionProperty, JsonObject pnfObject) {
148 if (executionProperty != null) {
149 pnfObject.addProperty(jsonProperty, executionProperty);
153 private String getAction(BuildingBlockExecution buildingBlockExecution) {
154 ExecuteBuildingBlock executeBuildingBlock = buildingBlockExecution.getVariable(BUILDING_BLOCK);
155 return executeBuildingBlock.getBuildingBlock().getBpmnAction();
158 private Pnf getPnf(BuildingBlockExecution buildingBlockExecution) throws BBObjectNotFoundException {
159 return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.PNF);
162 private ServiceInstance getServiceInstance(BuildingBlockExecution buildingBlockExecution)
163 throws BBObjectNotFoundException {
164 return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.SERVICE_INSTANCE_ID);