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 java.util.UUID;
25 import org.onap.aai.domain.yang.EsrVnfm;
26 import org.onap.aai.domain.yang.GenericVnf;
27 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
28 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
29 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmHelper;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InstantiateVnfRequest;
33 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
34 import org.onap.vnfmadapter.v1.model.CreateVnfRequest;
35 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
36 import org.onap.vnfmadapter.v1.model.DeleteVnfResponse;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
43 * Manages lifecycle operations towards the VNFMs.
46 public class LifecycleManager {
47 private static final Logger logger = LoggerFactory.getLogger(LifecycleManager.class);
48 private final AaiServiceProvider aaiServiceProvider;
49 private final VnfmServiceProvider vnfmServiceProvider;
50 private final AaiHelper aaiHelper;
51 private final VnfmHelper vnfmHelper;
52 private final JobManager jobManager;
55 LifecycleManager(final AaiServiceProvider aaiServiceProvider, final AaiHelper aaiHelper,
56 final VnfmHelper vnfmHelper, final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager) {
57 this.aaiServiceProvider = aaiServiceProvider;
58 this.vnfmServiceProvider = vnfmServiceProvider;
59 this.aaiHelper = aaiHelper;
60 this.vnfmHelper = vnfmHelper;
61 this.jobManager = jobManager;
65 * Create a VNF on a VNFM.
67 * @param vnfIdInAai the ID of the VNF in AAI
68 * @param request the create request
69 * @return the response to the request
71 public CreateVnfResponse createVnf(final String vnfIdInAai, final CreateVnfRequest request) {
72 final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
73 checkIfVnfAlreadyExistsInVnfm(genericVnf);
75 EsrVnfm vnfm = aaiHelper.getAssignedVnfm(genericVnf);
77 vnfm = aaiHelper.selectVnfm(genericVnf);
78 aaiHelper.addRelationshipFromGenericVnfToVnfm(genericVnf, vnfm.getVnfmId());
81 final String vnfIdInVnfm = sendCreateRequestToVnfm(genericVnf);
82 final String operationId = sendInstantiateRequestToVnfm(vnfm, genericVnf, request, vnfIdInAai, vnfIdInVnfm);
84 final String jobId = jobManager.createJob(vnfm.getVnfmId(), operationId, false);
85 final CreateVnfResponse response = new CreateVnfResponse();
86 response.setJobId(jobId);
90 private GenericVnf getGenericVnfFromAai(final String vnfIdInAai) {
91 final GenericVnf genericVnf = aaiServiceProvider.invokeGetGenericVnf(vnfIdInAai);
92 logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
96 private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
97 if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
98 Optional<InlineResponse201> response = Optional.absent();
100 response = vnfmServiceProvider.getVnf(genericVnf.getSelflink());
101 } catch (final Exception exception) {
102 logger.debug("Ignoring invalid self link in generic vnf", exception);
104 if (response.isPresent()) {
105 throw new IllegalArgumentException("VNF " + genericVnf.getVnfId()
106 + " is already defined on the VNFM, self link: " + genericVnf.getSelflink());
111 private String sendCreateRequestToVnfm(final GenericVnf genericVnf) {
112 // TODO call create request
113 genericVnf.setSelflink("http://dummy.value/until/create/implememted/vnfId");
117 private String sendInstantiateRequestToVnfm(final EsrVnfm vnfm, final GenericVnf genericVnf,
118 final CreateVnfRequest createVnfRequest, final String vnfIdInAai, final String vnfIdInVnfm) {
120 final InstantiateVnfRequest instantiateVnfRequest =
121 vnfmHelper.createInstantiateRequest(createVnfRequest.getTenant(), createVnfRequest);
122 final String jobId = vnfmServiceProvider.instantiateVnf(genericVnf.getSelflink(), instantiateVnfRequest);
124 logger.info("Instantiate VNF request successfully sent to " + genericVnf.getSelflink());
129 * Delete a VNF on a VNFM.
131 * @param vnfIdInAai the ID of the VNF in AAI
132 * @return the response to the request
134 public DeleteVnfResponse deleteVnf(final String vnfIdInAai) {
135 // vnfm ID and operation ID set to random value for now, will be set correctly once we implement
136 // terminate call towards the VNFM
137 final String jobId = jobManager.createJob(UUID.randomUUID().toString(), UUID.randomUUID().toString(), true);
138 final DeleteVnfResponse response = new DeleteVnfResponse();
139 response.setJobId(jobId);