7f0f9e3b3713d8790be058c7ae046e83270df106
[so.git] /
1 /*-
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
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
21 package org.onap.so.adapters.vnfmadapter.lifecycle;
22
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;
45
46 /**
47  * Manages lifecycle operations towards the VNFMs.
48  */
49 @Component
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;
57
58     @Autowired
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;
66     }
67
68     /**
69      * Create a VNF on a VNFM.
70      *
71      * @param vnfIdInAai the ID of the VNF in AAI
72      * @param request the create request
73      * @return the response to the request
74      */
75     public CreateVnfResponse createVnf(final String vnfIdInAai, final CreateVnfRequest request) {
76         final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
77         checkIfVnfAlreadyExistsInVnfm(genericVnf);
78
79         EsrVnfm vnfm = aaiHelper.getAssignedVnfm(genericVnf);
80         if (vnfm == null) {
81             vnfm = aaiHelper.selectVnfm(genericVnf);
82             aaiHelper.addRelationshipFromGenericVnfToVnfm(genericVnf, vnfm.getVnfmId());
83         }
84
85         final String vnfIdInVnfm = sendCreateRequestToVnfm(genericVnf);
86         createNotificationSubscription(vnfm.getVnfmId(), vnfIdInVnfm);
87         final String operationId = sendInstantiateRequestToVnfm(vnfm, genericVnf, request, vnfIdInAai, vnfIdInVnfm);
88
89         final String jobId = jobManager.createJob(vnfm.getVnfmId(), operationId, false);
90         final CreateVnfResponse response = new CreateVnfResponse();
91         response.setJobId(jobId);
92         return response;
93     }
94
95     private void checkIfVnfAlreadyExistsInVnfm(final GenericVnf genericVnf) {
96         if (genericVnf.getSelflink() != null && !genericVnf.getSelflink().isEmpty()) {
97             Optional<InlineResponse201> response = Optional.absent();
98             try {
99                 response = vnfmServiceProvider.getVnf(genericVnf.getSelflink());
100             } catch (final Exception exception) {
101                 logger.debug("Ignoring invalid self link in generic vnf", exception);
102             }
103             if (response.isPresent()) {
104                 throw new IllegalArgumentException("VNF " + genericVnf.getVnfId()
105                         + " is already defined on the VNFM, self link: " + genericVnf.getSelflink());
106             }
107         }
108     }
109
110     private String sendCreateRequestToVnfm(final GenericVnf genericVnf) {
111         // TODO call create request
112         genericVnf.setSelflink("http://dummy.value/until/create/implememted/vnfId");
113         return "vnfId";
114     }
115
116     private void createNotificationSubscription(final String vnfmId, final String vnfId) {
117         try {
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",
123                     exception);
124         }
125     }
126
127     private String sendInstantiateRequestToVnfm(final EsrVnfm vnfm, final GenericVnf genericVnf,
128             final CreateVnfRequest createVnfRequest, final String vnfIdInAai, final String vnfIdInVnfm) {
129
130         final InstantiateVnfRequest instantiateVnfRequest =
131                 vnfmHelper.createInstantiateRequest(createVnfRequest.getTenant(), createVnfRequest);
132         final String jobId = vnfmServiceProvider.instantiateVnf(genericVnf.getSelflink(), instantiateVnfRequest);
133
134         logger.info("Instantiate VNF request successfully sent to " + genericVnf.getSelflink());
135         return jobId;
136     }
137
138     /**
139      * Delete a VNF on a VNFM.
140      *
141      * @param vnfIdInAai the ID of the VNF in AAI
142      * @return the response to the request
143      */
144     public DeleteVnfResponse deleteVnf(final String vnfIdInAai) {
145         final GenericVnf genericVnf = getGenericVnfFromAai(vnfIdInAai);
146         final String vnfmId = getIdOfAssignedVnfm(genericVnf);
147
148         final String operationId = sendTerminateRequestToVnfm(genericVnf);
149         final String jobId = jobManager.createJob(vnfmId, operationId, true);
150
151         return new DeleteVnfResponse().jobId(jobId);
152     }
153
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);
158     }
159
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);
164         }
165         logger.debug("Retrieved generic VNF from AAI: " + genericVnf);
166         return genericVnf;
167     }
168
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());
173         }
174         return vnfmId;
175     }
176 }