summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/main/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/vfc/VfcNotificationSender.java
blob: 65cef408b3c5bba91166a2c6aee9cbc42769c8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.vfc;

import com.google.gson.Gson;
import com.nokia.cbam.lcm.v32.model.OperationExecution;
import com.nokia.cbam.lcm.v32.model.ScaleVnfRequest;
import com.nokia.cbam.lcm.v32.model.VnfLifecycleChangeNotification;
import java.util.ArrayList;
import java.util.Optional;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.INotificationSender;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.LifecycleChangeNotificationManager;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
import org.onap.vnfmdriver.model.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import static java.util.Optional.of;

import static com.google.common.collect.Iterables.tryFind;
import static com.nokia.cbam.lcm.v32.model.ScaleDirection.IN;
import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.JobManager.extractOnapJobId;
import static org.slf4j.LoggerFactory.getLogger;

/**
 * Responsible for sending notifications to VF-C
 */
@Component
@Conditional(value = Conditions.UseForVfc.class)
public class VfcNotificationSender implements INotificationSender {
    private static Logger logger = getLogger(VfcNotificationSender.class);
    private final DriverProperties driverProperties;
    private final VfcRestApiProvider vfcRestApiProvider;

    @Autowired
    VfcNotificationSender(DriverProperties driverProperties, VfcRestApiProvider vfcRestApiProvider) {
        this.driverProperties = driverProperties;
        this.vfcRestApiProvider = vfcRestApiProvider;
    }

    @Override
    public void processNotification(VnfLifecycleChangeNotification recievedNotification, OperationExecution operationExecution, Optional<ReportedAffectedConnectionPoints> affectedCps, String vimId) {
        VNFLCMNotification notificationToSend = new VNFLCMNotification();
        notificationToSend.setJobId(extractOnapJobId(operationExecution.getOperationParams()));
        notificationToSend.setOperation(getOperation(operationExecution, recievedNotification.getOperation()));
        notificationToSend.setVnfInstanceId(recievedNotification.getVnfInstanceId());
        if (LifecycleChangeNotificationManager.isTerminal(recievedNotification.getStatus())) {
            notificationToSend.setStatus(VnfLcmNotificationStatus.RESULT);
            addAffectedVirtualLinks(recievedNotification, notificationToSend);
            addAffectedVnfcs(vimId, recievedNotification.getVnfInstanceId(), notificationToSend, recievedNotification);
            addAffectedCps(vimId, notificationToSend, affectedCps);
        } else {
            notificationToSend.setStatus(VnfLcmNotificationStatus.START);
        }
        sendNotification(notificationToSend);
    }

    private void sendNotification(VNFLCMNotification notification) {
        try {
            if (logger.isInfoEnabled()) {
                logger.info("Sending LCN: {}", new Gson().toJson(notification));
            }
            vfcRestApiProvider.getNsLcmApi().vNFLCMNotification(driverProperties.getVnfmId(), notification.getVnfInstanceId(), notification);
        } catch (Exception e) {
            throw buildFatalFailure(logger, "Unable to send LCN to VF-C", e);
        }
    }

    private AffectedCp buildAffectedCp(String vimId, String vnfId, VnfCpNotificationType changeType, ReportedAffectedCp affectedCp) {
        AffectedCp onapAffectedCp = new AffectedCp();
        AffectedCpPortResource port = new AffectedCpPortResource();
        port.setInstId(affectedCp.getServerProviderId());
        port.setIpAddress(affectedCp.getIpAddress());
        port.setMacAddress(affectedCp.getMacAddress());
        port.setResourceid(affectedCp.getProviderId());
        port.setResourceName(affectedCp.getName());
        port.setTenant(affectedCp.getTenantId());
        port.setVimid(vimId);
        onapAffectedCp.setPortResource(port);
        onapAffectedCp.setCpdid(affectedCp.getCpId());
        onapAffectedCp.setCpinstanceid(vnfId + SEPARATOR + affectedCp.getCpId());
        onapAffectedCp.setVirtualLinkInstanceId(affectedCp.getNetworkProviderId());
        onapAffectedCp.setChangeType(changeType);
        //owner id & type can be left empty it will default to VNF id on VF-C
        return onapAffectedCp;
    }


    private void addAffectedVnfcs(String vimId, String vnfId, VNFLCMNotification notificationToSend, VnfLifecycleChangeNotification request) {
        if (request.getAffectedVnfcs() != null) {
            notificationToSend.setAffectedVnfc(new ArrayList<>());
            for (com.nokia.cbam.lcm.v32.model.AffectedVnfc affectedVnfc : request.getAffectedVnfcs()) {
                org.onap.vnfmdriver.model.AffectedVnfc onapVnfc = new org.onap.vnfmdriver.model.AffectedVnfc();
                onapVnfc.setChangeType(getChangeType(affectedVnfc.getChangeType()));
                onapVnfc.setVduId(affectedVnfc.getVduId());
                onapVnfc.setVmid(affectedVnfc.getComputeResource().getResourceId());
                onapVnfc.setVmname(extractServerName(affectedVnfc.getComputeResource().getAdditionalData()));
                onapVnfc.setVnfcInstanceId(vnfId + SEPARATOR + affectedVnfc.getId());
                onapVnfc.setVimid(vimId);
                notificationToSend.getAffectedVnfc().add(onapVnfc);
            }
        }
    }

