Merge "Removing so-monitoring module"
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / tasks / InstantiateNsTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 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 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.INSTANTIATE_NS_REQUEST_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NETWORK_SERVICE_DESCRIPTOR_PARAM_NAME;
24 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_INSTANCE_ID_PARAM_NAME;
25 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.VNF_CREATE_INSTANTIATE_REQUESTS;
26 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.FINISHED;
27 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
28 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.STARTED;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Optional;
34 import org.apache.commons.lang3.StringUtils;
35 import org.camunda.bpm.engine.delegate.DelegateExecution;
36 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.Tenant;
37 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.NsdInfo;
38 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPkgInfo;
39 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.exceptions.EtsiCatalogManagerRequestFailureException;
40 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.etsicatalog.EtsiCatalogPackageManagementServiceProvider;
41 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.NetworkServiceDescriptor;
42 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.nsd.VirtualNetworkFunction;
43 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNfInst;
44 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
45 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
46 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
47 import org.onap.so.etsi.nfvo.ns.lcm.model.InstantiateNsRequest;
48 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.stereotype.Component;
53
54 /**
55  * @author Waqas Ikram (waqas.ikram@est.tech)
56  * @author Andrew Lamb (andrew.a.lamb@est.tech)
57  *
58  */
59 @Component
60 public class InstantiateNsTask extends AbstractNetworkServiceTask {
61
62     private static final String IS_NS_INSTANTIATION_SUCCESSFUL_PARAM_NAME = "isNsInstantiationSuccessful";
63     private static final String VNFD_ID_TO_VNF_PKG_ID_MAPPING_PARAM_NAME = "vnfdIdToVnfPkgIdMapping";
64     private static final Logger logger = LoggerFactory.getLogger(InstantiateNsTask.class);
65     private final EtsiCatalogPackageManagementServiceProvider etsiCatalogPackageManagementServiceProvider;
66
67     @Autowired
68     public InstantiateNsTask(final DatabaseServiceProvider databaseServiceProvider,
69             final EtsiCatalogPackageManagementServiceProvider etsiCatalogPackageManagementServiceProvider) {
70         super(databaseServiceProvider);
71         this.etsiCatalogPackageManagementServiceProvider = etsiCatalogPackageManagementServiceProvider;
72
73     }
74
75     public void setJobStatusToStarted(final DelegateExecution execution) {
76         setJobStatus(execution, STARTED, "Instantiate NS workflow process started");
77     }
78
79     public void setJobStatusToFinished(final DelegateExecution execution) {
80         setJobStatus(execution, FINISHED, "Instantiate NS workflow process finished");
81     }
82
83     public void updateNsInstanceStatusToInstantiating(final DelegateExecution execution) {
84         logger.info("Executing updateNsInstanceStatusToInstantiating");
85         setJobStatus(execution, IN_PROGRESS, "Updating NfvoNsInst Status to " + State.INSTANTIATING);
86         updateNsInstanceStatus(execution, State.INSTANTIATING);
87
88         logger.info("Finished executing updateNsInstanceStatusToInstantiating  ...");
89     }
90
91
92     public void checkIfVnfInstantiationWasSuccessful(final DelegateExecution execution) {
93         logger.info("Executing checkIfVnfInstantiationWasSuccessful");
94
95         @SuppressWarnings("unchecked")
96         final List<CreateInstantiateRequest> requests =
97                 (List<CreateInstantiateRequest>) execution.getVariable(VNF_CREATE_INSTANTIATE_REQUESTS);
98
99         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
100         final List<NfvoNfInst> nfInstances = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstId);
101
102         if (nfInstances == null || nfInstances.isEmpty()) {
103             final String message = "Found empty nf instances";
104             abortOperation(execution, message);
105         }
106
107         if (requests.size() != nfInstances.size()) {
108             final String message = "Missing nf Instance. Request triggered: " + requests.size()
109                     + " Nf Instace reqeust found in database " + nfInstances.size();
110             abortOperation(execution, message);
111         }
112
113         execution.setVariable(IS_NS_INSTANTIATION_SUCCESSFUL_PARAM_NAME, true);
114
115         nfInstances.stream().forEach(instance -> {
116             if (!State.INSTANTIATED.equals(instance.getStatus())) {
117                 logger.error("VNF : {} {} instantiation failed", instance.getNfInstId(), instance.getName());
118                 execution.setVariable(IS_NS_INSTANTIATION_SUCCESSFUL_PARAM_NAME, false);
119             }
120         });
121
122         logger.info("Finished executing checkIfVnfInstantiationWasSuccessful  ...");
123     }
124
125     public void updateNsInstanceStatusToInstantiated(final DelegateExecution execution) {
126         logger.info("Executing updateNsInstanceStatusToInstantiated");
127         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
128         setJobStatus(execution, FINISHED, "Successfully " + State.INSTANTIATED + " NS: " + nsInstId);
129
130         updateNsInstanceStatus(execution, State.INSTANTIATED);
131         logger.info("Finished executing updateNsInstanceStatusToInstantiated  ...");
132     }
133
134     public void getAndParseNsdFromEtsiCatalog(final DelegateExecution execution) {
135         logger.info("Executing getAndParseNsdFromEtsiCatalog");
136         setJobStatus(execution, IN_PROGRESS, "Retrieving and parsing NSD from ETSI Catalog Manager");
137         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution);
138         final String nsPackageId = nfvoNsInst.getNsPackageId();
139
140         try {
141             final Optional<NetworkServiceDescriptor> optional =
142                     etsiCatalogPackageManagementServiceProvider.getNetworkServiceDescriptor(nsPackageId);
143
144             if (optional.isEmpty()) {
145                 final String message = "Unable to parse NSD " + nsPackageId;
146                 logger.error(message);
147                 abortOperation(execution, message);
148             }
149
150             final NetworkServiceDescriptor networkServiceDescriptor = optional.get();
151             logger.info("Found {}", networkServiceDescriptor);
152             execution.setVariable(NETWORK_SERVICE_DESCRIPTOR_PARAM_NAME, networkServiceDescriptor);
153
154         } catch (final EtsiCatalogManagerRequestFailureException failureException) {
155             final String message =
156                     "Unexpected exception occured while getting nsd content using nsPackageId: " + nsPackageId;
157             logger.error(message, failureException);
158
159             abortOperation(execution, message);
160         }
161
162         logger.info("Finished executing getAndParseNsdFromEtsiCatalog  ...");
163
164     }
165
166     public void prepareCreateInstantiateRequests(final DelegateExecution execution) {
167         logger.info("Executing prepareCreateInstantiateRequests ...");
168         setJobStatus(execution, IN_PROGRESS, "Preparing VNF CreateInstantiate requests");
169         final NetworkServiceDescriptor networkServiceDescriptor =
170                 (NetworkServiceDescriptor) execution.getVariable(NETWORK_SERVICE_DESCRIPTOR_PARAM_NAME);
171         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
172         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
173
174         final InstantiateNsRequest instantiateNsRequest =
175                 (InstantiateNsRequest) execution.getVariable(INSTANTIATE_NS_REQUEST_PARAM_NAME);
176
177         final List<NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf> additionalParamsForVnfList =
178                 instantiateNsRequest.getAdditionalParamsForVnf();
179
180         @SuppressWarnings("unchecked")
181         final Map<String, String> vnfdIdToVnfPkgIdMapping =
182                 (Map<String, String>) execution.getVariable(VNFD_ID_TO_VNF_PKG_ID_MAPPING_PARAM_NAME);
183
184         final List<CreateInstantiateRequest> requests = new ArrayList<>();
185
186         for (final VirtualNetworkFunction vnf : networkServiceDescriptor.getVnfs()) {
187             final List<String> vnfmInfoList = vnf.getVnfmInfoList();
188
189             final String vnfdId = vnf.getVnfdId();
190             final String vnfPkgId = vnfdIdToVnfPkgIdMapping.get(vnfdId);
191             if (vnfdId == null || vnfmInfoList.isEmpty() || vnfPkgId == null) {
192                 final String message = "Unable to find VnfdId/vnfmInfo/VnfPkgId for vnf: " + vnf;
193                 abortOperation(execution, message);
194             }
195
196             final Optional<NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf> additionalParamsForVnfOptional =
197                     getAdditionalParamsForVnf(vnfdId, additionalParamsForVnfList);
198
199             if (additionalParamsForVnfOptional.isEmpty()) {
200                 final String message = "Unable to find AdditionalParamsForVnf for " + vnfdId
201                         + " in instantiateNsRequest : " + instantiateNsRequest;
202                 abortOperation(execution, message);
203             }
204
205             final NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf additionalParamsForVnf =
206                     additionalParamsForVnfOptional.get();
207
208             @SuppressWarnings("unchecked")
209             final Map<String, String> additionalParams =
210                     (Map<String, String>) additionalParamsForVnf.getAdditionalParams();
211
212             final Optional<Tenant> optional = getTenant(additionalParams);
213             if (optional.isEmpty()) {
214                 final String message = "Unable to find Tenant information for " + vnfdId + " in instantiateNsRequest : "
215                         + instantiateNsRequest;
216                 abortOperation(execution, message);
217             }
218
219             final String vnfInstanceName = additionalParamsForVnf.getVnfInstanceName();
220             final String vnfName = StringUtils.isNotBlank(vnfInstanceName) ? vnfInstanceName
221                     : nfvoNsInst.getName() + "." + vnf.getVnfName();
222
223             final CreateInstantiateRequest createInstantiateRequest =
224                     new CreateInstantiateRequest().nsInstId(nsInstId).vnfdId(vnfdId).vnfName(vnfName).vnfPkgId(vnfPkgId)
225                             .nfType(vnfmInfoList.get(0)).tenant(optional.get()).additionalParams(additionalParams);
226
227             logger.info("Adding request to list: {}", createInstantiateRequest);
228             requests.add(createInstantiateRequest);
229
230         }
231         execution.setVariable(VNF_CREATE_INSTANTIATE_REQUESTS, requests);
232
233         logger.info("Finished executing prepareCreateInstantiateRequests  ...");
234     }
235
236     public void getVnfPkgIdForEachVnfdId(final DelegateExecution execution) {
237         logger.info("Executing getVnfPkgIdForEachVnfdId ...");
238
239         setJobStatus(execution, IN_PROGRESS, "Getting VnfPkgId for each VnfdId");
240
241         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
242         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
243         final String nsPackageId = nfvoNsInst.getNsPackageId();
244
245         final Map<String, String> vnfdIdToVnfPkgIdMapping = new HashMap<>();
246         try {
247             final Optional<NsdInfo> nsdInfoOptional =
248                     etsiCatalogPackageManagementServiceProvider.getNSPackageModel(nsPackageId);
249
250             if (nsdInfoOptional.isEmpty()) {
251                 final String message = "Unable to find NS package using NS package id: " + nsPackageId;
252                 logger.error(message);
253                 abortOperation(execution, message);
254             }
255
256             final NsdInfo nsdInfo = nsdInfoOptional.get();
257             for (final String vnfPkgId : nsdInfo.getVnfPkgIds()) {
258                 final Optional<VnfPkgInfo> optional =
259                         etsiCatalogPackageManagementServiceProvider.getVnfPkgInfo(vnfPkgId);
260
261                 if (optional.isEmpty()) {
262                     final String message = "Unable to find VNF package using NS vnfPkgId: " + vnfPkgId;
263                     logger.error(message);
264                     abortOperation(execution, message);
265                 }
266                 final VnfPkgInfo vnfPkgInfo = optional.get();
267                 vnfdIdToVnfPkgIdMapping.put(vnfPkgInfo.getVnfdId(), vnfPkgId);
268
269             }
270             logger.info("vnfdIdToVnfPkgIdMapping: {}", vnfdIdToVnfPkgIdMapping);
271             execution.setVariable(VNFD_ID_TO_VNF_PKG_ID_MAPPING_PARAM_NAME, vnfdIdToVnfPkgIdMapping);
272
273         } catch (final EtsiCatalogManagerRequestFailureException failureException) {
274             final String message =
275                     "Unexpected exception occured while getting VNF package using nsPackageId: " + nsPackageId;
276             logger.error(message, failureException);
277
278             abortOperation(execution, message);
279         }
280
281         logger.info("Finished executing getVnfPkgIdForEachVnfdId ...");
282
283     }
284
285     public void logTimeOut(final DelegateExecution execution) {
286         logger.error("Vnf instantiation timedOut ...");
287         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
288         final List<NfvoNfInst> nfInstances = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstId);
289         if (nfInstances != null) {
290             nfInstances.stream().forEach(instance -> {
291                 logger.info("Current status {} of vnf: {}", instance.getStatus(), instance.getName());
292             });
293         }
294     }
295
296     public void setJobStatusToError(final DelegateExecution execution) {
297         updateNsInstanceStatus(execution, State.FAILED);
298         setJobStatusToError(execution, "Instantiate NS workflow process failed");
299     }
300
301     private Optional<Tenant> getTenant(final Map<String, String> additionalParams) {
302         final String vimId = additionalParams.get("vim_id");
303         if (vimId != null) {
304             final String[] splitString = vimId.split("_");
305             if (splitString.length == 3) {
306                 logger.info("Found Tenant in additionalParams: {}", additionalParams);
307                 return Optional.of(
308                         new Tenant().cloudOwner(splitString[0]).regionName(splitString[1]).tenantId(splitString[2]));
309             }
310
311         }
312         logger.error("Unable to find Tenant in additionalParams: {}", additionalParams);
313         return Optional.empty();
314     }
315
316     private Optional<NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf> getAdditionalParamsForVnf(
317             final String vnfdId,
318             final List<NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf> additionalParamsForVnfList) {
319         final Optional<NsInstancesnsInstanceIdinstantiateAdditionalParamsForVnf> optional =
320                 additionalParamsForVnfList.stream().filter(entry -> vnfdId.equals(entry.getVnfProfileId())).findFirst();
321         if (optional.isPresent()) {
322             logger.info("Found AdditionalParamsForVnf in instantiateNsRequest using vnfdId: {}", vnfdId);
323
324         }
325         return optional;
326     }
327
328 }