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.VnfmServiceProvider;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
38 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
39 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
40 import org.slf4j.Logger;
43 * Performs updates to AAI based on a received notification. The updates are executed in a separate
44 * thread so as the notification response to the VNFM is not delayed.
46 public class NotificationHandler implements Runnable {
47 private static Logger logger = getLogger(NotificationHandler.class);
48 private final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification;
49 private final AaiHelper aaiHelper;
50 private final AaiServiceProvider aaiServiceProvider;
51 private final VnfmServiceProvider vnfmServiceProvider;
52 private final JobManager jobManager;
53 private final InlineResponse201 vnfInstance;
55 public NotificationHandler(final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification,
56 final AaiHelper aaiHelper, final AaiServiceProvider aaiServiceProvider,
57 final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager,
58 final InlineResponse201 vnfInstance) {
59 this.vnfLcmOperationOccurrenceNotification = vnfLcmOperationOccurrenceNotification;
60 this.aaiHelper = aaiHelper;
61 this.aaiServiceProvider = aaiServiceProvider;
62 this.vnfmServiceProvider = vnfmServiceProvider;
63 this.jobManager = jobManager;
64 this.vnfInstance = vnfInstance;
70 if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
71 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
73 handleVnfInstantiate();
81 } catch (final Exception exception) {
82 logger.error("Error encountered handling notification, AAI may not be updated correctly "
83 + vnfLcmOperationOccurrenceNotification, exception);
87 private void handleVnfInstantiate() {
88 if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
89 handleVnfInstantiateCompleted();
93 private void handleVnfInstantiateCompleted() {
94 final GenericVnf genericVnf =
95 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
96 final String ipAddress = getOamIpAddress(vnfInstance);
97 logger.debug("Updating " + genericVnf.getVnfId() + " with VNF OAM IP ADDRESS: " + ipAddress);
98 genericVnf.setIpv4OamAddress(ipAddress);
99 genericVnf.setOrchestrationStatus("Created");
101 aaiServiceProvider.invokePutGenericVnf(genericVnf);
103 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
104 vnfInstance.getVimConnectionInfo());
106 logger.debug("Finished handling notification for vnfm: " + vnfInstance.getId());
109 private String getOamIpAddress(final InlineResponse201 vnfInstance) {
111 logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
112 if (vnfInstance.getVnfConfigurableProperties() == null) {
113 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
116 final JSONObject properties = new JSONObject((Map) vnfInstance.getVnfConfigurableProperties());
117 return properties.get("vnfIpAddress").toString();
118 } catch (final JSONException jsonException) {
119 logger.error("Error getting vnfIpAddress", jsonException);
124 private void handleVnfTerminate() {
125 switch (vnfLcmOperationOccurrenceNotification.getOperationState()) {
127 handleVnfTerminateCompleted();
131 handleVnfTerminateFailed();
137 private void handleVnfTerminateFailed() {
138 final GenericVnf genericVnf =
139 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
140 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
141 vnfInstance.getVimConnectionInfo());
142 jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(), false);
145 private void handleVnfTerminateCompleted() {
146 final GenericVnf genericVnf =
147 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
148 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
149 vnfInstance.getVimConnectionInfo());
151 boolean deleteSuccessful = false;
153 vnfmServiceProvider.deleteVnf(genericVnf.getSelflink());
154 deleteSuccessful = true;
156 jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(),
158 genericVnf.setOrchestrationStatus("Assigned");
159 aaiServiceProvider.invokePutGenericVnf(genericVnf);
163 private void updateVservers(final VnfLcmOperationOccurrenceNotification notification, final String vnfId,
164 final List<InlineResponse201VimConnectionInfo> vnfInstancesVimConnectionInfo) {
165 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo = new HashMap<>();
166 for (final InlineResponse201VimConnectionInfo vimConnectionInfo : vnfInstancesVimConnectionInfo) {
167 vimConnectionIdToVimConnectionInfo.put(vimConnectionInfo.getId(), vimConnectionInfo);
170 for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
171 final InlineResponse201VimConnectionInfo vimConnectionInfo =
172 getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
173 switch (vnfc.getChangeType()) {
175 final Vserver vserver = aaiHelper.createVserver(vnfc);
176 aaiHelper.addRelationshipFromVserverVnfToGenericVnf(vserver, vnfId);
178 aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo),
179 getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver);
182 aaiServiceProvider.invokeDeleteVserver(getCloudOwner(vimConnectionInfo),
183 getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo),
184 vnfc.getComputeResource().getResourceId());
193 private InlineResponse201VimConnectionInfo getVimConnectionInfo(
194 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo,
195 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
196 final String vimConnectionId = vnfc.getComputeResource().getVimConnectionId();
197 return vimConnectionIdToVimConnectionInfo.get(vimConnectionId);
200 private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
201 final String vimId = vimConnectionInfo.getVimId();
202 return vimId.substring(0, vimId.indexOf("_"));
205 private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
206 final String vimId = vimConnectionInfo.getVimId();
207 return vimId.substring(vimId.indexOf("_") + 1);
210 private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
211 final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
212 return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();