2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
23 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_REQUEST_PARAM_NAME;
24 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_RESPONSE_PARAM_NAME;
25 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.DOT;
26 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.INPUT_PARAMETER;
27 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.SPACE;
28 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.UNDERSCORE;
29 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
31 import org.onap.so.bpmn.common.BuildingBlockExecution;
32 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
33 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
35 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
36 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
37 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
38 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
39 import org.onap.so.client.exception.ExceptionBuilder;
40 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
41 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
42 import org.onap.vnfmadapter.v1.model.Tenant;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.stereotype.Component;
48 import com.google.common.base.Optional;
51 * This class is executed from EtsiVnfInstantiateBB building block and it sends the create request
54 * @author waqas.ikram@est.tech
57 public class VnfmAdapterCreateVnfTask {
59 private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterCreateVnfTask.class);
61 private final ExtractPojosForBB extractPojosForBB;
62 private final ExceptionBuilder exceptionUtil;
63 private final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
66 public VnfmAdapterCreateVnfTask(final ExceptionBuilder exceptionUtil, final ExtractPojosForBB extractPojosForBB,
67 final VnfmAdapterServiceProvider vnfmAdapterServiceProvider) {
68 this.exceptionUtil = exceptionUtil;
69 this.extractPojosForBB = extractPojosForBB;
70 this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
74 * Create {@link CreateVnfRequest} object with required fields and store it in
75 * {@link org.camunda.bpm.engine.delegate.DelegateExecution}
77 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
79 public void buildCreateVnfRequest(final BuildingBlockExecution execution) {
81 LOGGER.debug("Executing buildCreateVnfRequest ...");
83 final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
84 final CloudRegion cloudRegion = buildingBlock.getCloudRegion();
86 final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
87 final ModelInfoGenericVnf modelInfoGenericVnf = vnf.getModelInfoGenericVnf();
89 final InputParameter inputParameter = getInputParameter(execution);
91 final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
93 createVnfRequest.setName(getName(vnf.getVnfName(), modelInfoGenericVnf.getModelInstanceName()));
94 createVnfRequest.setTenant(getTenant(cloudRegion));
95 createVnfRequest.setAdditionalParams(inputParameter.getAdditionalParams());
96 createVnfRequest.setExternalVirtualLinks(inputParameter.getExtVirtualLinks());
98 LOGGER.info("CreateVnfRequest : {}", createVnfRequest);
100 execution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, createVnfRequest);
102 LOGGER.debug("Finished executing buildCreateVnfRequest ...");
103 } catch (final Exception exception) {
104 LOGGER.error("Unable to execute buildCreateVnfRequest", exception);
105 exceptionUtil.buildAndThrowWorkflowException(execution, 1200, exception);
109 private InputParameter getInputParameter(final BuildingBlockExecution execution) {
110 final InputParameter inputParameter = execution.getVariable(INPUT_PARAMETER);
111 return inputParameter != null ? inputParameter : NullInputParameter.NULL_INSTANCE;
115 * Invoke VNFM adapter to create and instantiate VNF
117 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
119 public void invokeVnfmAdapter(final BuildingBlockExecution execution) {
121 LOGGER.debug("Executing invokeVnfmAdapter ...");
122 final CreateVnfRequest request = execution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
124 final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
126 final Optional<CreateVnfResponse> response =
127 vnfmAdapterServiceProvider.invokeCreateInstantiationRequest(vnf.getVnfId(), request);
129 if (!response.isPresent()) {
130 final String errorMessage = "Unexpected error while processing create and instantiation request";
131 LOGGER.error(errorMessage);
132 exceptionUtil.buildAndThrowWorkflowException(execution, 1201, errorMessage);
135 final CreateVnfResponse vnfResponse = response.get();
137 LOGGER.debug("Vnf instantiation response: {}", vnfResponse);
138 execution.setVariable(CREATE_VNF_RESPONSE_PARAM_NAME, vnfResponse);
140 LOGGER.debug("Finished executing invokeVnfmAdapter ...");
141 } catch (final Exception exception) {
142 LOGGER.error("Unable to invoke create and instantiation request", exception);
143 exceptionUtil.buildAndThrowWorkflowException(execution, 1202, exception);
147 private Tenant getTenant(final CloudRegion cloudRegion) {
148 final Tenant tenant = new Tenant();
149 tenant.setCloudOwner(cloudRegion.getCloudOwner());
150 tenant.setRegionName(cloudRegion.getLcpCloudRegionId());
151 tenant.setTenantId(cloudRegion.getTenantId());
155 private String getName(final String vnfName, final String modelInstanceName) {
156 if (modelInstanceName != null) {
157 return (vnfName + DOT + modelInstanceName).replaceAll(SPACE, UNDERSCORE);
159 return vnfName != null ? vnfName.replaceAll(SPACE, UNDERSCORE) : vnfName;