Merge "Re-commit some changes which were lost"
[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 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;
41 import java.util.Map;
42 import static org.slf4j.LoggerFactory.getLogger;
43
44 /**
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.
47  */
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;
56
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;
67     }
68
69     @Override
70     public void run() {
71         try {
72             if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
73                 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
74                     case INSTANTIATE:
75                         handleVnfInstantiate();
76                         break;
77                     case TERMINATE:
78                         handleVnfTerminate();
79                         break;
80                     default:
81                 }
82             }
83         } catch (final Exception exception) {
84             logger.error("Error encountered handling notification, AAI may not be updated correctly "
85                     + vnfLcmOperationOccurrenceNotification, exception);
86         }
87     }
88
89     private void handleVnfInstantiate() {
90         if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
91             handleVnfInstantiateCompleted();
92         }
93     }
94
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");
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 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");
113             return;
114         }
115         if (oamIpAddressSource.getType().equals(OamIpAddressType.LITERAL)) {
116             genericVnf.setIpv4OamAddress(oamIpAddressSource.getValue());
117         }
118         try {
119             logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
120             if (vnfInstance.getVnfConfigurableProperties() == null) {
121                 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
122             }
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);
127         }
128     }
129
130     private void handleVnfTerminate() {
131         switch (vnfLcmOperationOccurrenceNotification.getOperationState()) {
132             case COMPLETED:
133                 handleVnfTerminateCompleted();
134                 break;
135             case FAILED:
136             case ROLLING_BACK:
137                 handleVnfTerminateFailed();
138                 break;
139             default:
140         }
141     }
142
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);
149     }
150
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());
156
157         boolean deleteSuccessful = false;
158         try {
159             vnfmServiceProvider.deleteVnf(genericVnf.getSelflink());
160             deleteSuccessful = true;
161         } finally {
162             jobManager.notificationProcessedForOperation(vnfLcmOperationOccurrenceNotification.getId(),
163                     deleteSuccessful);
164             genericVnf.setOrchestrationStatus("Assigned");
165             aaiServiceProvider.invokePutGenericVnf(genericVnf);
166         }
167     }
168
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);
174         }
175
176         for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
177             final InlineResponse201VimConnectionInfo vimConnectionInfo =
178                     getVimConnectionInfo(vimConnectionIdToVimConnectionInfo, vnfc);
179             switch (vnfc.getChangeType()) {
180                 case ADDED:
181                     final Vserver vserver = aaiHelper.createVserver(vnfc);
182                     aaiHelper.addRelationshipFromVserverVnfToGenericVnf(vserver, vnfId);
183
184                     aaiServiceProvider.invokePutVserver(getCloudOwner(vimConnectionInfo),
185                             getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo), vserver);
186                     break;
187                 case REMOVED:
188                     aaiServiceProvider.invokeDeleteVserver(getCloudOwner(vimConnectionInfo),
189                             getCloudRegion(vimConnectionInfo), getTenant(vimConnectionInfo),
190                             vnfc.getComputeResource().getResourceId());
191                     break;
192                 case MODIFIED:
193                 case TEMPORARY:
194                 default:
195             }
196         }
197     }
198
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);
204     }
205
206     private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
207         final String vimId = vimConnectionInfo.getVimId();
208         return vimId.substring(0, vimId.indexOf("_"));
209     }
210
211     private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
212         final String vimId = vimConnectionInfo.getVimId();
213         return vimId.substring(vimId.indexOf("_") + 1);
214     }
215
216     private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
217         final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
218         return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();
219     }
220
221 }