aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-sol003-adapter-lcm/so-etsi-sol003-adapter-lcm-service/src/main/java/org/onap/so/adapters/etsisol003adapter/lcm/notificationhandling/NotificationHandler.java
blob: f23fac8115e8d8d8244ce5d7d5be1e2ff197149a (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.etsisol003adapter.lcm.notificationhandling;

import static org.slf4j.LoggerFactory.getLogger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.Relationship;
import org.onap.aai.domain.yang.Vserver;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.AaiHelper;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.AaiServiceProvider;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.OamIpAddressSource;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.aai.OamIpAddressSource.OamIpAddressType;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.VnfmServiceProvider;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201;
import org.onap.so.adapters.etsisol003adapter.lcm.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
import org.onap.so.adapters.etsisol003adapter.lcm.jobmanagement.JobManager;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs.ChangeTypeEnum;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification;
import org.onap.so.adapters.etsisol003adapter.lcm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
import org.slf4j.Logger;

/**
 * Performs updates to AAI based on a received notification. The updates are executed in a separate thread so as the
 * notification response to the VNFM is not delayed.
 */
public class NotificationHandler implements Runnable {
    private static Logger logger = getLogger(NotificationHandler.class);
    private final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification;
    private final AaiHelper aaiHelper;
    private final AaiServiceProvider aaiServiceProvider;
    private final VnfmServiceProvider vnfmServiceProvider;
    private final JobManager jobManager;
    private final InlineResponse201 vnfInstance;

    public NotificationHandler(final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification,
            final AaiHelper aaiHelper, final AaiServiceProvider aaiServiceProvider,
            final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager,
            final InlineResponse201 vnfInstance) {
        this.vnfLcmOperationOccurrenceNotification = vnfLcmOperationOccurrenceNotification;
        this.aaiHelper = aaiHelper;
        this.aaiServiceProvider = aaiServiceProvider;
        this.vnfmServiceProvider = vnfmServiceProvider;
        this.jobManager = jobManager;
        this.vnfInstance = vnfInstance;
    }

    @Override
    public void run() {
        try {
            if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
                switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
                    case INSTANTIATE:
                        handleVnfInstantiate();
                        break;
                    case TERMINATE:
                        handleVnfTerminate();
                        break;
                    default:
                }
            }
        } catch (final Exception exception) {
            logger.error("Error encountered handling notification, AAI may not be updated correctly "
                    + vnfLcmOperationOccurrenceNotification, exception);
        }
    }

    private void handleVnfInstantiate() {
        if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
            handleVnfInstantiateCompleted();
        }
    }

    private void handleVnfInstantiateCompleted() {
        final GenericVnf genericVnf = aaiServiceProvider
                .invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).getGenericVnf().get(0);

        final GenericVnf genericVnfPatch = new GenericVnf();
        genericVnfPatch.setVnfId(genericVnf.getVnfId());
        setOamIpAddress(genericVnfPatch, vnfInstance);
        genericVnfPatch.setOrchestrationStatus("Created");
        aaiServiceProvider.invokePatchGenericVnf(genericVnfPatch);

        addVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(), vnfInstance.getVimConnectionInfo());

        logger.debug("Finished handling notification for vnfm: " + vnfInstance.getId());
    }

    private void setOamIpAddress(final GenericVnf genericVnf, final InlineResponse201 vnfInstance) {
        final OamIpAddressSource oamIpAddressSource = aaiHelper.getOamIpAddressSource(vnfInstance.getId());
        if (oamIpAddressSource == null) {
            logger.warn("No source indicated for OAM IP address, no value will be set in AAI");
            return;
        }
        if (oamIpAddressSource.getType().equals(OamIpAddressType.LITERAL)) {
            genericVnf.setIpv4OamAddress(oamIpAddressSource.getValue());
        } else {
            try {
                logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
                if (vnfInstance.getVnfConfigurableProperties() == null) {
                    logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
                }
                final JSONObject properties = new JSONObject((Map) vnfInstance.getVnfConfigurableProperties());
                genericVnf.setIpv4OamAddress(properties.get(oamIpAddressSource.getValue()).toString());
            } catch (final JSONException jsonException) {
                logger.error("Error getting vnfIpAddress", jsonException);
            }
        }
    }

    private void handleVnfTerminate() {
        switch (vnfLcmOperationOccurrenceNotification.getOperationState()) {
            case COMPLETED:
                handleVnfTerminateCompleted();
                break;
            case FAILED:
            case ROLLING_BACK:
                handleVnfTerminateFailed();
                break;
            default:
        }
    }

    private void handleVnfTerminateFailed() {
        try {
            final GenericVnf genericVnf = aaiServiceProvider
                    .invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).getGenericVnf().get(0);
            deleteVserversFromAai(vnfLcmOperationOccurrenceNotification, genericVnf);
        } finally {
            jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId(),
                    false);
        }
    }

    private void handleVnfTerminateCompleted() {
        GenericVnf genericVnf = null;
        boolean vServersDeletedFromAai = false;
        boolean identifierDeletedFromVnfm = false;
        boolean genericVnfUpdated = false;
        try {
            genericVnf = aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref())
                    .getGenericVnf().get(0);
            vServersDeletedFromAai = deleteVserversFromAai(vnfLcmOperationOccurrenceNotification, genericVnf);
            identifierDeletedFromVnfm = deleteVnfIdentifierOnVnfm(genericVnf);
            genericVnfUpdated = patchVnfInAai(genericVnf.getVnfId(), "Assigned", identifierDeletedFromVnfm ? "" : null);
        } finally {
            jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId(),
                    vServersDeletedFromAai && identifierDeletedFromVnfm && genericVnfUpdated);
            jobManager.vnfDeleted(vnfLcmOperationOccurrenceNotification.getVnfLcmOpOccId());
        }
    }

    private void addVservers(final VnfLcmOperationOccurrenceNotification notification, final String vnfId,
            final List<InlineResponse201VimConnectionInfo> vnfInstancesVimConnectionInfo) {
        final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo = new HashMap<>();
        for (final InlineResponse201VimConnectionInfo vimConnectionInfo : vnfInstancesVimConnectionInfo) {
            vimConnectionIdToVimConnectionInfo.put(vimConnectionInfo.getId(), vimConnectionInfo);
        }

        for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
            final InlineResponse201VimConnectionInfo vimConnectionInfo =
                    getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
            if (ChangeTypeEnum.ADDED.equals(vnfc.getChangeType())) {
                final Vserver vserver = aaiHelper.createVserver(vnfc);
                aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo), getCloudRegion(vimConnectionInfo),
                        getTenant(vimConnectionInfo), vserver);

                aaiServiceProvider.invokePutVserverToVnfRelationship(getCloudOwner(vimConnectionInfo),
                        getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver, vnfId);
            }
        }
    }

    private boolean deleteVserversFromAai(final VnfLcmOperationOccurrenceNotification notification,
            final GenericVnf vnf) {
        try {
            for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
                if (ChangeTypeEnum.REMOVED.equals(vnfc.getChangeType())) {

                    final Relationship relationshipToVserver = aaiHelper.deleteRelationshipWithDataValue(vnf, "vserver",
                            "vserver.vserver-id", vnfc.getComputeResource().getResourceId());

                    aaiServiceProvider.invokeDeleteVserver(
                            aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-owner"),
                            aaiHelper.getRelationshipData(relationshipToVserver, "cloud-region.cloud-region-id"),
                            aaiHelper.getRelationshipData(relationshipToVserver, "tenant.tenant-id"),
                            vnfc.getComputeResource().getResourceId());
                }
            }
            return true;
        } catch (final Exception exception) {
            logger.error(
                    "Error encountered deleting vservers based on received notification, AAI may not be updated correctly "
                            + vnfLcmOperationOccurrenceNotification,
                    exception);
            return false;
        }
    }

    private boolean deleteVnfIdentifierOnVnfm(final GenericVnf genericVnf) {
        try {
            vnfmServiceProvider.deleteVnf(aaiHelper.getAssignedVnfm(genericVnf), genericVnf.getSelflink());
            return true;
        } catch (final Exception exception) {
            logger.error("Exception deleting the identifier " + genericVnf.getSelflink()
                    + " from the VNFM. The VNF has been terminated successfully but the identifier will remain on the VNFM.",
                    exception);
            return false;
        }
    }

    private boolean patchVnfInAai(final String vnfId, final String orchestrationStatus, final String selfLink) {
        try {
            final GenericVnf genericVnfPatch = new GenericVnf();
            genericVnfPatch.setVnfId(vnfId);
            genericVnfPatch.setOrchestrationStatus(orchestrationStatus);
            if (selfLink != null) {
                genericVnfPatch.setSelflink(selfLink);
            }
            aaiServiceProvider.invokePatchGenericVnf(genericVnfPatch);
            return true;
        } catch (final Exception exception) {
            logger.error(
                    "Error encountered setting orchestration status and/or self link based on received notification, AAI may not be updated correctly "
                            + vnfLcmOperationOccurrenceNotification,
                    exception);
            return false;
        }
    }

    private InlineResponse201VimConnectionInfo getVimConnectionInfo(
            final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo,
            final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
        final String vimConnectionId = vnfc.getComputeResource().getVimConnectionId();
        return vimConnectionIdToVimConnectionInfo.get(vimConnectionId);
    }

    private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
        final String vimId = vimConnectionInfo.getVimId();
        return vimId.substring(0, vimId.indexOf("_"));
    }

    private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
        final String vimId = vimConnectionInfo.getVimId();
        return vimId.substring(vimId.indexOf("_") + 1);
    }

    private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
        final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
        return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();
    }

}