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.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.http.HttpStatus;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.onap.so.client.exception.MapperException;
31 import org.onap.so.logger.MessageEnum;
32 import org.onap.so.logger.MsoLogger;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
35 import org.springframework.stereotype.Component;
36 import org.springframework.util.CollectionUtils;
38 import com.fasterxml.jackson.annotation.JsonInclude;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
43 public class SdnCommonTasks {
45 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, SDNCClient.class);
46 private static final String RESPONSE_CODE = "response-code";
47 private static final String RESPONSE_MESSAGE = "response-message";
48 private static final String NO_RESPONSE_FROM_SDNC = "Error did not receive a response from SDNC.";
49 private static final String BAD_RESPONSE_FROM_SDNC = "Error received a bad response from SDNC.";
50 private static final String SDNC_CODE_NOT_0_OR_IN_200_299 = "Error from SDNC: %s";
51 private static final String COULD_NOT_CONVERT_SDNC_POJO_TO_JSON = "ERROR: Could not convert SDNC pojo to json string.";
57 * @throws MapperException
59 public String buildJsonRequest(Object request) throws MapperException {
61 ObjectMapper objMapper = new ObjectMapper();
62 objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
64 jsonRequest = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);
65 } catch (JsonProcessingException e) {
66 msoLogger.error(MessageEnum.JAXB_EXCEPTION, COULD_NOT_CONVERT_SDNC_POJO_TO_JSON, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, e.getMessage());
67 throw new MapperException(COULD_NOT_CONVERT_SDNC_POJO_TO_JSON);
69 jsonRequest = "{\"input\":" + jsonRequest + "}";
70 msoLogger.info(jsonRequest);
79 public HttpHeaders getHttpHeaders(String auth) {
80 HttpHeaders httpHeader = new HttpHeaders();
81 httpHeader.set("Authorization", auth);
82 httpHeader.setContentType(MediaType.APPLICATION_JSON);
83 List<MediaType> acceptMediaTypes = new ArrayList<MediaType>();
84 acceptMediaTypes.add(MediaType.APPLICATION_JSON);
85 httpHeader.setAccept(acceptMediaTypes);
93 * @throws BadResponseException
95 public String validateSDNResponse(LinkedHashMap<String, Object> output) throws BadResponseException {
96 if (CollectionUtils.isEmpty(output)) {
97 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, NO_RESPONSE_FROM_SDNC, "BPMN",
98 MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_SDNC);
99 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
101 LinkedHashMap<String, Object> embeddedResponse =(LinkedHashMap<String, Object>) output.get("output");
102 String responseCode = "";
103 String responseMessage = "";
104 if (embeddedResponse != null) {
105 responseCode = (String) embeddedResponse.get(RESPONSE_CODE);
106 responseMessage = (String) embeddedResponse.get(RESPONSE_MESSAGE);
108 ObjectMapper objMapper = new ObjectMapper();
111 jsonResponse = objMapper.writeValueAsString(output);
112 msoLogger.debug(jsonResponse);
113 } catch (JsonProcessingException e) {
114 msoLogger.warnSimple("Could not convert SDNC Response to String", e);
117 msoLogger.info("ResponseCode: " + responseCode + " ResponseMessage: " + responseMessage);
118 int code = StringUtils.isNotEmpty(responseCode) ? Integer.parseInt(responseCode) : 0;
119 if (isHttpCodeSuccess(code)) {
120 msoLogger.info("Successful Response from SDNC");
123 String errorMessage = String.format(SDNC_CODE_NOT_0_OR_IN_200_299, responseMessage);
124 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, errorMessage, "BPMN", MsoLogger.getServiceName(),
125 MsoLogger.ErrorCode.DataError, errorMessage);
126 throw new BadResponseException(errorMessage);
134 * @throws BadResponseException
136 public String validateSDNGetResponse(LinkedHashMap<String, Object> output) throws BadResponseException {
137 if (CollectionUtils.isEmpty(output)) {
138 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, NO_RESPONSE_FROM_SDNC, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, NO_RESPONSE_FROM_SDNC);
139 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
141 ObjectMapper objMapper = new ObjectMapper();
142 msoLogger.debug("Using object mapper");
143 String stringOutput = "";
145 stringOutput = objMapper.writeValueAsString(output);
147 catch (Exception e) {
148 msoLogger.error(MessageEnum.RA_RESPONSE_FROM_SDNC, BAD_RESPONSE_FROM_SDNC, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, BAD_RESPONSE_FROM_SDNC);
149 throw new BadResponseException(BAD_RESPONSE_FROM_SDNC);
151 msoLogger.debug("Received from GET request: " + stringOutput);
156 private boolean isHttpCodeSuccess(int code) {
157 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;