Changed to unmaintained
[appc.git] / appc-event-listener / 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 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
39
40 public class Converter {
41
42     private Converter() {
43     }
44
45     public static DmaapOutgoingMessage convertJsonNodeToDmaapOutgoingMessage(DmaapMessage event, JsonNode inObj){
46
47         if (event == null || inObj == null) {
48             throw new IllegalArgumentException("One of given arguments is null");
49         }
50
51         DmaapOutgoingMessage outObj = new DmaapOutgoingMessage();
52         outObj.setBody(inObj);
53         outObj.setRpcName(event.getRpcName());
54         outObj.setVersion(event.getVersion());
55         outObj.setType("response");
56         outObj.setCorrelationID(event.getCorrelationID());
57         return outObj;
58     }
59
60     public static String convertDmaapOutgoingMessageToJsonString(DmaapMessage inObj) throws JsonProcessingException {
61
62         if (inObj == null)
63             throw new IllegalArgumentException("Input message is null");
64
65
66         ObjectMapper objectMapper = new ObjectMapper();
67         ObjectWriter writer = objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
68             .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)
69             .writer().withFeatures(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS);
70         return writer.writeValueAsString(inObj);
71
72     }
73
74     public static DmaapOutgoingMessage buildDmaapOutgoingMessageWithUnexpectedError(DmaapMessage event,
75         Exception inputException) {
76
77         if (event == null || inputException == null) {
78             throw new IllegalArgumentException("One of given arguments is null");
79         }
80
81         DmaapOutgoingMessage dmaapOutgoingMessage;
82         String errMsg =
83             StringUtils.isEmpty(inputException.getMessage()) ? inputException.toString() : inputException.getMessage();
84         JSONObject commonHeaderJsonObject = Mapper.toJsonObject(event.getBody().get("input").get("common-header"));
85         JSONObject jsonObjectOutput = new JSONObject().accumulate("common-header", commonHeaderJsonObject)
86             .accumulate("status", new JSONObject().accumulate("code", 200).accumulate("value", errMsg));
87         dmaapOutgoingMessage = new DmaapOutgoingMessage();
88         dmaapOutgoingMessage.setRpcName(event.getRpcName());
89         dmaapOutgoingMessage.setCorrelationID(event.getCorrelationID());
90         dmaapOutgoingMessage.setType("error");
91         dmaapOutgoingMessage.setVersion(event.getVersion());
92         JSONObject jsonObjectBody = new JSONObject().accumulate("output", jsonObjectOutput);
93         JsonNode jsonNodeBody = Mapper.toJsonNodeFromJsonString(jsonObjectBody.toString());
94         dmaapOutgoingMessage.setBody(jsonNodeBody);
95         return dmaapOutgoingMessage;
96     }
97
98     public static String extractRequestIdWithSubId(JsonNode dmaapBody) {
99
100         if (dmaapBody == null) {
101             throw new IllegalArgumentException("Dmaap body is null");
102         }
103
104         JsonNode commonHeaderJsonNode = dmaapBody.get("input").get("common-header");
105         String requestId = getValue(commonHeaderJsonNode, "request-id", "");
106         String subRequestId = getValue(commonHeaderJsonNode, "sub-request-id", "");
107         if (!StringUtils.isEmpty(subRequestId)) {
108             requestId = requestId + "-" + subRequestId;
109         }
110         return requestId;
111     }
112
113     public static Integer extractStatusCode(JsonNode event) {
114
115         if (event == null){
116             throw new IllegalArgumentException("Input event is null");
117         }
118
119         Integer statusCode;
120         statusCode = event.get("output").get("status").get("code").asInt();
121         return statusCode;
122     }
123
124     private static String getValue(JsonNode jsonNode, String name, String defaultValue) {
125         if (jsonNode == null) {
126             return defaultValue;
127         }
128         JsonNode childJsonNode = jsonNode.get(name);
129         if (childJsonNode == null) {
130             return defaultValue;
131         }
132         String value = childJsonNode.asText();
133         if (value == null) {
134             return defaultValue;
135         }
136         return value;
137     }
138
139 }