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 java.util.ArrayList;
23 import java.util.Optional;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
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.stereotype.Component;
33 import static java.util.Optional.of;
35 import static com.google.common.collect.Iterables.tryFind;
36 import static com.nokia.cbam.lcm.v32.model.ScaleDirection.IN;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
40 import static org.slf4j.LoggerFactory.getLogger;
43 * Responsible for sending notifications to VF-C
46 public class VfcNotificationSender implements INotificationSender {
47 private static Logger logger = getLogger(VfcNotificationSender.class);
48 private final VfcRestApiProvider vfcRestApiProvider;
51 VfcNotificationSender(VfcRestApiProvider vfcRestApiProvider) {
52 this.vfcRestApiProvider = vfcRestApiProvider;
56 public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, Optional<ReportedAffectedConnectionPoints> affectedCps, String vimId, String vnfmId) {
57 VNFLCMNotification notificationToSend = buildNotification();
58 notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
59 notificationToSend.setOperation(getOperation(operationExecution, recievedNotification.getOperation()));
60 notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
61 if (LifecycleChangeNotificationManager.isTerminal(recievedNotification.getStatus())) {
62 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
63 addAffectedVirtualLinks(recievedNotification, notificationToSend);
64 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
65 addAffectedCps(vimId, notificationToSend, affectedCps);
67 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
69 sendNotification(vnfmId, notificationToSend);
72 private VNFLCMNotification buildNotification() {
73 VNFLCMNotification notificationToSend = new VNFLCMNotification();
74 notificationToSend.setAffectedVirtualStorage(new ArrayList<>());
75 notificationToSend.setAffectedVnfc(new ArrayList<>());
76 notificationToSend.setAffectedVl(new ArrayList<>());
77 notificationToSend.setAffectedCp(new ArrayList<>());
78 return notificationToSend;
81 private void sendNotification(String vnfmId, VNFLCMNotification notification) {
83 if (logger.isInfoEnabled()) {
84 logger.info("Sending LCN: {}", new Gson().toJson(notification));
86 vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(vnfmId, notification.getVnfInstanceId(), notification).blockingFirst(null);
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 for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
115 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
116 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
117 onapVnfc.setVduId(affectedVnfc.getVduId());
118 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
119 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
120 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
121 onapVnfc.setVimid(vimId);
122 notificationToSend.getAffectedVnfc().add(onapVnfc);
127 private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
128 if (request.getAffectedVirtualLinks() != null) {
129 for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
130 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
131 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
132 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
133 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
134 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
135 onapVirtualLink.setNetworkResource(networkResource);
136 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
137 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
138 notification.getAffectedVl().add(onapVirtualLink);
143 private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
144 com.google.common.base.Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
145 com.google.common.base.Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
146 if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
147 return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.empty() : of(VnfCpNotificationType.CHANGED);
149 //the affected CP must be present in the pre or post
150 return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
154 private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, Optional<ReportedAffectedConnectionPoints> affectedCps) {
155 if (affectedCps.isPresent()) {
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;