98b9a22f23432d56f60071677ab379aa08df84dc
[vfc/nfvo/driver/vnfm/svnfm.git] /
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 import java.util.List;
36
37 import static com.google.common.base.Optional.of;
38 import static com.google.common.collect.Iterables.tryFind;
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.ILifecycleChangeNotificationManager.SEPARATOR;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
42 import static org.slf4j.LoggerFactory.getLogger;
43
44 /**
45  * Responsible for sending notifications to VF-C
46  */
47 @Component
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;
53
54     @Autowired
55     VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
56         this.driverProperties = driverProperties;
57         this.vfcRestApiProvider = vfcRestApiProvider;
58     }
59
60     @Override
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(driverProperties.getVnfmId(), recievedNotification.getVnfInstanceId(), operationExecution, recievedNotification.getOperation(), recievedNotification.getAffectedVnfcs()));
65         notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
66         switch (recievedNotification.getStatus()) {
67             case FINISHED:
68             case FAILED:
69                 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
70                 addAffectedVirtualLinks(recievedNotification, notificationToSend);
71                 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
72                 addAffectedCps(vimId, notificationToSend, affectedCps);
73                 break;
74             default:
75                 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
76                 break;
77         }
78         sendNotification(notificationToSend);
79     }
80
81     private void sendNotification(VNFLCMNotification notification) {
82         try {
83             logger.info("Sending LCN: " + new Gson().toJson(notification));
84             vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
85         } catch (Exception e) {
86             fatalFailure(logger, "Unable to send LCN to VF-C", e);
87         }
88     }
89
90     private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
91         AffectedCp onapAffectedCp = new AffectedCp();
92         AffectedCpPortResource port = new AffectedCpPortResource();
93         port.setInstId(affectedCp.getServerProviderId());
94         port.setIpAddress(affectedCp.getIpAddress());
95         port.setMacAddress(affectedCp.getMacAddress());
96         port.setResourceid(affectedCp.getProviderId());
97         port.setResourceName(affectedCp.getName());
98         port.setTenant(affectedCp.getTenantId());
99         port.setVimid(vimId);
100         onapAffectedCp.setPortResource(port);
101         onapAffectedCp.setCpdid(affectedCp.getCpId());
102         onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
103         onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
104         onapAffectedCp.setChangeType(changeType);
105         //owner id & type can be left empty it will default to VNF id on VF-C
106         return onapAffectedCp;
107     }
108
109
110     private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
111         if (request.getAffectedVnfcs() != null) {
112             notificationToSend.setAffectedVnfc(new ArrayList<>());
113             for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
114                 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
115                 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
116                 onapVnfc.setVduId(affectedVnfc.getVduId());
117                 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
118                 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
119                 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
120                 onapVnfc.setVimid(vimId);
121                 notificationToSend.getAffectedVnfc().add(onapVnfc);
122             }
123         }
124     }
125
126     private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
127         if (request.getAffectedVirtualLinks() != null) {
128             notification.setAffectedVl(new ArrayList<>());
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);
139             }
140         }
141     }
142
143     private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
144         Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
145         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.absent() : of(VnfCpNotificationType.CHANGED);
148         }
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(String vnfmId, String vnfId, OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type, List<com.nokia.cbam.lcm.v32.model.AffectedVnfc> affectedVnfcs) {
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                 switch (originalRequest.getType()) {
195                     case IN:
196                         return org.onap.vnfmdriver.model.OperationType.SCALEIN;
197                     default: //OUT
198                         return org.onap.vnfmdriver.model.OperationType.SCALEOUT;
199                 }
200             default:
201                 return org.onap.vnfmdriver.model.OperationType.HEAL;
202         }
203     }
204
205     private String extractServerName(Object additionalData) {
206         return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
207     }
208
209     private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
210         switch (changeType) {
211             case ADDED:
212                 return org.onap.vnfmdriver.model.VnfNotificationType.ADDED;
213             case REMOVED:
214                 return org.onap.vnfmdriver.model.VnfNotificationType.REMOVED;
215             default: //case MODIFIED:
216                 return org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED;
217         }
218     }
219
220 }