2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.notificationhandling;
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.util.HashMap;
25 import java.util.List;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.onap.aai.domain.yang.GenericVnf;
30 import org.onap.aai.domain.yang.Vserver;
31 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
32 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
38 import org.slf4j.Logger;
41 * Performs updates to AAI based on a received notification. The updates are executed in a separate
42 * thread so as the notification response to the VNFM is not delayed.
44 public class NotificationHandler implements Runnable {
45 private static Logger logger = getLogger(NotificationHandler.class);
46 private final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification;
47 private final AaiHelper aaiHelper;
48 private final AaiServiceProvider aaiServiceProvider;
51 private final InlineResponse201 vnfInstance;
53 public NotificationHandler(final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification,
54 final AaiHelper aaiHelper, final AaiServiceProvider aaiServiceProvider,
55 final InlineResponse201 vnfInstance) {
56 this.vnfLcmOperationOccurrenceNotification = vnfLcmOperationOccurrenceNotification;
57 this.aaiHelper = aaiHelper;
58 this.aaiServiceProvider = aaiServiceProvider;
59 this.vnfInstance = vnfInstance;
65 if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
66 final GenericVnf genericVnf =
67 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
69 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
71 handleVnfInstantiated(genericVnf);
76 } catch (final Exception exception) {
77 logger.error("Error encountered handling notification, AAI may not be updated correctly "
78 + vnfLcmOperationOccurrenceNotification, exception);
82 private void handleVnfInstantiated(final GenericVnf genericVnf) {
83 final String ipAddress = getOamIpAddress(vnfInstance);
84 logger.debug("Updating " + genericVnf.getVnfId() + " with VNF OAM IP ADDRESS: " + ipAddress);
85 genericVnf.setIpv4OamAddress(ipAddress);
86 genericVnf.setOrchestrationStatus("Created");
88 aaiServiceProvider.invokePutGenericVnf(genericVnf);
90 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
91 vnfInstance.getVimConnectionInfo());
93 logger.debug("Finished handling notification for vnfm: " + vnfInstance.getId());
96 private String getOamIpAddress(final InlineResponse201 vnfInstance) {
98 logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
99 if (vnfInstance.getVnfConfigurableProperties() == null) {
100 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
103 final JSONObject properties = new JSONObject((Map) vnfInstance.getVnfConfigurableProperties());
104 return properties.get("vnfIpAddress").toString();
105 } catch (final JSONException jsonException) {
106 logger.error("Error getting vnfIpAddress", jsonException);
111 private void updateVservers(final VnfLcmOperationOccurrenceNotification notification, final String vnfId,
112 final List<InlineResponse201VimConnectionInfo> vnfInstancesVimConnectionInfo) {
113 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo = new HashMap<>();
114 for (final InlineResponse201VimConnectionInfo vimConnectionInfo : vnfInstancesVimConnectionInfo) {
115 vimConnectionIdToVimConnectionInfo.put(vimConnectionInfo.getId(), vimConnectionInfo);
118 for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
120 switch (vnfc.getChangeType()) {
122 final Vserver vserver = aaiHelper.createVserver(vnfc);
123 aaiHelper.addRelationshipFromVserverVnfToGenericVnf(vserver, vnfId);
124 final InlineResponse201VimConnectionInfo vimConnectionInfo =
125 getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
126 aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo),
127 getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver);
137 private InlineResponse201VimConnectionInfo getVimConnectionInfo(
138 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo,
139 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
140 final String vimConnectionId = vnfc.getComputeResource().getVimConnectionId();
141 return vimConnectionIdToVimConnectionInfo.get(vimConnectionId);
144 private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
145 final String vimId = vimConnectionInfo.getVimId();
146 return vimId.substring(0, vimId.indexOf("_"));
149 private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
150 final String vimId = vimConnectionInfo.getVimId();
151 return vimId.substring(vimId.indexOf("_") + 1);
154 private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
155 final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
156 return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();