Search in sources :

Example 1 with NodeDto

use of org.openkilda.topo.builders.TeTopologyParser.TopologyDto.NodeDto in project open-kilda by telstra.

the class TeTopologyParser method parseTopologyEngineJson.

public static Topology parseTopologyEngineJson(String json) {
    TopologyDto topologyDto;
    ObjectMapper mapper = new ObjectMapper();
    try {
        topologyDto = mapper.readValue(json, TopologyDto.class);
    } catch (IOException ex) {
        throw new TopologyProcessingException(format("Unable to parse the topology '%s'.", json), ex);
    }
    Map<String, Switch> switches = new HashMap<>();
    // Assemble the switches from the provided nodes.
    topologyDto.getNodes().forEach(node -> {
        String name = node.getName();
        if (Strings.isNullOrEmpty(name)) {
            throw new TopologyProcessingException("The node must have a name.");
        }
        Objects.requireNonNull(name, "The name must be provided");
        String switchId = name.toUpperCase();
        switches.put(switchId, new Switch(switchId));
    });
    Map<String, Link> links = new HashMap<>();
    // Assemble the links from the provided outgoing_relationships.
    topologyDto.getNodes().forEach(node -> {
        String srcId = node.getName().toUpperCase();
        List<NodeDto> relations = node.getOutgoingRelationships();
        if (relations != null) {
            relations.forEach(relation -> {
                String dstId = relation.getName().toUpperCase();
                Link link = new Link(// TODO: probably reuse the same endpoint. Why not?
                new LinkEndpoint(switches.get(srcId), null, null), new LinkEndpoint(switches.get(dstId), null, null));
                links.put(link.getShortSlug(), link);
            });
        }
    });
    return new Topology(switches, links);
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Topology(org.openkilda.topo.Topology) Switch(org.openkilda.topo.Switch) TopologyProcessingException(org.openkilda.topo.exceptions.TopologyProcessingException) LinkEndpoint(org.openkilda.topo.LinkEndpoint) NodeDto(org.openkilda.topo.builders.TeTopologyParser.TopologyDto.NodeDto) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Link(org.openkilda.topo.Link)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Link (org.openkilda.topo.Link)1 LinkEndpoint (org.openkilda.topo.LinkEndpoint)1 Switch (org.openkilda.topo.Switch)1 Topology (org.openkilda.topo.Topology)1 NodeDto (org.openkilda.topo.builders.TeTopologyParser.TopologyDto.NodeDto)1 TopologyProcessingException (org.openkilda.topo.exceptions.TopologyProcessingException)1