2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   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
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.openecomp.appc.listener.util;
 
  24 import java.io.IOException;
 
  25 import java.util.ArrayList;
 
  26 import java.util.List;
 
  28 import com.fasterxml.jackson.databind.JsonNode;
 
  29 import org.json.JSONObject;
 
  30 import com.att.eelf.configuration.EELFLogger;
 
  31 import com.att.eelf.configuration.EELFManager;
 
  33 import com.fasterxml.jackson.databind.ObjectMapper;
 
  37     private static final EELFLogger LOG = EELFManager.getInstance().getLogger(Mapper.class);
 
  39     private static ObjectMapper mapper = new ObjectMapper();
 
  42      * @return The object mapper that we are using.
 
  44     public static ObjectMapper getMapper() {
 
  49      * Convert a String to a DcaeMessage
 
  52      *            The json string to try and parse
 
  53      * @return A DcaeMessage from the json string or null if it could not
 
  55     public static <T> T mapOne(String data, Class<T> cls) {
 
  57             return mapper.readValue(data, cls);
 
  58         } catch (Exception e) {
 
  59             LOG.warn(String.format("Could not map [ %s ] to %s", data, cls.getName()), e);
 
  64     public static <T> List<T> mapList(List<String> data, Class<T> cls) {
 
  65         List<T> out = new ArrayList<T>();
 
  66         for (String s : data) {
 
  67             T tmp = Mapper.mapOne(s, cls);
 
  76      * Convenience method to try and convert objects to json String
 
  79      *            The object to try and convert
 
  80      * @return A json string representing the object or null if it could not be converted
 
  82     public static String toJsonString(Object obj) {
 
  85             if (obj instanceof JSONObject) {
 
  86                 jsonStr = obj.toString();
 
  88                 jsonStr = mapper.writeValueAsString(obj);
 
  91         } catch (Exception e) {
 
  92             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
 
  97     public static JSONObject toJsonObject(Object obj) {
 
 100             if (obj.getClass().equals(String.class)) {
 
 101                 jsonStr = (String) obj;
 
 103                 jsonStr = mapper.writeValueAsString(obj);
 
 105             return new JSONObject(jsonStr);
 
 106         } catch (Exception e) {
 
 107             LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);
 
 111     public static JsonNode toJsonNodeFromJsonString(String jsonStr) {
 
 112         JsonNode jsonNode = null;
 
 113         if(jsonStr != null) {
 
 115                 jsonNode = mapper.readTree(jsonStr);
 
 116             } catch (IOException e) {
 
 117                 LOG.warn(String.format("Could not map %s to jsonNode.", jsonStr), e);
 
 122     public static JsonNode toJsonNode(Object obj) {
 
 123         JsonNode jsonNode = null;
 
 124         String jsonStr = toJsonString(obj);
 
 125         if(jsonStr != null) {
 
 127                 jsonNode = mapper.readTree(jsonStr);
 
 128             } catch (IOException e) {
 
 129                 LOG.warn(String.format("Could not map %s to JSONObject.", obj), e);