2 * Copyright 2016-2017, Nokia Corporation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.so;
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;
36 import static java.util.Optional.of;
37 import static java.util.Optional.ofNullable;
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.*;
46 * Responsible for providing access to AAI APIs.
47 * Handles authentication and mandatory parameters.
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;
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;
69 * Creates the VNF in SO terminology
71 * @param vnfmId the identifier of the VNFM
72 * @param request the VNF creation request
73 * @return the VNF creation response
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()));
84 * Activate the VNF in SO terminology
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
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());
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
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());
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
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());
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
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);
162 driverRequest.setTerminationType(VnfTerminationType.GRACEFUL);
163 driverRequest.setGracefulTerminationTimeout(soRequest.getGracefulTerminationTimeoutInMs().toString());
166 return buildJobHandler(lifecycleManager.terminateAndDelete(vnfmId, vnfId, driverRequest, httpResponse).getJobId());
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
176 public void delete(String vnfmId, String vnfId) {
177 lifecycleManager.deleteVnf(vnfmId, vnfId);
181 * Execute a custom operation on a VNF
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
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());
197 * @param jobId the identifier of the job
198 * @param vnfmId the identifier of the VNFM
199 * @return the details of the job
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);
214 jobDetail.setStatus(FAILED);
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;
225 return VMWARE_VCLOUD_INFO;
229 private Object buildAdditionalParameters(Object additionalParams) {
230 return additionalParams;
233 private SoJobHandler buildJobHandler(String jobId) {
234 SoJobHandler jobHandler = new SoJobHandler();
235 jobHandler.setJobId(jobId);
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);
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);
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());
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);
286 private ZoneInfo locateOrCreateZone(List<ZoneInfo> zones, String vduId) {
287 for (ZoneInfo zone : zones) {
288 if (zone.getId().equals(vduId)) {
292 ZoneInfo zoneInfo = new ZoneInfo();
293 zoneInfo.setId(vduId);
298 private void addMissing(VnfExtCpData extCpData, String ipAddress) {
299 if (extCpData.getAddresses() == null) {
300 extCpData.setAddresses(new ArrayList<>());
302 for (NetworkAddress networkAddress : extCpData.getAddresses()) {
303 if (ipAddress.equals(networkAddress.getIp())) {
307 NetworkAddress address = new NetworkAddress();
308 address.setIp(ipAddress);
309 extCpData.getAddresses().add(address);
312 private VnfExtCpData createExtVirtualLinkData(List<VnfExtCpData> extCps, String cpdId) {
313 for (VnfExtCpData extCp : extCps) {
314 if (extCp.getCpdId().equals(cpdId)) {
318 VnfExtCpData extCp = new VnfExtCpData();
319 extCp.setCpdId(cpdId);
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;