diff options
author | Prudence Au <prudence.au@amdocs.com> | 2018-10-26 15:34:22 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-10-26 15:34:22 +0000 |
commit | 28b5ce6794f46f196ea7c005b61d25f8b57d69f7 (patch) | |
tree | 2d49d87a8e7a71836e62d426a5ad208a289aca6e /src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java | |
parent | a33f302265772b2720245118c864813b596a0ef8 (diff) | |
parent | 696b3aab041353afd74e26c0b9a569d0b1b9e254 (diff) |
Merge "Adding API Mapping infrastructure"
Diffstat (limited to 'src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java')
-rw-r--r-- | src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java b/src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java index 806514c..8701c89 100644 --- a/src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java +++ b/src/main/java/org/onap/pomba/contextbuilder/sdnc/util/RestUtil.java @@ -34,6 +34,16 @@ import org.onap.pomba.contextbuilder.sdnc.exception.AuditException; import org.onap.pomba.contextbuilder.sdnc.service.rs.RestService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.onap.aai.restclient.client.OperationResult; +import org.onap.aai.restclient.client.RestClient; +import javax.ws.rs.core.MediaType; +import com.sun.jersey.core.util.MultivaluedMapImpl; +import java.util.Map; +import java.util.Collections; +import javax.ws.rs.core.MultivaluedMap; +import org.json.JSONArray; +import org.json.JSONException; +import org.onap.pomba.contextbuilder.sdnc.model.ServiceEntity; public class RestUtil { @@ -46,6 +56,22 @@ public class RestUtil { public static final String FROM_APP_ID = "X-FromAppId"; public static final String AUTHORIZATION = "Authorization"; + // AAI related + private static final String APP_NAME = "sdncCtxBuilder"; + private static final String EMPTY_JSON_STRING = "{}"; + private static final String JSON_ATT_RESOURCE_TYPE = "resource-type"; + private static final String JSON_ATT_RESOURCE_LINK = "resource-link"; + private static final String RESULT_DATA = "result-data"; + private static final String CATALOG_SERVICE_INSTANCE = "service-instance"; + private static final String CUSTOMER_ID_STRING = "/customers/customer/"; + private static final String SERVICE_TYPE_STRING = "/service-subscriptions/service-subscription/"; + private static final String CUSTOMER = "customer"; + private static final String JSON_ATT_GLOBAL_CUSTOMER_ID = "global-customer-id"; + private static final String JSON_ATT_SUBSCRIBER_TYPE = "subscriber-type"; + private static final String JSON_ATT_SUBSCRIBER_NAME = "subscriber-name"; + + private static final String FORWARD_SLASH = "/"; + // Parameters for Query SDNC Model Data REST API URL private static final String SERVICE_INSTANCE_ID = "serviceInstanceId"; @@ -147,4 +173,189 @@ public class RestUtil { return MessageFormat.format(siPath, vfModuleLink); } + /** + * Get customer info from multiple AAI api + * @param aaiClient + * @param aaiBaseUrl + * @param aaiBasicAuthorization + * @param aaiPathToSearchNodeQuery + * @param aaiPathToCustomerQuery + * @param serviceInstaceId + * @param transactionId + * @return + * @throws AuditException + */ + public static ServiceEntity getServiceEntity(RestClient aaiClient, + String aaiBaseUrl, + String aaiBasicAuthorization, + String aaiPathToSearchNodeQuery, + String aaiPathToCustomerQuery, + String serviceInstanceId, + String transactionId)throws AuditException { + + String obtainResourceLink_url = generateUrl_ForResourceLink(aaiBaseUrl, aaiPathToSearchNodeQuery, serviceInstanceId); + String aaiResourceData = getAaiResource(aaiClient, obtainResourceLink_url, aaiBasicAuthorization, transactionId, MediaType.valueOf(MediaType.APPLICATION_JSON)); + + // Handle the case if the service instance is not found in AAI + if (isEmptyJson(aaiResourceData)) { + log.info(" Service Instance {} is not found from AAI", serviceInstanceId); + // Only return the empty Json on the root level. i.e service instance + return null; + } + + String resourceLink = extractResourceLinkBasedOnServiceInstance(aaiResourceData, CATALOG_SERVICE_INSTANCE); + + ServiceEntity serviceEntityObj = createServiceEntityObj (resourceLink); //customerId and serviceType are updated here. + if (serviceEntityObj != null) { + serviceEntityObj.setServiceInstanceId(serviceInstanceId); + String customerId = serviceEntityObj.getCustomerId(); + // Obtain customerType and customerName + String obtainCustomer_url = generateUrl_ForCustomer (aaiBaseUrl, aaiPathToCustomerQuery, customerId); + String aaiCustomerData = getAaiResource(aaiClient, obtainCustomer_url, aaiBasicAuthorization, transactionId, MediaType.valueOf(MediaType.APPLICATION_JSON)); + if (isEmptyJson(aaiCustomerData)) { + log.info(" Customer name {} is not found from AAI", customerId); + // Only return the empty Json on the root level. i.e service instance + throw new AuditException(INTERNAL_SERVER_ERROR + ": Customer ID cannot be found from AAI :" + customerId); + } + // Update customerType and customerName to the existing serviceEntityObj + updateServiceEntityObj ( aaiCustomerData , serviceEntityObj); + } + + return serviceEntityObj; + } + + private static boolean isEmptyJson(String serviceInstancePayload) { + return (serviceInstancePayload.equals(EMPTY_JSON_STRING)); + } + + private static String generateUrl_ForResourceLink (String aaiBaseURL, String aaiPathToSearchNodeQuery ,String serviceInstanceId) { + return aaiBaseURL + aaiPathToSearchNodeQuery + serviceInstanceId; + } + + private static String generateUrl_ForCustomer (String aaiBaseURL, String aaiPathToCustomerQuery ,String customerId) { + return aaiBaseURL + aaiPathToCustomerQuery + customerId; + } + + @SuppressWarnings("unchecked") + private static Map<String, List<String>> buildHeaders(String aaiBasicAuthorization, String transactionId) { + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + headers.put(TRANSACTION_ID, Collections.singletonList(transactionId)); + headers.put(FROM_APP_ID, Collections.singletonList(APP_NAME)); + headers.put(AUTHORIZATION, Collections.singletonList(aaiBasicAuthorization)); + return headers; + } + + private static String getAaiResource(RestClient client, String url, String aaiBasicAuthorization, String transId, MediaType mediaType) + throws AuditException { + OperationResult result = client.get(url, buildHeaders(aaiBasicAuthorization, transId), MediaType.valueOf(MediaType.APPLICATION_JSON)); + + if (result.getResultCode() == 200) { + return result.getResult(); + } else if (result.getResultCode() == 404) { + // Resource not found, generate empty JSON format + log.info("Resource for {} is not found: {}", url, "return empty Json format"); + return new JSONObject().toString(); + + } else { + throw new AuditException(INTERNAL_SERVER_ERROR + " with " + result.getFailureCause()); + } + } + + /* + * Extract the resource-Link from Json payload. For example + * { + * "result-data": [ + * { + * "resource-type": "service-instance", + * "resource-link": "/aai/v11/business/customers/customer/DemoCust_651800ed-2a3c-45f5-b920-85c1ed155fc2/service-subscriptions/service-subscription/vFW/service-instances/service-instance/adc3cc2a-c73e-414f-8ddb-367de81300cb" + * } + * ] + * } + */ + private static String extractResourceLinkBasedOnServiceInstance(String payload, String catalog) throws AuditException { + String resourceLink = null; + log.info("Fetching the resource-link based on resource-type=" + catalog); + + try { + JSONArray result_data_list = new JSONObject(payload).getJSONArray(RESULT_DATA); + if (result_data_list != null) { + for (int i = 0; i < result_data_list.length(); i++) { + JSONObject obj = result_data_list.optJSONObject(i); + if (obj.has(JSON_ATT_RESOURCE_TYPE) && (obj.getString(JSON_ATT_RESOURCE_TYPE).equals(catalog) )) { + resourceLink = obj.getString(JSON_ATT_RESOURCE_LINK); + log.info(resourceLink); + return resourceLink; + } + } + } + } catch (JSONException e) { + log.error(e.getMessage()); + throw new AuditException("Json Reader Parse Error " + e.getMessage()); + } + + log.warn("resource-link CANNOT be found: ", payload ); + + return resourceLink; + } + + /* + * Extract the "subscriber-name" and "subscriber-type" from Json payload. For example + * { + * "global-customer-id": "OttoonMorph36", + * "subscriber-name": "OttoonMorph36", + * "subscriber-type": "CUST", + * "resource-version": "1526324315029" + * } + */ + private static void updateServiceEntityObj(String payload, ServiceEntity serviceEntityObj) throws AuditException { + if (serviceEntityObj == null ) { + throw new AuditException("null pointer serviceEntityObj"); + } + String customerId = serviceEntityObj.getCustomerId(); + log.info("Fetching the subscriber type based on customer-id = " + customerId); + + try { + JSONObject obj = new JSONObject (payload); + if (obj.has(JSON_ATT_GLOBAL_CUSTOMER_ID) && (obj.getString(JSON_ATT_GLOBAL_CUSTOMER_ID).equals(customerId) )) { + serviceEntityObj.setCustomerType(obj.getString(JSON_ATT_SUBSCRIBER_TYPE)); + serviceEntityObj.setCustomerName(obj.getString(JSON_ATT_SUBSCRIBER_NAME)); + return; + } + } catch (JSONException e) { + log.error(e.getMessage()); + throw new AuditException("Json Reader Parse Error " + e.getMessage()); + } + + log.warn("subscriberType (" + customerId + ") CANNOT be found: ", payload ); + + return; + } + + /* Look for "/customers/customer/" and "/service-subscriptions/service-subscription/" in resourceLink + * and find the customer id and service type. + * For example, + * "/aai/v11/business/customers/customer/DemoCust_651800ed-2a3c-45f5-b920-85c1ed155fc2/service-subscriptions/service-subscription/vFW/service-instances/service-instance/adc3cc2a-c73e-414f-8ddb-367de81300cb" + * customerId = DemoCust_651800ed-2a3c-45f5-b920-85c1ed155fc2 + * serviceType = vFW + * */ + + private static ServiceEntity createServiceEntityObj (String resourceLink) { + int customer_id_idx = resourceLink.indexOf(CUSTOMER_ID_STRING); + int service_type_idx = resourceLink.indexOf(SERVICE_TYPE_STRING); + + if (( customer_id_idx < 0 ) || ( service_type_idx < 0 )) { + return null; + } + + ServiceEntity serviceEntity = new ServiceEntity (); + serviceEntity.setCustomerId(abstractStrInfo(resourceLink, CUSTOMER_ID_STRING)); + serviceEntity.setServiceType(abstractStrInfo(resourceLink, SERVICE_TYPE_STRING)); + return serviceEntity; + } + + private static String abstractStrInfo (String origStr, String matchStr) { + String after = origStr.substring( origStr.indexOf(matchStr) + matchStr.length()); + + return after.substring(0, after.indexOf(FORWARD_SLASH)); + } } |