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.LifecycleChangeNotificationManager;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
28 import org.onap.vnfmdriver.model.*;
29 import org.slf4j.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.Conditional;
32 import org.springframework.stereotype.Component;
34 import java.util.ArrayList;
35 import java.util.Optional;
37 import static com.google.common.collect.Iterables.tryFind;
38 import static com.nokia.cbam.lcm.v32.model.ScaleDirection.IN;
39 import static java.util.Optional.of;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
43 import static org.slf4j.LoggerFactory.getLogger;
46 * Responsible for sending notifications to VF-C
49 @Conditional(value = Conditions.UseForVfc.class)
50 public class VfcNotificationSender implements INotificationSender {
51 private static Logger logger = getLogger(VfcNotificationSender.class);
52 private final DriverProperties driverProperties;
53 private final VfcRestApiProvider vfcRestApiProvider;
56 VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
57 this.driverProperties = driverProperties;
58 this.vfcRestApiProvider = vfcRestApiProvider;
62 public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, Optional<ReportedAffectedConnectionPoints> affectedCps, String vimId) {
63 VNFLCMNotification notificationToSend = new VNFLCMNotification();
64 notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
65 notificationToSend.setOperation(getOperation(operationExecution, recievedNotification.getOperation()));
66 notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
67 if (LifecycleChangeNotificationManager.isTerminal(recievedNotification.getStatus())) {
68 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
69 addAffectedVirtualLinks(recievedNotification, notificationToSend);
70 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
71 addAffectedCps(vimId, notificationToSend, affectedCps);
73 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
75 sendNotification(notificationToSend);
78 private void sendNotification(VNFLCMNotification notification) {
80 if (logger.isInfoEnabled()) {
81 logger.info("Sending LCN: {}", new Gson().toJson(notification));
83 vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
84 } catch (Exception e) {
85 throw buildFatalFailure(logger, "Unable to send LCN to VF-C", e);
89 private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
90 AffectedCp onapAffectedCp = new AffectedCp();
91 AffectedCpPortResource port = new AffectedCpPortResource();
92 port.setInstId(affectedCp.getServerProviderId());
93 port.setIpAddress(affectedCp.getIpAddress());
94 port.setMacAddress(affectedCp.getMacAddress());
95 port.setResourceid(affectedCp.getProviderId());
96 port.setResourceName(affectedCp.getName());
97 port.setTenant(affectedCp.getTenantId());
99 onapAffectedCp.setPortResource(port);
100 onapAffectedCp.setCpdid(affectedCp.getCpId());
101 onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
102 onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
103 onapAffectedCp.setChangeType(changeType);
104 //owner id & type can be left empty it will default to VNF id on VF-C
105 return onapAffectedCp;
109 private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
110 if (request.getAffectedVnfcs() != null) {
111 notificationToSend.setAffectedVnfc(new ArrayList<>());
112 for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
113 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
114 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
115 onapVnfc.setVduId(affectedVnfc.getVduId());
116 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
117 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
118 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
119 onapVnfc.setVimid(vimId);
120 notificationToSend.getAffectedVnfc().add(onapVnfc);
125 private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
126 if (request.getAffectedVirtualLinks() != null) {
127 notification.setAffectedVl(new ArrayList<>());
128 for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
129 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
130 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
131 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
132 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
133 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
134 onapVirtualLink.setNetworkResource(networkResource);
135 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
136 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
137 notification.getAffectedVl().add(onapVirtualLink);
142 private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
143 com.google.common.base.Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
144 com.google.common.base.Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
145 if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
146 return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.empty() : of(VnfCpNotificationType.CHANGED);
148 //the affected CP must be present in the pre or post
149 return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
153 private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, Optional<ReportedAffectedConnectionPoints> affectedCps) {
154 if (affectedCps.isPresent()) {
155 notificationToSend.setAffectedCp(new ArrayList<>());
156 for (ReportedAffectedCp pre : affectedCps.get().getPre()) {
157 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), pre);
158 if (of(VnfCpNotificationType.REMOVED).equals(changeType)) {
159 addModifiedCp(vimId, notificationToSend, pre, changeType);
162 for (ReportedAffectedCp post : affectedCps.get().getPost()) {
163 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), post);
164 if (of(VnfCpNotificationType.ADDED).equals(changeType)) {
165 addModifiedCp(vimId, notificationToSend, post, changeType);
167 if (of(VnfCpNotificationType.CHANGED).equals(changeType)) {
168 addModifiedCp(vimId, notificationToSend, post, changeType);
174 private void addModifiedCp(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedCp post, Optional<VnfCpNotificationType> changeType) {
175 if (post.getCpdId() != null) {
176 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
177 onapAffectedCp.setCpdid(post.getCpdId());
178 notificationToSend.getAffectedCp().add(onapAffectedCp);
180 if (post.getEcpdId() != null) {
181 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
182 onapAffectedCp.setCpdid(post.getEcpdId());
183 notificationToSend.getAffectedCp().add(onapAffectedCp);
187 private org.onap.vnfmdriver.model.OperationType getOperation(OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type) {
188 if (type == com.nokia.cbam.lcm.v32.model.OperationType.TERMINATE) {
189 return OperationType.TERMINAL;
190 } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE) {
191 return OperationType.INSTANTIATE;
192 } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.SCALE) {
193 if (IN == new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class).getType()) {
194 return OperationType.SCALEIN;
196 return OperationType.SCALEOUT;
199 return OperationType.HEAL;
203 private String extractServerName(Object additionalData) {
204 return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
207 private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
208 if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.ADDED) {
209 return VnfNotificationType.ADDED;
210 } else if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.REMOVED) {
211 return VnfNotificationType.REMOVED;
213 return VnfNotificationType.MODIFIED;