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