26439b5cd801b34f2d369b3331e0e42779824e8a
[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.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 org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
27 import org.onap.vnfmdriver.model.*;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Conditional;
31 import org.springframework.stereotype.Component;
32
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.fatalFailure;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.ILifecycleChangeNotificationManager.SEPARATOR;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
39 import static org.slf4j.LoggerFactory.getLogger;
40
41 /**
42  * Responsible for sending notifications to VF-C
43  */
44 @Component
45 @Conditional(value = Conditions.UseForVfc.class)
46 public class VfcNotificationSender implements INotificationSender {
47     private static Logger logger = getLogger(VfcNotificationSender.class);
48     private final DriverProperties driverProperties;
49     private final VfcRestApiProvider vfcRestApiProvider;
50
51     @Autowired
52     VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
53         this.driverProperties = driverProperties;
54         this.vfcRestApiProvider = vfcRestApiProvider;
55     }
56
57     @Override
58     public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, ReportedAffectedConnectionPoints affectedCps, String vimId) {
59         VNFLCMNotification notificationToSend = new VNFLCMNotification();
60         notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
61         notificationToSend.setOperation(getOperation(driverProperties.getVnfmId(), recievedNotification.getVnfInstanceId(), operationExecution, recievedNotification.getOperation(), recievedNotification.getAffectedVnfcs()));
62         notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
63         switch (recievedNotification.getStatus()) {
64             case FINISHED:
65             case FAILED:
66                 notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
67                 addAffectedVirtualLinks(recievedNotification, notificationToSend);
68                 addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
69                 addAffectedCps(vimId, notificationToSend, affectedCps);
70                 break;
71             default:
72                 notificationToSend.setStatus(VnfLcmNotificationStatus.START);
73                 break;
74         }
75         sendNotification(notificationToSend);
76     }
77
78     private void sendNotification(VNFLCMNotification notification) {
79         try {
80             logger.info("Sending LCN: " + new Gson().toJson(notification));
81             vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
82         } catch (Exception e) {
83             fatalFailure(logger, "Unable to send LCN to VF-C", e);
84         }
85     }
86
87     private AffectedCp buildAffectedCp(String vimId, String vnfId, ReportedAffectedCp affectedCp) {
88         AffectedCp onapAffectedCp = new AffectedCp();
89         AffectedCpPortResource port = new AffectedCpPortResource();
90         port.setInstId(affectedCp.getServerProviderId());
91         port.setIpAddress(affectedCp.getIpAddress());
92         port.setMacAddress(affectedCp.getMacAddress());
93         port.setResourceid(affectedCp.getProviderId());
94         port.setResourceName(affectedCp.getName());
95         port.setTenant(affectedCp.getTenantId());
96         port.setVimid(vimId);
97         onapAffectedCp.setPortResource(port);
98         onapAffectedCp.setCpdid(affectedCp.getCpId());
99         onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
100         onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
101         onapAffectedCp.setChangeType(transform(affectedCp.getChangeType()));
102         //owner id & type can be left empty it will default to VNF id on VF-C
103         return onapAffectedCp;
104     }
105
106     private VnfCpNotificationType transform(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
107         switch (changeType) {
108             case ADDED:
109                 return VnfCpNotificationType.ADDED;
110             case REMOVED:
111                 return VnfCpNotificationType.REMOVED;
112             default: //can only be MODIFIED
113                 return VnfCpNotificationType.CHANGED;
114         }
115     }
116
117     private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
118         if (request.getAffectedVnfcs() != null) {
119             notificationToSend.setAffectedVnfc(new ArrayList<>());
120             for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
121                 org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
122                 onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
123                 onapVnfc.setVduId(affectedVnfc.getVduId());
124                 onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
125                 onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
126                 onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
127                 onapVnfc.setVimid(vimId);
128                 notificationToSend.getAffectedVnfc().add(onapVnfc);
129             }
130         }
131     }
132
133     private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
134         if (request.getAffectedVirtualLinks() != null) {
135             notification.setAffectedVl(new ArrayList<>());
136             for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
137                 org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
138                 onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
139                 onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
140                 onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
141                 AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
142                 onapVirtualLink.setNetworkResource(networkResource);
143                 networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
144                 networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
145                 notification.getAffectedVl().add(onapVirtualLink);
146             }
147         }
148     }
149
150     private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedConnectionPoints affectedCps) {
151         if (affectedCps != null) {
152             notificationToSend.setAffectedCp(new ArrayList<>());
153             for (ReportedAffectedCp affectedCp : affectedCps.getPost()) {
154                 if (affectedCp.getCpdId() != null) {
155                     AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), affectedCp);
156                     onapAffectedCp.setCpdid(affectedCp.getCpdId());
157                     notificationToSend.getAffectedCp().add(onapAffectedCp);
158                 }
159                 if (affectedCp.getEcpdId() != null) {
160                     AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), affectedCp);
161                     onapAffectedCp.setCpdid(affectedCp.getEcpdId());
162                     notificationToSend.getAffectedCp().add(onapAffectedCp);
163                 }
164             }
165         }
166     }
167
168     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) {
169         switch (type) {
170             case TERMINATE:
171                 return org.onap.vnfmdriver.model.OperationType.TERMINAL;
172             case INSTANTIATE:
173                 return org.onap.vnfmdriver.model.OperationType.INSTANTIATE;
174             case SCALE:
175                 ScaleVnfRequest originalRequest = new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class);
176                 switch (originalRequest.getType()) {
177                     case IN:
178                         return org.onap.vnfmdriver.model.OperationType.SCALEIN;
179                     default: //OUT
180                         return org.onap.vnfmdriver.model.OperationType.SCALEOUT;
181                 }
182             default:
183                 return org.onap.vnfmdriver.model.OperationType.HEAL;
184         }
185     }
186
187     private String extractServerName(Object additionalData) {
188         return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
189     }
190
191     private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
192         switch (changeType) {
193             case ADDED:
194                 return org.onap.vnfmdriver.model.VnfNotificationType.ADDED;
195             case REMOVED:
196                 return org.onap.vnfmdriver.model.VnfNotificationType.REMOVED;
197             default: //case MODIFIED:
198                 return org.onap.vnfmdriver.model.VnfNotificationType.MODIFIED;
199         }
200     }
201
202 }