use of org.graphdrawing.graphml.GraphmlType in project opennms by OpenNMS.
the class GraphmlRepositoryImpl method findByName.
@Override
public GraphmlType findByName(String name) throws IOException {
Objects.requireNonNull(name);
if (!exists(name)) {
throw new NoSuchElementException("No GraphML file found with name " + name);
}
GraphmlType graphmlType = JAXB.unmarshal(new File(buildGraphmlFilepath(name)), GraphmlType.class);
return graphmlType;
}
use of org.graphdrawing.graphml.GraphmlType in project opennms by OpenNMS.
the class GraphMLWriter method convert.
public static GraphmlType convert(GraphML graphML) throws InvalidGraphException {
GraphmlType graphmlType = new GraphmlType();
addProperties(graphmlType, KeyForType.GRAPHML, graphML, dataType -> graphmlType.getGraphOrData().add(dataType));
for (GraphMLGraph eachGraph : graphML.getGraphs()) {
GraphType graphType = new GraphType();
graphType.setId(eachGraph.getId());
addProperties(graphmlType, KeyForType.GRAPH, eachGraph, dataType -> graphType.getDataOrNodeOrEdge().add(dataType));
for (GraphMLNode eachNode : eachGraph.getNodes()) {
NodeType nodeType = new NodeType();
nodeType.setId(eachNode.getId());
graphType.getDataOrNodeOrEdge().add(nodeType);
addProperties(graphmlType, KeyForType.NODE, eachNode, dataType -> nodeType.getDataOrPort().add(dataType));
}
for (GraphMLEdge eachEdge : eachGraph.getEdges()) {
EdgeType edgeType = new EdgeType();
edgeType.setId(eachEdge.getId());
edgeType.setSource(eachEdge.getSource().getId());
edgeType.setTarget(eachEdge.getTarget().getId());
graphType.getDataOrNodeOrEdge().add(edgeType);
addProperties(graphmlType, KeyForType.EDGE, eachEdge, dataType -> edgeType.getData().add(dataType));
}
graphmlType.getGraphOrData().add(graphType);
}
return graphmlType;
}
use of org.graphdrawing.graphml.GraphmlType in project opennms by OpenNMS.
the class GraphMLWriter method write.
public static void write(GraphML graphML, File file, ProcessHook... hooks) throws InvalidGraphException {
GraphmlType graphmlType = convert(graphML);
if (hooks != null) {
for (ProcessHook eachHook : hooks) {
eachHook.process(graphML, graphmlType);
}
}
JAXB.marshal(graphmlType, file);
}
use of org.graphdrawing.graphml.GraphmlType in project opennms by OpenNMS.
the class AssetGraphMLProvider method createAssetTopology.
/**
* Generates and installs a new AssetTopology defined by the config
* @param config
*/
public synchronized void createAssetTopology(GeneratorConfig config) {
Objects.requireNonNull(config);
try {
LOG.debug("Creating Asset Topology providerId: {}, label: {}, config: {}", config.getProviderId(), config.getLabel(), config);
if (graphmlRepository.exists(config.getProviderId())) {
throw new IllegalStateException(String.format("GraphML Provider with id '%s' (label: %s) already exists", config.getProviderId(), config.getLabel()));
}
if (assetGraphDefinitionRepository.exists(config.getProviderId())) {
throw new IllegalStateException(String.format("Asset Graph Definition with id '%s' (label: %s) already exists", config.getProviderId(), config.getLabel()));
}
final GraphML graphML = transactionOperations.execute(status -> new AssetGraphGenerator(nodeProvider).generateGraphs(config));
final GraphmlType graphmlType = GraphMLWriter.convert(graphML);
assetGraphDefinitionRepository.addConfigDefinition(config);
graphmlRepository.save(config.getProviderId(), config.getLabel(), graphmlType);
} catch (Exception ex) {
LOG.error("Could not create Asset Topology", ex);
throw new RuntimeException(ex);
}
}
use of org.graphdrawing.graphml.GraphmlType in project opennms by OpenNMS.
the class GraphmlRepositoryImplTest method testCreateReadDelete.
@Test
public void testCreateReadDelete() throws Exception {
final String NAME = "test-graph";
// Create
GraphmlRepositoryImpl graphmlRepository = new GraphmlRepositoryImpl();
GraphmlType graphmlType = JAXB.unmarshal(getClass().getResource("/test-graph.xml"), GraphmlType.class);
graphmlRepository.save(NAME, "Label *yay*", graphmlType);
// Verify that xml was generated
Assert.assertEquals(true, graphmlRepository.exists(NAME));
// Verify cfg
Properties properties = new Properties();
properties.load(new FileInputStream(GraphmlRepositoryImpl.buildCfgFilepath(NAME)));
Assert.assertEquals("Label *yay*", properties.get(GraphmlRepositoryImpl.LABEL));
Assert.assertEquals(GraphmlRepositoryImpl.buildGraphmlFilepath(NAME), properties.get(GraphmlRepositoryImpl.TOPOLOGY_LOCATION));
// Read
GraphmlType byName = graphmlRepository.findByName(NAME);
// Verify Read
ByteArrayOutputStream graphmlTypeOutputStream = new ByteArrayOutputStream();
ByteArrayOutputStream byNameOutputStream = new ByteArrayOutputStream();
JAXB.marshal(graphmlType, graphmlTypeOutputStream);
JAXB.marshal(byName, byNameOutputStream);
// The GraphML java classes are generated and do not
// overwrite equals() and hashCode() methods.
// We have to check for equality like this
XmlTest.initXmlUnit();
XmlTest.assertXmlEquals(new String(graphmlTypeOutputStream.toByteArray()), new String(byNameOutputStream.toByteArray()));
// Delete
graphmlRepository.delete(NAME);
Assert.assertEquals(false, graphmlRepository.exists(NAME));
Assert.assertEquals(false, graphmlRepository.exists(NAME));
}
Aggregations