lowered code smells
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / LCM / conv / Converter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.listener.LCM.conv;
25
26 import com.fasterxml.jackson.annotation.JsonInclude;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.MapperFeature;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.fasterxml.jackson.databind.ObjectWriter;
32 import com.fasterxml.jackson.databind.SerializationFeature;
33 import org.apache.commons.lang3.StringUtils;
34 import org.json.JSONObject;
35 import org.onap.appc.listener.LCM.model.DmaapMessage;
36 import org.onap.appc.listener.LCM.model.DmaapOutgoingMessage;
37 import org.onap.appc.listener.util.Mapper;
38
39 public class Converter {
40
41     private Converter() {
42     }
43
44     public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
45
46         if (event == null || inObj == null) {
47             throw new IllegalArgumentException("One of given arguments is null");
48         }
49
50         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
51         outObj.setBody(inObj);
52         outObj.setRpcName(event.getRpcName());
53         outObj.setVersion(event.getVersion());
54         outObj.setType("response");
55         outObj.setCorrelationID(event.getCorrelationID());
56         return outObj;
57     }
58
59     public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
60
61         if (inObj == null)
62             throw new IllegalArgumentException("Input message is null");
63
64
65         ObjectMapper objectMapper = new ObjectMapper();
66         ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
67             .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
68             .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
69         return writer.writeValueAsString(inObj);
70
71     }
72
73     public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
74         Exception inputException) {
75
76         if (event == null || inputException == null) {
77             throw new IllegalArgumentException("One of given arguments is null");
78         }
79
80         DmaapOutgoingMessage dmaapOutgoingMessage;
81         String errMsg =
82             StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
83         JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
84         JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
85             .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
86         dmaapOutgoingMessage = new DmaapOutgoingMessage();
87         dmaapOutgoingMessage.setRpcName(event.getRpcName());
88         dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
89         dmaapOutgoingMessage.setType("error");
90         dmaapOutgoingMessage.setVersion(event.getVersion());
91         JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
92         JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
93         dmaapOutgoingMessage.setBody(jsonNodeBody);
94         return dmaapOutgoingMessage;
95     }
96
97     public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
98
99         if (dmaapBody == null) {
100             throw new IllegalArgumentException("Dmaap body is null");
101         }
102
103         JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
104         String requestId = getValue(commonHeaderJsonNode, "request-id", "");
105         String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
106         if (!StringUtils.isEmpty(subRequestId)) {
107             requestId = requestId + "-" + subRequestId;
108         }
109         return requestId;
110     }
111
112     public static Integer extractStatusCode(JsonNode event) {
113
114         if (event == null){
115             throw new IllegalArgumentException("Input event is null");
116         }
117
118         Integer statusCode;
119         statusCode = event.get("output").get("status").get("code").asInt();
120         return statusCode;
121     }
122
123     private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
124         if (jsonNode == null) {
125             return defaultValue;
126         }
127         JsonNode childJsonNode = jsonNode.get(name);
128         if (childJsonNode == null) {
129             return defaultValue;
130         }
131         String value = childJsonNode.asText();
132         if (value == null) {
133             return defaultValue;
134         }
135         return value;
136     }
137
138 }