summaryrefslogtreecommitdiffstats
path: root/pomba/service-decomposition/src/main/java/org/onap/sdnc/apps/pomba/servicedecomposition/util/RestUtil.java
blob: c9e2c501005a6fa653512966a0f6aafae9b8bedb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * ============LICENSE_START===================================================
 * Copyright (c) 2018 Amdocs
 * ============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=====================================================
 */
package org.onap.sdnc.apps.pomba.servicedecomposition.util;

import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.FETCH_RESOURCE_FAILED;
import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.INVALID_URL;
import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.RELATIONSHIP_LINK_PARSE_ERROR;
import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.SERVICE_INSTANCE_NOT_FOUND;
import static org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException.Error.SERVICE_RELATIONSHIP_PARSE_ERROR;

import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response.Status;
import org.json.JSONArray;
import org.json.JSONObject;
import org.onap.aai.restclient.client.OperationResult;
import org.onap.aai.restclient.client.RestClient;
import org.onap.logging.ref.slf4j.ONAPLogAdapter;
import org.onap.sdnc.apps.pomba.servicedecomposition.exception.DiscoveryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




public class RestUtil {
    // Parameters for Query AAI Model Data API
    // HTTP headers
    private static final String TRANSACTION_ID = "X-TransactionId";
    private static final String FROM_APP_ID = "X-FromAppId";
    private static final String APP_NAME = "aaiCtxBuilder";

    private static final Resource GENERIC_VNF = new Resource("generic-vnf");

    public static class Resource {
        private String resourceName;
        private String collectionName;

        private Resource(String resource) {
            this.resourceName = resource;
            this.collectionName = resource + "s";
        }

        private String getResourceName() {
            return this.resourceName;
        }

        private String getCollectionName() {
            return this.collectionName;
        }
    }

    private static final String JSON_ATT_RELATED_TO = "related-to";
    private static final String JSON_ATT_RELATED_LINK = "related-link";

    private static final String EMPTY_JSON_STRING = "{}";

    private static final String DEPTH = "?depth=2";
    private static Logger logger = LoggerFactory.getLogger(RestUtil.class);

    /**
     * Validates the URL parameter.
     *
     * @throws DiscoveryException if there is missing parameter
     */
    public static void validateURL(String serviceInstanceId) throws DiscoveryException {

        if (serviceInstanceId == null || serviceInstanceId.isEmpty()) {
            throw new DiscoveryException(INVALID_URL, Status.BAD_REQUEST);
        }
    }

    private static String generateServiceInstanceURL(String siPath, String serviceInstanceId) {
        return MessageFormat.format(siPath, serviceInstanceId);
    }


    /*
     * Trigger external API call to AAI to retrieve Service Instance data (i.e. genericVNF and VNFC)
     */

    /**
     * @param aaiClient
     * @param baseURL
     * @param aaiServiceInstancePath
     * @param transactionId
     * @param serviceInstanceId
     * @param modelVersionId
     * @param modelInvariantId
     * @return
     * @throws DiscoveryException
     */
    public static JSONObject retrieveAAIModelData(RestClient aaiClient, String baseURL, String aaiServiceInstancePath, String aaiResourceList,
            String transactionId, String serviceInstanceId, ONAPLogAdapter adapter) throws DiscoveryException {

        // Follow two variables for transform purpose
        String url = baseURL + generateServiceInstanceURL(aaiServiceInstancePath, serviceInstanceId);
        // Response from service instance API call
        JSONObject serviceInstancePayload = new JSONObject(
                getResource(aaiClient, url, transactionId));
        // Handle the case if the service instance is not found in AAI
        if (serviceInstancePayload == null || serviceInstancePayload.length() == 0) {
            logger.info("Service Instance " + serviceInstanceId + " is not found from AAI");
            // Only return the empty Json on the root level. i.e service instance
            throw new DiscoveryException(SERVICE_INSTANCE_NOT_FOUND, Status.NOT_FOUND);
        }

        HashMap<String, List<String>> relationMap = extractServiceRelationShips(serviceInstancePayload);
        logger.info("The number of the relationships for service instance id {} is: {}", serviceInstanceId,
                relationMap.size());

        JSONObject response = processVNFRelationMap(aaiClient, aaiResourceList, baseURL, transactionId, relationMap, serviceInstancePayload);
        return response;
    }


