dc17848e7a4f1f56eeda104129dd1451d28e9f70
[sdnc/apps.git] /
1 /*\r
2  * Copyright 2014 Bazaarvoice, Inc.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package org.onap.sdnc.apps.pomba.networkdiscovery.service.util;\r
17 \r
18 import com.bazaarvoice.jolt.Chainr;\r
19 import com.bazaarvoice.jolt.JsonUtils;\r
20 import com.google.gson.JsonElement;\r
21 import com.google.gson.JsonParser;\r
22 \r
23 import java.io.File;\r
24 import java.util.ArrayList;\r
25 import java.util.Iterator;\r
26 import java.util.List;\r
27 import java.util.Map.Entry;\r
28 import java.util.Set;\r
29 \r
30 import org.onap.pomba.common.datatypes.DataQuality;\r
31 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;\r
32 \r
33 public class TransformationUtil {\r
34 \r
35     private static final String CONFIG_JOLT = "config/jolt";\r
36     private static final String EMPTY_STRING = "";\r
37 \r
38     public TransformationUtil() {\r
39         throw new IllegalStateException("Utility class");\r
40     }\r
41 \r
42     /**\r
43      * Transforms the sourceJson using the jolt specification for the given resourceType.\r
44      * \r
45      * @param sourceJson\r
46      * @param resourceType\r
47      * @return\r
48      */\r
49     public static String transform(String sourceJson, String resourceType) {\r
50 \r
51         Object sourceObject = JsonUtils.jsonToObject(sourceJson);\r
52 \r
53         List<Object> chainrSpecJSON = JsonUtils.filepathToList(CONFIG_JOLT + File.separator + resourceType + ".json");\r
54         Chainr chainr = Chainr.fromSpec(chainrSpecJSON);\r
55         Object output = chainr.transform(sourceObject);\r
56 \r
57         return JsonUtils.toJsonString(output);\r
58     }\r
59 \r
60     /**\r
61      * Converts the second level of JsonElements from the given json to a list of\r
62      * Network Discovery Attributes.\r
63      * \r
64      * @param json\r
65      * @return\r
66      */\r
67     public static List<Attribute> toAttributeList(String json) {\r
68 \r
69         JsonParser parser = new JsonParser();\r
70         JsonElement elem = parser.parse(json);\r
71 \r
72         Set<Entry<String, JsonElement>> entrySet = elem.getAsJsonObject().entrySet();\r
73 \r
74         List<Attribute> result = new ArrayList<>();\r
75 \r
76         Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();\r
77         while (iter.hasNext()) {\r
78             Entry<String, JsonElement> next = iter.next();\r
79 \r
80             JsonElement vserverElem = next.getValue();\r
81             Set<Entry<String, JsonElement>> vserverEntrySet = vserverElem.getAsJsonObject().entrySet();\r
82             Iterator<Entry<String, JsonElement>> vserverIter = vserverEntrySet.iterator();\r
83             while (vserverIter.hasNext()) {\r
84                 Entry<String, JsonElement> vserverNext = vserverIter.next();\r
85                 Attribute attr = new Attribute();\r
86                 attr.setName(vserverNext.getKey());\r
87                 if (vserverNext.getValue().isJsonNull()) {\r
88                     attr.setValue(EMPTY_STRING);\r
89                 } else {\r
90                     attr.setValue(vserverNext.getValue().getAsString());\r
91                 }\r
92                 attr.setDataQuality(DataQuality.ok());\r
93                 result.add(attr);\r
94             }\r
95         }\r
96         return result;\r
97     }\r
98 \r
99 }