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 org.json.JSONException;
24 import org.json.JSONObject;
25 import org.onap.aai.domain.yang.GenericVnf;
26 import org.onap.aai.domain.yang.Vserver;
27 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiHelper;
28 import org.onap.so.adapters.vnfmadapter.extclients.aai.AaiServiceProvider;
29 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource;
30 import org.onap.so.adapters.vnfmadapter.extclients.aai.OamIpAddressSource.OamIpAddressType;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.VnfmServiceProvider;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.lcn.model.VnfLcmOperationOccurrenceNotification.OperationStateEnum;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201VimConnectionInfo;
37 import org.onap.so.adapters.vnfmadapter.jobmanagement.JobManager;
38 import org.slf4j.Logger;
39 import java.util.HashMap;
40 import java.util.List;
42 import static org.slf4j.LoggerFactory.getLogger;
45 * Performs updates to AAI based on a received notification. The updates are executed in a separate thread so as the
46 * notification response to the VNFM is not delayed.
48 public class NotificationHandler implements Runnable {
49 private static Logger logger = getLogger(NotificationHandler.class);
50 private final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification;
51 private final AaiHelper aaiHelper;
52 private final AaiServiceProvider aaiServiceProvider;
53 private final VnfmServiceProvider vnfmServiceProvider;
54 private final JobManager jobManager;
55 private final InlineResponse201 vnfInstance;
57 public NotificationHandler(final VnfLcmOperationOccurrenceNotification vnfLcmOperationOccurrenceNotification,
58 final AaiHelper aaiHelper, final AaiServiceProvider aaiServiceProvider,
59 final VnfmServiceProvider vnfmServiceProvider, final JobManager jobManager,
60 final InlineResponse201 vnfInstance) {
61 this.vnfLcmOperationOccurrenceNotification = vnfLcmOperationOccurrenceNotification;
62 this.aaiHelper = aaiHelper;
63 this.aaiServiceProvider = aaiServiceProvider;
64 this.vnfmServiceProvider = vnfmServiceProvider;
65 this.jobManager = jobManager;
66 this.vnfInstance = vnfInstance;
72 if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
73 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
75 handleVnfInstantiate();
83 } catch (final Exception exception) {
84 logger.error("Error encountered handling notification, AAI may not be updated correctly "
85 + vnfLcmOperationOccurrenceNotification, exception);
89 private void handleVnfInstantiate() {
90 if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
91 handleVnfInstantiateCompleted();
95 private void handleVnfInstantiateCompleted() {
96 final GenericVnf genericVnf =
97 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
98 setOamIpAddress(genericVnf, vnfInstance);
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 void setOamIpAddress(final GenericVnf genericVnf, final InlineResponse201 vnfInstance) {
110 final OamIpAddressSource oamIpAddressSource = aaiHelper.getOamIpAddressSource(vnfInstance.getId());
111 if (oamIpAddressSource == null) {
112 logger.warn("No source indicated for OAM IP address, no value will be set in AAI");
115 if (oamIpAddressSource.getType().equals(OamIpAddressType.LITERAL)) {
116 genericVnf.setIpv4OamAddress(oamIpAddressSource.getValue());
119 logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
120 if (vnfInstance.getVnfConfigurableProperties() == null) {
121 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
123 final JSONObject properties = new JSONObject((Map) vnfInstance.getVnfConfigurableProperties());
124 genericVnf.setIpv4OamAddress(properties.get(oamIpAddressSource.getValue()).toString());
125 } catch (final JSONException jsonException) {
126 logger.error("Error getting vnfIpAddress", jsonException);
130 private void handleVnfTerminate() {
131 switch (vnfLcmOperationOccurrenceNotification.getOperationState()) {
133 handleVnfTerminateCompleted();
137 handleVnfTerminateFailed();
143 private void handleVnfTerminateFailed() {
144 final GenericVnf genericVnf =
145 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
146 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
147 vnfInstance.getVimConnectionInfo());
148 jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(), false);
151 private void handleVnfTerminateCompleted() {
152 final GenericVnf genericVnf =
153 aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
154 updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
155 vnfInstance.getVimConnectionInfo());
157 boolean deleteSuccessful = false;
159 vnfmServiceProvider.deleteVnf(genericVnf.getSelflink());
160 deleteSuccessful = true;
162 jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(),
164 genericVnf.setOrchestrationStatus("Assigned");
165 aaiServiceProvider.invokePutGenericVnf(genericVnf);
169 private void updateVservers(final VnfLcmOperationOccurrenceNotification notification, final String vnfId,
170 final List<InlineResponse201VimConnectionInfo> vnfInstancesVimConnectionInfo) {
171 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo = new HashMap<>();
172 for (final InlineResponse201VimConnectionInfo vimConnectionInfo : vnfInstancesVimConnectionInfo) {
173 vimConnectionIdToVimConnectionInfo.put(vimConnectionInfo.getId(), vimConnectionInfo);
176 for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
177 final InlineResponse201VimConnectionInfo vimConnectionInfo =
178 getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
179 switch (vnfc.getChangeType()) {
181 final Vserver vserver = aaiHelper.createVserver(vnfc);
182 aaiHelper.addRelationshipFromVserverVnfToGenericVnf(vserver, vnfId);
184 aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo),
185 getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver);
188 aaiServiceProvider.invokeDeleteVserver(getCloudOwner(vimConnectionInfo),
189 getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo),
190 vnfc.getComputeResource().getResourceId());
199 private InlineResponse201VimConnectionInfo getVimConnectionInfo(
200 final Map<String, InlineResponse201VimConnectionInfo> vimConnectionIdToVimConnectionInfo,
201 final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc) {
202 final String vimConnectionId = vnfc.getComputeResource().getVimConnectionId();
203 return vimConnectionIdToVimConnectionInfo.get(vimConnectionId);
206 private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
207 final String vimId = vimConnectionInfo.getVimId();
208 return vimId.substring(0, vimId.indexOf("_"));
211 private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
212 final String vimId = vimConnectionInfo.getVimId();
213 return vimId.substring(vimId.indexOf("_") + 1);
216 private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
217 final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
218 return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();