Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / LifecycleManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18
19
20 import com.google.gson.Gson;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.nokia.cbam.catalog.v1.model.CatalogAdapterVnfpackage;
24 import com.nokia.cbam.lcm.v32.model.*;
25 import com.nokia.cbam.lcm.v32.model.ScaleDirection;
26 import java.util.*;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import javax.servlet.http.HttpServletResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.IGrantManager;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.StoreLoader;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.UserVisibleError;
34 import org.onap.vnfmdriver.model.ExtVirtualLinkInfo;
35 import org.onap.vnfmdriver.model.*;
36 import org.onap.vnfmdriver.model.VimInfo;
37 import org.onap.vnfmdriver.model.VnfInfo;
38 import org.slf4j.Logger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41 import org.yaml.snakeyaml.Yaml;
42
43 import static java.lang.Integer.parseInt;
44 import static java.nio.charset.StandardCharsets.UTF_8;
45
46 import static com.google.common.base.Splitter.on;
47 import static com.google.common.collect.Iterables.find;
48 import static com.google.common.collect.Lists.newArrayList;
49 import static com.google.common.collect.Sets.newHashSet;
50 import static com.nokia.cbam.lcm.v32.model.InstantiationState.INSTANTIATED;
51 import static com.nokia.cbam.lcm.v32.model.OperationStatus.FINISHED;
52 import static com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE;
53 import static com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum.*;
54 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.*;
55 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
56 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
57 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST;
58 import static org.slf4j.LoggerFactory.getLogger;
59 import static org.springframework.util.StringUtils.isEmpty;
60
61 /**
62  * Responsible for executing lifecycle operation on the VNF
63  */
64 @Component
65 public class LifecycleManager {
66     public static final String ONAP_CSAR_ID = "onapCsarId";
67     public static final long OPERATION_STATUS_POLLING_INTERVAL_IN_MS = 5000L;
68     /**
69      * The key of the CBAM VNF extension for the identifier of the VNFM in ONAP
70      */
71     public static final String EXTERNAL_VNFM_ID = "externalVnfmId";
72     public static final String SCALE_OPERATION_NAME = "scale";
73     private static Logger logger = getLogger(LifecycleManager.class);
74     private final CatalogManager catalogManager;
75     private final IGrantManager grantManager;
76     private final JobManager jobManager;
77     private final ILifecycleChangeNotificationManager notificationManager;
78     private final CbamRestApiProvider cbamRestApiProvider;
79     private final VimInfoProvider vimInfoProvider;
80
81     /**
82      * Runs asynchronous operations in the background
83      */
84     private ExecutorService executorService = Executors.newCachedThreadPool();
85
86     @Autowired
87     LifecycleManager(CatalogManager catalogManager, IGrantManager grantManager, CbamRestApiProvider restApiProvider, VimInfoProvider vimInfoProvider, JobManager jobManager, ILifecycleChangeNotificationManager notificationManager) {
88         this.vimInfoProvider = vimInfoProvider;
89         this.grantManager = grantManager;
90         this.cbamRestApiProvider = restApiProvider;
91         this.jobManager = jobManager;
92         this.notificationManager = notificationManager;
93         this.catalogManager = catalogManager;
94     }
95
96     /**
97      * @param vimId the VIM identifier
98      * @return the name of the region
99      */
100     public static String getRegionName(String vimId) {
101         return newArrayList(on(SEPARATOR).split(vimId)).get(1);
102     }
103
104     /**
105      * @param vimId the VIM identifier
106      * @return the owner of the cloud
107      */
108     public static String getCloudOwner(String vimId) {
109         return newArrayList(on(SEPARATOR).split(vimId)).get(0);
110     }
111
112     private static OperationExecution findLastInstantiation(List<OperationExecution> operationExecutions) {
113         return find(NEWEST_OPERATIONS_FIRST.sortedCopy(operationExecutions), op -> INSTANTIATE.equals(op.getOperationType()));
114     }
115
116     /**
117      * Create the VNF. It consists of the following steps
118      * <ul>
119      * <li>upload the VNF package to CBAM package (if not already there)</li>
120      * <li>create the VNF on CBAM</li>
121      * <li>modify attributes of the VNF (add onapCsarId field)</li>
122      * </ul>
123      * The rollback of the failed operation is not implemented
124      * <ul>
125      * <li>delete the VNF if error occurs before instantiation</li>
126      * <li>terminate & delete VNF if error occurs after instantiation</li>
127      * </ul>
128      *
129      * @param vnfmId      the identifier of the VNFM
130      * @param csarId      the identifier of the VNF package
131      * @param vnfName     the name of the VNF
132      * @param description the description of the VNF
133      * @return the VNF creation result
134      */
135     public VnfCreationResult create(String vnfmId, String csarId, String vnfName, String description) {
136         logOperationInput("not yet specified", "creation", csarId);
137         try {
138             CatalogAdapterVnfpackage cbamPackage = catalogManager.preparePackageInCbam(vnfmId, csarId);
139             CreateVnfRequest vnfCreateRequest = new CreateVnfRequest();
140             vnfCreateRequest.setVnfdId(cbamPackage.getVnfdId());
141             vnfCreateRequest.setName(vnfName);
142             vnfCreateRequest.setDescription(description);
143             com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsPost(vnfCreateRequest, NOKIA_LCM_API_VERSION).blockingFirst();
144             addVnfdIdToVnfModifyableAttributeExtensions(vnfmId, vnfInfo.getId(), csarId);
145             return new VnfCreationResult(vnfInfo, cbamPackage.getVnfdId());
146         } catch (Exception e) {
147             throw buildFatalFailure(logger, "Unable to create the VNF", e);
148         }
149     }
150
151     private void logOperationInput(String vnfId, String operationName, Object payload) {
152         if (logger.isInfoEnabled()) {
153             logger.info("Starting {} operation on VNF with {} identifier with {} parameter", operationName, vnfId, new Gson().toJson(payload));
154         }
155     }
156
157     /**
158      * Instantiate the VNF
159      *
160      * @param vnfmId               the identifier of the VNFM
161      * @param request              the VNF instantiation request
162      * @param httpResponse         the HTTP response that corresponds to the VNF instantiation request
163      * @param additionalParameters additional parameters
164      * @param vnfId                thr identifier of the VNF
165      * @param vnfdId               the identifier of the VNF package in CBAM
166      * @return the instantiation response
167      */
168     public VnfInstantiateResponse instantiate(String vnfmId, VnfInstantiateRequest request, HttpServletResponse httpResponse, AdditionalParameters additionalParameters, String vnfId, String vnfdId) {
169         logOperationInput(vnfId, "instantiation", request);
170         validateVimType(additionalParameters.getVimType());
171         VnfInstantiateResponse response = new VnfInstantiateResponse();
172         response.setVnfInstanceId(vnfId);
173         String vimId = getVimId(request.getAdditionalParam());
174         JobInfo spawnJob = scheduleExecution(vnfId, httpResponse, "instantiate", jobInfo ->
175                 instantiateVnf(vnfmId, request, additionalParameters, vnfdId, vnfId, vimId, jobInfo)
176         );
177         response.setJobId(spawnJob.getJobId());
178         return response;
179     }
180
181     /**
182      * Instantiate (VF-C terminology) the VNF. It consists of the following steps
183      * <ul>
184      * <li>upload the VNF package to CBAM package (if not already there)</li>
185      * <li>create the VNF on CBAM</li>
186      * <li>modify attributes of the VNF (add onapCsarId field)</li>
187      * <li>asynchronously</li>
188      * <li>request grant from VF-C</li>
189      * <li>instantiate VNF on CBAM</li>
190      * <li>return VNF & job id (after create VNF on CBAM)</li>
191      * <li></li>
192      * </ul>
193      * The rollback of the failed operation is not implemented
194      * <ul>
195      * <li>delete the VNF if error occurs before instantiation</li>
196      * <li>terminate & delete VNf if error occurs after instantiation</li>
197      * </ul>
198      *
199      * @param vnfmId       the identifier of the VNFM
200      * @param request      the instantiation request
201      * @param httpResponse the HTTP response
202      * @return the instantiation response
203      */
204     public VnfInstantiateResponse createAndInstantiate(String vnfmId, VnfInstantiateRequest request, HttpServletResponse httpResponse) {
205         AdditionalParameters additionalParameters = convertInstantiationAdditionalParams(request.getVnfPackageId(), request.getAdditionalParam());
206         validateVimType(additionalParameters.getVimType());
207         VnfCreationResult creationResult = create(vnfmId, request.getVnfDescriptorId(), request.getVnfInstanceName(), request.getVnfInstanceDescription());
208         return instantiate(vnfmId, request, httpResponse, additionalParameters, creationResult.vnfInfo.getId(), creationResult.vnfdId);
209     }
210
211     private void instantiateVnf(String vnfmId, VnfInstantiateRequest request, AdditionalParameters additionalParameters, String vnfdId, String vnfId, String vimId, JobInfo jobInfo) {
212         String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, vnfdId);
213         GrantVNFResponseVim vim = grantManager.requestGrantForInstantiate(vnfmId, vnfId, vimId, request.getVnfPackageId(), additionalParameters.getInstantiationLevel(), vnfdContent, jobInfo.getJobId());
214         if (vim.getVimId() == null) {
215             throw buildFatalFailure(logger, "VF-C did not send VIM identifier in grant response");
216         }
217         VimInfo vimInfo = vimInfoProvider.getVimInfo(vim.getVimId());
218         InstantiateVnfRequest instantiationRequest = new InstantiateVnfRequest();
219         addExternalLinksToRequest(request.getExtVirtualLink(), additionalParameters, instantiationRequest, vimId);
220         instantiationRequest.getVims().add(addVim(additionalParameters, vimId, vim, vimInfo));
221         instantiationRequest.setFlavourId(getFlavorId(vnfdContent));
222         instantiationRequest.setComputeResourceFlavours(additionalParameters.getComputeResourceFlavours());
223         instantiationRequest.setGrantlessMode(true);
224         instantiationRequest.setInstantiationLevelId(additionalParameters.getInstantiationLevel());
225         instantiationRequest.setSoftwareImages(additionalParameters.getSoftwareImages());
226         instantiationRequest.setZones(additionalParameters.getZones());
227         instantiationRequest.setExtManagedVirtualLinks(additionalParameters.getExtManagedVirtualLinks());
228         for (ExtVirtualLinkData extVirtualLinkData : additionalParameters.getExtVirtualLinks()) {
229             instantiationRequest.addExtVirtualLinksItem(extVirtualLinkData);
230         }
231         JsonObject root = new Gson().toJsonTree(jobInfo).getAsJsonObject();
232         if (additionalParameters.getAdditionalParams() != null) {
233             for (Map.Entry<String, JsonElement> item : new Gson().toJsonTree(additionalParameters.getAdditionalParams()).getAsJsonObject().entrySet()) {
234                 root.add(item.getKey(), item.getValue());
235             }
236         } else {
237             logger.warn("No additional parameters were specified for the operation");
238         }
239         instantiationRequest.setAdditionalParams(root);
240         OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdInstantiatePost(vnfId, instantiationRequest, NOKIA_LCM_API_VERSION).blockingFirst();
241         waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
242     }
243
244     private com.nokia.cbam.lcm.v32.model.VimInfo addVim(AdditionalParameters additionalParameters, String vimId, GrantVNFResponseVim vim, VimInfo vimInfo) {
245         if (additionalParameters.getVimType() == OPENSTACK_V2_INFO) {
246             return buildOpenStackV2INFO(vimId, vim, vimInfo);
247
248         } else if (additionalParameters.getVimType() == OPENSTACK_V3_INFO) {
249             return buildOpenStackV3INFO(vimId, additionalParameters, vim, vimInfo);
250
251         } else {
252             //OTHER VIM TYPE is not possible
253             return buildVcloudInfo(vimId, vimInfo);
254         }
255     }
256
257     private void validateVimType(com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum vimType) {
258         if (com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO.equals(vimType)) {
259             throw buildFatalFailure(logger, "Only " + OPENSTACK_V2_INFO + ", " + OPENSTACK_V3_INFO + " and " + VMWARE_VCLOUD_INFO + " is the supported VIM types");
260         }
261     }
262
263     private String getVimId(Object additionalParams) {
264         return childElement(new Gson().toJsonTree(additionalParams).getAsJsonObject(), "vimId").getAsString();
265     }
266
267     private AdditionalParameters convertInstantiationAdditionalParams(String csarId, Object additionalParams) {
268         JsonObject vnfParameters = child(child(new Gson().toJsonTree(additionalParams).getAsJsonObject(), "inputs"), "vnfs");
269         if (!vnfParameters.has(csarId)) {
270             throw buildFatalFailure(logger, "The additional parameter section does not contain setting for VNF with " + csarId + " CSAR id");
271         }
272         JsonElement additionalParamsForVnf = vnfParameters.get(csarId);
273         return new Gson().fromJson(additionalParamsForVnf, AdditionalParameters.class);
274     }
275
276     private String getFlavorId(String vnfdContent) {
277         JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
278         JsonObject capabilities = child(child(child(root, "topology_template"), "substitution_mappings"), "capabilities");
279         JsonObject deploymentFlavorProperties = child(child(capabilities, "deployment_flavour"), "properties");
280         return childElement(deploymentFlavorProperties, "flavour_id").getAsString();
281     }
282
283     private Set<Map.Entry<String, JsonElement>> getAcceptableOperationParameters(String vnfdContent, String categroryOfOperation, String operationName) {
284         JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
285         JsonObject interfaces = child(child(child(root, "topology_template"), "substitution_mappings"), "interfaces");
286         JsonObject additionalParameters = child(child(child(child(interfaces, categroryOfOperation), operationName), "inputs"), "additional_parameters");
287         return additionalParameters.entrySet();
288     }
289
290     private void addExternalLinksToRequest(List<ExtVirtualLinkInfo> extVirtualLinks, AdditionalParameters additionalParameters, InstantiateVnfRequest instantiationRequest, String vimId) {
291         for (ExtVirtualLinkInfo extVirtualLink : extVirtualLinks) {
292             ExtVirtualLinkData cbamExternalVirtualLink = new ExtVirtualLinkData();
293             cbamExternalVirtualLink.setVimId(vimId);
294             cbamExternalVirtualLink.setResourceId(extVirtualLink.getResourceId());
295             VnfExtCpData ecp = new VnfExtCpData();
296             cbamExternalVirtualLink.setExtVirtualLinkId(extVirtualLink.getVlInstanceId());
297             cbamExternalVirtualLink.getExtCps().add(ecp);
298             ecp.setCpdId(extVirtualLink.getCpdId());
299             List<NetworkAddress> addresses = additionalParameters.getExternalConnectionPointAddresses().get(extVirtualLink.getCpdId());
300             ecp.setAddresses(addresses);
301             instantiationRequest.addExtVirtualLinksItem(cbamExternalVirtualLink);
302         }
303     }
304
305     private void addVnfdIdToVnfModifyableAttributeExtensions(String vnfmId, String vnfId, String onapCsarId) {
306         ModifyVnfInfoRequest request = new ModifyVnfInfoRequest();
307         VnfProperty onapCsarIdProperty = new VnfProperty();
308         onapCsarIdProperty.setName(ONAP_CSAR_ID);
309         onapCsarIdProperty.setValue(onapCsarId);
310         request.setExtensions(new ArrayList<>());
311         request.getExtensions().add(onapCsarIdProperty);
312         VnfProperty externalVnfmIdProperty = new VnfProperty();
313         externalVnfmIdProperty.setName(EXTERNAL_VNFM_ID);
314         externalVnfmIdProperty.setValue(vnfmId);
315         request.getExtensions().add(externalVnfmIdProperty);
316         request.setVnfConfigurableProperties(null);
317         try {
318             OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdPatch(vnfId, request, NOKIA_LCM_API_VERSION).blockingFirst();
319             waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
320         } catch (Exception e) {
321             throw buildFatalFailure(logger, "Unable to set the " + ONAP_CSAR_ID + " property on the VNF", e);
322         }
323     }
324
325     private OPENSTACKV3INFO buildOpenStackV3INFO(String vimId, AdditionalParameters additionalParameters, GrantVNFResponseVim vim, org.onap.vnfmdriver.model.VimInfo vimInfo) {
326         OPENSTACKV3INFO openstackv3INFO = new OPENSTACKV3INFO();
327         openstackv3INFO.setVimInfoType(OPENSTACK_V3_INFO);
328         OpenStackAccessInfoV3 accessInfov3 = new OpenStackAccessInfoV3();
329         openstackv3INFO.accessInfo(accessInfov3);
330         accessInfov3.setPassword(vimInfo.getPassword());
331         accessInfov3.setDomain(additionalParameters.getDomain());
332         accessInfov3.setProject(vim.getAccessInfo().getTenant());
333         accessInfov3.setRegion(getRegionName(vimId));
334         accessInfov3.setUsername(vimInfo.getUserName());
335         openstackv3INFO.setInterfaceInfo(getEndpointInfo(vimInfo));
336         openstackv3INFO.setId(vimId);
337         return openstackv3INFO;
338     }
339
340     private OPENSTACKV2INFO buildOpenStackV2INFO(String vimId, GrantVNFResponseVim vim, org.onap.vnfmdriver.model.VimInfo vimInfo) {
341         OPENSTACKV2INFO openstackv2INFO = new OPENSTACKV2INFO();
342         openstackv2INFO.setVimInfoType(OPENSTACK_V2_INFO);
343         OpenStackAccessInfoV2 accessInfo = new OpenStackAccessInfoV2();
344         openstackv2INFO.setAccessInfo(accessInfo);
345         accessInfo.setPassword(vimInfo.getPassword());
346         accessInfo.setTenant(vim.getAccessInfo().getTenant());
347         accessInfo.setUsername(vimInfo.getUserName());
348         accessInfo.setRegion(getRegionName(vimId));
349         EndpointInfo interfaceEndpoint = getEndpointInfo(vimInfo);
350         openstackv2INFO.setInterfaceInfo(interfaceEndpoint);
351         openstackv2INFO.setId(vimId);
352         return openstackv2INFO;
353     }
354
355     private EndpointInfo getEndpointInfo(VimInfo vimInfo) {
356         EndpointInfo interfaceEndpoint = new EndpointInfo();
357         if (!isEmpty(vimInfo.getSslInsecure())) {
358             interfaceEndpoint.setSkipCertificateHostnameCheck(Boolean.parseBoolean(vimInfo.getSslInsecure()));
359             interfaceEndpoint.setSkipCertificateVerification(Boolean.parseBoolean(vimInfo.getSslInsecure()));
360         } else {
361             interfaceEndpoint.setSkipCertificateHostnameCheck(true);
362             interfaceEndpoint.setSkipCertificateVerification(true);
363         }
364         interfaceEndpoint.setEndpoint(vimInfo.getUrl());
365         if (!interfaceEndpoint.isSkipCertificateVerification()) {
366             interfaceEndpoint.setTrustedCertificates(new ArrayList<>());
367             for (String trustedCertificate : StoreLoader.getCertifacates(vimInfo.getSslCacert())) {
368                 interfaceEndpoint.getTrustedCertificates().add(trustedCertificate.getBytes(UTF_8));
369             }
370         }
371         return interfaceEndpoint;
372     }
373
374     private VMWAREVCLOUDINFO buildVcloudInfo(String vimId, org.onap.vnfmdriver.model.VimInfo vimInfo) {
375         VMWAREVCLOUDINFO vcloudInfo = new VMWAREVCLOUDINFO();
376         vcloudInfo.setVimInfoType(VMWARE_VCLOUD_INFO);
377         VCloudAccessInfo accessInfo = new VCloudAccessInfo();
378         vcloudInfo.setAccessInfo(accessInfo);
379         accessInfo.setPassword(vimInfo.getPassword());
380         accessInfo.setUsername(vimInfo.getUserName());
381         accessInfo.setOrganization(getRegionName(vimId));
382         vcloudInfo.setInterfaceInfo(getEndpointInfo(vimInfo));
383         vcloudInfo.setId(vimId);
384         return vcloudInfo;
385     }
386
387     /**
388      * Terminates and deletes the VNF
389      * <ul>
390      * <li>fails if the VNF does not exist</li>
391      * <li>terminates if instantiated</li>
392      * <li>deletes the VNF</li>
393      * </ul>
394      *
395      * @param vnfmId       the identifier of the VNFM
396      * @param vnfId        the identifier of the VNF
397      * @param request      the termination request
398      * @param httpResponse the HTTP response
399      * @return the job for polling the progress of the termination
400      */
401     public JobInfo terminateVnf(String vnfmId, String vnfId, VnfTerminateRequest request, HttpServletResponse httpResponse) {
402         logOperationInput(vnfId, "termination", request);
403         return scheduleExecution(vnfId, httpResponse, "terminate", jobInfo -> {
404             TerminateVnfRequest cbamRequest = new TerminateVnfRequest();
405             if (request.getTerminationType() == null) {
406                 cbamRequest.setTerminationType(TerminationType.FORCEFUL);
407             } else {
408                 if (request.getTerminationType().equals(VnfTerminationType.GRACEFUL)) {
409                     cbamRequest.setTerminationType(TerminationType.GRACEFUL);
410                     cbamRequest.setGracefulTerminationTimeout(parseInt(request.getGracefulTerminationTimeout()));
411                 } else {
412                     cbamRequest.setTerminationType(TerminationType.FORCEFUL);
413                 }
414             }
415             cbamRequest.setAdditionalParams(new Gson().toJsonTree(jobInfo).getAsJsonObject());
416             com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
417             if (vnf.getInstantiationState() == INSTANTIATED) {
418                 terminateVnf(vnfmId, vnfId, jobInfo, cbamRequest, vnf);
419             } else {
420                 cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdDelete(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
421             }
422         });
423     }
424
425     private void terminateVnf(String vnfmId, String vnfId, JobInfo jobInfo, TerminateVnfRequest cbamRequest, com.nokia.cbam.lcm.v32.model.VnfInfo vnf) {
426         String vimId = getVimIdFromInstantiationRequest(vnfmId, vnf);
427         grantManager.requestGrantForTerminate(vnfmId, vnfId, vimId, getVnfdIdFromModifyableAttributes(vnf), vnf, jobInfo.getJobId());
428         OperationExecution terminationOperation = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdTerminatePost(vnfId, cbamRequest, NOKIA_LCM_API_VERSION).blockingFirst();
429         OperationExecution finishedOperation = waitForOperationToFinish(vnfmId, vnfId, terminationOperation.getId());
430         if (finishedOperation.getStatus() == FINISHED) {
431             notificationManager.waitForTerminationToBeProcessed(finishedOperation.getId());
432             logger.info("Deleting VNF with {}", vnfId);
433             cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdDelete(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
434             logger.info("VNF with {} has been deleted", vnfId);
435
436         } else {
437             logger.error("Unable to terminate VNF the operation did not finish with success");
438         }
439     }
440
441     private String getVimIdFromInstantiationRequest(String vnfmId, com.nokia.cbam.lcm.v32.model.VnfInfo vnf) {
442         OperationExecution lastInstantiation = findLastInstantiation(vnf.getOperationExecutions());
443         Object operationParameters = cbamRestApiProvider.getCbamOperationExecutionApi(vnfmId).operationExecutionsOperationExecutionIdOperationParamsGet(lastInstantiation.getId(), NOKIA_LCM_API_VERSION).blockingFirst();
444         JsonObject root = new Gson().toJsonTree(operationParameters).getAsJsonObject();
445         return childElement(childElement(root, "vims").getAsJsonArray().get(0).getAsJsonObject(), "id").getAsString();
446     }
447
448     private String getVnfdIdFromModifyableAttributes(com.nokia.cbam.lcm.v32.model.VnfInfo vnf) {
449         return find(vnf.getExtensions(), p -> p.getName().equals(ONAP_CSAR_ID)).getValue().toString();
450     }
451
452     /**
453      * @param vnfmId the identifier of the VNFM
454      * @param vnfId  the identifier of the VNF
455      * @return the current state of the VNF
456      */
457     public VnfInfo queryVnf(String vnfmId, String vnfId) {
458         try {
459             com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
460             VnfInfo vnfInfo = new VnfInfo();
461             vnfInfo.setVersion(cbamVnfInfo.getVnfSoftwareVersion());
462             vnfInfo.setVnfInstanceId(vnfId);
463             String onapCsarId = getVnfdIdFromModifyableAttributes(cbamVnfInfo);
464             vnfInfo.setVnfdId(onapCsarId);
465             vnfInfo.setVnfPackageId(onapCsarId);
466             vnfInfo.setVnfInstanceDescription(cbamVnfInfo.getDescription());
467             vnfInfo.setVnfInstanceName(cbamVnfInfo.getName());
468             vnfInfo.setVnfProvider(cbamVnfInfo.getVnfProvider());
469             vnfInfo.setVnfStatus("ACTIVE");
470             vnfInfo.setVnfType("Kuku");
471             return vnfInfo;
472         } catch (Exception e) {
473             throw buildFatalFailure(logger, "Unable to query VNF (" + vnfId + ")", e);
474         }
475     }
476
477     private ScaleDirection convert(org.onap.vnfmdriver.model.ScaleDirection direction) {
478         if (org.onap.vnfmdriver.model.ScaleDirection.IN.equals(direction)) {
479             return ScaleDirection.IN;
480         } else {
481             return ScaleDirection.OUT;
482         }
483     }
484
485     /**
486      * Scale the VNF
487      *
488      * @param vnfmId       the identifier of the VNFM
489      * @param vnfId        the identifier of the VNF
490      * @param request      the scale request
491      * @param httpResponse the HTTP response
492      * @return the job for tracking the scale
493      */
494     public JobInfo scaleVnf(String vnfmId, String vnfId, VnfScaleRequest request, HttpServletResponse httpResponse) {
495         logOperationInput(vnfId, SCALE_OPERATION_NAME, request);
496         return scheduleExecution(vnfId, httpResponse, SCALE_OPERATION_NAME, jobInfo -> {
497             ScaleVnfRequest cbamRequest = new ScaleVnfRequest();
498             cbamRequest.setAspectId(request.getAspectId());
499             cbamRequest.setNumberOfSteps(Integer.valueOf(request.getNumberOfSteps()));
500             cbamRequest.setType(convert(request.getType()));
501             com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
502             JsonObject root = new Gson().toJsonTree(jobInfo).getAsJsonObject();
503             com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
504             String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, cbamVnfInfo.getVnfdId());
505             Set<Map.Entry<String, JsonElement>> acceptableOperationParameters = getAcceptableOperationParameters(vnfdContent, "Basic", SCALE_OPERATION_NAME);
506             buildAdditionalParameters(request, root, acceptableOperationParameters);
507             cbamRequest.setAdditionalParams(root);
508             grantManager.requestGrantForScale(vnfmId, vnfId, getVimIdFromInstantiationRequest(vnfmId, vnf), getVnfdIdFromModifyableAttributes(vnf), request, jobInfo.getJobId());
509             OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdScalePost(vnfId, cbamRequest, NOKIA_LCM_API_VERSION).blockingFirst();
510             waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
511         });
512     }
513
514     private void buildAdditionalParameters(VnfScaleRequest request, JsonObject root, Set<Map.Entry<String, JsonElement>> acceptableOperationParameters) {
515         if (request.getAdditionalParam() != null) {
516             for (Map.Entry<String, JsonElement> item : new Gson().toJsonTree(request.getAdditionalParam()).getAsJsonObject().entrySet()) {
517                 if (isParameterAccepted(acceptableOperationParameters, item)) {
518                     root.add(item.getKey(), item.getValue());
519                 }
520             }
521         } else {
522             logger.warn("No additional parameters were passed for scaling");
523         }
524     }
525
526     private boolean isParameterAccepted(Set<Map.Entry<String, JsonElement>> acceptableOperationParameters, Map.Entry<String, JsonElement> item) {
527         boolean found = false;
528         for (Map.Entry<String, JsonElement> acceptableOperationParameter : acceptableOperationParameters) {
529             if (acceptableOperationParameter.getKey().equals(item.getKey())) {
530                 found = true;
531             }
532         }
533         return found;
534     }
535
536     /**
537      * Heal the VNF
538      *
539      * @param vnfmId       the identifier of the VNFM
540      * @param vnfId        the identifier of the VNF
541      * @param request      the heal request
542      * @param httpResponse the HTTP response
543      * @param vnfcId       the identifer of thr VNFC to be healed
544      * @return the job for tracking the heal
545      */
546     public JobInfo healVnf(String vnfmId, String vnfId, VnfHealRequest request, Optional<String> vnfcId, HttpServletResponse httpResponse) {
547         logOperationInput(vnfId, "heal", request);
548         return scheduleExecution(vnfId, httpResponse, "heal", job -> {
549             HealVnfRequest cbamHealRequest = new HealVnfRequest();
550             Map<String, String> additionalParams = new HashMap<>();
551             additionalParams.put("vmName", request.getAffectedvm().getVmname());
552             additionalParams.put("action", request.getAction());
553             additionalParams.put("jobId", job.getJobId());
554             additionalParams.put("vnfcId", vnfcId.orElse("unknown"));
555             cbamHealRequest.setAdditionalParams(additionalParams);
556             com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
557             String vimId = getVimIdFromInstantiationRequest(vnfmId, vnf);
558             grantManager.requestGrantForHeal(vnfmId, vnfId, vimId, getVnfdIdFromModifyableAttributes(vnf), request, job.getJobId());
559             OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdHealPost(vnfId, cbamHealRequest, NOKIA_LCM_API_VERSION).blockingFirst();
560             waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
561         });
562     }
563
564     private JobInfo scheduleExecution(String vnfId, HttpServletResponse httpResponse, String operation, AsynchronousExecution asynchronExecution) {
565         JobInfo jobInfo = new JobInfo();
566         jobInfo.setJobId(jobManager.spawnJob(vnfId, httpResponse));
567         executorService.submit(() -> {
568             try {
569                 asynchronExecution.execute(jobInfo);
570             } catch (RuntimeException e) {
571                 logger.error("Unable to " + operation + " VNF with " + vnfId + " identifier", e);
572                 jobManager.jobFinished(jobInfo.getJobId());
573                 throw e;
574             } catch (Exception e) {
575                 String msg = "Unable to " + operation + " VNF with " + vnfId + " identifier";
576                 logger.error(msg, e);
577                 //the job can only be signaled to be finished after the error is logged
578                 jobManager.jobFinished(jobInfo.getJobId());
579                 throw new UserVisibleError(msg, e);
580             }
581             jobManager.jobFinished(jobInfo.getJobId());
582         });
583         return jobInfo;
584     }
585
586     private OperationExecution waitForOperationToFinish(String vnfmId, String vnfId, String operationExecutionId) {
587         while (true) {
588             try {
589                 OperationExecution operationExecution = find(cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdOperationExecutionsGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst(), opEx -> operationExecutionId.equals(opEx.getId()));
590                 if (hasOperationFinished(operationExecution)) {
591                     logger.debug("Operation finished with " + operationExecution.getId());
592                     return operationExecution;
593                 }
594             } catch (Exception e) {
595                 //swallow exception and retry
596                 logger.warn("Unable to retrieve operations details", e);
597             }
598             systemFunctions().sleep(OPERATION_STATUS_POLLING_INTERVAL_IN_MS);
599         }
600     }
601
602     private boolean hasOperationFinished(OperationExecution operationExecution) {
603         return newHashSet(FINISHED, OperationStatus.FAILED).contains(operationExecution.getStatus());
604     }
605
606     @FunctionalInterface
607     private interface AsynchronousExecution {
608         void execute(JobInfo job);
609     }
610
611     public static class VnfCreationResult {
612         private final com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo;
613         private final String vnfdId;
614
615         VnfCreationResult(com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo, String vnfdId) {
616             this.vnfInfo = vnfInfo;
617             this.vnfdId = vnfdId;
618         }
619
620         public com.nokia.cbam.lcm.v32.model.VnfInfo getVnfInfo() {
621             return vnfInfo;
622         }
623
624     }
625 }