Changed to unmaintained
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / util / Mapper.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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.listener.util;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.json.JSONObject;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 public class Mapper {
38
39     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
40
41     private static ObjectMapper mapper = new ObjectMapper();
42
43     /**
44      * @return The object mapper that we are using.
45      */
46     public static ObjectMapper getMapper() {
47         return mapper;
48     }
49
50     /**
51      * Convert a String to a DcaeMessage
52      * 
53      * @param data
54      *            The json string to try and parse
55      * @return A DcaeMessage from the json string or null if it could not
56      */
57     public static <T> T mapOne(String data, Class<T> cls) {
58         try {
59             return mapper.readValue(data, cls);
60         } catch (Exception e) {
61             LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
62             return null;
63         }
64     }
65
66     public static <T> List<T> mapList(List<String> data, Class<T> cls) {
67         List<T> out = new ArrayList<T>();
68         for (String s : data) {
69             T tmp = Mapper.mapOne(s, cls);
70             if (tmp != null) {
71                 out.add(tmp);
72             }
73         }
74         return out;
75     }
76
77     /**
78      * Convenience method to try and convert objects to json String
79      * 
80      * @param obj
81      *            The object to try and convert
82      * @return A json string representing the object or null if it could not be converted
83      */
84     public static String toJsonString(Object obj) {
85         String jsonStr;
86         try {
87             if (obj instanceof JSONObject) {
88                 jsonStr = obj.toString();
89             }else {
90                 jsonStr = mapper.writeValueAsString(obj);
91             }
92             return jsonStr;
93         } catch (Exception e) {
94             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
95             return null;
96         }
97     }
98
99     public static JSONObject toJsonObject(Object obj) {
100         String jsonStr;
101         try {
102             if (obj.getClass().equals(String.class)) {
103                 jsonStr = (String) obj;
104             } else {
105                 jsonStr = mapper.writeValueAsString(obj);
106             }
107             return new JSONObject(jsonStr);
108         } catch (Exception e) {
109             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
110             return null;
111         }
112     }
113     public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
114         JsonNode jsonNode = null;
115         if(jsonStr != null) {
116             try {
117                 jsonNode = mapper.readTree(jsonStr);
118             } catch (IOException e) {
119                 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
120             }
121         }
122         return jsonNode;
123     }
124     public static JsonNode toJsonNode(Object obj) {
125         JsonNode jsonNode = null;
126         String jsonStr = toJsonString(obj);
127         if(jsonStr != null) {
128             try {
129                 jsonNode = mapper.readTree(jsonStr);
130             } catch (IOException e) {
131                 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
132             }
133         }
134         return jsonNode;
135     }
136 }