    /**
     * @param aaiClient
     * @param baseURL
     * @param transactionId
     * @param relationMap
     * @throws DiscoveryException
     */
    private static JSONObject processVNFRelationMap(RestClient aaiClient, String aaiResourceList, String baseURL, String transactionId,
            HashMap<String, List<String>> relationMap, JSONObject serviceInstancePayload) throws DiscoveryException {
        List<JSONObject> vnfLst = new ArrayList<JSONObject>(); // List of the VNF JSON along with related resources

        JSONObject response = serviceInstancePayload;

        List<Resource> resourceTypes = getResourceTypes(aaiResourceList);

        if (relationMap.get(GENERIC_VNF.getResourceName()) != null) {
            List<JSONObject> vnfList = processResourceList(aaiClient, baseURL, transactionId, GENERIC_VNF.getResourceName(),
                    relationMap.get(GENERIC_VNF.getResourceName()));
            // Logic to Create the Generic VNF JSON and extract further relationships
            for (JSONObject vnfPayload : vnfList) {
                for (Resource resourceType : resourceTypes) {
                    List<String> vnfcLinkLst = extractRelatedLink(vnfPayload, resourceType.getResourceName());
                    if (vnfcLinkLst != null && !vnfcLinkLst.isEmpty()) {
                        logger.info("The number of the API call for vnfc is:" + vnfcLinkLst.size());
                        List<JSONObject> vnfcList = processResourceList(aaiClient, baseURL, transactionId,
                                resourceType.getResourceName(), vnfcLinkLst);
                        if (vnfcList != null) {
                            vnfPayload.put(resourceType.getCollectionName(), vnfcList);
                        }
                    } else {
                        logger.info("No " + resourceType.getResourceName() + " found for vnf-id:" + vnfPayload.getString("vnf-id"));
                    }
                }
            // Add final vnf payload to list
            vnfLst.add(vnfPayload);
            }
        } else {
            logger.info("No " + GENERIC_VNF.getResourceName() +  " found for :" + serviceInstancePayload.getString("service-instance-id"));
        }

        // Add generic vnf with related resource payload to response
        if (vnfLst != null && !vnfLst.isEmpty()) {
            response.put(GENERIC_VNF.getCollectionName(), vnfLst);
        }
        return response;

    }


    /**
     * @param aaiClient
     * @param aaiBaseURL
     * @param transactionId
     * @param resourceType
     * @param resourceList
     * @return
     * @throws DiscoveryException
     */
    private static List<JSONObject> processResourceList(RestClient aaiClient, String aaiBaseURL, String transactionId,
            String resourceType, List<String> resourceList) throws DiscoveryException {
        List<JSONObject> resourcePayloadList = new ArrayList<JSONObject>();
        for (String resourceLink : resourceList) {
            String resourceURL = aaiBaseURL + resourceLink;
            // With latest AAI development, in order to retrieve the both generic VNF + vf_module, we can use
            // one API call but with depth=2
            if (resourceType.equals(GENERIC_VNF.getResourceName())) {
                resourceURL += DEPTH;
            }

            // Response from generic VNF API call
            JSONObject resourcePayload = new JSONObject(
                    getResource(aaiClient, resourceURL, transactionId));
            if (resourcePayload == null || resourcePayload.length() == 0) {
                logger.info("Resource with url " + resourceLink + " is not found from AAI");
            } else {
                resourcePayloadList.add(resourcePayload);
            }
        }
        return resourcePayloadList;
    }


