Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / sdnc / SdnCommonTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.sdnc;
22
23 import java.util.LinkedHashMap;
24
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;
36
37 import com.fasterxml.jackson.annotation.JsonInclude;
38 import com.fasterxml.jackson.core.JsonProcessingException;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 @Component
42 public class SdnCommonTasks {
43
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.";
51
52     /***
53      * 
54      * @param request
55      * @return
56      * @throws MapperException
57      */
58     public String buildJsonRequest(Object request) throws MapperException {
59         String jsonRequest;
60         ObjectMapper objMapper = new ObjectMapper();
61         objMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
62         try {
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);
67         }
68         jsonRequest = "{\"input\":" + jsonRequest + "}";
69         msoLogger.info(jsonRequest);
70         return jsonRequest;
71     }
72
73     /***
74      * 
75      * @param auth
76      * @return
77      */
78     public HttpHeaders getHttpHeaders(String auth) {
79         HttpHeaders httpHeader = new HttpHeaders();
80         httpHeader.set("Authorization", auth);
81         httpHeader.setContentType(MediaType.APPLICATION_JSON);
82         return httpHeader;
83     }
84
85     /***
86      * 
87      * @param output
88      * @return
89      * @throws BadResponseException
90      */
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);
95         }
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;
103         } else {
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);
107         }
108     }
109     
110     /***
111      * 
112      * @param output
113      * @return
114      * @throws BadResponseException
115      */
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);
120         }
121         ObjectMapper objMapper = new ObjectMapper();
122         msoLogger.debug("Using object mapper");
123         String stringOutput = "";
124         try {
125                 stringOutput = objMapper.writeValueAsString(output);
126         }
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);
130         }
131         msoLogger.debug("Received from GET request: " + stringOutput);
132         return stringOutput;
133     }
134
135     private boolean isHttpCodeSuccess(int code) {
136         return code >= HttpStatus.SC_OK && code < HttpStatus.SC_MULTIPLE_CHOICES || code == 0;
137     }
138 }