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 = new VNFLCMNotification();
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 void sendNotification(String vnfmId, VNFLCMNotification notification) {
74 if (logger.isInfoEnabled()) {
75 logger.info("Sending LCN: {}", new Gson().toJson(notification));
77 vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(vnfmId, notification.getVnfInstanceId(), notification);
78 } catch (Exception e) {
79 throw buildFatalFailure(logger, "Unable to send LCN to VF-C", e);
83 private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
84 AffectedCp onapAffectedCp = new AffectedCp();
85 AffectedCpPortResource port = new AffectedCpPortResource();
86 port.setInstId(affectedCp.getServerProviderId());
87 port.setIpAddress(affectedCp.getIpAddress());
88 port.setMacAddress(affectedCp.getMacAddress());
89 port.setResourceid(affectedCp.getProviderId());
90 port.setResourceName(affectedCp.getName());
91 port.setTenant(affectedCp.getTenantId());
93 onapAffectedCp.setPortResource(port);
94 onapAffectedCp.setCpdid(affectedCp.getCpId());
95 onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
96 onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
97 onapAffectedCp.setChangeType(changeType);
98 //owner id & type can be left empty it will default to VNF id on VF-C
99 return onapAffectedCp;
103 private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
104 if (request.getAffectedVnfcs() != null) {
105 notificationToSend.setAffectedVnfc(new ArrayList<>());
106 for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
107 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
108 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
109 onapVnfc.setVduId(affectedVnfc.getVduId());
110 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
111 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
112 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
113 onapVnfc.setVimid(vimId);
114 notificationToSend.getAffectedVnfc().add(onapVnfc);
119 private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
120 if (request.getAffectedVirtualLinks() != null) {
121 notification.setAffectedVl(new ArrayList<>());
122 for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
123 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
124 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
125 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
126 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
127 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
128 onapVirtualLink.setNetworkResource(networkResource);
129 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
130 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
131 notification.getAffectedVl().add(onapVirtualLink);
136 private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
137 com.google.common.base.Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
138 com.google.common.base.Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
139 if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
140 return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.empty() : of(VnfCpNotificationType.CHANGED);
142 //the affected CP must be present in the pre or post
143 return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
147 private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, Optional<ReportedAffectedConnectionPoints> affectedCps) {
148 if (affectedCps.isPresent()) {
149 notificationToSend.setAffectedCp(new ArrayList<>());
150 for (ReportedAffectedCp pre : affectedCps.get().getPre()) {
151 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), pre);
152 if (of(VnfCpNotificationType.REMOVED).equals(changeType)) {
153 addModifiedCp(vimId, notificationToSend, pre, changeType);
156 for (ReportedAffectedCp post : affectedCps.get().getPost()) {
157 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), post);
158 if (of(VnfCpNotificationType.ADDED).equals(changeType)) {
159 addModifiedCp(vimId, notificationToSend, post, changeType);
161 if (of(VnfCpNotificationType.CHANGED).equals(changeType)) {
162 addModifiedCp(vimId, notificationToSend, post, changeType);
168 private void addModifiedCp(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedCp post, Optional<VnfCpNotificationType> changeType) {
169 if (post.getCpdId() != null) {
170 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
171 onapAffectedCp.setCpdid(post.getCpdId());
172 notificationToSend.getAffectedCp().add(onapAffectedCp);
174 if (post.getEcpdId() != null) {
175 AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
176 onapAffectedCp.setCpdid(post.getEcpdId());
177 notificationToSend.getAffectedCp().add(onapAffectedCp);
181 private org.onap.vnfmdriver.model.OperationType getOperation(OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type) {
182 if (type == com.nokia.cbam.lcm.v32.model.OperationType.TERMINATE) {
183 return OperationType.TERMINAL;
184 } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE) {
185 return OperationType.INSTANTIATE;
186 } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.SCALE) {
187 if (IN == new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class).getType()) {
188 return OperationType.SCALEIN;
190 return OperationType.SCALEOUT;
193 return OperationType.HEAL;
197 private String extractServerName(Object additionalData) {
198 return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
201 private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
202 if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.ADDED) {
203 return VnfNotificationType.ADDED;
204 } else if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.REMOVED) {
205 return VnfNotificationType.REMOVED;
207 return VnfNotificationType.MODIFIED;