EntityEventPolicy: fix CER issue
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / util / NodeUtils.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.util;
26
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.Map;
32
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.ObjectWriter;
37 import com.fasterxml.jackson.databind.SerializationFeature;
38
39 public class NodeUtils {
40   /**
41   * Generate unique sha digest. This method is copy over from NodeUtils class in AAIUI
42   *
43   * @param keys the keys
44   * @return the string
45   */
46   public static String generateUniqueShaDigest(String... keys) {
47     if ((keys == null) || keys.length == 0) {
48       return null;
49     }
50   
51     final String keysStr = Arrays.asList(keys).toString();
52     final String hashedId = org.apache.commons.codec.digest.DigestUtils.sha256Hex(keysStr);
53   
54     return hashedId;
55   }
56   
57   /**
58    * Extract field value from object.
59    *
60    * @param node the node
61    * @param fieldName the field name
62    * @return the string
63    */
64   public static String extractFieldValueFromObject(JsonNode node, String fieldName) {
65
66     if (node == null) {
67       return null;
68     }
69
70     if (node.isObject()) {
71
72       JsonNode valueNode = node.get(fieldName);
73
74       if (valueNode != null) {
75
76         if (valueNode.isValueNode()) {
77           return valueNode.asText();
78         }
79       }
80
81     }
82     return null;
83
84   }
85
86   /**
87    * Convert json str to json node.
88    *
89    * @param jsonStr the json str
90    * @return the json node
91    * @throws IOException Signals that an I/O exception has occurred.
92    */
93   public static JsonNode convertJsonStrToJsonNode(String jsonStr) throws IOException {
94     ObjectMapper mapper = new ObjectMapper();
95     if (jsonStr == null || jsonStr.length() == 0) {
96       return null;
97     }
98
99     return mapper.readTree(jsonStr);
100   }
101   
102   /**
103    * Extract objects by key.
104    *
105    * @param node the node
106    * @param searchKey the search key
107    * @param foundObjects the found objects
108    */
109   public static void extractObjectsByKey(JsonNode node, String searchKey,
110       Collection<JsonNode> foundObjects) {
111
112     if ( node == null ) {
113       return;
114     }
115     
116     if (node.isObject()) {
117       Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
118
119       while (nodeIterator.hasNext()) {
120         Map.Entry<String, JsonNode> entry = nodeIterator.next();
121         if (!entry.getValue().isValueNode()) {
122           extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
123         }
124
125         String name = entry.getKey();
126         if (name.equalsIgnoreCase(searchKey)) {
127
128           JsonNode entryNode = entry.getValue();
129
130           if (entryNode.isArray()) {
131
132             Iterator<JsonNode> arrayItemsIterator = entryNode.elements();
133             while (arrayItemsIterator.hasNext()) {
134               foundObjects.add(arrayItemsIterator.next());
135             }
136
137           } else {
138             foundObjects.add(entry.getValue());
139           }
140
141
142         }
143       }
144     } else if (node.isArray()) {
145       Iterator<JsonNode> arrayItemsIterator = node.elements();
146       while (arrayItemsIterator.hasNext()) {
147         extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
148       }
149
150     }
151
152   }
153   
154   /**
155    * Convert object to json.
156    *
157    * @param object the object
158    * @param pretty the pretty
159    * @return the string
160    * @throws JsonProcessingException the json processing exception
161    */
162   public static String convertObjectToJson(Object object, boolean pretty)
163       throws JsonProcessingException {
164     ObjectWriter ow = null;
165
166     ObjectMapper mapper = new ObjectMapper();
167     mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
168     
169     if (pretty) {
170       ow = mapper.writer().withDefaultPrettyPrinter();
171
172     } else {
173       ow = mapper.writer();
174     }
175
176     return ow.writeValueAsString(object);
177   }
178
179 }