f7667c57adf1390e974edb45b10cc0b8cae40c5d
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / vfc / VfcNotificationSender.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;
17
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;
33
34 import java.util.ArrayList;
35
36 import static com.google.common.base.Optional.of;
37 import static com.google.common.collect.Iterables.tryFind;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
41 import static org.slf4j.LoggerFactory.getLogger;
42
43 /**
44  * Responsible for sending notifications to VF-C
45  */
46 @Component
47 @Conditional(value = Conditions.UseForVfc.class)
48 public class VfcNotificationSender implements INotificationSender {
49     private static Logger logger = getLogger(VfcNotificationSender.class);
50     private final DriverProperties driverProperties;
51     private final VfcRestApiProvider vfcRestApiProvider;
52
53     @Autowired
54     VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
55         this.driverProperties = driverProperties;
56         this.vfcRestApiProvider = vfcRestApiProvider;
57     }
58
59     @Override
60     public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, ReportedAffectedConnectionPoints affectedCps, String vimId) {
61         VNFLCMNotification notificationToSend = new VNFLCMNotification();
62         notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
63         notificationToSend.setOperation(getOperation(operationExecution, recievedNotification.getOperation()));
64         notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
65         switch (recievedNotification.getStatus()) {
66             case FINISHED:
67             case FAILED:
68                 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
69                 addAffectedVirtualLinks(recievedNotification, notificationToSend);
70                 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
71                 addAffectedCps(vimId, notificationToSend, affectedCps);
72                 break;
73             default:
74                 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
75                 break;
76         }
77         sendNotification(notificationToSend);
78     }
79
80     private void sendNotification(VNFLCMNotification notification) {
81         try {
82             if (logger.isInfoEnabled()) {
83                 logger.info("Sending LCN: " + new Gson().toJson(notification));
84             }
85             vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
86         } catch (Exception e) {
87             fatalFailure(logger, "Unable to send LCN to VF-C", e);
88         }
89     }
90
91     private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
92         AffectedCp onapAffectedCp = new AffectedCp();
93         AffectedCpPortResource port = new AffectedCpPortResource();
94         port.setInstId(affectedCp.getServerProviderId());
95         port.setIpAddress(affectedCp.getIpAddress());
96         port.setMacAddress(affectedCp.getMacAddress());
97         port.setResourceid(affectedCp.getProviderId());
98         port.setResourceName(affectedCp.getName());
99         port.setTenant(affectedCp.getTenantId());
100         port.setVimid(vimId);
101         onapAffectedCp.setPortResource(port);
102         onapAffectedCp.setCpdid(affectedCp.getCpId());
103         onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
104         onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
105         onapAffectedCp.setChangeType(changeType);
106         //owner id & type can be left empty it will default to VNF id on VF-C
107         return onapAffectedCp;
108     }
109
110
111     private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
112         if (request.getAffectedVnfcs() != null) {
113             notificationToSend.setAffectedVnfc(new ArrayList<>());
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);
123             }
124         }
125     }
126
127     private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
128         if (request.getAffectedVirtualLinks() != null) {
129             notification.setAffectedVl(new ArrayList<>());
130             for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
131                 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
132                 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
133                 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
134                 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
135                 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
136                 onapVirtualLink.setNetworkResource(networkResource);
137                 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
138                 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
139                 notification.getAffectedVl().add(onapVirtualLink);
140             }
141         }
142     }
143
144     private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
145         Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
146         Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
147         if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
148             return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.absent() : of(VnfCpNotificationType.CHANGED);
149         } else {
150             //the affected CP must be present in the pre or post
151             return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
152         }
153     }
154
155     private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedConnectionPoints affectedCps) {
156         if (affectedCps != null) {
157             notificationToSend.setAffectedCp(new ArrayList<>());
158             for (ReportedAffectedCp pre : affectedCps.getPre()) {
159                 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps, pre);
160                 if (of(VnfCpNotificationType.REMOVED).equals(changeType)) {
161                     addModifiedCp(vimId, notificationToSend, pre, changeType);
162                 }
163             }
164             for (ReportedAffectedCp post : affectedCps.getPost()) {
165                 Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps, post);
166                 if (of(VnfCpNotificationType.ADDED).equals(changeType) || of(VnfCpNotificationType.CHANGED).equals(changeType)) {
167                     addModifiedCp(vimId, notificationToSend, post, changeType);
168                 }
169             }
170         }
171     }
172
173     private void addModifiedCp(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedCp post, Optional<VnfCpNotificationType> changeType) {
174         if (post.getCpdId() != null) {
175             AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
176             onapAffectedCp.setCpdid(post.getCpdId());
177             notificationToSend.getAffectedCp().add(onapAffectedCp);
178         }
179         if (post.getEcpdId() != null) {
180             AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
181             onapAffectedCp.setCpdid(post.getEcpdId());
182             notificationToSend.getAffectedCp().add(onapAffectedCp);
183         }
184     }
185
186     private org.onap.vnfmdriver.model.OperationType getOperation(OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type) {
187         switch (type) {
188             case TERMINATE:
189                 return org.onap.vnfmdriver.model.OperationType.TERMINAL;
190             case INSTANTIATE:
191                 return org.onap.vnfmdriver.model.OperationType.INSTANTIATE;
192             case SCALE:
193                 ScaleVnfRequest originalRequest = new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class);
194                 if (originalRequest.getType() == com.nokia.cbam.lcm.v32.model.ScaleDirection.IN) {
195                     return OperationType.SCALEIN;
196                 } else {
197                     return OperationType.SCALEOUT;
198                 }
199             default:
200                 return org.onap.vnfmdriver.model.OperationType.HEAL;
201         }
202     }
203
204     private String extractServerName(Object additionalData) {
205         return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
206     }
207
208     private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
209         switch (changeType) {
210             case ADDED:
211                 return org.onap.vnfmdriver.model.VnfNotificationType.ADDED;
212             case REMOVED:
213                 return org.onap.vnfmdriver.model.VnfNotificationType.REMOVED;
214             default: //case MODIFIED:
215                 return org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED;
216         }
217     }
218
219 }