c6e07e83a9cc5be33a995e092c803ad0f1dd8227
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / so / SoLifecycleManager.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 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.so;
17
18
19 import com.nokia.cbam.lcm.v32.model.*;
20 import com.nokia.cbam.lcm.v32.model.VimInfo;
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.servlet.http.HttpServletResponse;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VimInfoProvider;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.AdditionalParameters;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager;
30 import org.onap.vnfmadapter.so.model.*;
31 import org.onap.vnfmdriver.model.ExtVirtualLinkInfo;
32 import org.onap.vnfmdriver.model.*;
33 import org.onap.vnfmdriver.model.VnfInfo;
34 import org.slf4j.Logger;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 import static java.util.Optional.of;
39
40 import static com.nokia.cbam.lcm.v32.model.VimInfo.VimInfoTypeEnum.*;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.VnfcManager.buildCbamId;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider.NOKIA_LCM_API_VERSION;
43 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getVnfdIdFromModifyableAttributes;
44 import static org.onap.vnfmadapter.so.model.SoJobStatus.*;
45 import static org.slf4j.LoggerFactory.getLogger;
46 import static org.springframework.util.StringUtils.isEmpty;
47
48 /**
49  * Responsible for providing access to AAI APIs.
50  * Handles authentication and mandatory parameters.
51  */
52
53 @Component
54 public class SoLifecycleManager {
55     private static Logger logger = getLogger(SoLifecycleManager.class);
56     private final LifecycleManager lifecycleManager;
57     private final VimInfoProvider vimInfoProvider;
58     private final CbamRestApiProvider cbamRestApiProvider;
59     private final JobManager jobManager;
60
61     @Autowired
62     SoLifecycleManager(LifecycleManager lifecycleManager, VimInfoProvider vimInfoProvider, CbamRestApiProvider cbamRestApiProvider, JobManager jobManager) {
63         this.lifecycleManager = lifecycleManager;
64         this.vimInfoProvider = vimInfoProvider;
65         this.cbamRestApiProvider = cbamRestApiProvider;
66         this.jobManager = jobManager;
67     }
68
69     /**
70      * Creates the VNF in SO terminology
71      *
72      * @param vnfmId  the identifier of the VNFM
73      * @param request the VNF creation request
74      * @return the VNF creation response
75      */
76     public SoVnfCreationResponse create(String vnfmId, SoVnfCreationRequest request) {
77         SoVnfCreationResponse response = new SoVnfCreationResponse();
78         LifecycleManager.VnfCreationResult result = lifecycleManager.create(vnfmId, request.getCsarId(), request.getName(), request.getDescription());
79         response.setVnfId(result.getVnfInfo().getId());
80         return response;
81     }
82
83     /**
84      * Activate the VNF in SO terminology
85      *
86      * @param vnfmId       the identifier of the VNFM
87      * @param vnfId        the identifier of the VNF
88      * @param soRequest    the VNF activation request
89      * @param httpResponse the HTTP response
90      * @return the job handler of the VNF activation
91      */
92     public SoJobHandler activate(String vnfmId, String vnfId, SoVnfActivationRequest soRequest, HttpServletResponse httpResponse) {
93         AdditionalParameters additionalParameters = new AdditionalParameters();
94         additionalParameters.setAdditionalParams(buildAdditionalParameters(soRequest.getAdditionalParams()));
95         String vimId = soRequest.getVimId();
96         org.onap.vnfmdriver.model.VimInfo vimInfo = vimInfoProvider.getVimInfo(vimId);
97         additionalParameters.setVimType(vimTypeHeuristic(vimInfo.getUrl()));
98         processVdus(soRequest, additionalParameters, vimId);
99         if (isEmpty(additionalParameters.getInstantiationLevel())) {
100             additionalParameters.setInstantiationLevel("default");
101         }
102         processNetworks(soRequest, additionalParameters, vimId);
103         processZones(soRequest, additionalParameters, vimId);
104         com.nokia.cbam.lcm.v32.model.VnfInfo cbamVnfInfo = cbamRestApiProvider.getCbamLcmApi(vnfmId).vnfsVnfInstanceIdGet(vnfId, NOKIA_LCM_API_VERSION).blockingFirst();
105         String onapVnfdId = getVnfdIdFromModifyableAttributes(cbamVnfInfo);
106         VnfInfo vnfInfo = lifecycleManager.queryVnf(vnfmId, vnfId);
107         List<ExtVirtualLinkInfo> externalVirtualLinks = new ArrayList<>();
108         VnfInstantiateResponse instantiate = lifecycleManager.instantiate(vnfmId, externalVirtualLinks, httpResponse, soRequest.getAdditionalParams(), additionalParameters, vnfId, onapVnfdId, vnfInfo.getVnfdId());
109         return buildJobHandler(instantiate.getJobId());
110     }
111
112     /**
113      * Scale the VNF
114      *
115      * @param vnfmId       the identifier of the VNFM
116      * @param vnfId        the identifier of the VNF
117      * @param soRequest    the VNF scale request
118      * @param httpResponse the HTTP response
119      * @return the job handler of the VNF activation
120      */
121     public SoJobHandler scale(String vnfmId, String vnfId, SoVnfScaleRequest soRequest, HttpServletResponse httpResponse) {
122         org.onap.vnfmdriver.model.VnfScaleRequest driverRequest = new org.onap.vnfmdriver.model.VnfScaleRequest();
123         driverRequest.setAdditionalParam(buildAdditionalParameters(soRequest.getAdditionalParams()));
124         driverRequest.setAspectId(soRequest.getAspectId());
125         driverRequest.setNumberOfSteps(soRequest.getSteps().toString());
126         driverRequest.setType(soRequest.getDirection() == SoScaleDirection.IN ? org.onap.vnfmdriver.model.ScaleDirection.IN : org.onap.vnfmdriver.model.ScaleDirection.OUT);
127         return buildJobHandler(lifecycleManager.scaleVnf(vnfmId, vnfId, driverRequest, httpResponse).getJobId());
128     }
129
130     /**
131      * Heal the VNF
132      *
133      * @param vnfmId       the identifier of the VNFM
134      * @param vnfId        the identifier of the VNF
135      * @param request      the VNF heal request
136      * @param httpResponse the HTTP response
137      * @return the job handler of the VNF activation
138      */
139     public SoJobHandler heal(String vnfmId, String vnfId, SoVnfHealRequest request, HttpServletResponse httpResponse) {
140         org.onap.vnfmdriver.model.VnfHealRequest vnfHealRequest = new org.onap.vnfmdriver.model.VnfHealRequest();
141         VnfHealRequestAffectedvm affectedVm = new VnfHealRequestAffectedvm();
142         affectedVm.setVimid("notUsedByDriver");
143         affectedVm.setVduid("notUsedByDriver");
144         affectedVm.setVmname("unknown");
145         vnfHealRequest.setAffectedvm(affectedVm);
146         vnfHealRequest.setAction("heal");
147         return buildJobHandler(lifecycleManager.healVnf(vnfmId, vnfId, vnfHealRequest, of(buildCbamId(request.getVnfcId())), httpResponse).getJobId());
148     }
149
150     /**
151      * Deactivate the VNF
152      *
153      * @param vnfmId       the identifier of the VNFM
154      * @param vnfId        the identifier of the VNF
155      * @param soRequest    the VNF deactivation request
156      * @param httpResponse the HTTP response
157      * @return the job handler of the VNF activation
158      */
159     public SoJobHandler deactivate(String vnfmId, String vnfId, SoVnfTerminationRequest soRequest, HttpServletResponse httpResponse) {
160         VnfTerminateRequest driverRequest = new VnfTerminateRequest();
161         driverRequest.setTerminationType(soRequest.getMode() == SoTerminationMode.FORCEFUL ? VnfTerminationType.FORCEFUL : VnfTerminationType.GRACEFUL);
162         driverRequest.setGracefulTerminationTimeout(soRequest.getGracefulTerminationTimeoutInMs().toString());
163         return buildJobHandler(lifecycleManager.terminateAndDelete(vnfmId, vnfId, driverRequest, httpResponse).getJobId());
164     }
165
166     /**
167      * Delete the VNF
168      *
169      * @param vnfmId the identifier of the VNFM
170      * @param vnfId  the identifier of the VNF
171      * @return the job handler of the VNF activation
172      */
173     public void delete(String vnfmId, String vnfId) {
174         lifecycleManager.deleteVnf(vnfmId, vnfId);
175     }
176
177     /**
178      * Execute a custom operation on a VNF
179      *
180      * @param vnfmId       the identifier of the VNFM
181      * @param vnfId        the identifier of the VNF
182      * @param request      the VNF custom
183      * @param httpResponse the HTTP response
184      * @return the job handler of the VNF activation
185      */
186     public SoJobHandler customOperation(String vnfmId, String vnfId, SoVnfCustomOperation request, HttpServletResponse httpResponse) {
187         String operationId = request.getOperationId();
188         CustomOperationRequest cbamRequest = new CustomOperationRequest();
189         cbamRequest.setAdditionalParams(buildAdditionalParameters(request.getAdditionalParams()));
190         return buildJobHandler(lifecycleManager.customOperation(vnfmId, vnfId, operationId, request.getAdditionalParams(), httpResponse).getJobId());
191     }
192
193     /**
194      * @param jobId  the identifier of the job
195      * @param vnfmId the identifier of the VNFM
196      * @return the details of the job
197      */
198     public SoJobDetail getJobDetails(String vnfmId, String jobId) {
199         SoJobDetail jobDetail = new SoJobDetail();
200         jobDetail.setJobId(jobId);
201         JobStatus currentStatus = jobManager.getJob(vnfmId, jobId).getResponseDescriptor().getStatus();
202         if (JobStatus.STARTED.equals(currentStatus)) {
203             jobDetail.setStatus(STARTED);
204         } else if (JobStatus.PROCESSING.equals(currentStatus)) {
205             jobDetail.setStatus(STARTED);
206         } else if (JobStatus.FINISHED.equals(currentStatus)) {
207             jobDetail.setStatus(FINISHED);
208         } else if (JobStatus.TIMEOUT.equals(currentStatus)) {
209             jobDetail.setStatus(FAILED);
210         } else {//ERROR
211             jobDetail.setStatus(FAILED);
212         }
213         return jobDetail;
214     }
215
216     private VimInfo.VimInfoTypeEnum vimTypeHeuristic(String url) {
217         if (url.contains("/v3")) {
218             return OPENSTACK_V3_INFO;
219         } else if (url.contains("/v2")) {
220             return OPENSTACK_V2_INFO;
221         } else {
222             return VMWARE_VCLOUD_INFO;
223         }
224     }
225
226     private Object buildAdditionalParameters(Object additionalParams) {
227         return additionalParams;
228     }
229
230     private SoJobHandler buildJobHandler(String jobId) {
231         SoJobHandler jobHandler = new SoJobHandler();
232         jobHandler.setJobId(jobId);
233         return jobHandler;
234     }
235
236     private void processVdus(SoVnfActivationRequest request, AdditionalParameters additionalParameters, String vimId) {
237         if (request.getVduMappings() != null) {
238             for (SoVduMapping vduMapping : request.getVduMappings()) {
239                 VimComputeResourceFlavour flavour = new VimComputeResourceFlavour();
240                 flavour.setVimId(vimId);
241                 flavour.setVnfdVirtualComputeDescId(vduMapping.getVduId());
242                 flavour.setResourceId(vduMapping.getFlavourId());
243                 additionalParameters.getComputeResourceFlavours().add(flavour);
244                 VimSoftwareImage image = new VimSoftwareImage();
245                 image.setVimId(vimId);
246                 image.setResourceId(vduMapping.getImageId());
247                 image.setVnfdSoftwareImageId(vduMapping.getVduId() + CbamUtils.SEPARATOR + "image");
248                 additionalParameters.getSoftwareImages().add(image);
249             }
250         }
251     }
252
253     private void processNetworks(SoVnfActivationRequest request, AdditionalParameters additionalParameters, String vimId) {
254         if (request.getNetworkMappings() != null) {
255             for (SoNetworkMapping networkMapping : request.getNetworkMappings()) {
256                 ExtVirtualLinkData extVirtualLinkData = createExtVirtualLinkData(additionalParameters, networkMapping.getVldId());
257                 extVirtualLinkData.setVimId(vimId);
258                 extVirtualLinkData.setResourceId(networkMapping.getNetworkProviderId());
259                 if (networkMapping.getAssignedAddresses() != null) {
260                     for (SoAssignedAddresses assignedAddresses : networkMapping.getAssignedAddresses()) {
261                         VnfExtCpData extCpData = createExtVirtualLinkData(extVirtualLinkData.getExtCps(), assignedAddresses.getCpdId());
262                         addMissing(extCpData, assignedAddresses.getIpAddress());
263                     }
264                 }
265             }
266         }
267     }
268
269     private void processZones(SoVnfActivationRequest request, AdditionalParameters additionalParameters, String vimId) {
270         if (request.getServerMappings() != null) {
271             for (SoServerMapping serverMapping : request.getServerMappings()) {
272                 ZoneInfo zone = locateOrCreateZone(additionalParameters.getZones(), serverMapping.getVduId());
273                 zone.setResourceId(serverMapping.getAvailabilityZoneId());
274                 zone.setVimId(vimId);
275             }
276         }
277     }
278
279     private ZoneInfo locateOrCreateZone(List<ZoneInfo> zones, String vduId) {
280         if (zones != null) {
281             for (ZoneInfo zone : zones) {
282                 if (zone.getId().equals(vduId)) {
283                     return zone;
284                 }
285             }
286         }
287         ZoneInfo zoneInfo = new ZoneInfo();
288         zoneInfo.setId(vduId);
289         zones.add(zoneInfo);
290         return zoneInfo;
291     }
292
293     private void addMissing(VnfExtCpData extCpData, String ipAddress) {
294         if (extCpData.getAddresses() == null) {
295             extCpData.setAddresses(new ArrayList<>());
296         }
297         for (NetworkAddress networkAddress : extCpData.getAddresses()) {
298             if (ipAddress.equals(networkAddress.getIp())) {
299                 return;
300             }
301         }
302         NetworkAddress address = new NetworkAddress();
303         address.setIp(ipAddress);
304         extCpData.getAddresses().add(address);
305     }
306
307     private VnfExtCpData createExtVirtualLinkData(List<VnfExtCpData> extCps, String cpdId) {
308         if (extCps != null) {
309             for (VnfExtCpData extCp : extCps) {
310                 if (extCp.getCpdId().equals(cpdId)) {
311                     return extCp;
312                 }
313             }
314         }
315         VnfExtCpData extCp = new VnfExtCpData();
316         extCp.setCpdId(cpdId);
317         extCps.add(extCp);
318         return extCp;
319     }
320
321     private ExtVirtualLinkData createExtVirtualLinkData(AdditionalParameters additionalParameters, String virtualLinkId) {
322         ExtVirtualLinkData nonExistingVl = new ExtVirtualLinkData();
323         nonExistingVl.setExtVirtualLinkId(virtualLinkId);
324         additionalParameters.getExtVirtualLinks().add(nonExistingVl);
325         return nonExistingVl;
326     }
327 }