2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.sdnc;
23 import java.util.LinkedHashMap;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.http.HttpStatus;
27 import org.json.JSONObject;
28 import org.onap.so.client.exception.BadResponseException;
29 import org.onap.so.client.exception.MapperException;
30 import org.onap.so.logger.MessageEnum;
31 import org.onap.so.logger.MsoLogger;
32 import org.springframework.http.HttpHeaders;
33 import org.springframework.http.MediaType;
34 import org.springframework.stereotype.Component;
35 import org.springframework.util.CollectionUtils;
37 import com.fasterxml.jackson.annotation.JsonInclude;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
42 public class SdnCommonTasks {
44 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCClient.class);
45 private static final String RESPONSE_CODE = "response-code";
46 private static final String RESPONSE_MESSAGE = "response-message";
47 private static final String NO_RESPONSE_FROM_SDNC = "Error did not receive a response from SDNC.";
48 private static final String BAD_RESPONSE_FROM_SDNC = "Error received a bad response from SDNC.";
49 private static final String SDNC_CODE_NOT_0_OR_IN_200_299 = "Error from SDNC: %s";
50 private static final String COULD_NOT_CONVERT_SDNC_POJO_TO_JSON = "ERROR: Could not convert SDNC pojo to json string.";
56 * @throws MapperException
58 public String buildJsonRequest(Object request) throws MapperException {
60 ObjectMapper objMapper = new ObjectMapper();
61 objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
63 jsonRequest = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);
64 } catch (JsonProcessingException e) {
65 msoLogger.error(MessageEnum.JAXB_EXCEPTION, COULD_NOT_CONVERT_SDNC_POJO_TO_JSON, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, e.getMessage());
66 throw new MapperException(COULD_NOT_CONVERT_SDNC_POJO_TO_JSON);
68 jsonRequest = "{\"input\":" + jsonRequest + "}";
69 msoLogger.info(jsonRequest);
78 public HttpHeaders getHttpHeaders(String auth) {
79 HttpHeaders httpHeader = new HttpHeaders();
80 httpHeader.set("Authorization", auth);
81 httpHeader.setContentType(MediaType.APPLICATION_JSON);
89 * @throws BadResponseException
91 public String validateSDNResponse(LinkedHashMap<?, ?> output) throws BadResponseException {
92 if (CollectionUtils.isEmpty(output)) {
93 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, NO_RESPONSE_FROM_SDNC, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_SDNC);
94 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
96 String responseCode = (String) output.get(RESPONSE_CODE);
97 String responseMessage = (String) output.get(RESPONSE_MESSAGE);
98 msoLogger.info("ResponseCode: " + responseCode + " ResponseMessage: " + responseMessage);
99 int code = StringUtils.isNotEmpty(responseCode) ? Integer.parseInt(responseCode) : 0;
100 if (isHttpCodeSuccess(code)) {
101 msoLogger.info("Successful Response from SDNC");
102 return responseMessage;
104 String errorMessage = String.format(SDNC_CODE_NOT_0_OR_IN_200_299, responseMessage);
105 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, errorMessage, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, errorMessage);
106 throw new BadResponseException(errorMessage);
114 * @throws BadResponseException
116 public String validateSDNGetResponse(LinkedHashMap<?, ?> output) throws BadResponseException {
117 if (CollectionUtils.isEmpty(output)) {
118 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, NO_RESPONSE_FROM_SDNC, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_SDNC);
119 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
121 ObjectMapper objMapper = new ObjectMapper();
122 msoLogger.debug("Using object mapper");
123 String stringOutput = "";
125 stringOutput = objMapper.writeValueAsString(output);
127 catch (Exception e) {
128 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, BAD_RESPONSE_FROM_SDNC, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, BAD_RESPONSE_FROM_SDNC);
129 throw new BadResponseException(BAD_RESPONSE_FROM_SDNC);
131 msoLogger.debug("Received from GET request: " + stringOutput);
135 private boolean isHttpCodeSuccess(int code) {
136 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;