From 1ae4a5642022ff156021ae5b8e85473aa2b5a177 Mon Sep 17 00:00:00 2001 From: tragait Date: Mon, 21 Oct 2019 14:26:08 -0400 Subject: Generic cds payload for vnf/vf-module/service. Things done- -Fixed review comments. -Added logic to create payload using scope and action. -Populate instance parameters from SO-request to CDS payload for VNF. -Implementation of setting orchestration status after CDS completes its operation based on SO scope. -Make use of ControllerRunnable interface. -Changed actionname to actionName to action.value as per Oscar's suggestion. -Setting orchestration status after CDS completes its operation for VF-Module. -Populate instance paraneters from SO to CDS for VF-Module. Issue-ID: SO-2312 Signed-off-by: prathamesh Change-Id: I026d9f7909e486c68f274b7e56c8bd7c8fdcd521 Signed-off-by: prathamesh Signed-off-by: tragait --- .../client/cds/AbstractCDSProcessingBBUtils.java | 169 +++++++++++++-------- .../org/onap/so/client/cds/CDSRequestProvider.java | 76 +++++++++ .../cds/ConfigureInstanceParamsForVfModule.java | 79 ++++++++++ .../client/cds/ConfigureInstanceParamsForVnf.java | 73 +++++++++ .../cds/ExtractServiceFromUserParameters.java | 53 +++++++ .../onap/so/client/cds/GeneratePayloadForCds.java | 105 +++++++++++++ .../org/onap/so/client/cds/PayloadConstants.java | 34 +++++ .../so/client/cds/ServiceCDSRequestProvider.java | 89 +++++++++++ .../so/client/cds/VfModuleCDSRequestProvider.java | 121 +++++++++++++++ .../onap/so/client/cds/VnfCDSRequestProvider.java | 106 +++++++++++++ .../exception/PayloadGenerationException.java | 39 +++++ 11 files changed, 881 insertions(+), 63 deletions(-) create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/CDSRequestProvider.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVfModule.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVnf.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ExtractServiceFromUserParameters.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/GeneratePayloadForCds.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/PayloadConstants.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ServiceCDSRequestProvider.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VfModuleCDSRequestProvider.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VnfCDSRequestProvider.java create mode 100644 bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/exception/PayloadGenerationException.java (limited to 'bpmn/MSOCommonBPMN/src/main/java/org/onap') diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/AbstractCDSProcessingBBUtils.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/AbstractCDSProcessingBBUtils.java index 424a4f3ecc..ebced8e933 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/AbstractCDSProcessingBBUtils.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/AbstractCDSProcessingBBUtils.java @@ -22,14 +22,18 @@ package org.onap.so.client.cds; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.Struct; +import com.google.protobuf.Struct.Builder; +import com.google.protobuf.util.JsonFormat; +import io.grpc.Status; import org.camunda.bpm.engine.delegate.DelegateExecution; import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers; import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader; import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; +import org.onap.so.bpmn.common.BuildingBlockExecution; import org.onap.so.client.PreconditionFailedException; import org.onap.so.client.RestPropertiesLoader; import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean; @@ -39,15 +43,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.google.protobuf.InvalidProtocolBufferException; -import com.google.protobuf.Struct; -import com.google.protobuf.Struct.Builder; -import com.google.protobuf.util.JsonFormat; -import io.grpc.Status; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; /** * Util class to support Call to CDS client - * */ @Component public class AbstractCDSProcessingBBUtils { @@ -60,11 +60,7 @@ public class AbstractCDSProcessingBBUtils { private static final String RESPONSE_PAYLOAD = "CDSResponsePayload"; private static final String CDS_STATUS = "CDSStatus"; private static final String EXEC_INPUT = "executionServiceInput"; - - - /** - * indicate exception thrown. - */ + private static final String EXECUTION_OBJECT = "executionObject"; private static final String EXCEPTION = "Exception"; @Autowired @@ -76,34 +72,33 @@ public class AbstractCDSProcessingBBUtils { * @param execution DelegateExecution object */ public void constructExecutionServiceInputObject(DelegateExecution execution) { - logger.trace("Start AbstractCDSProcessingBBUtils.preProcessRequest "); + logger.trace("Start AbstractCDSProcessingBBUtils.preProcessRequest for DelegateExecution object."); try { AbstractCDSPropertiesBean executionObject = - (AbstractCDSPropertiesBean) execution.getVariable("executionObject"); - - String payload = executionObject.getRequestObject(); - - CommonHeader commonHeader = CommonHeader.newBuilder().setOriginatorId(executionObject.getOriginatorId()) - .setRequestId(executionObject.getRequestId()).setSubRequestId(executionObject.getSubRequestId()) - .build(); - ActionIdentifiers actionIdentifiers = - ActionIdentifiers.newBuilder().setBlueprintName(executionObject.getBlueprintName()) - .setBlueprintVersion(executionObject.getBlueprintVersion()) - .setActionName(executionObject.getActionName()).setMode(executionObject.getMode()).build(); - - Builder struct = Struct.newBuilder(); - try { - JsonFormat.parser().merge(payload, struct); - } catch (InvalidProtocolBufferException e) { - logger.error("Failed to parse received message. blueprint({}:{}) for action({}). {}", - executionObject.getBlueprintVersion(), executionObject.getBlueprintName(), - executionObject.getActionName(), e); - } + (AbstractCDSPropertiesBean) execution.getVariable(EXECUTION_OBJECT); + + ExecutionServiceInput executionServiceInput = prepareExecutionServiceInput(executionObject); + + execution.setVariable(EXEC_INPUT, executionServiceInput); + + } catch (Exception ex) { + exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex); + } + } - ExecutionServiceInput executionServiceInput = - ExecutionServiceInput.newBuilder().setCommonHeader(commonHeader) - .setActionIdentifiers(actionIdentifiers).setPayload(struct.build()).build(); + /** + * Extracting data from execution object and building the ExecutionServiceInput Object + * + * @param execution BuildingBlockExecution object + */ + public void constructExecutionServiceInputObject(BuildingBlockExecution execution) { + logger.trace("Start AbstractCDSProcessingBBUtils.preProcessRequest for BuildingBlockExecution object."); + + try { + AbstractCDSPropertiesBean executionObject = execution.getVariable(EXECUTION_OBJECT); + + ExecutionServiceInput executionServiceInput = prepareExecutionServiceInput(executionObject); execution.setVariable(EXEC_INPUT, executionServiceInput); @@ -119,50 +114,98 @@ public class AbstractCDSProcessingBBUtils { */ public void sendRequestToCDSClient(DelegateExecution execution) { - logger.trace("Start AbstractCDSProcessingBBUtils.sendRequestToCDSClient "); + logger.trace("Start AbstractCDSProcessingBBUtils.sendRequestToCDSClient for DelegateExecution object."); try { - CDSProperties props = RestPropertiesLoader.getInstance().getNewImpl(CDSProperties.class); - if (props == null) { - throw new PreconditionFailedException( - "No RestProperty.CDSProperties implementation found on classpath, can't create client."); - } - ExecutionServiceInput executionServiceInput = (ExecutionServiceInput) execution.getVariable(EXEC_INPUT); + CDSResponse cdsResponse = getCdsResponse(executionServiceInput); + execution.setVariable(CDS_STATUS, cdsResponse.status); - CDSResponse cdsResponse = new CDSResponse(); - - try (CDSProcessingClient cdsClient = new CDSProcessingClient(new ResponseHandler(cdsResponse))) { - CountDownLatch countDownLatch = cdsClient.sendRequest(executionServiceInput); - countDownLatch.await(props.getTimeout(), TimeUnit.SECONDS); - } catch (InterruptedException ex) { - logger.error("Caught exception in sendRequestToCDSClient in AbstractCDSProcessingBBUtils : ", ex); - Thread.currentThread().interrupt(); + if (cdsResponse.payload != null) { + String payload = JsonFormat.printer().print(cdsResponse.payload); + execution.setVariable(RESPONSE_PAYLOAD, payload); } - String cdsResponseStatus = cdsResponse.status; + } catch (Exception ex) { + exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex); + } + } - /** - * throw CDS failed exception. - */ - if (!cdsResponseStatus.equals(SUCCESS)) { - throw new BadResponseException("CDS call failed with status: " + cdsResponse.status - + " and errorMessage: " + cdsResponse.errorMessage); - } + /** + * get the executionServiceInput object from execution and send a request to CDS Client and wait for TIMEOUT period + * + * @param execution BuildingBlockExecution object + */ + public void sendRequestToCDSClient(BuildingBlockExecution execution) { - execution.setVariable(CDS_STATUS, cdsResponseStatus); + logger.trace("Start AbstractCDSProcessingBBUtils.sendRequestToCDSClient for BuildingBlockExecution object."); + try { + ExecutionServiceInput executionServiceInput = execution.getVariable(EXEC_INPUT); + CDSResponse cdsResponse = getCdsResponse(executionServiceInput); + execution.setVariable(CDS_STATUS, cdsResponse.status); if (cdsResponse.payload != null) { String payload = JsonFormat.printer().print(cdsResponse.payload); execution.setVariable(RESPONSE_PAYLOAD, payload); } - - } catch (Exception ex) { exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex); } } + private CDSResponse getCdsResponse(ExecutionServiceInput executionServiceInput) throws BadResponseException { + CDSProperties props = RestPropertiesLoader.getInstance().getNewImpl(CDSProperties.class); + if (props == null) { + throw new PreconditionFailedException( + "No RestProperty.CDSProperties implementation found on classpath, can't create client."); + } + + CDSResponse cdsResponse = new CDSResponse(); + + try (CDSProcessingClient cdsClient = new CDSProcessingClient(new ResponseHandler(cdsResponse))) { + CountDownLatch countDownLatch = cdsClient.sendRequest(executionServiceInput); + countDownLatch.await(props.getTimeout(), TimeUnit.SECONDS); + } catch (InterruptedException ex) { + logger.error("Caught exception in sendRequestToCDSClient in AbstractCDSProcessingBBUtils : ", ex); + Thread.currentThread().interrupt(); + } + + String cdsResponseStatus = cdsResponse.status; + + /** + * throw CDS failed exception. + */ + if (!cdsResponseStatus.equals(SUCCESS)) { + throw new BadResponseException("CDS call failed with status: " + cdsResponse.status + " and errorMessage: " + + cdsResponse.errorMessage); + } + return cdsResponse; + } + + private ExecutionServiceInput prepareExecutionServiceInput(AbstractCDSPropertiesBean executionObject) { + String payload = executionObject.getRequestObject(); + + CommonHeader commonHeader = CommonHeader.newBuilder().setOriginatorId(executionObject.getOriginatorId()) + .setRequestId(executionObject.getRequestId()).setSubRequestId(executionObject.getSubRequestId()) + .build(); + ActionIdentifiers actionIdentifiers = + ActionIdentifiers.newBuilder().setBlueprintName(executionObject.getBlueprintName()) + .setBlueprintVersion(executionObject.getBlueprintVersion()) + .setActionName(executionObject.getActionName()).setMode(executionObject.getMode()).build(); + + Builder struct = Struct.newBuilder(); + try { + JsonFormat.parser().merge(payload, struct); + } catch (InvalidProtocolBufferException e) { + logger.error("Failed to parse received message. blueprint({}:{}) for action({}). {}", + executionObject.getBlueprintVersion(), executionObject.getBlueprintName(), + executionObject.getActionName(), e); + } + + return ExecutionServiceInput.newBuilder().setCommonHeader(commonHeader).setActionIdentifiers(actionIdentifiers) + .setPayload(struct.build()).build(); + } + private class ResponseHandler implements CDSProcessingListener { private CDSResponse cdsResponse; diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/CDSRequestProvider.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/CDSRequestProvider.java new file mode 100644 index 0000000000..e01de69b74 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/CDSRequestProvider.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.client.exception.PayloadGenerationException; +import java.util.Optional; +import static org.onap.so.client.cds.PayloadConstants.REQUEST; +import static org.onap.so.client.cds.PayloadConstants.SEPARATOR; + +public interface CDSRequestProvider { + + /** + * Build entire payload for CDS. + * + * @param action - action could be assign/deploy/undeploy etc. + * @return "payload":{ "config--":{ // information about resolution key, property configuration and + * template prefix based on the scope and action} + * @throws PayloadGenerationException If fail to build the payload. + */ + Optional buildRequestPayload(String action) throws PayloadGenerationException; + + /** + * Get the blueprint name for CDS payload + * + * @return blueprint name + */ + String getBlueprintName(); + + /** + * Get the blueprint version for CDS payload + * + * @return blueprint version + */ + String getBlueprintVersion(); + + /** + * Set the executionObject(BuildingBlockExecution or DelegateExecution for PNF) + * + * @param executionObject object could be BuildingBlockExecution or DelegateExecution. + */ + void setExecutionObject(T executionObject); + + + /** + * Build Request payload for CDS + * + * @param cdsPropertyObject - Json Object + * @param action - action could be assign/deploy/undeploy etc. + * @return Request Payload + */ + default String buildRequestJsonObject(JsonObject cdsPropertyObject, String action) { + String requestBasedOnAction = action.concat(SEPARATOR).concat(REQUEST); + JsonObject requestObject = new JsonObject(); + requestObject.add(requestBasedOnAction, cdsPropertyObject); + return requestObject.toString(); + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVfModule.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVfModule.java new file mode 100644 index 0000000000..6f850fa898 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVfModule.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.client.cds.ExtractServiceFromUserParameters; +import org.onap.so.client.exception.PayloadGenerationException; +import org.onap.so.serviceinstancebeans.Service; +import org.onap.so.serviceinstancebeans.VfModules; +import org.onap.so.serviceinstancebeans.Vnfs; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; + +@Component +public class ConfigureInstanceParamsForVfModule { + + @Autowired + private ExtractServiceFromUserParameters extractServiceFromUserParameters; + + /** + * Read instance parameters for VF-Module and put into JsonObject. + * + * @param jsonObject- JsonObject which will hold the payload to send to CDS. + * @param userParamsFromRequest - User parameters for a vf-module + * @param vnfCustomizationUuid - Unique ID for vnf. + * @param vfModuleCustomizationUuid - Unique ID for vf-module. + * @throws PayloadGenerationException- If it doesn't able to populate instance parameters from SO payload. + */ + public void populateInstanceParams(JsonObject jsonObject, List> userParamsFromRequest, + String vnfCustomizationUuid, String vfModuleCustomizationUuid) throws PayloadGenerationException { + try { + Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest); + + List> instanceParamsList = + getInstanceParams(service, vnfCustomizationUuid, vfModuleCustomizationUuid); + + instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream()) + .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue())); + } catch (Exception e) { + throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e); + } + } + + private List> getInstanceParams(Service service, String vnfCustomizationUuid, + String vfModuleCustomizationUuid) throws PayloadGenerationException { + + Vnfs foundedVnf = service.getResources().getVnfs().stream() + .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst() + .orElseThrow(() -> new PayloadGenerationException(String + .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid))); + + VfModules vfModule = foundedVnf.getVfModules().stream().filter( + vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid)) + .findFirst().orElseThrow(() -> new PayloadGenerationException(String + .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid))); + + return vfModule.getInstanceParams(); + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVnf.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVnf.java new file mode 100644 index 0000000000..22c9a7bee4 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ConfigureInstanceParamsForVnf.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.client.exception.PayloadGenerationException; +import org.onap.so.serviceinstancebeans.Service; +import org.onap.so.serviceinstancebeans.Vnfs; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Component +public class ConfigureInstanceParamsForVnf { + + @Autowired + private ExtractServiceFromUserParameters extractServiceFromUserParameters; + + /** + * Read instance parameters for VNF and put into JsonObject. + * + * @param jsonObject - JsonObject which will hold the payload to send to CDS. + * @param userParamsFromRequest - User parameters. + * @param modelCustomizationUuid - Unique ID for Vnf. + * @throws PayloadGenerationException if it doesn't able to populate instance parameters from SO payload. + */ + public void populateInstanceParams(JsonObject jsonObject, List> userParamsFromRequest, + String modelCustomizationUuid) throws PayloadGenerationException { + try { + Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest); + List> instanceParamsList = getInstanceParamForVnf(service, modelCustomizationUuid); + + instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream()) + .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue())); + } catch (Exception e) { + throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e); + } + } + + private List> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid) + throws PayloadGenerationException { + Optional foundedVnf = service.getResources().getVnfs().stream() + .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid)) + .findFirst(); + + if (foundedVnf.isPresent()) { + return foundedVnf.get().getInstanceParams(); + } else { + throw new PayloadGenerationException(String.format( + "Can not find vnf for genericVnfModelCustomizationUuid: %s", genericVnfModelCustomizationUuid)); + } + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ExtractServiceFromUserParameters.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ExtractServiceFromUserParameters.java new file mode 100644 index 0000000000..43fabd3253 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ExtractServiceFromUserParameters.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.client.cds; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.onap.so.client.exception.PayloadGenerationException; +import org.onap.so.serviceinstancebeans.Service; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; + +@Component +public class ExtractServiceFromUserParameters { + + private static final String SERVICE_KEY = "service"; + + @Autowired + private ObjectMapper objectMapper; + + public Service getServiceFromRequestUserParams(List> userParams) throws Exception { + Map serviceMap = userParams.stream().filter(key -> key.containsKey(SERVICE_KEY)).findFirst() + .orElseThrow(() -> new Exception("Can not find service in userParams section in generalBuildingBlock")); + return getServiceObjectFromServiceMap(serviceMap); + } + + private Service getServiceObjectFromServiceMap(Map serviceMap) throws PayloadGenerationException { + try { + String serviceFromJson = objectMapper.writeValueAsString(serviceMap.get(SERVICE_KEY)); + return objectMapper.readValue(serviceFromJson, Service.class); + } catch (Exception e) { + throw new PayloadGenerationException("An exception occurred while converting json object to Service object", + e); + } + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/GeneratePayloadForCds.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/GeneratePayloadForCds.java new file mode 100644 index 0000000000..0766dae8e1 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/GeneratePayloadForCds.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean; +import org.onap.so.client.exception.PayloadGenerationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import java.util.UUID; + +@Component +public class GeneratePayloadForCds { + + private static final String ORIGINATOR_ID = "SO"; + private static final String MODE = "sync"; + private static final String BUILDING_BLOCK = "buildingBlock"; + + @Autowired + private VnfCDSRequestProvider vnfCDSRequestProvider; + + @Autowired + private VfModuleCDSRequestProvider vfModuleCDSRequestProvider; + + @Autowired + private ServiceCDSRequestProvider serviceCDSRequestProvider; + + @Autowired + private ExtractPojosForBB extractPojosForBB; + + /** + * Build properties like (blueprint name, version, action etc..) along with the request payload. + * + * @param execution - A building block execution object. + * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information. + * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS. + */ + public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution) + throws PayloadGenerationException { + + ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK); + BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock(); + String scope = buildingBlock.getBpmnScope(); + String action = buildingBlock.getBpmnAction(); + + + CDSRequestProvider requestProvider = getRequestProviderByScope(scope); + requestProvider.setExecutionObject(execution); + + final String requestPayload = requestProvider.buildRequestPayload(action) + .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS")); + + final String requestId = execution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId(); + final AbstractCDSPropertiesBean cdsPropertiesBean = new AbstractCDSPropertiesBean(); + cdsPropertiesBean.setRequestObject(requestPayload); + cdsPropertiesBean.setBlueprintName(requestProvider.getBlueprintName()); + cdsPropertiesBean.setBlueprintVersion(requestProvider.getBlueprintVersion()); + cdsPropertiesBean.setRequestId(requestId); + cdsPropertiesBean.setOriginatorId(ORIGINATOR_ID); + cdsPropertiesBean.setSubRequestId(UUID.randomUUID().toString()); + cdsPropertiesBean.setActionName(action); + cdsPropertiesBean.setMode(MODE); + + return cdsPropertiesBean; + } + + private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException { + CDSRequestProvider requestProvider; + switch (scope) { + case "vnf": + requestProvider = vnfCDSRequestProvider; + break; + case "vfModule": + requestProvider = vfModuleCDSRequestProvider; + break; + case "service": + requestProvider = serviceCDSRequestProvider; + break; + default: + throw new PayloadGenerationException("No scope defined with " + scope); + } + return requestProvider; + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/PayloadConstants.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/PayloadConstants.java new file mode 100644 index 0000000000..e758f548f7 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/PayloadConstants.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +public final class PayloadConstants { + + private PayloadConstants() { + + } + + public static final String REQUEST = "request"; + public static final String PROPERTIES = "properties"; + public static final String SCOPE = "scope"; + public static final String ACTION = "action"; + public static final String SEPARATOR = "-"; +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ServiceCDSRequestProvider.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ServiceCDSRequestProvider.java new file mode 100644 index 0000000000..12c841a2c6 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/ServiceCDSRequestProvider.java @@ -0,0 +1,89 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2020 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance; +import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.exception.PayloadGenerationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import java.util.Optional; +import static org.onap.so.client.cds.PayloadConstants.PROPERTIES; +import static org.onap.so.client.cds.PayloadConstants.SEPARATOR; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class ServiceCDSRequestProvider implements CDSRequestProvider { + + private static final String EMPTY_STRING = ""; + private String resolutionKey; + private BuildingBlockExecution execution; + + @Autowired + private ExtractPojosForBB extractPojosForBB; + + @Override + public String getBlueprintName() { + return EMPTY_STRING; + } + + @Override + public String getBlueprintVersion() { + return EMPTY_STRING; + } + + @Override + public void setExecutionObject(T executionObject) { + execution = (BuildingBlockExecution) executionObject; + } + + @Override + public Optional buildRequestPayload(String action) throws PayloadGenerationException { + JsonObject cdsPropertyObject = new JsonObject(); + JsonObject serviceObject = new JsonObject(); + try { + ServiceInstance serviceInstance = + extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); + + resolutionKey = serviceInstance.getServiceInstanceName(); + + // TODO Need to figure out how to populate blueprint name and version for service. + + serviceObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId()); + serviceObject.addProperty("service-model-uuid", + serviceInstance.getModelInfoServiceInstance().getModelUuid()); + + } catch (Exception e) { + throw new PayloadGenerationException("Failed to buildPropertyObjectForService", e); + } + + cdsPropertyObject.addProperty("resolution-key", resolutionKey); + cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, serviceObject); + + return Optional.of(buildRequestJsonObject(cdsPropertyObject, action)); + } + +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VfModuleCDSRequestProvider.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VfModuleCDSRequestProvider.java new file mode 100644 index 0000000000..bba8925f21 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VfModuleCDSRequestProvider.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance; +import org.onap.so.bpmn.servicedecomposition.bbobjects.VfModule; +import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.exception.PayloadGenerationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import static org.onap.so.client.cds.PayloadConstants.PROPERTIES; +import static org.onap.so.client.cds.PayloadConstants.SEPARATOR; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class VfModuleCDSRequestProvider implements CDSRequestProvider { + private String blueprintName; + private String blueprintVersion; + private BuildingBlockExecution execution; + + @Autowired + private ExtractPojosForBB extractPojosForBB; + + @Autowired + private ConfigureInstanceParamsForVfModule configureInstanceParamsForVfModule; + + @Override + public String getBlueprintName() { + return blueprintName; + } + + @Override + public String getBlueprintVersion() { + return blueprintVersion; + } + + @Override + public void setExecutionObject(T executionObject) { + execution = (BuildingBlockExecution) executionObject; + } + + @Override + public Optional buildRequestPayload(String action) throws PayloadGenerationException { + JsonObject cdsPropertyObject = new JsonObject(); + JsonObject vfModuleObject = new JsonObject(); + String vfModuleName; + + try { + ServiceInstance serviceInstance = + extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); + GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID); + + final String modelCustomizationUuidForVnf = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid(); + + blueprintName = genericVnf.getBlueprintName(); + blueprintVersion = genericVnf.getBlueprintVersion(); + + VfModule vfModule = extractPojosForBB.extractByKey(execution, ResourceKey.VF_MODULE_ID); + vfModuleName = vfModule.getVfModuleName(); + + final String modelCustomizationUuidForVfModule = + vfModule.getModelInfoVfModule().getModelCustomizationUUID(); + + vfModuleObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId()); + vfModuleObject.addProperty("service-model-uuid", + serviceInstance.getModelInfoServiceInstance().getModelUuid()); + vfModuleObject.addProperty("vnf-id", genericVnf.getVnfId()); + vfModuleObject.addProperty("vnf-name", genericVnf.getVnfName()); + vfModuleObject.addProperty("vf-module-id", vfModule.getVfModuleId()); + vfModuleObject.addProperty("vf-module-name", vfModule.getVfModuleName()); + vfModuleObject.addProperty("vf-module-customization-uuid", + vfModule.getModelInfoVfModule().getModelCustomizationUUID()); + + final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock(); + List> userParamsFromRequest = + buildingBlock.getRequestContext().getRequestParameters().getUserParams(); + + configureInstanceParamsForVfModule.populateInstanceParams(vfModuleObject, userParamsFromRequest, + modelCustomizationUuidForVnf, modelCustomizationUuidForVfModule); + } catch (Exception e) { + throw new PayloadGenerationException("Failed to buildPropertyObject for VF-Module", e); + } + + // Not sure for resolutionKey should be same as vfModule name. + cdsPropertyObject.addProperty("resolution-key", vfModuleName); + cdsPropertyObject.addProperty("template-prefix", vfModuleName + action); + cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vfModuleObject); + + return Optional.of(buildRequestJsonObject(cdsPropertyObject, action)); + } + +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VnfCDSRequestProvider.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VnfCDSRequestProvider.java new file mode 100644 index 0000000000..d33976d229 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/cds/VnfCDSRequestProvider.java @@ -0,0 +1,106 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.cds; + +import com.google.gson.JsonObject; +import org.onap.so.bpmn.common.BuildingBlockExecution; +import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf; +import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance; +import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock; +import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey; +import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB; +import org.onap.so.client.exception.PayloadGenerationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import static org.onap.so.client.cds.PayloadConstants.PROPERTIES; +import static org.onap.so.client.cds.PayloadConstants.SEPARATOR; + +@Component +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class VnfCDSRequestProvider implements CDSRequestProvider { + private String blueprintName; + private String blueprintVersion; + private BuildingBlockExecution execution; + + @Autowired + private ExtractPojosForBB extractPojosForBB; + + @Autowired + private ConfigureInstanceParamsForVnf configureInstanceParamsForVnf; + + @Override + public String getBlueprintName() { + return blueprintName; + } + + @Override + public String getBlueprintVersion() { + return blueprintVersion; + } + + @Override + public void setExecutionObject(T executionObject) { + execution = (BuildingBlockExecution) executionObject; + } + + @Override + public Optional buildRequestPayload(String action) throws PayloadGenerationException { + JsonObject cdsPropertyObject = new JsonObject(); + JsonObject vnfObject = new JsonObject(); + String resolutionKey; + try { + ServiceInstance serviceInstance = + extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID); + GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID); + + final String modelCustomizationUuid = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid(); + + resolutionKey = genericVnf.getVnfName(); + blueprintName = genericVnf.getBlueprintName(); + blueprintVersion = genericVnf.getBlueprintVersion(); + + vnfObject.addProperty("service-instance-id", serviceInstance.getServiceInstanceId()); + vnfObject.addProperty("service-model-uuid", serviceInstance.getModelInfoServiceInstance().getModelUuid()); + vnfObject.addProperty("vnf-id", genericVnf.getVnfId()); + vnfObject.addProperty("vnf-name", genericVnf.getVnfName()); + vnfObject.addProperty("vnf-customization-uuid", modelCustomizationUuid); + + final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock(); + List> userParamsFromRequest = + buildingBlock.getRequestContext().getRequestParameters().getUserParams(); + + configureInstanceParamsForVnf.populateInstanceParams(vnfObject, userParamsFromRequest, + modelCustomizationUuid); + } catch (Exception e) { + throw new PayloadGenerationException("Failed to buildPropertyObjectForVnf", e); + } + + cdsPropertyObject.addProperty("resolution-key", resolutionKey); + cdsPropertyObject.add(action + SEPARATOR + PROPERTIES, vnfObject); + + return Optional.of(buildRequestJsonObject(cdsPropertyObject, action)); + } +} diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/exception/PayloadGenerationException.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/exception/PayloadGenerationException.java new file mode 100644 index 0000000000..3c148a17a5 --- /dev/null +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/exception/PayloadGenerationException.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2019 Bell Canada + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.exception; + +public class PayloadGenerationException extends Exception { + + /** + * @param message The message to dump + */ + public PayloadGenerationException(final String message) { + super(message); + } + + /** + * @param message The message to dump. + * @param cause The throwable cause object. + */ + public PayloadGenerationException(final String message, final Throwable cause) { + super(message, cause); + } +} -- cgit 1.2.3-korg