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