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.vfc;
18 import com.google.gson.Gson;
19 import com.nokia.cbam.lcm.v32.model.OperationExecution;
20 import com.nokia.cbam.lcm.v32.model.ScaleVnfRequest;
21 import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification;
22 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
27 import org.onap.vnfmdriver.model.*;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Conditional;
31 import org.springframework.stereotype.Component;
33 import java.util.ArrayList;
34 import java.util.List;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager.SEPARATOR;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
39 import static org.slf4j.LoggerFactory.getLogger;
42 * Responsible for sending notifications to VF-C
45 @Conditional(value = Conditions.UseForVfc.class)
46 public class VfcNotificationSender implements INotificationSender {
47 private static Logger logger = getLogger(VfcNotificationSender.class);
48 private final DriverProperties driverProperties;
49 private final VfcRestApiProvider vfcRestApiProvider;
52 VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
53 this.driverProperties = driverProperties;
54 this.vfcRestApiProvider = vfcRestApiProvider;
58 public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, ReportedAffectedConnectionPoints affectedCps, String vimId) {
59 VNFLCMNotification notificationToSend = new VNFLCMNotification();
60 notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
61 notificationToSend.setOperation(getOperation(driverProperties.getVnfmId(), recievedNotification.getVnfInstanceId(), operationExecution, recievedNotification.getOperation(), recievedNotification.getAffectedVnfcs()));
62 notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
63 switch (recievedNotification.getStatus()) {
66 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
67 addAffectedVirtualLinks(recievedNotification, notificationToSend);
68 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
69 addAffectedCps(vimId, notificationToSend, affectedCps);
72 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
75 sendNotification(notificationToSend);
78 private void sendNotification(VNFLCMNotification notification) {
80 logger.info("Sending LCN: " + new Gson().toJson(notification));
81 vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
82 } catch (Exception e) {
83 fatalFailure(logger, "Unable to send LCN to VF-C", e);
87 private AffectedCp buildAffectedCp(String vimId, String vnfId, ReportedAffectedCp affectedCp) {
88 AffectedCp onapAffectedCp = new AffectedCp();
89 AffectedCpPortResource port = new AffectedCpPortResource();
90 port.setInstId(affectedCp.getServerProviderId());
91 port.setIpAddress(affectedCp.getIpAddress());
92 port.setMacAddress(affectedCp.getMacAddress());
93 port.setResourceid(affectedCp.getProviderId());
94 port.setResourceName(affectedCp.getName());
95 port.setTenant(affectedCp.getTenantId());
97 onapAffectedCp.setPortResource(port);
98 onapAffectedCp.setCpdid(affectedCp.getCpId());
99 onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
100 onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
101 onapAffectedCp.setChangeType(transform(affectedCp.getChangeType()));
102 //owner id & type can be left empty it will default to VNF id on VF-C
103 return onapAffectedCp;
106 private VnfCpNotificationType transform(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
107 switch (changeType) {
109 return VnfCpNotificationType.ADDED;
111 return VnfCpNotificationType.REMOVED;
112 default: //can only be MODIFIED
113 return VnfCpNotificationType.CHANGED;
117 private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
118 if (request.getAffectedVnfcs() != null) {
119 notificationToSend.setAffectedVnfc(new ArrayList<>());
120 for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
121 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
122 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
123 onapVnfc.setVduId(affectedVnfc.getVduId());
124 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
125 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
126 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
127 onapVnfc.setVimid(vimId);
128 notificationToSend.getAffectedVnfc().add(onapVnfc);
133 private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
134 if (request.getAffectedVirtualLinks() != null) {
135 notification.setAffectedVl(new ArrayList<>());
136 for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
137 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
138 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
139 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
140 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
141 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
142 onapVirtualLink.setNetworkResource(networkResource);
143 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
144 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
145 notification.getAffectedVl().add(onapVirtualLink);
150 private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedConnectionPoints affectedCps) {
151 if (affectedCps != null) {
152 notificationToSend.setAffectedCp(new ArrayList<>());
153 for (ReportedAffectedCp affectedCp : affectedCps.getPost()) {
154 if (affectedCp.getCpdId() != null) {
155 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), affectedCp);
156 onapAffectedCp.setCpdid(affectedCp.getCpdId());
157 notificationToSend.getAffectedCp().add(onapAffectedCp);
159 if (affectedCp.getEcpdId() != null) {
160 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), affectedCp);
161 onapAffectedCp.setCpdid(affectedCp.getEcpdId());
162 notificationToSend.getAffectedCp().add(onapAffectedCp);
168 private org.onap.vnfmdriver.model.OperationType getOperation(String vnfmId, String vnfId, OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type, List<com.nokia.cbam.lcm.v32.model.AffectedVnfc> affectedVnfcs) {
171 return org.onap.vnfmdriver.model.OperationType.TERMINAL;
173 return org.onap.vnfmdriver.model.OperationType.INSTANTIATE;
175 ScaleVnfRequest originalRequest = new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class);
176 switch (originalRequest.getType()) {
178 return org.onap.vnfmdriver.model.OperationType.SCALEIN;
180 return org.onap.vnfmdriver.model.OperationType.SCALEOUT;
183 return org.onap.vnfmdriver.model.OperationType.HEAL;
187 private String extractServerName(Object additionalData) {
188 return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
191 private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
192 switch (changeType) {
194 return org.onap.vnfmdriver.model.VnfNotificationType.ADDED;
196 return org.onap.vnfmdriver.model.VnfNotificationType.REMOVED;
197 default: //case MODIFIED:
198 return org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED;