Notification handling for instantiate
[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.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;
39
40 /**
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.
43  */
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;
49
50
51     private final InlineResponse201 vnfInstance;
52
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;
60     }
61
62     @Override
63     public void run() {
64         try {
65             if (vnfLcmOperationOccurrenceNotification.getOperationState().equals(OperationStateEnum.COMPLETED)) {
66                 final GenericVnf genericVnf =
67                         aaiServiceProvider.invokeQueryGenericVnf(vnfInstance.getLinks().getSelf().getHref()).get(0);
68
69                 switch (vnfLcmOperationOccurrenceNotification.getOperation()) {
70                     case INSTANTIATE:
71                         handleVnfInstantiated(genericVnf);
72                         break;
73                     default:
74                 }
75             }
76         } catch (final Exception exception) {
77             logger.error("Error encountered handling notification, AAI may not be updated correctly "
78                     + vnfLcmOperationOccurrenceNotification, exception);
79         }
80     }
81
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");
87
88         aaiServiceProvider.invokePutGenericVnf(genericVnf);
89
90         updateVservers(vnfLcmOperationOccurrenceNotification, genericVnf.getVnfId(),
91                 vnfInstance.getVimConnectionInfo());
92
93         logger.debug("Finished handling notification for vnfm: " + vnfInstance.getId());
94     }
95
96     private String getOamIpAddress(final InlineResponse201 vnfInstance) {
97         try {
98             logger.debug("ConfigurableProperties: " + vnfInstance.getVnfConfigurableProperties());
99             if (vnfInstance.getVnfConfigurableProperties() == null) {
100                 logger.warn("No ConfigurableProperties, cannot set OAM IP Address");
101                 return null;
102             }
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);
107             return null;
108         }
109     }
110
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);
116         }
117
118         for (final LcnVnfLcmOperationOccurrenceNotificationAffectedVnfcs vnfc : notification.getAffectedVnfcs()) {
119
120             switch (vnfc.getChangeType()) {
121                 case ADDED:
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);
128                     break;
129                 case REMOVED:
130                 case MODIFIED:
131                 case TEMPORARY:
132                 default:
133             }
134         }
135     }
136
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);
142     }
143
144     private String getCloudOwner(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
145         final String vimId = vimConnectionInfo.getVimId();
146         return vimId.substring(0, vimId.indexOf("_"));
147     }
148
149     private String getCloudRegion(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
150         final String vimId = vimConnectionInfo.getVimId();
151         return vimId.substring(vimId.indexOf("_") + 1);
152     }
153
154     private String getTenant(final InlineResponse201VimConnectionInfo vimConnectionInfo) {
155         final JSONObject vimConnectionJsonObject = new JSONObject(vimConnectionInfo);
156         return vimConnectionJsonObject.getJSONObject("accessInfo").get("projectId").toString();
157     }
158
159 }