    private void addAffectedVirtualLinks(VnfLifecycleChangeNotification request, VNFLCMNotification notification) {
        if (request.getAffectedVirtualLinks() != null) {
            notification.setAffectedVl(new ArrayList<>());
            for (com.nokia.cbam.lcm.v32.model.AffectedVirtualLink affectedVirtualLink : request.getAffectedVirtualLinks()) {
                org.onap.vnfmdriver.model.AffectedVirtualLink onapVirtualLink = new org.onap.vnfmdriver.model.AffectedVirtualLink();
                onapVirtualLink.setVlInstanceId(request.getVnfInstanceId() + SEPARATOR + affectedVirtualLink.getId());
                onapVirtualLink.setChangeType(getChangeType(affectedVirtualLink.getChangeType()));
                onapVirtualLink.setVldid(affectedVirtualLink.getVirtualLinkDescId());
                AffectedVirtualLinkNetworkResource networkResource = new AffectedVirtualLinkNetworkResource();
                onapVirtualLink.setNetworkResource(networkResource);
                networkResource.setResourceId(affectedVirtualLink.getResource().getResourceId());
                networkResource.setResourceType(AffectedVirtualLinkType.NETWORK);
                notification.getAffectedVl().add(onapVirtualLink);
            }
        }
    }

    private Optional<VnfCpNotificationType> getChangeType(ReportedAffectedConnectionPoints affectedCps, ReportedAffectedCp affectedCp) {
        com.google.common.base.Optional<ReportedAffectedCp> cpBeforeOperation = tryFind(affectedCps.getPre(), pre -> affectedCp.getCpId().equals(pre.getCpId()));
        com.google.common.base.Optional<ReportedAffectedCp> cpAfterOperation = tryFind(affectedCps.getPost(), post -> affectedCp.getCpId().equals(post.getCpId()));
        if (cpBeforeOperation.isPresent() && cpAfterOperation.isPresent()) {
            return cpAfterOperation.get().equals(cpBeforeOperation.get()) ? Optional.empty() : of(VnfCpNotificationType.CHANGED);
        } else {
            //the affected CP must be present in the pre or post
            return of((cpAfterOperation.isPresent() ? VnfCpNotificationType.ADDED : VnfCpNotificationType.REMOVED));
        }
    }

    private void addAffectedCps(String vimId, VNFLCMNotification notificationToSend, Optional<ReportedAffectedConnectionPoints> affectedCps) {
        if (affectedCps.isPresent()) {
            notificationToSend.setAffectedCp(new ArrayList<>());
            for (ReportedAffectedCp pre : affectedCps.get().getPre()) {
                Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), pre);
                if (of(VnfCpNotificationType.REMOVED).equals(changeType)) {
                    addModifiedCp(vimId, notificationToSend, pre, changeType);
                }
            }
            for (ReportedAffectedCp post : affectedCps.get().getPost()) {
                Optional<VnfCpNotificationType> changeType = getChangeType(affectedCps.get(), post);
                if (of(VnfCpNotificationType.ADDED).equals(changeType)) {
                    addModifiedCp(vimId, notificationToSend, post, changeType);
                }
                if (of(VnfCpNotificationType.CHANGED).equals(changeType)) {
                    addModifiedCp(vimId, notificationToSend, post, changeType);
                }
            }
        }
    }

    private void addModifiedCp(String vimId, VNFLCMNotification notificationToSend, ReportedAffectedCp post, Optional<VnfCpNotificationType> changeType) {
        if (post.getCpdId() != null) {
            AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
            onapAffectedCp.setCpdid(post.getCpdId());
            notificationToSend.getAffectedCp().add(onapAffectedCp);
        }
        if (post.getEcpdId() != null) {
            AffectedCp onapAffectedCp = buildAffectedCp(vimId, notificationToSend.getVnfInstanceId(), changeType.get(), post);
            onapAffectedCp.setCpdid(post.getEcpdId());
            notificationToSend.getAffectedCp().add(onapAffectedCp);
        }
    }

    private org.onap.vnfmdriver.model.OperationType getOperation(OperationExecution operationExecution, com.nokia.cbam.lcm.v32.model.OperationType type) {
        if (type == com.nokia.cbam.lcm.v32.model.OperationType.TERMINATE) {
            return OperationType.TERMINAL;
        } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.INSTANTIATE) {
            return OperationType.INSTANTIATE;
        } else if (type == com.nokia.cbam.lcm.v32.model.OperationType.SCALE) {
            if (IN == new Gson().fromJson(new Gson().toJson(operationExecution.getOperationParams()), ScaleVnfRequest.class).getType()) {
                return OperationType.SCALEIN;
            } else {
                return OperationType.SCALEOUT;
            }
        } else {
            return OperationType.HEAL;
        }
    }

    private String extractServerName(Object additionalData) {
        return new Gson().toJsonTree(additionalData).getAsJsonObject().get("name").getAsString();
    }

    private org.onap.vnfmdriver.model.VnfNotificationType getChangeType(com.nokia.cbam.lcm.v32.model.ChangeType changeType) {
        if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.ADDED) {
            return VnfNotificationType.ADDED;
        } else if (changeType == com.nokia.cbam.lcm.v32.model.ChangeType.REMOVED) {
            return VnfNotificationType.REMOVED;
        } else {
            return VnfNotificationType.MODIFIED;
        }
    }

}