2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.lifecycle;
23 import com.google.common.base.Optional;
24 import org.onap.aai.domain.yang.EsrVnfm;
25 import org.onap.aai.domain.yang.GenericVnf;
26 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
27 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
28 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmHelper;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.TerminateVnfRequest.TerminationTypeEnum;
35 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
36 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfNotFoundException;
37 import org.onap.so.adapters.vnfmadapter.rest.exceptions.VnfmNotFoundException;
38 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
39 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
40 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
47 * Manages lifecycle operations towards the VNFMs.
50 public class LifecycleManager {
51 private static final Logger logger = LoggerFactory.getLogger(LifecycleManager.class);
52 private final AaiServiceProvider aaiServiceProvider;
53 private final VnfmServiceProvider vnfmServiceProvider;
54 private final AaiHelper aaiHelper;
55 private final VnfmHelper vnfmHelper;
56 private final JobManager jobManager;
59 LifecycleManager(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper,
60 final VnfmHelper vnfmHelper, final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager) {
61 this.aaiServiceProvider = aaiServiceProvider;
62 this.vnfmServiceProvider = vnfmServiceProvider;
63 this.aaiHelper = aaiHelper;
64 this.vnfmHelper = vnfmHelper;
65 this.jobManager = jobManager;
69 * Create a VNF on a VNFM.
71 * @param vnfIdInAai the ID of the VNF in AAI
72 * @param request the create request
73 * @return the response to the request
75 public CreateVnfResponse createVnf(final String vnfIdInAai, final CreateVnfRequest request) {
76 final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
77 checkIfVnfAlreadyExistsInVnfm(genericVnf);
79 EsrVnfm vnfm = aaiHelper.getAssignedVnfm(genericVnf);
81 vnfm = aaiHelper.selectVnfm(genericVnf);
82 aaiHelper.addRelationshipFromGenericVnfToVnfm(genericVnf, vnfm.getVnfmId());
85 final String vnfIdInVnfm = sendCreateRequestToVnfm(genericVnf);
86 createNotificationSubscription(vnfm.getVnfmId(), vnfIdInVnfm);
87 final String operationId = sendInstantiateRequestToVnfm(vnfm, genericVnf, request, vnfIdInAai, vnfIdInVnfm);
89 final String jobId = jobManager.createJob(vnfm.getVnfmId(), operationId, false);
90 final CreateVnfResponse response = new CreateVnfResponse();
91 response.setJobId(jobId);
95 private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
96 if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
97 Optional<InlineResponse201> response = Optional.absent();
99 response = vnfmServiceProvider.getVnf(genericVnf.getSelflink());
100 } catch (final Exception exception) {
101 logger.debug("Ignoring invalid self link in generic vnf", exception);
103 if (response.isPresent()) {
104 throw new IllegalArgumentException("VNF " + genericVnf.getVnfId()
105 + " is already defined on the VNFM, self link: " + genericVnf.getSelflink());
110 private String sendCreateRequestToVnfm(final GenericVnf genericVnf) {
111 // TODO call create request
112 genericVnf.setSelflink("http://dummy.value/until/create/implememted/vnfId");
116 private void createNotificationSubscription(final String vnfmId, final String vnfId) {
118 final LccnSubscriptionRequest subscriptionRequest = vnfmHelper.createNotificationSubscriptionRequest(vnfId);
119 vnfmServiceProvider.subscribeForNotifications(vnfmId, subscriptionRequest);
120 } catch (final Exception exception) {
121 logger.warn("Subscription for notifications to VNFM: " + vnfmId + " for VNF " + vnfId
122 + " failed. AAI will not be updated unless the VNFM is configured by other means to send notifications relating to this VNF",
127 private String sendInstantiateRequestToVnfm(final EsrVnfm vnfm, final GenericVnf genericVnf,
128 final CreateVnfRequest createVnfRequest, final String vnfIdInAai, final String vnfIdInVnfm) {
130 final InstantiateVnfRequest instantiateVnfRequest =
131 vnfmHelper.createInstantiateRequest(createVnfRequest.getTenant(), createVnfRequest);
132 final String jobId = vnfmServiceProvider.instantiateVnf(genericVnf.getSelflink(), instantiateVnfRequest);
134 logger.info("Instantiate VNF request successfully sent to " + genericVnf.getSelflink());
139 * Delete a VNF on a VNFM.
141 * @param vnfIdInAai the ID of the VNF in AAI
142 * @return the response to the request
144 public DeleteVnfResponse deleteVnf(final String vnfIdInAai) {
145 final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
146 final String vnfmId = getIdOfAssignedVnfm(genericVnf);
148 final String operationId = sendTerminateRequestToVnfm(genericVnf);
149 final String jobId = jobManager.createJob(vnfmId, operationId, true);
151 return new DeleteVnfResponse().jobId(jobId);
154 private String sendTerminateRequestToVnfm(final GenericVnf genericVnf) {
155 final TerminateVnfRequest terminateVnfRequest = new TerminateVnfRequest();
156 terminateVnfRequest.setTerminationType(TerminationTypeEnum.FORCEFUL);
157 return vnfmServiceProvider.terminateVnf(genericVnf.getSelflink(), terminateVnfRequest);
160 private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
161 final GenericVnf genericVnf = aaiServiceProvider.invokeGetGenericVnf(vnfIdInAai);
162 if (genericVnf == null) {
163 throw new VnfNotFoundException("VNF not found in AAI: " + vnfIdInAai);
165 logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
169 private String getIdOfAssignedVnfm(final GenericVnf genericVnf) {
170 final String vnfmId = aaiHelper.getIdOfAssignedVnfm(genericVnf);
171 if (vnfmId == null) {
172 throw new VnfmNotFoundException("No VNFM found in AAI for VNF " + genericVnf.getVnfId());