Search in sources :

Example 1 with HttpClient

use of org.onap.so.client.HttpClient in project so by onap.

the class SDCClientHelper method postActivateOperationalEnvironment.

/**
 * Send POST request to SDC for operational activation
 *
 * @param serviceModelVersionI - String
 * @param operationalEnvironmentId - String
 * @param workloadContext - String
 * @return sdcResponseJsonObj - JSONObject object
 * @throws JSONException
 */
public JSONObject postActivateOperationalEnvironment(String serviceModelVersionId, String operationalEnvironmentId, String workloadContext) throws ApiException {
    JSONObject sdcResponseJsonObj = new JSONObject();
    try {
        String urlString = this.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
        String jsonPayload = this.buildJsonWorkloadContext(workloadContext);
        String basicAuthCred = getBasicAuth();
        if (basicAuthCred == null || "".equals(basicAuthCred)) {
            ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.BusinessProcessError).build();
            throw new ValidateException.Builder(" SDC credentials 'mso.sdc.client.auth' not setup in properties file!", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_DETAILED_SERVICE_ERROR).errorInfo(errorLoggerInfo).build();
        }
        URL url = new URL(urlString);
        HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
        httpClient.addBasicAuthHeader(sdcClientAuth, msoKey);
        httpClient.addAdditionalHeader("X-ECOMP-InstanceID", sdcActivateInstanceId);
        httpClient.addAdditionalHeader("Content-Type", SDCClientHelper.SDC_CONTENT_TYPE);
        httpClient.addAdditionalHeader("Accept", SDCClientHelper.SDC_ACCEPT_TYPE);
        httpClient.addAdditionalHeader("USER_ID", sdcActivateUserId);
        Response apiResponse = setHttpPostResponse(httpClient, jsonPayload);
        int statusCode = apiResponse.getStatus();
        ;
        String responseData = apiResponse.readEntity(String.class);
        sdcResponseJsonObj = enhanceJsonResponse(new JSONObject(responseData), statusCode);
    } catch (Exception ex) {
        logger.debug("calling SDC Exception message:", ex);
        String errorMessage = " Encountered Error while calling SDC POST Activate. " + ex.getMessage();
        logger.debug(errorMessage);
        sdcResponseJsonObj.put(STATUS_CODE, String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
        sdcResponseJsonObj.put(MESSAGE_ID, "");
        sdcResponseJsonObj.put(MESSAGE, errorMessage);
    }
    return sdcResponseJsonObj;
}
Also used : ErrorLoggerInfo(org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo) Response(javax.ws.rs.core.Response) JSONObject(org.json.JSONObject) UriBuilder(javax.ws.rs.core.UriBuilder) HttpClient(org.onap.so.client.HttpClient) URL(java.net.URL) ApiException(org.onap.so.apihandlerinfra.exceptions.ApiException) ValidateException(org.onap.so.apihandlerinfra.exceptions.ValidateException) JSONException(org.json.JSONException)

Example 2 with HttpClient

use of org.onap.so.client.HttpClient in project so by onap.

the class ActivitySpecsActions method createActivitySpec.

public String createActivitySpec(String hostname, ActivitySpec activitySpec) {
    if (activitySpec == null) {
        return null;
    }
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
        String payload = mapper.writer().writeValueAsString(activitySpec);
        String urlString = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
        URL url = new URL(urlString);
        HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
        httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
        Response response = httpClient.post(payload);
        int statusCode = response.getStatus();
        if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
            logger.warn(LoggingAnchor.THREE, "ActivitySpec", activitySpec.getName(), "already exists in SDC");
            return null;
        }
        if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
            logger.warn(LoggingAnchor.THREE, "Error creating activity spec", activitySpec.getName(), statusCode);
            return null;
        }
        if (response.getEntity() == null) {
            logger.warn(LoggingAnchor.TWO, "No activity spec response returned", activitySpec.getName());
            return null;
        }
        ActivitySpecCreateResponse activitySpecCreateResponse = response.readEntity(ActivitySpecCreateResponse.class);
        if (activitySpecCreateResponse == null) {
            logger.warn(LoggingAnchor.TWO, "Unable to read activity spec", activitySpec.getName());
            return null;
        }
        return activitySpecCreateResponse.getId();
    } catch (Exception e) {
        logger.warn(LoggingAnchor.TWO, "Exception creating activitySpec", e);
    }
    return null;
}
Also used : ActivitySpecCreateResponse(org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse) Response(javax.ws.rs.core.Response) ActivitySpecCreateResponse(org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse) HttpClient(org.onap.so.client.HttpClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) URL(java.net.URL)

Example 3 with HttpClient

use of org.onap.so.client.HttpClient in project so by onap.

the class ActivitySpecsActions method certifyActivitySpec.

public boolean certifyActivitySpec(String hostname, String activitySpecId) {
    if (activitySpecId == null) {
        return false;
    }
    try {
        String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
        String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
        URL url = new URL(urlString);
        HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
        httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
        Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
        int statusCode = response.getStatus();
        if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
            logger.warn(LoggingAnchor.THREE, "ActivitySpec with id", activitySpecId, "is already certified in SDC");
            return false;
        }
        if (statusCode != HttpStatus.SC_OK) {
            logger.warn(LoggingAnchor.THREE, "Error certifying activity", activitySpecId, statusCode);
            return false;
        }
        return true;
    } catch (Exception e) {
        logger.warn(LoggingAnchor.TWO, "Exception certifying activitySpec", e);
        return false;
    }
}
Also used : ActivitySpecCreateResponse(org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse) Response(javax.ws.rs.core.Response) HttpClient(org.onap.so.client.HttpClient) URL(java.net.URL)

Example 4 with HttpClient

use of org.onap.so.client.HttpClient in project so by onap.

the class ResourceRequestBuilder method getServiceInstnace.

public static Map<String, Object> getServiceInstnace(String uuid) throws Exception {
    String catalogEndPoint = UrnPropertiesReader.getVariable("mso.catalog.db.endpoint");
    HttpClient client = new HttpClientFactory().newJsonClient(UriBuilder.fromUri(catalogEndPoint).path(SERVICE_URL_SERVICE_INSTANCE).queryParam("serviceModelUuid", uuid).build().toURL(), ONAPComponents.CATALOG_DB);
    client.addAdditionalHeader("Accept", "application/json");
    client.addAdditionalHeader("Authorization", UrnPropertiesReader.getVariable("mso.db.auth"));
    Response apiResponse = client.get();
    String value = apiResponse.readEntity(String.class);
    ObjectMapper objectMapper = new ObjectMapper();
    HashMap<String, Object> map = objectMapper.readValue(value, HashMap.class);
    return map;
}
Also used : Response(javax.ws.rs.core.Response) HttpClient(org.onap.so.client.HttpClient) HttpClientFactory(org.onap.so.client.HttpClientFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

Response (javax.ws.rs.core.Response)4 HttpClient (org.onap.so.client.HttpClient)4 URL (java.net.URL)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ActivitySpecCreateResponse (org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse)2 UriBuilder (javax.ws.rs.core.UriBuilder)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 ApiException (org.onap.so.apihandlerinfra.exceptions.ApiException)1 ValidateException (org.onap.so.apihandlerinfra.exceptions.ValidateException)1 ErrorLoggerInfo (org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo)1 HttpClientFactory (org.onap.so.client.HttpClientFactory)1