2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
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;
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.vnfmdriver.model.ExtVirtualLinkInfo;
34 import org.onap.vnfmdriver.model.*;
35 import org.onap.vnfmdriver.model.VimInfo;
36 import org.onap.vnfmdriver.model.VnfInfo;
37 import org.slf4j.Logger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40 import org.yaml.snakeyaml.Yaml;
42 import static java.lang.Integer.parseInt;
43 import static java.nio.charset.StandardCharsets.UTF_8;
45 import static com.google.common.base.Splitter.on;
46 import static com.google.common.collect.Iterables.find;
47 import static com.google.common.collect.Lists.newArrayList;
48 import static com.google.common.collect.Sets.newHashSet;
49 import static com.nokia.cbam.lcm.v32.model.InstantiationState.INSTANTIATED;
50 import static com.nokia.cbam.lcm.v32.model.OperationStatus.FINISHED;
51 import static com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE;
52 import static com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum.*;
53 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.*;
54 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
55 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
56 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager.NEWEST_OPERATIONS_FIRST;
57 import static org.slf4j.LoggerFactory.getLogger;
58 import static org.springframework.util.StringUtils.isEmpty;
61 * Responsible for executing lifecycle operation on the VNF
64 public class LifecycleManager {
65 public static final String ONAP_CSAR_ID = "onapCsarId";
66 public static final long OPERATION_STATUS_POLLING_INTERVAL_IN_MS = 5000L;
68 * The key of the CBAM VNF extension for the identifier of the VNFM in ONAP
70 public static final String EXTERNAL_VNFM_ID = "externalVnfmId";
71 public static final String SCALE_OPERATION_NAME = "scale";
72 private static Logger logger = getLogger(LifecycleManager.class);
73 private final CatalogManager catalogManager;
74 private final IGrantManager grantManager;
75 private final JobManager jobManager;
76 private final ILifecycleChangeNotificationManager notificationManager;
77 private final CbamRestApiProvider cbamRestApiProvider;
78 private final VimInfoProvider vimInfoProvider;
81 * Runs asynchronous operations in the background
83 private ExecutorService executorService = Executors.newCachedThreadPool();
86 LifecycleManager(CatalogManager catalogManager, IGrantManager grantManager, CbamRestApiProvider restApiProvider, VimInfoProvider vimInfoProvider, JobManager jobManager, ILifecycleChangeNotificationManager notificationManager) {
87 this.vimInfoProvider = vimInfoProvider;
88 this.grantManager = grantManager;
89 this.cbamRestApiProvider = restApiProvider;
90 this.jobManager = jobManager;
91 this.notificationManager = notificationManager;
92 this.catalogManager = catalogManager;
96 * @param vimId the VIM identifier
97 * @return the name of the region
99 public static String getRegionName(String vimId) {
100 return newArrayList(on(SEPARATOR).split(vimId)).get(1);
104 * @param vimId the VIM identifier
105 * @return the owner of the cloud
107 public static String getCloudOwner(String vimId) {
108 return newArrayList(on(SEPARATOR).split(vimId)).get(0);
111 private static OperationExecution findLastInstantiation(List<OperationExecution> operationExecutions) {
112 return find(NEWEST_OPERATIONS_FIRST.sortedCopy(operationExecutions), op -> INSTANTIATE.equals(op.getOperationType()));
115 public static String getVnfdIdFromModifyableAttributes(com.nokia.cbam.lcm.v32.model.VnfInfo vnf) {
116 return find(vnf.getExtensions(), p -> p.getName().equals(ONAP_CSAR_ID)).getValue().toString();
120 * Create the VNF. It consists of the following steps
122 * <li>upload the VNF package to CBAM package (if not already there)</li>
123 * <li>create the VNF on CBAM</li>
124 * <li>modify attributes of the VNF (add onapCsarId field)</li>
126 * The rollback of the failed operation is not implemented
128 * <li>delete the VNF if error occurs before instantiation</li>
129 * <li>terminateVnf & delete VNF if error occurs after instantiation</li>
132 * @param vnfmId the identifier of the VNFM
133 * @param csarId the identifier of the VNF package
134 * @param vnfName the name of the VNF
135 * @param description the description of the VNF
136 * @return the VNF creation result
138 public VnfCreationResult create(String vnfmId, String csarId, String vnfName, String description) {
139 logOperationInput("not yet specified", "creation", csarId);
141 CatalogAdapterVnfpackage cbamPackage = catalogManager.preparePackageInCbam(vnfmId, csarId);
142 CreateVnfRequest vnfCreateRequest = new CreateVnfRequest();
143 vnfCreateRequest.setVnfdId(cbamPackage.getVnfdId());
144 vnfCreateRequest.setName(vnfName);
145 vnfCreateRequest.setDescription(description);
146 com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsPost(vnfCreateRequest, NOKIA_LCM_API_VERSION).blockingFirst();
147 addVnfdIdToVnfModifyableAttributeExtensions(vnfmId, vnfInfo.getId(), csarId);
148 return new VnfCreationResult(vnfInfo, cbamPackage.getVnfdId());
149 } catch (Exception e) {
150 throw buildFatalFailure(logger, "Unable to create the VNF", e);
154 private void logOperationInput(String vnfId, String operationName, Object payload) {
155 if (logger.isInfoEnabled()) {
156 logger.info("Starting {} operation on VNF with {} identifier with {} parameter", operationName, vnfId, new Gson().toJson(payload));
161 * Instantiate the VNF
163 * @param vnfmId the identifier of the VNFM
164 * @param externalVirtualLinks the external virtual links of the VNF
165 * @param httpResponse the HTTP response that corresponds to the VNF instantiation request
166 * @param additionalParameters additional parameters
167 * @param vnfId the identifier of the VNF
168 * @param vnfmVnfdId the identifier of the VNF package in CBAM
169 * @param operationAdditionalParameters the additional parameters of the operation
170 * @param onapVnfdId the identifier of the VNFD in the VNFM
171 * @return the instantiation response
173 @SuppressWarnings("squid:S00107") //wrapping them into an object makes the code less readable
174 public VnfInstantiateResponse instantiate(String vnfmId, List<ExtVirtualLinkInfo> externalVirtualLinks, HttpServletResponse httpResponse, Object operationAdditionalParameters, AdditionalParameters additionalParameters, String vnfId, String onapVnfdId, String vnfmVnfdId) {
175 logOperationInput(vnfId, "instantiation", additionalParameters);
176 validateVimType(additionalParameters.getVimType());
177 VnfInstantiateResponse response = new VnfInstantiateResponse();
178 response.setVnfInstanceId(vnfId);
179 String vimId = getVimId(operationAdditionalParameters);
180 JobInfo spawnJob = scheduleExecution(vnfId, httpResponse, "instantiate", jobInfo ->
181 instantiateVnf(vnfmId, externalVirtualLinks, additionalParameters, onapVnfdId, vnfmVnfdId, vnfId, vimId, jobInfo)
183 response.setJobId(spawnJob.getJobId());
188 * Instantiate (VF-C terminology) the VNF. It consists of the following steps
190 * <li>upload the VNF package to CBAM package (if not already there)</li>
191 * <li>create the VNF on CBAM</li>
192 * <li>modify attributes of the VNF (add onapCsarId field)</li>
193 * <li>asynchronously</li>
194 * <li>request grant from VF-C</li>
195 * <li>instantiate VNF on CBAM</li>
196 * <li>return VNF & job id (after create VNF on CBAM)</li>
199 * The rollback of the failed operation is not implemented
201 * <li>delete the VNF if error occurs before instantiation</li>
202 * <li>terminateVnf & delete VNf if error occurs after instantiation</li>
205 * @param vnfmId the identifier of the VNFM
206 * @param request the instantiation request
207 * @param httpResponse the HTTP response
208 * @return the instantiation response
210 public VnfInstantiateResponse createAndInstantiate(String vnfmId, VnfInstantiateRequest request, HttpServletResponse httpResponse) {
211 AdditionalParameters additionalParameters = convertInstantiationAdditionalParams(request.getVnfPackageId(), request.getAdditionalParam());
212 validateVimType(additionalParameters.getVimType());
213 VnfCreationResult creationResult = create(vnfmId, request.getVnfDescriptorId(), request.getVnfInstanceName(), request.getVnfInstanceDescription());
214 return instantiate(vnfmId, request.getExtVirtualLink(), httpResponse, request.getAdditionalParam(), additionalParameters, creationResult.vnfInfo.getId(), request.getVnfPackageId(), creationResult.vnfdId);
217 @SuppressWarnings("squid:S00107") //wrapping them into an object makes the code less readable
218 private void instantiateVnf(String vnfmId, List<ExtVirtualLinkInfo> extVirtualLinkInfos, AdditionalParameters additionalParameters, String onapVnfdId, String vnfmVnfdId, String vnfId, String vimId, JobInfo jobInfo) {
219 String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, vnfmVnfdId);
220 GrantVNFResponseVim vim = grantManager.requestGrantForInstantiate(vnfmId, vnfId, vimId, onapVnfdId, additionalParameters.getInstantiationLevel(), vnfdContent, jobInfo.getJobId());
221 if (vim.getVimId() == null) {
222 throw buildFatalFailure(logger, "VF-C did not send VIM identifier in grant response");
224 VimInfo vimInfo = vimInfoProvider.getVimInfo(vim.getVimId());
225 InstantiateVnfRequest instantiationRequest = new InstantiateVnfRequest();
226 addExternalLinksToRequest(extVirtualLinkInfos, additionalParameters, instantiationRequest, vimId);
227 instantiationRequest.getVims().add(addVim(additionalParameters, vimId, vim, vimInfo));
228 instantiationRequest.setFlavourId(getFlavorId(vnfdContent));
229 instantiationRequest.setComputeResourceFlavours(additionalParameters.getComputeResourceFlavours());
230 instantiationRequest.setGrantlessMode(true);
231 instantiationRequest.setInstantiationLevelId(additionalParameters.getInstantiationLevel());
232 instantiationRequest.setSoftwareImages(additionalParameters.getSoftwareImages());
233 instantiationRequest.setZones(additionalParameters.getZones());
234 instantiationRequest.setExtManagedVirtualLinks(additionalParameters.getExtManagedVirtualLinks());
235 for (ExtVirtualLinkData extVirtualLinkData : additionalParameters.getExtVirtualLinks()) {
236 instantiationRequest.addExtVirtualLinksItem(extVirtualLinkData);
238 JsonObject root = new Gson().toJsonTree(jobInfo).getAsJsonObject();
239 if (additionalParameters.getAdditionalParams() != null) {
240 for (Map.Entry<String, JsonElement> item : new Gson().toJsonTree(additionalParameters.getAdditionalParams()).getAsJsonObject().entrySet()) {
241 root.add(item.getKey(), item.getValue());
244 logger.warn("No additional parameters were specified for the operation");
246 instantiationRequest.setAdditionalParams(root);
247 OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdInstantiatePost(vnfId, instantiationRequest, NOKIA_LCM_API_VERSION).blockingFirst();
248 waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
251 private com.nokia.cbam.lcm.v32.model.VimInfo addVim(AdditionalParameters additionalParameters, String vimId, GrantVNFResponseVim vim, VimInfo vimInfo) {
252 if (additionalParameters.getVimType() == OPENSTACK_V2_INFO) {
253 return buildOpenStackV2INFO(vimId, vim, vimInfo);
255 } else if (additionalParameters.getVimType() == OPENSTACK_V3_INFO) {
256 return buildOpenStackV3INFO(vimId, vim, vimInfo);
259 //OTHER VIM TYPE is not possible
260 return buildVcloudInfo(vimId, vimInfo);
264 private void validateVimType(com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum vimType) {
265 if (com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum.OTHER_VIM_INFO.equals(vimType)) {
266 throw buildFatalFailure(logger, "Only " + OPENSTACK_V2_INFO + ", " + OPENSTACK_V3_INFO + " and " + VMWARE_VCLOUD_INFO + " is the supported VIM types");
270 private String getVimId(Object additionalParams) {
271 return childElement(new Gson().toJsonTree(additionalParams).getAsJsonObject(), "vimId").getAsString();
274 private AdditionalParameters convertInstantiationAdditionalParams(String csarId, Object additionalParams) {
275 JsonObject vnfParameters = child(child(new Gson().toJsonTree(additionalParams).getAsJsonObject(), "inputs"), "vnfs");
276 if (!vnfParameters.has(csarId)) {
277 throw buildFatalFailure(logger, "The additional parameter section does not contain setting for VNF with " + csarId + " CSAR id");
279 JsonElement additionalParamsForVnf = vnfParameters.get(csarId);
280 return new Gson().fromJson(additionalParamsForVnf, AdditionalParameters.class);
283 private String getFlavorId(String vnfdContent) {
284 JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
285 JsonObject capabilities = child(child(child(root, "topology_template"), "substitution_mappings"), "capabilities");
286 JsonObject deploymentFlavorProperties = child(child(capabilities, "deployment_flavour"), "properties");
287 return childElement(deploymentFlavorProperties, "flavour_id").getAsString();
290 private Set<Map.Entry<String, JsonElement>> getAcceptableOperationParameters(String vnfdContent, String categroryOfOperation, String operationName) {
291 JsonObject root = new Gson().toJsonTree(new Yaml().load(vnfdContent)).getAsJsonObject();
292 JsonObject interfaces = child(child(child(root, "topology_template"), "substitution_mappings"), "interfaces");
293 JsonObject additionalParameters = child(child(child(child(interfaces, categroryOfOperation), operationName), "inputs"), "additional_parameters");
294 return additionalParameters.entrySet();
297 private void addExternalLinksToRequest(List<ExtVirtualLinkInfo> extVirtualLinks, AdditionalParameters additionalParameters, InstantiateVnfRequest instantiationRequest, String vimId) {
298 for (ExtVirtualLinkInfo extVirtualLink : extVirtualLinks) {
299 ExtVirtualLinkData cbamExternalVirtualLink = new ExtVirtualLinkData();
300 cbamExternalVirtualLink.setVimId(vimId);
301 cbamExternalVirtualLink.setResourceId(extVirtualLink.getResourceId());
302 VnfExtCpData ecp = new VnfExtCpData();
303 cbamExternalVirtualLink.setExtVirtualLinkId(extVirtualLink.getVlInstanceId());
304 cbamExternalVirtualLink.getExtCps().add(ecp);
305 ecp.setCpdId(extVirtualLink.getCpdId());
306 List<NetworkAddress> addresses = additionalParameters.getExternalConnectionPointAddresses().get(extVirtualLink.getCpdId());
307 ecp.setAddresses(addresses);
308 instantiationRequest.addExtVirtualLinksItem(cbamExternalVirtualLink);
312 private void addVnfdIdToVnfModifyableAttributeExtensions(String vnfmId, String vnfId, String onapCsarId) {
313 ModifyVnfInfoRequest request = new ModifyVnfInfoRequest();
314 VnfProperty onapCsarIdProperty = new VnfProperty();
315 onapCsarIdProperty.setName(ONAP_CSAR_ID);
316 onapCsarIdProperty.setValue(onapCsarId);
317 request.setExtensions(new ArrayList<>());
318 request.getExtensions().add(onapCsarIdProperty);
319 VnfProperty externalVnfmIdProperty = new VnfProperty();
320 externalVnfmIdProperty.setName(EXTERNAL_VNFM_ID);
321 externalVnfmIdProperty.setValue(vnfmId);
322 request.getExtensions().add(externalVnfmIdProperty);
323 request.setVnfConfigurableProperties(null);
325 OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdPatch(vnfId, request, NOKIA_LCM_API_VERSION).blockingFirst();
326 waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
327 } catch (Exception e) {
328 throw buildFatalFailure(logger, "Unable to set the " + ONAP_CSAR_ID + " property on the VNF", e);
332 private OPENSTACKV3INFO buildOpenStackV3INFO(String vimId, GrantVNFResponseVim vim, org.onap.vnfmdriver.model.VimInfo vimInfo) {
333 OPENSTACKV3INFO openstackv3INFO = new OPENSTACKV3INFO();
334 openstackv3INFO.setVimInfoType(OPENSTACK_V3_INFO);
335 OpenStackAccessInfoV3 accessInfov3 = new OpenStackAccessInfoV3();
336 openstackv3INFO.accessInfo(accessInfov3);
337 accessInfov3.setPassword(vimInfo.getPassword());
338 accessInfov3.setDomain(vimInfo.getDomain());
339 accessInfov3.setProject(vim.getAccessInfo().getTenant());
340 accessInfov3.setRegion(getRegionName(vimId));
341 accessInfov3.setUsername(vimInfo.getUserName());
342 openstackv3INFO.setInterfaceInfo(getEndpointInfo(vimInfo));
343 openstackv3INFO.setId(vimId);
344 return openstackv3INFO;
347 private OPENSTACKV2INFO buildOpenStackV2INFO(String vimId, GrantVNFResponseVim vim, org.onap.vnfmdriver.model.VimInfo vimInfo) {
348 OPENSTACKV2INFO openstackv2INFO = new OPENSTACKV2INFO();
349 openstackv2INFO.setVimInfoType(OPENSTACK_V2_INFO);
350 OpenStackAccessInfoV2 accessInfo = new OpenStackAccessInfoV2();
351 openstackv2INFO.setAccessInfo(accessInfo);
352 accessInfo.setPassword(vimInfo.getPassword());
353 accessInfo.setTenant(vim.getAccessInfo().getTenant());
354 accessInfo.setUsername(vimInfo.getUserName());
355 accessInfo.setRegion(getRegionName(vimId));
356 EndpointInfo interfaceEndpoint = getEndpointInfo(vimInfo);
357 openstackv2INFO.setInterfaceInfo(interfaceEndpoint);
358 openstackv2INFO.setId(vimId);
359 return openstackv2INFO;
362 private EndpointInfo getEndpointInfo(VimInfo vimInfo) {
363 EndpointInfo interfaceEndpoint = new EndpointInfo();
364 if (!isEmpty(vimInfo.getSslInsecure())) {
365 interfaceEndpoint.setSkipCertificateHostnameCheck(Boolean.parseBoolean(vimInfo.getSslInsecure()));
366 interfaceEndpoint.setSkipCertificateVerification(Boolean.parseBoolean(vimInfo.getSslInsecure()));
368 interfaceEndpoint.setSkipCertificateHostnameCheck(true);
369 interfaceEndpoint.setSkipCertificateVerification(true);
371 interfaceEndpoint.setEndpoint(vimInfo.getUrl());
372 if (!interfaceEndpoint.isSkipCertificateVerification()) {
373 interfaceEndpoint.setTrustedCertificates(new ArrayList<>());
374 for (String trustedCertificate : StoreLoader.getCertifacates(vimInfo.getSslCacert())) {
375 interfaceEndpoint.getTrustedCertificates().add(trustedCertificate.getBytes(UTF_8));
378 return interfaceEndpoint;
381 private VMWAREVCLOUDINFO buildVcloudInfo(String vimId, org.onap.vnfmdriver.model.VimInfo vimInfo) {
382 VMWAREVCLOUDINFO vcloudInfo = new VMWAREVCLOUDINFO();
383 vcloudInfo.setVimInfoType(VMWARE_VCLOUD_INFO);
384 VCloudAccessInfo accessInfo = new VCloudAccessInfo();
385 vcloudInfo.setAccessInfo(accessInfo);
386 accessInfo.setPassword(vimInfo.getPassword());
387 accessInfo.setUsername(vimInfo.getUserName());
388 accessInfo.setOrganization(getRegionName(vimId));
389 vcloudInfo.setInterfaceInfo(getEndpointInfo(vimInfo));
390 vcloudInfo.setId(vimId);
395 * Terminates and deletes the VNF
397 * <li>fails if the VNF does not exist</li>
398 * <li>terminates if instantiated</li>
399 * <li>deletes the VNF</li>
402 * @param vnfmId the identifier of the VNFM
403 * @param vnfId the identifier of the VNF
404 * @param request the termination request
405 * @param httpResponse the HTTP response
406 * @return the job for polling the progress of the termination
408 public JobInfo terminateAndDelete(String vnfmId, String vnfId, VnfTerminateRequest request, HttpServletResponse httpResponse) {
409 logOperationInput(vnfId, "termination", request);
410 return scheduleExecution(vnfId, httpResponse, "terminateVnf", jobInfo -> {
411 terminateVnf(vnfmId, vnfId, request, jobInfo);
412 deleteVnf(vnfmId, vnfId);
419 * <li>fails if the VNF does not exist</li>
420 * <li>terminates if instantiated</li>
421 * <li>deletes the VNF</li>
424 * @param vnfmId the identifier of the VNFM
425 * @param vnfId the identifier of the VNF
426 * @param request the termination request
427 * @param httpResponse the HTTP response
428 * @return the job for polling the progress of the termination
430 public JobInfo terminate(String vnfmId, String vnfId, VnfTerminateRequest request, HttpServletResponse httpResponse) {
431 logOperationInput(vnfId, "termination", request);
432 return scheduleExecution(vnfId, httpResponse, "terminate", jobInfo -> terminateVnf(vnfmId, vnfId, request, jobInfo));
435 private void terminateVnf(String vnfmId, String vnfId, VnfTerminateRequest request, JobInfo jobInfo) {
436 TerminateVnfRequest cbamRequest = new TerminateVnfRequest();
437 setState(request, cbamRequest);
438 cbamRequest.setAdditionalParams(new Gson().toJsonTree(jobInfo).getAsJsonObject());
439 com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
440 if (vnf.getInstantiationState() == INSTANTIATED) {
441 String vimId = getVimIdFromInstantiationRequest(vnfmId, vnf);
442 grantManager.requestGrantForTerminate(vnfmId, vnfId, vimId, getVnfdIdFromModifyableAttributes(vnf), vnf, jobInfo.getJobId());
443 OperationExecution terminationOperation = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdTerminatePost(vnfId, cbamRequest, NOKIA_LCM_API_VERSION).blockingFirst();
444 OperationExecution finishedOperation = waitForOperationToFinish(vnfmId, vnfId, terminationOperation.getId());
445 if (finishedOperation.getStatus() == FINISHED) {
446 notificationManager.waitForTerminationToBeProcessed(finishedOperation.getId());
448 throw buildFatalFailure(logger, "Unable to terminate VNF the operation did not finish with success");
451 logger.warn("The VNF with {} identifier is not instantiated no termination is required", vnfId);
455 private void setState(VnfTerminateRequest request, TerminateVnfRequest cbamRequest) {
456 if (request.getTerminationType() == null) {
457 cbamRequest.setTerminationType(TerminationType.FORCEFUL);
459 if (request.getTerminationType().equals(VnfTerminationType.GRACEFUL)) {
460 cbamRequest.setTerminationType(TerminationType.GRACEFUL);
461 cbamRequest.setGracefulTerminationTimeout(parseInt(request.getGracefulTerminationTimeout()));
463 cbamRequest.setTerminationType(TerminationType.FORCEFUL);
471 * @param vnfmId the identifier of the VNFM
472 * @param vnfId the identifier fo the VNF
474 public void deleteVnf(String vnfmId, String vnfId) {
475 logger.info("Deleting VNF with {} identifier", vnfId);
476 cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdDelete(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
477 logger.info("The VNF with {} identifier has been deleted", vnfId);
480 private String getVimIdFromInstantiationRequest(String vnfmId, com.nokia.cbam.lcm.v32.model.VnfInfo vnf) {
481 OperationExecution lastInstantiation = findLastInstantiation(vnf.getOperationExecutions());
482 Object operationParameters = cbamRestApiProvider.getCbamOperationExecutionApi(vnfmId).operationExecutionsOperationExecutionIdOperationParamsGet(lastInstantiation.getId(), NOKIA_LCM_API_VERSION).blockingFirst();
483 JsonObject root = new Gson().toJsonTree(operationParameters).getAsJsonObject();
484 return childElement(childElement(root, "vims").getAsJsonArray().get(0).getAsJsonObject(), "id").getAsString();
488 * @param vnfmId the identifier of the VNFM
489 * @param vnfId the identifier of the VNF
490 * @return the current state of the VNF
492 public VnfInfo queryVnf(String vnfmId, String vnfId) {
494 com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
495 return convertVnfInfo(vnfId, cbamVnfInfo);
496 } catch (Exception e) {
497 throw buildFatalFailure(logger, "Unable to query VNF (" + vnfId + ")", e);
501 private VnfInfo convertVnfInfo(String vnfId, com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo) {
502 VnfInfo vnfInfo = new VnfInfo();
503 vnfInfo.setVersion(cbamVnfInfo.getVnfSoftwareVersion());
504 vnfInfo.setVnfInstanceId(vnfId);
505 String onapCsarId = getVnfdIdFromModifyableAttributes(cbamVnfInfo);
506 vnfInfo.setVnfdId(onapCsarId);
507 vnfInfo.setVnfPackageId(onapCsarId);
508 vnfInfo.setVnfInstanceDescription(cbamVnfInfo.getDescription());
509 vnfInfo.setVnfInstanceName(cbamVnfInfo.getName());
510 vnfInfo.setVnfProvider(cbamVnfInfo.getVnfProvider());
511 vnfInfo.setVnfStatus("ACTIVE");
512 vnfInfo.setVnfType("Kuku");
516 private ScaleDirection convert(org.onap.vnfmdriver.model.ScaleDirection direction) {
517 if (org.onap.vnfmdriver.model.ScaleDirection.IN.equals(direction)) {
518 return ScaleDirection.IN;
520 return ScaleDirection.OUT;
527 * @param vnfmId the identifier of the VNFM
528 * @param vnfId the identifier of the VNF
529 * @param request the scale request
530 * @param httpResponse the HTTP response
531 * @return the job for tracking the scale
533 public JobInfo scaleVnf(String vnfmId, String vnfId, VnfScaleRequest request, HttpServletResponse httpResponse) {
534 logOperationInput(vnfId, SCALE_OPERATION_NAME, request);
535 return scheduleExecution(vnfId, httpResponse, SCALE_OPERATION_NAME, jobInfo -> {
536 ScaleVnfRequest cbamRequest = new ScaleVnfRequest();
537 cbamRequest.setAspectId(request.getAspectId());
538 cbamRequest.setNumberOfSteps(Integer.valueOf(request.getNumberOfSteps()));
539 cbamRequest.setType(convert(request.getType()));
540 com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
541 JsonObject root = new Gson().toJsonTree(jobInfo).getAsJsonObject();
542 com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
543 String vnfdContent = catalogManager.getCbamVnfdContent(vnfmId, cbamVnfInfo.getVnfdId());
544 Set<Map.Entry<String, JsonElement>> acceptableOperationParameters = getAcceptableOperationParameters(vnfdContent, "Basic", SCALE_OPERATION_NAME);
545 buildAdditionalParameters(request, root, acceptableOperationParameters);
546 cbamRequest.setAdditionalParams(root);
547 grantManager.requestGrantForScale(vnfmId, vnfId, getVimIdFromInstantiationRequest(vnfmId, vnf), getVnfdIdFromModifyableAttributes(vnf), request, jobInfo.getJobId());
548 OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdScalePost(vnfId, cbamRequest, NOKIA_LCM_API_VERSION).blockingFirst();
549 waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
553 private void buildAdditionalParameters(VnfScaleRequest request, JsonObject root, Set<Map.Entry<String, JsonElement>> acceptableOperationParameters) {
554 if (request.getAdditionalParam() != null) {
555 for (Map.Entry<String, JsonElement> item : new Gson().toJsonTree(request.getAdditionalParam()).getAsJsonObject().entrySet()) {
556 if (isParameterAccepted(acceptableOperationParameters, item)) {
557 root.add(item.getKey(), item.getValue());
561 logger.warn("No additional parameters were passed for scaling");
565 private boolean isParameterAccepted(Set<Map.Entry<String, JsonElement>> acceptableOperationParameters, Map.Entry<String, JsonElement> item) {
566 boolean found = false;
567 for (Map.Entry<String, JsonElement> acceptableOperationParameter : acceptableOperationParameters) {
568 if (acceptableOperationParameter.getKey().equals(item.getKey())) {
578 * @param vnfmId the identifier of the VNFM
579 * @param vnfId the identifier of the VNF
580 * @param request the heal request
581 * @param httpResponse the HTTP response
582 * @param vnfcId the identifer of thr VNFC to be healed
583 * @return the job for tracking the heal
585 public JobInfo healVnf(String vnfmId, String vnfId, VnfHealRequest request, Optional<String> vnfcId, HttpServletResponse httpResponse) {
586 logOperationInput(vnfId, "heal", request);
587 return scheduleExecution(vnfId, httpResponse, "heal", job -> {
588 HealVnfRequest cbamHealRequest = new HealVnfRequest();
589 Map<String, String> additionalParams = new HashMap<>();
590 additionalParams.put("vmName", request.getAffectedvm().getVmname());
591 additionalParams.put("action", request.getAction());
592 additionalParams.put("jobId", job.getJobId());
593 additionalParams.put("vnfcId", vnfcId.orElse("unknown"));
594 cbamHealRequest.setAdditionalParams(additionalParams);
595 com.nokia.cbam.lcm.v32.model.VnfInfo vnf = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
596 String vimId = getVimIdFromInstantiationRequest(vnfmId, vnf);
597 grantManager.requestGrantForHeal(vnfmId, vnfId, vimId, getVnfdIdFromModifyableAttributes(vnf), request, job.getJobId());
598 OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdHealPost(vnfId, cbamHealRequest, NOKIA_LCM_API_VERSION).blockingFirst();
599 waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
603 public JobInfo customOperation(String vnfmId, String vnfId, String operationId, Object additionalParams, HttpServletResponse httpResponse) {
604 logOperationInput(vnfId, "custom", additionalParams);
605 return scheduleExecution(vnfId, httpResponse, "custom", job -> {
606 CustomOperationRequest cbamRequest = new CustomOperationRequest();
607 cbamRequest.setAdditionalParams(additionalParams);
608 OperationExecution operationExecution = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdCustomCustomOperationNamePost(vnfId, operationId, cbamRequest, NOKIA_LCM_API_VERSION).blockingFirst();
609 waitForOperationToFinish(vnfmId, vnfId, operationExecution.getId());
613 private JobInfo scheduleExecution(String vnfId, HttpServletResponse httpResponse, String operation, AsynchronousExecution asynchronExecution) {
614 JobInfo jobInfo = new JobInfo();
615 jobInfo.setJobId(jobManager.spawnJob(vnfId, httpResponse));
616 executorService.submit(() -> {
618 asynchronExecution.execute(jobInfo);
619 } catch (RuntimeException e) {
620 logger.error("Unable to " + operation + " VNF with " + vnfId + " identifier", e);
621 jobManager.jobFinished(jobInfo.getJobId());
624 jobManager.jobFinished(jobInfo.getJobId());
629 private OperationExecution waitForOperationToFinish(String vnfmId, String vnfId, String operationExecutionId) {
632 OperationExecution operationExecution = find(cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdOperationExecutionsGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst(), opEx -> operationExecutionId.equals(opEx.getId()));
633 if (hasOperationFinished(operationExecution)) {
634 logger.debug("Operation finished with " + operationExecution.getId());
635 return operationExecution;
637 } catch (Exception e) {
638 //swallow exception and retry
639 logger.warn("Unable to retrieve operations details", e);
641 systemFunctions().sleep(OPERATION_STATUS_POLLING_INTERVAL_IN_MS);
645 private boolean hasOperationFinished(OperationExecution operationExecution) {
646 return newHashSet(FINISHED, OperationStatus.FAILED).contains(operationExecution.getStatus());
650 private interface AsynchronousExecution {
651 void execute(JobInfo job);
654 public static class VnfCreationResult {
655 private final com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo;
657 private final String vnfdId;
659 public VnfCreationResult(com.nokia.cbam.lcm.v32.model.VnfInfo vnfInfo, String vnfdId) {
660 this.vnfInfo = vnfInfo;
661 this.vnfdId = vnfdId;
664 public com.nokia.cbam.lcm.v32.model.VnfInfo getVnfInfo() {