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.common.base.Optional;
19 import com.google.gson.Gson;
20 import com.nokia.cbam.lcm.v32.model.OperationExecution;
21 import com.nokia.cbam.lcm.v32.model.ScaleVnfRequest;
22 import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
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;
36 import static com.google.common.base.Optional.of;
37 import static com.google.common.collect.Iterables.tryFind;
38 import static com.nokia.cbam.lcm.v32.model.ScaleDirection.IN;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
42 import static org.slf4j.LoggerFactory.getLogger;
45 * Responsible for sending notifications to VF-C
48 @Conditional(value = Conditions.UseForVfc.class)
49 public class VfcNotificationSender implements INotificationSender {
50 private static Logger logger = getLogger(VfcNotificationSender.class);
51 private final DriverProperties driverProperties;
52 private final VfcRestApiProvider vfcRestApiProvider;
55 VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
56 this.driverProperties = driverProperties;
57 this.vfcRestApiProvider = vfcRestApiProvider;
61 public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, ReportedAffectedConnectionPoints affectedCps, String vimId) {
62 VNFLCMNotification notificationToSend = new VNFLCMNotification();
63 notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
64 notificationToSend.setOperation(getOperation(operationExecution, recievedNotification.getOperation()));
65 notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
66 switch (recievedNotification.getStatus()) {
69 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
70 addAffectedVirtualLinks(recievedNotification, notificationToSend);
71 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
72 addAffectedCps(vimId, notificationToSend, affectedCps);
75 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
78 sendNotification(notificationToSend);
81 private void sendNotification(VNFLCMNotification notification) {
83 if (logger.isInfoEnabled()) {
84 logger.info("Sending LCN: " + new Gson().toJson(notification));
86 vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
87 } catch (Exception e) {
88 throw buildFatalFailure(logger, "Unable to send LCN to VF-C", e);
92 private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
93 AffectedCp onapAffectedCp = new AffectedCp();
94 AffectedCpPortResource port = new AffectedCpPortResource();
95 port.setInstId(affectedCp.getServerProviderId());
96 port.setIpAddress(affectedCp.getIpAddress());
97 port.setMacAddress(affectedCp.getMacAddress());
98 port.setResourceid(affectedCp.getProviderId());
99 port.setResourceName(affectedCp.getName());
100 port.setTenant(affectedCp.getTenantId());
101 port.setVimid(vimId);
102 onapAffectedCp.setPortResource(port);
103 onapAffectedCp.setCpdid(affectedCp.getCpId());
104 onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
105 onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
106 onapAffectedCp.setChangeType(changeType);
107 //owner id & type can be left empty it will default to VNF id on VF-C
108 return onapAffectedCp;
112 private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
113 if (request.getAffectedVnfcs() != null) {
114 notificationToSend.setAffectedVnfc(new ArrayList<>());
115 for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
116 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
117 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
118 onapVnfc.setVduId(affectedVnfc.getVduId());
119 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
120 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
121 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
122 onapVnfc.setVimid(vimId);
123 notificationToSend.getAffectedVnfc().add(onapVnfc);
128 private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
129 if (request.getAffectedVirtualLinks() != null) {
130 notification.setAffectedVl(new ArrayList<>());
131 for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
132 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
133 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
134 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
135 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
136 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
137 onapVirtualLink.setNetworkResource(networkResource);
138 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
139 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
140 notification.getAffectedVl().add(onapVirtualLink);
145 private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
146 Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
147 Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
148 if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
149 return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.absent() : of(VnfCpNotificationType.CHANGED);
151 //the affected CP must be present in the pre or post
152 return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
156 private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedConnectionPoints affectedCps) {
157 if (affectedCps != null) {
158 notificationToSend.setAffectedCp(new ArrayList<>());
159 for (ReportedAffectedCp pre : affectedCps.getPre()) {
160 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps, pre);
161 if (of(VnfCpNotificationType.REMOVED).equals(changeType)) {
162 addModifiedCp(vimId, notificationToSend, pre, changeType);
165 for (ReportedAffectedCp post : affectedCps.getPost()) {
166 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps, post);
167 if (of(VnfCpNotificationType.ADDED).equals(changeType) || 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) {
190 return org.onap.vnfmdriver.model.OperationType.TERMINAL;
192 return org.onap.vnfmdriver.model.OperationType.INSTANTIATE;
194 if (IN == new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class).getType()) {
195 return OperationType.SCALEIN;
197 return OperationType.SCALEOUT;
200 return org.onap.vnfmdriver.model.OperationType.HEAL;
204 private String extractServerName(Object additionalData) {
205 return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
208 private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
209 switch (changeType) {
211 return org.onap.vnfmdriver.model.VnfNotificationType.ADDED;
213 return org.onap.vnfmdriver.model.VnfNotificationType.REMOVED;
214 default: //case MODIFIED:
215 return org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED;