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;
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
33 import org.onap.aai.cl.api.Logger;
34 import org.onap.aai.cl.eelf.LoggerFactory;
35 import org.onap.aai.modelloader.gizmo.GizmoBulkPayload;
36 import org.onap.aai.modelloader.gizmo.GizmoEdge;
37 import org.onap.aai.modelloader.gizmo.GizmoEdgeOperation;
38 import org.onap.aai.modelloader.gizmo.GizmoVertex;
39 import org.onap.aai.modelloader.gizmo.GizmoVertexOperation;
40 import org.onap.aai.modelloader.service.ModelLoaderMsgs;
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.NodeList;
45 import org.xml.sax.InputSource;
46 import org.xml.sax.SAXException;
48 public class GizmoTranslator {
50 private enum NodeType {
65 private static Logger logger = LoggerFactory.getInstance().getLogger(GizmoTranslator.class.getName());
67 public static String translate(String xmlPayload) throws ParserConfigurationException, SAXException, IOException {
68 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
69 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
70 DocumentBuilder builder = factory.newDocumentBuilder();
71 InputSource is = new InputSource(new StringReader(xmlPayload));
72 Document doc = builder.parse(is);
74 GizmoBulkPayload gizmoPayload = new GizmoBulkPayload();
76 processNode(doc.getDocumentElement(), null, null, gizmoPayload);
78 return gizmoPayload.toJson();
81 private static void processNode(Node node, Node parentNode, GizmoVertexOperation parentVertexOp, GizmoBulkPayload gizmoPayload) {
82 if (!(node instanceof Element)) {
86 Node newParent = null;
87 NodeType nodeType = getNodeType(node);
91 case MODEL_ELEMENT_VERTEX:
92 case NQ_ELEMENT_VERTEX:
93 parentVertexOp = createGizmoVertexOp(node, GizmoBulkPayload.ADD_OP);
94 gizmoPayload.addVertexOperation(parentVertexOp);
95 if (parentNode != null) {
96 gizmoPayload.addEdgeOperation(createGizmoEdgeOp(node, parentNode));
101 processRelationship((Element)node, parentVertexOp, gizmoPayload);
102 newParent = parentNode;
105 newParent = parentNode;
109 NodeList childNodes = node.getChildNodes();
110 for (int ix = 0; ix < childNodes.getLength(); ix++) {
111 processNode(childNodes.item(ix), newParent, parentVertexOp, gizmoPayload);
115 private static void processRelationship(Element relationshipNode, GizmoVertexOperation sourceNode, GizmoBulkPayload gizmoPayload) {
116 NodeList relatedToList = relationshipNode.getElementsByTagName("related-to");
117 if (relatedToList.getLength() != 1) {
118 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship");
122 GizmoVertex targetVertex = new GizmoVertex();
123 targetVertex.setType(relatedToList.item(0).getTextContent().trim());
125 NodeList relationData = relationshipNode.getElementsByTagName("relationship-data");
126 for (int ix = 0; ix < relationData.getLength(); ix++) {
127 Element relationNode = (Element)relationData.item(ix);
128 NodeList keyList = relationNode.getElementsByTagName("relationship-key");
129 NodeList valueList = relationNode.getElementsByTagName("relationship-value");
131 if ( (keyList.getLength() != 1) || (valueList.getLength() != 1) ) {
132 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR, "Unable to resolve relationship. Missing key/value.");
136 String[] keyBits = keyList.item(0).getTextContent().trim().split("\\.");
137 String value = valueList.item(0).getTextContent().trim();
139 if (keyBits[0].equalsIgnoreCase(targetVertex.getType())) {
140 targetVertex.setProperty(keyBits[1], value);
144 gizmoPayload.addVertexOperation(new GizmoVertexOperation(GizmoBulkPayload.EXISTS_OP, getVertexId(targetVertex), targetVertex));
146 GizmoEdge edge = new GizmoEdge();
148 edge.setSource("$" + getVertexId(sourceNode.getVertex()));
149 edge.setTarget("$" + getVertexId(targetVertex));
151 gizmoPayload.addEdgeOperation(new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge));
154 private static GizmoEdgeOperation createGizmoEdgeOp(Node node, Node parentNode) {
155 GizmoEdge edge = new GizmoEdge();
157 edge.setSource("$" + getVertexId(createGizmoVertex(node)));
158 edge.setTarget("$" + getVertexId(createGizmoVertex(parentNode)));
160 GizmoEdgeOperation edgeOp = new GizmoEdgeOperation(GizmoBulkPayload.ADD_OP, edge.getSource() + "_" + edge.getTarget(), edge);
165 private static GizmoVertexOperation createGizmoVertexOp(Node node, String operationType) {
166 GizmoVertex vertex = createGizmoVertex(node);
167 GizmoVertexOperation addOp = new GizmoVertexOperation(operationType, getVertexId(vertex), vertex);
171 private static String getVertexId(GizmoVertex vertex) {
172 StringBuilder sb = new StringBuilder();
173 sb.append(vertex.getType());
174 for (Map.Entry<String, String> entry : vertex.getProperties().entrySet()) {
175 sb.append("-" + entry.getValue());
178 return sb.toString();
181 private static GizmoVertex createGizmoVertex(Node node) {
182 GizmoVertex vertex = new GizmoVertex();
183 vertex.setType(node.getNodeName().trim());
185 NodeList childNodes = node.getChildNodes();
187 for (int ix = 0; ix < childNodes.getLength(); ix++) {
188 if (getNodeType(childNodes.item(ix)).equals(NodeType.ATTRIBUTE)) {
189 vertex.setProperty(childNodes.item(ix).getNodeName().trim(), childNodes.item(ix).getTextContent().trim());
193 // Special case for model-element, where we need to generate an id field
194 if (getNodeType(node).equals(NodeType.MODEL_ELEMENT_VERTEX)) {
195 vertex.setProperty("model-element-uuid", generateModelElementId((Element)node));
198 // Special case for nq-element, where we need to generate an id field
199 if (getNodeType(node).equals(NodeType.NQ_ELEMENT_VERTEX)) {
200 vertex.setProperty("named-query-element-uuid", generateModelElementId((Element)node));
206 // Generate a unique hash to store as the id for this node
207 private static String generateModelElementId(Element node) {
208 Set<String> elemSet = new HashSet<String>();
210 NodeList childNodes = node.getElementsByTagName("*");
211 for (int ix = 0; ix < childNodes.getLength(); ix++) {
212 NodeType nt = getNodeType(childNodes.item(ix));
213 if ( nt.equals(NodeType.ATTRIBUTE) || nt.equals(NodeType.RELATIONSHIP_KEY) || nt.equals(NodeType.RELATIONSHIP_VALUE) ) {
214 elemSet.add(childNodes.item(ix).getTextContent().trim());
218 return Integer.toString(elemSet.hashCode());
221 private static NodeType getNodeType(Node node) {
222 if (!(node instanceof Element)) {
223 return NodeType.UNKNOWN;
226 if (node.getNodeName().equalsIgnoreCase("relationship-list")) {
227 return NodeType.RELATIONSHIP_LIST;
230 if (node.getNodeName().equalsIgnoreCase("relationship")) {
231 return NodeType.RELATIONSHIP;
234 if (node.getNodeName().equalsIgnoreCase("relationship-data")) {
235 return NodeType.RELATIONSHIP_DATA;
238 if (node.getNodeName().equalsIgnoreCase("related-to")) {
239 return NodeType.RELATED_TO;
242 if (node.getNodeName().equalsIgnoreCase("relationship-key")) {
243 return NodeType.RELATIONSHIP_KEY;
246 if (node.getNodeName().equalsIgnoreCase("relationship-value")) {
247 return NodeType.RELATIONSHIP_VALUE;
250 if (node.getNodeName().equalsIgnoreCase("model-element")) {
251 return NodeType.MODEL_ELEMENT_VERTEX;
254 if (node.getNodeName().equalsIgnoreCase("named-query-element")) {
255 return NodeType.NQ_ELEMENT_VERTEX;
258 NodeList childNodes = node.getChildNodes();
259 int childElements = countChildElements(childNodes);
261 if ( (childElements == 0) && (node.getTextContent() != null) && (!node.getTextContent().trim().isEmpty()) ) {
262 return NodeType.ATTRIBUTE;
265 for (int ix = 0; ix < childNodes.getLength(); ix++) {
266 if (getNodeType(childNodes.item(ix)) == NodeType.ATTRIBUTE) {
267 return NodeType.VERTEX;
271 if (childElements > 0) {
272 return NodeType.CONTAINER;
275 return NodeType.UNKNOWN;
278 static int countChildElements(NodeList nodes) {
280 for (int ix = 0; ix < nodes.getLength(); ix++) {
281 if (nodes.item(ix) instanceof Element) {