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.champcore.ie;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.LinkedList;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Optional;
36 import java.util.concurrent.atomic.AtomicInteger;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 import javax.xml.parsers.ParserConfigurationException;
41 import javax.xml.stream.XMLOutputFactory;
42 import javax.xml.stream.XMLStreamException;
43 import javax.xml.stream.XMLStreamWriter;
45 import org.onap.aai.champcore.ChampAPI;
46 import org.onap.aai.champcore.ChampGraph;
47 import org.onap.aai.champcore.ChampTransaction;
48 import org.onap.aai.champcore.exceptions.ChampMarshallingException;
49 import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
50 import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
51 import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
52 import org.onap.aai.champcore.exceptions.ChampTransactionException;
53 import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
54 import org.onap.aai.champcore.model.ChampObject;
55 import org.onap.aai.champcore.model.ChampObjectIndex;
56 import org.onap.aai.champcore.model.ChampRelationship;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.NamedNodeMap;
61 import org.w3c.dom.Node;
62 import org.w3c.dom.NodeList;
63 import org.xml.sax.InputSource;
64 import org.xml.sax.SAXException;
66 public class GraphMLImporterExporter implements Importer, Exporter {
68 private static final Logger LOGGER = LoggerFactory.getLogger(GraphMLImporterExporter.class);
70 private static class GraphMLKey {
71 private final String id;
72 private final String attrName;
73 private final String attrType;
75 public GraphMLKey(String id, String attrName, Class<?> attrType) {
77 this.attrName = attrName;
79 if (attrType.equals(Boolean.class)) {
80 this.attrType = "boolean";
81 } else if (attrType.equals(Integer.class)) {
82 this.attrType = "int";
83 } else if (attrType.equals(Long.class)) {
84 this.attrType = "long";
85 } else if (attrType.equals(Float.class)) {
86 this.attrType = "float";
87 } else if (attrType.equals(Double.class)) {
88 this.attrType = "double";
89 } else if (attrType.equals(String.class)) {
90 this.attrType = "string";
92 throw new RuntimeException("Cannot handle type " + attrType + " in GraphML");
97 public void importData(ChampAPI api, InputStream is) {
100 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
101 factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
102 final DocumentBuilder builder = factory.newDocumentBuilder();
103 final InputSource inputSource = new InputSource(is);
104 final Document doc = builder.parse(inputSource);
106 final Map<String, Map<String, String>> nodePropertyDefinitions = new HashMap<String, Map<String, String>> ();
107 final Map<String, Map<String, String>> edgePropertyDefinitions = new HashMap<String, Map<String, String>> ();
108 final Set<Map<String, String>> nodeDefaults = new HashSet<Map<String, String>> ();
109 final Set<Map<String, String>> edgeDefaults = new HashSet<Map<String, String>> ();
111 final NodeList keys = doc.getElementsByTagName("key");
113 for (int i = 0; i < keys.getLength(); i++) {
114 final Node key = keys.item(i);
115 final String id = key.getAttributes().getNamedItem("id").getNodeValue();
116 final String attrName = key.getAttributes().getNamedItem("attr.name").getNodeValue();
117 final String attrType = key.getAttributes().getNamedItem("attr.type").getNodeValue();
118 final String elementType = key.getAttributes().getNamedItem("for").getNodeValue();
119 final Map<String, String> propertyDefinitions = new HashMap<String, String> ();
121 propertyDefinitions.put("attr.name", attrName);
122 propertyDefinitions.put("attr.type", attrType);
124 final NodeList keyChildren = key.getChildNodes();
126 for (int j = 0; j < keyChildren.getLength(); j++) {
127 final Node keyChild = keyChildren.item(j);
129 if (keyChild.getNodeType() != Node.ELEMENT_NODE) continue;
131 if (keyChild.getNodeName().equals("default")) {
132 propertyDefinitions.put("default", keyChild.getFirstChild().getNodeValue());
134 if (elementType.equals("node")) nodeDefaults.add(propertyDefinitions);
135 else if (elementType.equals("edge")) edgeDefaults.add(propertyDefinitions);
139 if (elementType.equals("node")) {
140 nodePropertyDefinitions.put(id, propertyDefinitions);
141 } else if (elementType.equals("edge")) {
142 edgePropertyDefinitions.put(id, propertyDefinitions);
144 LOGGER.warn("Unknown element type {}, skipping", elementType);
148 final NodeList graphs = doc.getElementsByTagName("graph");
150 for (int i = 0; i < graphs.getLength(); i++) {
151 final Node graph = graphs.item(i);
152 final String graphName = graph.getAttributes().getNamedItem("id").getNodeValue();
153 final NodeList nodesAndEdges = graph.getChildNodes();
155 List<String> fields = new ArrayList<String>();
156 fields.add("importAssignedId");
157 api.getGraph(graphName).storeObjectIndex(ChampObjectIndex.create()
158 .ofName("importAssignedId")
163 for (int j = 0; j < nodesAndEdges.getLength(); j++) {
164 final Node nodeOrEdge = nodesAndEdges.item(j);
166 if (nodeOrEdge.getNodeType() != Node.ELEMENT_NODE) continue;
168 if (nodeOrEdge.getNodeName().equals("node")) {
169 writeNode(api.getGraph(graphName), nodeOrEdge, nodePropertyDefinitions, nodeDefaults);
170 } else if (nodeOrEdge.getNodeName().equals("edge")) {
171 writeEdge(api.getGraph(graphName), nodeOrEdge, edgePropertyDefinitions, edgeDefaults);
173 LOGGER.warn("Unknown object {} found in graphML, skipping", nodeOrEdge.getNodeName());
177 } catch (ParserConfigurationException e) {
178 throw new RuntimeException("Failed to setup DocumentBuilder", e);
179 } catch (SAXException e) {
180 throw new RuntimeException("Failed to parse input stream", e);
181 } catch (IOException e) {
182 throw new RuntimeException("Failed to parse input stream", e);
186 private void writeEdge(ChampGraph graph, Node edge, Map<String, Map<String, String>> edgePropertyDefinitions, Set<Map<String, String>> edgeDefaults) {
187 final NamedNodeMap edgeAttributes = edge.getAttributes();
188 final NodeList data = edge.getChildNodes();
189 final Object sourceKey = edgeAttributes.getNamedItem("source").getNodeValue();
190 final Object targetKey = edgeAttributes.getNamedItem("target").getNodeValue();
191 ChampObject sourceObject=null;
192 ChampObject targetObject=null;
195 final Optional<ChampObject> source = graph.queryObjects(Collections.singletonMap("importAssignedId", sourceKey), Optional.empty()).findFirst();
196 final Optional<ChampObject> target = graph.queryObjects(Collections.singletonMap("importAssignedId", targetKey), Optional.empty()).findFirst();
198 if (!source.isPresent()) {
199 sourceObject = graph.storeObject(ChampObject.create()
204 } else sourceObject = source.get();
206 if (!target.isPresent()) {
207 targetObject = graph.storeObject(ChampObject.create()
212 } else targetObject = target.get();
214 } catch (ChampMarshallingException e) {
215 LOGGER.error("Failed to marshall object to backend type, skipping this edge", e);
217 } catch (ChampSchemaViolationException e) {
218 LOGGER.error("Source/target object violates schema constraint(s)", e);
220 } catch (ChampObjectNotExistsException e) {
221 LOGGER.error("Failed to update existing source/target ChampObject", e);
223 } catch (ChampTransactionException e) {
224 LOGGER.error("Failed to commit or rollback transaction", e);
227 final ChampRelationship.Builder champRelBuilder = new ChampRelationship.Builder(sourceObject, targetObject, "undefined");
229 for (Map<String, String> defaultProperty : edgeDefaults) {
230 champRelBuilder.property(defaultProperty.get("attr.name"), defaultProperty.get("default"));
233 for (int k = 0; k < data.getLength(); k++) {
234 final Node datum = data.item(k);
236 if (datum.getNodeType() != Node.ELEMENT_NODE) continue;
238 final String nodeProperty = datum.getAttributes().getNamedItem("key").getNodeValue();
239 final Map<String, String> nodePropertyDefinition = edgePropertyDefinitions.get(nodeProperty);
241 switch (nodePropertyDefinition.get("attr.type")) {
243 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), Boolean.valueOf(datum.getFirstChild().getNodeValue()));
246 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), Integer.valueOf(datum.getFirstChild().getNodeValue()));
249 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), Long.valueOf(datum.getFirstChild().getNodeValue()));
252 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), Float.valueOf(datum.getFirstChild().getNodeValue()));
255 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), Double.valueOf(datum.getFirstChild().getNodeValue()));
258 champRelBuilder.property(nodePropertyDefinition.get("attr.name"), datum.getFirstChild().getNodeValue());
261 throw new RuntimeException("Unknown node property attr.type " + nodePropertyDefinition.get("attr.type"));
265 final ChampRelationship relToStore = champRelBuilder.build();
268 graph.storeRelationship(relToStore, Optional.empty());
269 } catch (ChampMarshallingException e) {
270 LOGGER.warn("Failed to marshall ChampObject to backend type", e);
271 } catch (ChampSchemaViolationException e) {
272 LOGGER.error("Failed to store object (schema violated): " + relToStore, e);
273 } catch (ChampRelationshipNotExistsException e) {
274 LOGGER.error("Failed to update existing ChampRelationship", e);
275 } catch (ChampObjectNotExistsException e) {
276 LOGGER.error("Objects bound to relationship do not exist (should never happen)");
277 } catch (ChampUnmarshallingException e) {
278 LOGGER.error("Failed to unmarshall ChampObject to backend type");
279 } catch (ChampTransactionException e) {
280 LOGGER.error("Failed to commit or rollback transaction");
285 private void writeNode(ChampGraph graph, Node node, Map<String, Map<String, String>> nodePropertyDefinitions, Set<Map<String, String>> nodeDefaults) {
286 final NamedNodeMap nodeAttributes = node.getAttributes();
287 final Object importAssignedId = nodeAttributes.getNamedItem("id").getNodeValue();
288 final NodeList data = node.getChildNodes();
289 final Map<String, Object> properties = new HashMap<String, Object> ();
291 for (int k = 0; k < data.getLength(); k++) {
292 final Node datum = data.item(k);
294 if (datum.getNodeType() != Node.ELEMENT_NODE) continue;
296 final String nodeProperty = datum.getAttributes().getNamedItem("key").getNodeValue();
297 final Map<String, String> nodePropertyDefinition = nodePropertyDefinitions.get(nodeProperty);
299 switch (nodePropertyDefinition.get("attr.type")) {
301 properties.put(nodePropertyDefinition.get("attr.name"), Boolean.valueOf(datum.getFirstChild().getNodeValue()));
304 properties.put(nodePropertyDefinition.get("attr.name"), Integer.valueOf(datum.getFirstChild().getNodeValue()));
307 properties.put(nodePropertyDefinition.get("attr.name"), Long.valueOf(datum.getFirstChild().getNodeValue()));
310 properties.put(nodePropertyDefinition.get("attr.name"), Float.valueOf(datum.getFirstChild().getNodeValue()));
313 properties.put(nodePropertyDefinition.get("attr.name"), Double.valueOf(datum.getFirstChild().getNodeValue()));
316 properties.put(nodePropertyDefinition.get("attr.name"), datum.getFirstChild().getNodeValue());
319 throw new RuntimeException("Unknown node property attr.type " + nodePropertyDefinition.get("attr.type"));
323 if (!properties.containsKey("type")) throw new RuntimeException("No type provided for object (was this GraphML exported by Champ?)");
325 final ChampObject.Builder champObjBuilder = new ChampObject.Builder((String) properties.get("type"));
327 for (Map<String, String> defaultProperty : nodeDefaults) {
328 champObjBuilder.property(defaultProperty.get("attr.name"), defaultProperty.get("default"));
331 properties.remove("type");
333 champObjBuilder.properties(properties)
334 .property("importAssignedId", importAssignedId);
336 final ChampObject objectToStore = champObjBuilder.build();
339 graph.storeObject(objectToStore, Optional.empty());
340 } catch (ChampMarshallingException e) {
341 LOGGER.warn("Failed to marshall ChampObject to backend type", e);
342 } catch (ChampSchemaViolationException e) {
343 LOGGER.error("Failed to store object (schema violated): " + objectToStore, e);
344 } catch (ChampObjectNotExistsException e) {
345 LOGGER.error("Failed to update existing ChampObject", e);
346 } catch (ChampTransactionException e) {
347 LOGGER.error("Failed to commit or rollback transaction");
352 public void exportData(ChampGraph graph, OutputStream os) {
354 final XMLOutputFactory output = XMLOutputFactory.newInstance();
357 final XMLStreamWriter writer = output.createXMLStreamWriter(os);
359 writer.writeStartDocument();
360 writer.writeStartElement("graphml");
361 writer.writeDefaultNamespace("http://graphml.graphdrawing.org/xmlns");
362 writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
363 writer.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd");
365 final List<ChampObject> nodes = new LinkedList<ChampObject> ();
366 final List<ChampRelationship> edges = new LinkedList<ChampRelationship> ();
367 final Map<String, GraphMLKey> nodeKeys = new HashMap<String, GraphMLKey> ();
368 final Map<String, GraphMLKey> edgeKeys = new HashMap<String, GraphMLKey> ();
369 final AtomicInteger elementCount = new AtomicInteger();
371 graph.queryObjects(Collections.emptyMap(), Optional.empty()).forEach(object -> {
374 for (Map.Entry<String, Object> property : object.getProperties().entrySet()) {
375 if (nodeKeys.containsKey(property.getKey())) continue;
377 nodeKeys.put(property.getKey(), new GraphMLKey("d" + elementCount.incrementAndGet(), property.getKey(), property.getValue().getClass()));
380 nodeKeys.put("type", new GraphMLKey("d" + elementCount.incrementAndGet(), "type", String.class));
383 graph.queryRelationships(Collections.emptyMap(), Optional.empty()).forEach(relationship -> {
384 edges.add(relationship);
386 for (Map.Entry<String, Object> property : relationship.getProperties().entrySet()) {
387 if (nodeKeys.containsKey(property.getKey())) continue;
389 edgeKeys.put(property.getKey(), new GraphMLKey("d" + elementCount.incrementAndGet(), property.getKey(), property.getValue().getClass()));
392 edgeKeys.put("type", new GraphMLKey("d" + elementCount.incrementAndGet(), "type", String.class));
395 for (Entry<String, GraphMLKey> nodeKey : nodeKeys.entrySet()) {
396 final GraphMLKey graphMlKey = nodeKey.getValue();
398 writer.writeStartElement("key");
399 writer.writeAttribute("id", graphMlKey.id);
400 writer.writeAttribute("for", "node");
401 writer.writeAttribute("attr.name", graphMlKey.attrName);
402 writer.writeAttribute("attr.type", graphMlKey.attrType);
403 writer.writeEndElement();
406 for (Entry<String, GraphMLKey> edgeKey : edgeKeys.entrySet()) {
407 final GraphMLKey graphMlKey = edgeKey.getValue();
409 writer.writeStartElement("key");
410 writer.writeAttribute("id", graphMlKey.id);
411 writer.writeAttribute("for", "edge");
412 writer.writeAttribute("attr.name", graphMlKey.attrName);
413 writer.writeAttribute("attr.type", graphMlKey.attrType);
414 writer.writeEndElement();
417 for (ChampObject object : nodes) {
419 writer.writeStartElement("node");
420 writer.writeAttribute("id", String.valueOf(object.getKey().get()));
422 writer.writeStartElement("data");
423 writer.writeAttribute("key", nodeKeys.get("type").id);
424 writer.writeCharacters(object.getType());
425 writer.writeEndElement();
427 for (Entry<String, Object> property : object.getProperties().entrySet()) {
428 final GraphMLKey key = nodeKeys.get(property.getKey());
430 writer.writeStartElement("data");
431 writer.writeAttribute("key", key.id);
432 writer.writeCharacters(String.valueOf(property.getValue()));
433 writer.writeEndElement();
436 writer.writeEndElement();
437 } catch (XMLStreamException e) {
438 throw new RuntimeException("Failed to write edge to output stream", e);
442 for (ChampRelationship relationship : edges) {
444 writer.writeStartElement("edge");
445 writer.writeAttribute("id", String.valueOf(relationship.getKey().get()));
447 writer.writeStartElement("data");
448 writer.writeAttribute("key", edgeKeys.get("type").id);
449 writer.writeCharacters(relationship.getType());
450 writer.writeEndElement();
452 for (Entry<String, Object> property : relationship.getProperties().entrySet()) {
453 final GraphMLKey key = edgeKeys.get(property.getKey());
455 writer.writeStartElement("data");
456 writer.writeAttribute("key", key.id);
457 writer.writeCharacters(String.valueOf(property.getValue()));
458 writer.writeEndElement();
461 writer.writeEndElement();
462 } catch (XMLStreamException e) {
463 throw new RuntimeException("Failed to write edge to output stream", e);
467 writer.writeEndElement();
468 writer.writeEndDocument();
470 } catch (XMLStreamException | ChampTransactionException e) {
471 throw new RuntimeException(e);