    /**
     * @param serviceInstancePayload
     * @param relationMap
     * @return
     * @throws DiscoveryException
     */
    private static HashMap<String, List<String>> extractServiceRelationShips(JSONObject payload)
            throws DiscoveryException {

        JSONArray relationships = null;
        HashMap<String, List<String>> relationMap = new HashMap<String, List<String>>();
        logger.info("Fetching Service Instance Relationships");
        try {
            JSONObject relationshipList = payload.getJSONObject("relationship-list");
            if (relationshipList != null) {
                relationships = relationshipList.getJSONArray("relationship");
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new DiscoveryException(SERVICE_RELATIONSHIP_PARSE_ERROR, Status.INTERNAL_SERVER_ERROR,
                    e.getMessage());
        }

        if (relationships != null && relationships.length() > 0) {
            for (int i = 0; i < relationships.length(); i++) {
                JSONObject obj = relationships.optJSONObject(i);
                String relatedToObj = obj.getString(JSON_ATT_RELATED_TO);
                String relatedLinkObj = obj.getString(JSON_ATT_RELATED_LINK);

                if (relatedToObj == null) {
                    logger.info("Related-To Object found null");
                    continue;
                }
                List<String> relatedLinkList = relationMap.get(relatedToObj);
                if (relatedLinkList == null) {
                    relatedLinkList = new ArrayList<>();
                    relationMap.put(relatedToObj, relatedLinkList);
                }
                relatedLinkList.add(relatedLinkObj);
            }
        }
        return relationMap;
    }

    /**
     * @param client
     * @param url
     * @param transId
     * @param mediaType
     * @return
     * @throws DiscoveryException
     */
    private static String getResource(RestClient client, String url, String transId)
            throws DiscoveryException {
        OperationResult result = client.get(url, buildHeaders(transId), MediaType.valueOf(MediaType.APPLICATION_JSON));

        if (result.getResultCode() == 200) {
            String jsonString = result.getResult();
            return jsonString;
        } else if (result.getResultCode() == 404) {
            // Resource not found, generate empty JSON format
            logger.info("Resource for " + url + " is not found " + "return empty Json format");
            return EMPTY_JSON_STRING;
        } else {
            throw new DiscoveryException(FETCH_RESOURCE_FAILED, Status.INTERNAL_SERVER_ERROR, result.getFailureCause());
        }
    }

    /**
     * Extract the related-Link from Json payload. For example
     * {
     *    "related-to": "vnfc",
     *    "related-link": "/aai/v11/network/vnfcs/vnfc/zrdm5aepdg01vmg003",
     *    "relationship-data": [
     *       {
     *          "relationship-key": "vnfc.vnfc-name",
     *          "relationship-value": "zrdm5aepdg01vmg003"
     *       }
     *    ]
     * }
     */
    private static List<String> extractRelatedLink(JSONObject payload, String catalog) throws DiscoveryException {
        List<String> relatedLinkList = new ArrayList<String>();
        JSONArray relationships = null;
        logger.info("Fetching relationships for resource type: " + catalog);
        try {
            JSONObject relationshipLst = payload.getJSONObject("relationship-list");
            if (relationshipLst != null) {
                relationships = relationshipLst.getJSONArray("relationship");
            }

        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new DiscoveryException(RELATIONSHIP_LINK_PARSE_ERROR, Status.INTERNAL_SERVER_ERROR, e.getMessage());
        }

        if (relationships != null && relationships.length() > 0) {
            for (int i = 0; i < relationships.length(); i++) {
                Object relatedToObj = null;
                Object relatedLinkObj = null;

                JSONObject obj = relationships.optJSONObject(i);
                relatedToObj = obj.get(JSON_ATT_RELATED_TO);

                if (relatedToObj.toString().equals(catalog)) {
                    relatedLinkObj = obj.get(JSON_ATT_RELATED_LINK);
                    if (relatedLinkObj != null) {
                        relatedLinkList.add(relatedLinkObj.toString());
                    }
                }

            }
        }
        if (relatedLinkList != null) {
            logger.info(
                    "Number of relationships found for resource type: " + catalog + " are: " + relatedLinkList.size());
        }
        return relatedLinkList;
    }



    private static Map<String, List<String>> buildHeaders(String transactionId) {
        MultivaluedMap<String, String> headers = new MultivaluedMapImpl();
        headers.put(TRANSACTION_ID, Collections.singletonList(transactionId));
        headers.put(FROM_APP_ID, Collections.singletonList(APP_NAME));
        return headers;
    }

    private static List<Resource> getResourceTypes(String aaiResourceList) {
        List<Resource> resources = new ArrayList<Resource>();
        String[] resourceList = aaiResourceList.split(",");
        for (int i = 0; i < resourceList.length; i++) {
                resources.add(new Resource(resourceList[i]));
        }
        return resources;
    }

}