b82ed86bebe7f44fe9d6e1d470353977f7b442fb
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / main / java / org / onap / so / adapters / vnfmadapter / notificationhandling / NotificationHandler.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.notificationhandling;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
41
42 /**
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.
45  */
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;
54
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;
65     }
66
67     @Override
68     public void run() {
69         try {
70             if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
71                 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
72                     case INSTANTIATE:
73                         handleVnfInstantiate();
74                         break;
75                     case TERMINATE:
76                         handleVnfTerminate();
77                         break;
78                     default:
79                 }
80             }
81         } catch (final Exception exception) {
82             logger.error("Error encountered handling notification, AAI may not be updated correctly "
83                     + vnfLcmOperationOccurrenceNotification, exception);
84         }
85     }
86
87     private void handleVnfInstantiate() {
88         if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
89             handleVnfInstantiateCompleted();
90         }
91     }
92
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");
100
101         aaiServiceProvider.invokePutGenericVnf(genericVnf);
102
103         updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
104                 vnfInstance.getVimConnectionInfo());
105
106         logger.debug("Finished handling notification for vnfm: " + vnfInstance.getId());
107     }
108
109     private String getOamIpAddress(final InlineResponse201 vnfInstance) {
110         try {
111             logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
112             if (vnfInstance.getVnfConfigurableProperties() == null) {
113                 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
114                 return null;
115             }
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);
120             return null;
121         }
122     }
123
124     private void handleVnfTerminate() {
125         switch (vnfLcmOperationOccurrenceNotification.getOperationState()) {
126             case COMPLETED:
127                 handleVnfTerminateCompleted();
128                 break;
129             case FAILED:
130             case ROLLING_BACK:
131                 handleVnfTerminateFailed();
132                 break;
133             default:
134         }
135     }
136
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);
143     }
144
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());
150
151         boolean deleteSuccessful = false;
152         try {
153             vnfmServiceProvider.deleteVnf(genericVnf.getSelflink());
154             deleteSuccessful = true;
155         } finally {
156             jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(),
157                     deleteSuccessful);
158             genericVnf.setOrchestrationStatus("Assigned");
159             aaiServiceProvider.invokePutGenericVnf(genericVnf);
160         }
161     }
162
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);
168         }
169
170         for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
171             final InlineResponse201VimConnectionInfo vimConnectionInfo =
172                     getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
173             switch (vnfc.getChangeType()) {
174                 case ADDED:
175                     final Vserver vserver = aaiHelper.createVserver(vnfc);
176                     aaiHelper.addRelationshipFromVserverVnfToGenericVnf(vserver, vnfId);
177
178                     aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo),
179                             getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver);
180                     break;
181                 case REMOVED:
182                     aaiServiceProvider.invokeDeleteVserver(getCloudOwner(vimConnectionInfo),
183                             getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo),
184                             vnfc.getComputeResource().getResourceId());
185                     break;
186                 case MODIFIED:
187                 case TEMPORARY:
188                 default:
189             }
190         }
191     }
192
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);
198     }
199
200     private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
201         final String vimId = vimConnectionInfo.getVimId();
202         return vimId.substring(0, vimId.indexOf("_"));
203     }
204
205     private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
206         final String vimId = vimConnectionInfo.getVimId();
207         return vimId.substring(vimId.indexOf("_") + 1);
208     }
209
210     private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
211         final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
212         return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();
213     }
214
215 }