2 * Copyright 2014 Bazaarvoice, Inc.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.onap.sdnc.apps.pomba.networkdiscovery.service.util;
\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
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
30 import org.onap.pomba.common.datatypes.DataQuality;
\r
31 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
\r
33 public class TransformationUtil {
\r
35 private static final String CONFIG_JOLT = "config/jolt";
\r
36 private static final String EMPTY_STRING = "";
\r
38 private TransformationUtil() {
\r
39 throw new IllegalStateException("Utility class");
\r
43 * Transforms the sourceJson using the jolt specification for the given resourceType.
\r
46 * @param resourceType
\r
49 public static String transform(String sourceJson, String resourceType) {
\r
51 Object sourceObject = JsonUtils.jsonToObject(sourceJson);
\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
57 return JsonUtils.toJsonString(output);
\r
61 * Converts the second level of JsonElements from the given json to a list of
\r
62 * Network Discovery Attributes.
\r
67 public static List<Attribute> toAttributeList(String json) {
\r
69 JsonParser parser = new JsonParser();
\r
70 JsonElement elem = parser.parse(json);
\r
72 Set<Entry<String, JsonElement>> entrySet = elem.getAsJsonObject().entrySet();
\r
74 List<Attribute> result = new ArrayList<>();
\r
76 Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();
\r
77 while (iter.hasNext()) {
\r
78 Entry<String, JsonElement> next = iter.next();
\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
90 attr.setValue(vserverNext.getValue().getAsString());
\r
92 attr.setDataQuality(DataQuality.ok());
\r