Search in sources :

Example 1 with GraphmlType

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;
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with 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;
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) GraphType(org.graphdrawing.graphml.GraphType) NodeType(org.graphdrawing.graphml.NodeType) EdgeType(org.graphdrawing.graphml.EdgeType)

Example 3 with 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);
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType)

Example 4 with GraphmlType

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);
    }
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) GraphML(org.opennms.features.graphml.model.GraphML)

Example 5 with GraphmlType

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));
}
Also used : GraphmlType(org.graphdrawing.graphml.GraphmlType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Test(org.junit.Test) XmlTest(org.opennms.core.test.xml.XmlTest)

Aggregations

GraphmlType (org.graphdrawing.graphml.GraphmlType)7 File (java.io.File)2 EdgeType (org.graphdrawing.graphml.EdgeType)2 GraphType (org.graphdrawing.graphml.GraphType)2 NodeType (org.graphdrawing.graphml.NodeType)2 GraphML (org.opennms.features.graphml.model.GraphML)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Properties (java.util.Properties)1 Collectors (java.util.stream.Collectors)1 JAXB (javax.xml.bind.JAXB)1 DataType (org.graphdrawing.graphml.DataType)1 KeyForType (org.graphdrawing.graphml.KeyForType)1 KeyType (org.graphdrawing.graphml.KeyType)1 KeyTypeType (org.graphdrawing.graphml.KeyTypeType)1 Test (org.junit.Test)1 XmlTest (org.opennms.core.test.xml.XmlTest)1