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.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;
38 import static java.util.Optional.of;
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;
49 * Responsible for providing access to AAI APIs.
50 * Handles authentication and mandatory parameters.
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;
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;
70 * Creates the VNF in SO terminology
72 * @param vnfmId the identifier of the VNFM
73 * @param request the VNF creation request
74 * @return the VNF creation response
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());
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 if (isEmpty(additionalParameters.getInstantiationLevel())) {
100 additionalParameters.setInstantiationLevel("default");
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());
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
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());
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
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());
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
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());
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
173 public void delete(String vnfmId, String vnfId) {
174 lifecycleManager.deleteVnf(vnfmId, vnfId);
178 * Execute a custom operation on a VNF
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
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());
194 * @param jobId the identifier of the job
195 * @param vnfmId the identifier of the VNFM
196 * @return the details of the job
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);
211 jobDetail.setStatus(FAILED);
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;
222 return VMWARE_VCLOUD_INFO;
226 private Object buildAdditionalParameters(Object additionalParams) {
227 return additionalParams;
230 private SoJobHandler buildJobHandler(String jobId) {
231 SoJobHandler jobHandler = new SoJobHandler();
232 jobHandler.setJobId(jobId);
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);
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());
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);
279 private ZoneInfo locateOrCreateZone(List<ZoneInfo> zones, String vduId) {
281 for (ZoneInfo zone : zones) {
282 if (zone.getId().equals(vduId)) {
287 ZoneInfo zoneInfo = new ZoneInfo();
288 zoneInfo.setId(vduId);
293 private void addMissing(VnfExtCpData extCpData, String ipAddress) {
294 if (extCpData.getAddresses() == null) {
295 extCpData.setAddresses(new ArrayList<>());
297 for (NetworkAddress networkAddress : extCpData.getAddresses()) {
298 if (ipAddress.equals(networkAddress.getIp())) {
302 NetworkAddress address = new NetworkAddress();
303 address.setIp(ipAddress);
304 extCpData.getAddresses().add(address);
307 private VnfExtCpData createExtVirtualLinkData(List<VnfExtCpData> extCps, String cpdId) {
308 if (extCps != null) {
309 for (VnfExtCpData extCp : extCps) {
310 if (extCp.getCpdId().equals(cpdId)) {
315 VnfExtCpData extCp = new VnfExtCpData();
316 extCp.setCpdId(cpdId);
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;