Refactor SOL003 Adapter to organize its modules
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / VnfmAdapterCreateVnfTask.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
22
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;
30 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfRequest;
31 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
32 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.Tenant;
33 import org.onap.so.bpmn.common.BuildingBlockExecution;
34 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.InputParameter;
35 import org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.utils.NullInputParameter;
36 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
38 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
39 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoGenericVnf;
40 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
41 import org.onap.so.client.exception.ExceptionBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
46 import com.google.common.base.Optional;
47
48 /**
49  * This class is executed from EtsiVnfInstantiateBB building block and it sends the create request to the VNFM adapter
50  * 
51  * @author waqas.ikram@est.tech
52  */
53 @Component
54 public class VnfmAdapterCreateVnfTask {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(VnfmAdapterCreateVnfTask.class);
57
58     private final ExtractPojosForBB extractPojosForBB;
59     private final ExceptionBuilder exceptionUtil;
60     private final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
61
62     @Autowired
63     public VnfmAdapterCreateVnfTask(final ExceptionBuilder exceptionUtil, final ExtractPojosForBB extractPojosForBB,
64             final VnfmAdapterServiceProvider vnfmAdapterServiceProvider) {
65         this.exceptionUtil = exceptionUtil;
66         this.extractPojosForBB = extractPojosForBB;
67         this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
68     }
69
70     /**
71      * Create {@link CreateVnfRequest} object with required fields and store it in
72      * {@link org.camunda.bpm.engine.delegate.DelegateExecution}
73      * 
74      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
75      */
76     public void buildCreateVnfRequest(final BuildingBlockExecution execution) {
77         try {
78             LOGGER.debug("Executing buildCreateVnfRequest  ...");
79
80             final GeneralBuildingBlock buildingBlock = execution.getGeneralBuildingBlock();
81             final CloudRegion cloudRegion = buildingBlock.getCloudRegion();
82
83             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
84             final ModelInfoGenericVnf modelInfoGenericVnf = vnf.getModelInfoGenericVnf();
85
86             final InputParameter inputParameter = getInputParameter(execution);
87
88             final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
89
90             createVnfRequest.setName(getName(vnf.getVnfName(), modelInfoGenericVnf.getModelInstanceName()));
91             createVnfRequest.setTenant(getTenant(cloudRegion));
92             createVnfRequest.setAdditionalParams(inputParameter.getAdditionalParams());
93             createVnfRequest.setExternalVirtualLinks(inputParameter.getExtVirtualLinks());
94
95             LOGGER.info("CreateVnfRequest : {}", createVnfRequest);
96
97             execution.setVariable(CREATE_VNF_REQUEST_PARAM_NAME, createVnfRequest);
98
99             LOGGER.debug("Finished executing buildCreateVnfRequest ...");
100         } catch (final Exception exception) {
101             LOGGER.error("Unable to execute buildCreateVnfRequest", exception);
102             exceptionUtil.buildAndThrowWorkflowException(execution, 1200, exception);
103         }
104     }
105
106     private InputParameter getInputParameter(final BuildingBlockExecution execution) {
107         final InputParameter inputParameter = execution.getVariable(INPUT_PARAMETER);
108         return inputParameter != null ? inputParameter : NullInputParameter.NULL_INSTANCE;
109     }
110
111     /**
112      * Invoke VNFM adapter to create and instantiate VNF
113      * 
114      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
115      */
116     public void invokeVnfmAdapter(final BuildingBlockExecution execution) {
117         try {
118             LOGGER.debug("Executing invokeVnfmAdapter  ...");
119             final CreateVnfRequest request = execution.getVariable(CREATE_VNF_REQUEST_PARAM_NAME);
120
121             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
122
123             final Optional<CreateVnfResponse> response =
124                     vnfmAdapterServiceProvider.invokeCreateInstantiationRequest(vnf.getVnfId(), request);
125
126             if (!response.isPresent()) {
127                 final String errorMessage = "Unexpected error while processing create and instantiation request";
128                 LOGGER.error(errorMessage);
129                 exceptionUtil.buildAndThrowWorkflowException(execution, 1201, errorMessage);
130             }
131
132             final CreateVnfResponse vnfResponse = response.get();
133
134             LOGGER.debug("Vnf instantiation response: {}", vnfResponse);
135             execution.setVariable(CREATE_VNF_RESPONSE_PARAM_NAME, vnfResponse);
136
137             LOGGER.debug("Finished executing invokeVnfmAdapter ...");
138         } catch (final Exception exception) {
139             LOGGER.error("Unable to invoke create and instantiation request", exception);
140             exceptionUtil.buildAndThrowWorkflowException(execution, 1202, exception);
141         }
142     }
143
144     private Tenant getTenant(final CloudRegion cloudRegion) {
145         final Tenant tenant = new Tenant();
146         tenant.setCloudOwner(cloudRegion.getCloudOwner());
147         tenant.setRegionName(cloudRegion.getLcpCloudRegionId());
148         tenant.setTenantId(cloudRegion.getTenantId());
149         return tenant;
150     }
151
152     private String getName(final String vnfName, final String modelInstanceName) {
153         if (modelInstanceName != null) {
154             return (vnfName + DOT + modelInstanceName).replaceAll(SPACE, UNDERSCORE);
155         }
156         return vnfName != null ? vnfName.replaceAll(SPACE, UNDERSCORE) : vnfName;
157     }
158
159 }