X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fmodelloader%2Futil%2FGizmoTranslator.java;h=2f4469f73d3f98ea2bc094225480dbf63bc8a262;hb=c919bbafb8163529c56588f06caee60d1d87356f;hp=69e59719a8ebc90f8d11c6acace37262f72c5e8d;hpb=270656abb1ed24930b2f9e57f5bef659494f5e8e;p=aai%2Fmodel-loader.git diff --git a/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java b/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java index 69e5971..2f4469f 100644 --- a/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java +++ b/src/main/java/org/onap/aai/modelloader/util/GizmoTranslator.java @@ -2,8 +2,8 @@ * ============LICENSE_START========================================== * org.onap.aai * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,11 +25,9 @@ import java.io.StringReader; import java.util.HashSet; import java.util.Map; import java.util.Set; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; - import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.modelloader.gizmo.GizmoBulkPayload; @@ -46,7 +44,7 @@ import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class GizmoTranslator { - + private enum NodeType { VERTEX, ATTRIBUTE, @@ -61,20 +59,22 @@ public class GizmoTranslator { NQ_ELEMENT_VERTEX, UNKNOWN } - + private static Logger logger = LoggerFactory.getInstance().getLogger(GizmoTranslator.class.getName()); - + public static String translate(String xmlPayload) throws ParserConfigurationException, SAXException, IOException { + logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT, "Process XML model artifact: " + xmlPayload); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xmlPayload)); Document doc = builder.parse(is); - + GizmoBulkPayload gizmoPayload = new GizmoBulkPayload(); - + processNode(doc.getDocumentElement(), null, null, gizmoPayload); - + return gizmoPayload.toJson(); } @@ -85,9 +85,9 @@ public class GizmoTranslator { Node newParent = null; NodeType nodeType = getNodeType(node); - + switch (nodeType) { - case VERTEX: + case VERTEX: case MODEL_ELEMENT_VERTEX: case NQ_ELEMENT_VERTEX: parentVertexOp = createGizmoVertexOp(node, GizmoBulkPayload.ADD_OP); @@ -105,7 +105,7 @@ public class GizmoTranslator { newParent = parentNode; break; } - + NodeList childNodes = node.getChildNodes(); for (int ix = 0; ix < childNodes.getLength(); ix++) { processNode(childNodes.item(ix), newParent, parentVertexOp, gizmoPayload); @@ -118,54 +118,51 @@ public class GizmoTranslator { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship"); return; } - + GizmoVertex targetVertex = new GizmoVertex(); targetVertex.setType(relatedToList.item(0).getTextContent().trim()); - + NodeList relationData = relationshipNode.getElementsByTagName("relationship-data"); for (int ix = 0; ix < relationData.getLength(); ix++) { Element relationNode = (Element)relationData.item(ix); NodeList keyList = relationNode.getElementsByTagName("relationship-key"); NodeList valueList = relationNode.getElementsByTagName("relationship-value"); - + if ( (keyList.getLength() != 1) || (valueList.getLength() != 1) ) { logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship. Missing key/value."); return; } - + String[] keyBits = keyList.item(0).getTextContent().trim().split("\\."); String value = valueList.item(0).getTextContent().trim(); - + if (keyBits[0].equalsIgnoreCase(targetVertex.getType())) { targetVertex.setProperty(keyBits[1], value); } } - + gizmoPayload.addVertexOperation(new GizmoVertexOperation(GizmoBulkPayload.EXISTS_OP, getVertexId(targetVertex), targetVertex)); - + GizmoEdge edge = new GizmoEdge(); - + edge.setSource("$" + getVertexId(sourceNode.getVertex())); edge.setTarget("$" + getVertexId(targetVertex)); - + gizmoPayload.addEdgeOperation(new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge)); } private static GizmoEdgeOperation createGizmoEdgeOp(Node node, Node parentNode) { GizmoEdge edge = new GizmoEdge(); - + edge.setSource("$" + getVertexId(createGizmoVertex(node))); edge.setTarget("$" + getVertexId(createGizmoVertex(parentNode))); - - GizmoEdgeOperation edgeOp = new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge); - - return edgeOp; + + return new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge); } private static GizmoVertexOperation createGizmoVertexOp(Node node, String operationType) { - GizmoVertex vertex = createGizmoVertex(node); - GizmoVertexOperation addOp = new GizmoVertexOperation(operationType, getVertexId(vertex), vertex); - return addOp; + GizmoVertex vertex = createGizmoVertex(node); + return new GizmoVertexOperation(operationType, getVertexId(vertex), vertex); } private static String getVertexId(GizmoVertex vertex) { @@ -174,7 +171,7 @@ public class GizmoTranslator { for (Map.Entry entry : vertex.getProperties().entrySet()) { sb.append("-" + entry.getValue()); } - + return sb.toString(); } @@ -189,12 +186,12 @@ public class GizmoTranslator { vertex.setProperty(childNodes.item(ix).getNodeName().trim(), childNodes.item(ix).getTextContent().trim()); } } - + // Special case for model-element, where we need to generate an id field if (getNodeType(node).equals(NodeType.MODEL_ELEMENT_VERTEX)) { vertex.setProperty("model-element-uuid", generateModelElementId((Element)node)); } - + // Special case for nq-element, where we need to generate an id field if (getNodeType(node).equals(NodeType.NQ_ELEMENT_VERTEX)) { vertex.setProperty("named-query-element-uuid", generateModelElementId((Element)node)); @@ -202,10 +199,32 @@ public class GizmoTranslator { return vertex; } - + // Generate a unique hash to store as the id for this node private static String generateModelElementId(Element node) { - Set elemSet = new HashSet(); + Set elemSet = new HashSet<>(); + + // Get the parent model version / named query version + String parentVersion = null; + Node parentNode = node.getParentNode(); + while ( (parentNode != null) && (parentVersion == null) ) { + if (getNodeType(parentNode).equals(NodeType.VERTEX)) { + NodeList childNodes = ((Element)parentNode).getElementsByTagName("*"); + for (int ix = 0; ix < childNodes.getLength(); ix++) { + if (childNodes.item(ix).getNodeName().equalsIgnoreCase("named-query-uuid") || + childNodes.item(ix).getNodeName().equalsIgnoreCase("model-version-id")) { + parentVersion = childNodes.item(ix).getTextContent().trim(); + break; + } + } + } + + parentNode = parentNode.getParentNode(); + } + + if (parentVersion != null) { + elemSet.add(parentVersion); + } NodeList childNodes = node.getElementsByTagName("*"); for (int ix = 0; ix < childNodes.getLength(); ix++) { @@ -214,7 +233,7 @@ public class GizmoTranslator { elemSet.add(childNodes.item(ix).getTextContent().trim()); } } - + return Integer.toString(elemSet.hashCode()); } @@ -222,59 +241,59 @@ public class GizmoTranslator { if (!(node instanceof Element)) { return NodeType.UNKNOWN; } - + if (node.getNodeName().equalsIgnoreCase("relationship-list")) { return NodeType.RELATIONSHIP_LIST; } - + if (node.getNodeName().equalsIgnoreCase("relationship")) { return NodeType.RELATIONSHIP; } - + if (node.getNodeName().equalsIgnoreCase("relationship-data")) { return NodeType.RELATIONSHIP_DATA; } - + if (node.getNodeName().equalsIgnoreCase("related-to")) { return NodeType.RELATED_TO; } - + if (node.getNodeName().equalsIgnoreCase("relationship-key")) { return NodeType.RELATIONSHIP_KEY; } - + if (node.getNodeName().equalsIgnoreCase("relationship-value")) { return NodeType.RELATIONSHIP_VALUE; } - + if (node.getNodeName().equalsIgnoreCase("model-element")) { return NodeType.MODEL_ELEMENT_VERTEX; } - + if (node.getNodeName().equalsIgnoreCase("named-query-element")) { return NodeType.NQ_ELEMENT_VERTEX; } - + NodeList childNodes = node.getChildNodes(); int childElements = countChildElements(childNodes); - + if ( (childElements == 0) && (node.getTextContent() != null) && (!node.getTextContent().trim().isEmpty()) ) { return NodeType.ATTRIBUTE; } - + for (int ix = 0; ix < childNodes.getLength(); ix++) { if (getNodeType(childNodes.item(ix)) == NodeType.ATTRIBUTE) { return NodeType.VERTEX; } } - + if (childElements > 0) { return NodeType.CONTAINER; } - + return NodeType.UNKNOWN; } - + static int countChildElements(NodeList nodes) { int count = 0; for (int ix = 0; ix < nodes.getLength(); ix++) { @@ -282,7 +301,7 @@ public class GizmoTranslator { count++; } } - - return count; + + return count; } }