2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.client.sdnc;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27 import java.util.List;
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.http.HttpStatus;
30 import org.onap.so.client.exception.BadResponseException;
31 import org.onap.so.client.exception.MapperException;
32 import org.onap.so.logger.ErrorCode;
33 import org.onap.so.logger.MessageEnum;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.http.HttpHeaders;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Component;
39 import org.springframework.util.CollectionUtils;
40 import com.fasterxml.jackson.annotation.JsonInclude;
41 import com.fasterxml.jackson.core.JsonProcessingException;
42 import com.fasterxml.jackson.databind.ObjectMapper;
45 public class SdnCommonTasks {
47 private static final Logger logger = LoggerFactory.getLogger(SDNCClient.class);
48 private static final String RESPONSE_CODE = "response-code";
49 private static final String RESPONSE_MESSAGE = "response-message";
50 private static final String NO_RESPONSE_FROM_SDNC = "Error did not receive a response from SDNC.";
51 private static final String BAD_RESPONSE_FROM_SDNC = "Error received a bad response from SDNC.";
52 private static final String SDNC_CODE_NOT_0_OR_IN_200_299 = "Error from SDNC: %s";
53 private static final String COULD_NOT_CONVERT_SDNC_POJO_TO_JSON =
54 "ERROR: Could not convert SDNC pojo to json string.";
55 private static final String BRACKETS = "{} {} {} {} {}";
61 * @throws MapperException
63 public String buildJsonRequest(Object request) throws MapperException {
65 ObjectMapper objMapper = new ObjectMapper();
66 objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
68 jsonRequest = objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(request);
69 } catch (JsonProcessingException e) {
70 logger.error(BRACKETS, MessageEnum.JAXB_EXCEPTION.toString(), COULD_NOT_CONVERT_SDNC_POJO_TO_JSON, "BPMN",
71 ErrorCode.DataError.getValue(), e.getMessage());
72 throw new MapperException(COULD_NOT_CONVERT_SDNC_POJO_TO_JSON);
74 jsonRequest = "{\"input\":" + jsonRequest + "}";
75 logger.info(jsonRequest);
84 public HttpHeaders getHttpHeaders(String auth) {
85 HttpHeaders httpHeader = new HttpHeaders();
86 httpHeader.set("Authorization", auth);
87 httpHeader.setContentType(MediaType.APPLICATION_JSON);
88 List<MediaType> acceptMediaTypes = new ArrayList<>();
89 acceptMediaTypes.add(MediaType.APPLICATION_JSON);
90 httpHeader.setAccept(acceptMediaTypes);
98 * @throws BadResponseException
100 public String validateSDNResponse(LinkedHashMap<String, Object> output) throws BadResponseException {
101 if (CollectionUtils.isEmpty(output)) {
102 logger.error(BRACKETS, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), NO_RESPONSE_FROM_SDNC, "BPMN",
103 ErrorCode.UnknownError.getValue(), NO_RESPONSE_FROM_SDNC);
104 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
106 LinkedHashMap<String, Object> embeddedResponse = (LinkedHashMap<String, Object>) output.get("output");
107 String responseCode = "";
108 String responseMessage = "";
109 if (embeddedResponse != null) {
110 responseCode = (String) embeddedResponse.get(RESPONSE_CODE);
111 responseMessage = (String) embeddedResponse.get(RESPONSE_MESSAGE);
113 ObjectMapper objMapper = new ObjectMapper();
116 jsonResponse = objMapper.writeValueAsString(output);
117 logger.debug(jsonResponse);
118 } catch (JsonProcessingException e) {
119 logger.warn("Could not convert SDNC Response to String", e);
122 logger.info("ResponseCode: {} ResponseMessage: {}", responseCode, responseMessage);
123 int code = StringUtils.isNotEmpty(responseCode) ? Integer.parseInt(responseCode) : 0;
124 if (isHttpCodeSuccess(code)) {
125 logger.info("Successful Response from SDNC");
128 String errorMessage = String.format(SDNC_CODE_NOT_0_OR_IN_200_299, responseMessage);
129 logger.error(BRACKETS, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), errorMessage, "BPMN",
130 ErrorCode.DataError.getValue(), errorMessage);
131 throw new BadResponseException(errorMessage);
139 * @throws BadResponseException
141 public String validateSDNGetResponse(LinkedHashMap<String, Object> output) throws BadResponseException {
142 if (CollectionUtils.isEmpty(output)) {
143 logger.error(BRACKETS, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), NO_RESPONSE_FROM_SDNC, "BPMN",
144 ErrorCode.UnknownError.getValue(), NO_RESPONSE_FROM_SDNC);
145 throw new BadResponseException(NO_RESPONSE_FROM_SDNC);
147 ObjectMapper objMapper = new ObjectMapper();
148 logger.debug("Using object mapper");
149 String stringOutput = "";
151 stringOutput = objMapper.writeValueAsString(output);
152 } catch (Exception e) {
153 logger.error(BRACKETS, MessageEnum.RA_RESPONSE_FROM_SDNC.toString(), BAD_RESPONSE_FROM_SDNC, "BPMN",
154 ErrorCode.UnknownError.getValue(), BAD_RESPONSE_FROM_SDNC);
155 throw new BadResponseException(BAD_RESPONSE_FROM_SDNC);
157 logger.debug("Received from GET request: {}", stringOutput);
162 private boolean isHttpCodeSuccess(int code) {
163 return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;