2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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
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============================================
21 package org.onap.aai.modelloader.util;
23 import java.io.IOException;
24 import java.io.StringReader;
25 import java.util.HashSet;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import org.onap.aai.cl.api.Logger;
32 import org.onap.aai.cl.eelf.LoggerFactory;
33 import org.onap.aai.modelloader.gizmo.GizmoBulkPayload;
34 import org.onap.aai.modelloader.gizmo.GizmoEdge;
35 import org.onap.aai.modelloader.gizmo.GizmoEdgeOperation;
36 import org.onap.aai.modelloader.gizmo.GizmoVertex;
37 import org.onap.aai.modelloader.gizmo.GizmoVertexOperation;
38 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Element;
41 import org.w3c.dom.Node;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
46 public class GizmoTranslator {
48 private enum NodeType {
63 private static Logger logger = LoggerFactory.getInstance().getLogger(GizmoTranslator.class.getName());
65 public static String translate(String xmlPayload) throws ParserConfigurationException, SAXException, IOException {
66 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
67 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
68 DocumentBuilder builder = factory.newDocumentBuilder();
69 InputSource is = new InputSource(new StringReader(xmlPayload));
70 Document doc = builder.parse(is);
72 GizmoBulkPayload gizmoPayload = new GizmoBulkPayload();
74 processNode(doc.getDocumentElement(), null, null, gizmoPayload);
76 return gizmoPayload.toJson();
79 private static void processNode(Node node, Node parentNode, GizmoVertexOperation parentVertexOp, GizmoBulkPayload gizmoPayload) {
80 if (!(node instanceof Element)) {
84 Node newParent = null;
85 NodeType nodeType = getNodeType(node);
89 case MODEL_ELEMENT_VERTEX:
90 case NQ_ELEMENT_VERTEX:
91 parentVertexOp = createGizmoVertexOp(node, GizmoBulkPayload.ADD_OP);
92 gizmoPayload.addVertexOperation(parentVertexOp);
93 if (parentNode != null) {
94 gizmoPayload.addEdgeOperation(createGizmoEdgeOp(node, parentNode));
99 processRelationship((Element)node, parentVertexOp, gizmoPayload);
100 newParent = parentNode;
103 newParent = parentNode;
107 NodeList childNodes = node.getChildNodes();
108 for (int ix = 0; ix < childNodes.getLength(); ix++) {
109 processNode(childNodes.item(ix), newParent, parentVertexOp, gizmoPayload);
113 private static void processRelationship(Element relationshipNode, GizmoVertexOperation sourceNode, GizmoBulkPayload gizmoPayload) {
114 NodeList relatedToList = relationshipNode.getElementsByTagName("related-to");
115 if (relatedToList.getLength() != 1) {
116 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship");
120 GizmoVertex targetVertex = new GizmoVertex();
121 targetVertex.setType(relatedToList.item(0).getTextContent().trim());
123 NodeList relationData = relationshipNode.getElementsByTagName("relationship-data");
124 for (int ix = 0; ix < relationData.getLength(); ix++) {
125 Element relationNode = (Element)relationData.item(ix);
126 NodeList keyList = relationNode.getElementsByTagName("relationship-key");
127 NodeList valueList = relationNode.getElementsByTagName("relationship-value");
129 if ( (keyList.getLength() != 1) || (valueList.getLength() != 1) ) {
130 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship. Missing key/value.");
134 String[] keyBits = keyList.item(0).getTextContent().trim().split("\\.");
135 String value = valueList.item(0).getTextContent().trim();
137 if (keyBits[0].equalsIgnoreCase(targetVertex.getType())) {
138 targetVertex.setProperty(keyBits[1], value);
142 gizmoPayload.addVertexOperation(new GizmoVertexOperation(GizmoBulkPayload.EXISTS_OP, getVertexId(targetVertex), targetVertex));
144 GizmoEdge edge = new GizmoEdge();
146 edge.setSource("$" + getVertexId(sourceNode.getVertex()));
147 edge.setTarget("$" + getVertexId(targetVertex));
149 gizmoPayload.addEdgeOperation(new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge));
152 private static GizmoEdgeOperation createGizmoEdgeOp(Node node, Node parentNode) {
153 GizmoEdge edge = new GizmoEdge();
155 edge.setSource("$" + getVertexId(createGizmoVertex(node)));
156 edge.setTarget("$" + getVertexId(createGizmoVertex(parentNode)));
158 return new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge);
161 private static GizmoVertexOperation createGizmoVertexOp(Node node, String operationType) {
162 GizmoVertex vertex = createGizmoVertex(node);
163 return new GizmoVertexOperation(operationType, getVertexId(vertex), vertex);
166 private static String getVertexId(GizmoVertex vertex) {
167 StringBuilder sb = new StringBuilder();
168 sb.append(vertex.getType());
169 for (Map.Entry<String, String> entry : vertex.getProperties().entrySet()) {
170 sb.append("-" + entry.getValue());
173 return sb.toString();
176 private static GizmoVertex createGizmoVertex(Node node) {
177 GizmoVertex vertex = new GizmoVertex();
178 vertex.setType(node.getNodeName().trim());
180 NodeList childNodes = node.getChildNodes();
182 for (int ix = 0; ix < childNodes.getLength(); ix++) {
183 if (getNodeType(childNodes.item(ix)).equals(NodeType.ATTRIBUTE)) {
184 vertex.setProperty(childNodes.item(ix).getNodeName().trim(), childNodes.item(ix).getTextContent().trim());
188 // Special case for model-element, where we need to generate an id field
189 if (getNodeType(node).equals(NodeType.MODEL_ELEMENT_VERTEX)) {
190 vertex.setProperty("model-element-uuid", generateModelElementId((Element)node));
193 // Special case for nq-element, where we need to generate an id field
194 if (getNodeType(node).equals(NodeType.NQ_ELEMENT_VERTEX)) {
195 vertex.setProperty("named-query-element-uuid", generateModelElementId((Element)node));
201 // Generate a unique hash to store as the id for this node
202 private static String generateModelElementId(Element node) {
203 Set<String> elemSet = new HashSet<>();
205 NodeList childNodes = node.getElementsByTagName("*");
206 for (int ix = 0; ix < childNodes.getLength(); ix++) {
207 NodeType nt = getNodeType(childNodes.item(ix));
208 if ( nt.equals(NodeType.ATTRIBUTE) || nt.equals(NodeType.RELATIONSHIP_KEY) || nt.equals(NodeType.RELATIONSHIP_VALUE) ) {
209 elemSet.add(childNodes.item(ix).getTextContent().trim());
213 return Integer.toString(elemSet.hashCode());
216 private static NodeType getNodeType(Node node) {
217 if (!(node instanceof Element)) {
218 return NodeType.UNKNOWN;
221 if (node.getNodeName().equalsIgnoreCase("relationship-list")) {
222 return NodeType.RELATIONSHIP_LIST;
225 if (node.getNodeName().equalsIgnoreCase("relationship")) {
226 return NodeType.RELATIONSHIP;
229 if (node.getNodeName().equalsIgnoreCase("relationship-data")) {
230 return NodeType.RELATIONSHIP_DATA;
233 if (node.getNodeName().equalsIgnoreCase("related-to")) {
234 return NodeType.RELATED_TO;
237 if (node.getNodeName().equalsIgnoreCase("relationship-key")) {
238 return NodeType.RELATIONSHIP_KEY;
241 if (node.getNodeName().equalsIgnoreCase("relationship-value")) {
242 return NodeType.RELATIONSHIP_VALUE;
245 if (node.getNodeName().equalsIgnoreCase("model-element")) {
246 return NodeType.MODEL_ELEMENT_VERTEX;
249 if (node.getNodeName().equalsIgnoreCase("named-query-element")) {
250 return NodeType.NQ_ELEMENT_VERTEX;
253 NodeList childNodes = node.getChildNodes();
254 int childElements = countChildElements(childNodes);
256 if ( (childElements == 0) && (node.getTextContent() != null) && (!node.getTextContent().trim().isEmpty()) ) {
257 return NodeType.ATTRIBUTE;
260 for (int ix = 0; ix < childNodes.getLength(); ix++) {
261 if (getNodeType(childNodes.item(ix)) == NodeType.ATTRIBUTE) {
262 return NodeType.VERTEX;
266 if (childElements > 0) {
267 return NodeType.CONTAINER;
270 return NodeType.UNKNOWN;
273 static int countChildElements(NodeList nodes) {
275 for (int ix = 0; ix < nodes.getLength(); ix++) {
276 if (nodes.item(ix) instanceof Element) {