2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.compatibility.topology;
10 import java.util.Iterator;
12 import org.opendaylight.controller.sal.compatibility.InventoryMapping;
13 import org.opendaylight.controller.sal.core.ConstructionException;
14 import org.opendaylight.controller.sal.core.Edge;
15 import org.opendaylight.controller.sal.core.NodeConnector;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
30 import com.google.common.base.Splitter;
32 public class TopologyMapping {
33 private static final String HEAD_TAIL_STRING = "::::";
34 private static final Splitter HEAD_TAIL_SPLITTER = Splitter.on(HEAD_TAIL_STRING);
36 public TopologyMapping(final TopologyKey path, final InstanceIdentifier<Topology> key) {
37 // No-op for now. Multi-instance will require fixing InventoryMapping first.
40 public Edge toAdTopologyEdge(final InstanceIdentifier<Link> identifier) throws ConstructionException {
41 @SuppressWarnings("unchecked")
42 final LinkKey linkKey = ((KeyedInstanceIdentifier<Link, LinkKey>)identifier).getKey();
44 final Iterator<String> it = HEAD_TAIL_SPLITTER.split(linkKey.getLinkId().getValue()).iterator();
45 final NodeConnector tail = InventoryMapping.nodeConnectorFromId(it.next());
46 final NodeConnector head = InventoryMapping.nodeConnectorFromId(it.next());
47 return new Edge(tail, head);
50 public NodeConnector toAdTopologyNodeConnector(final InstanceIdentifier<TerminationPoint> identifier) {
51 @SuppressWarnings("unchecked")
52 final TerminationPointKey tpKey = ((KeyedInstanceIdentifier<TerminationPoint, TerminationPointKey>)identifier).getKey();
54 return InventoryMapping.nodeConnectorFromId(tpKey.getTpId().getValue());
57 public org.opendaylight.controller.sal.core.Node toAdTopologyNode(final InstanceIdentifier<Node> identifier) {
58 @SuppressWarnings("unchecked")
59 final NodeKey nodeKey = ((KeyedInstanceIdentifier<Node, NodeKey>)identifier).getKey();
61 return InventoryMapping.nodeFromNodeId(nodeKey.getNodeId().getValue());
64 public NodeKey toTopologyNodeKey(final org.opendaylight.controller.sal.core.Node node) {
65 return new NodeKey(new NodeId(InventoryMapping.toNodeId(node)));
68 public TerminationPointKey toTopologyTerminationPointKey(final NodeConnector nc) {
69 return new TerminationPointKey(new TpId(InventoryMapping.toNodeConnectorId(nc)));
72 public LinkKey toTopologyLinkKey(final Edge edge) {
73 final TerminationPointKey sourceTp = toTopologyTerminationPointKey(edge.getTailNodeConnector());
74 final TerminationPointKey destTp = toTopologyTerminationPointKey(edge.getHeadNodeConnector());
76 final StringBuilder sb = new StringBuilder();
77 sb.append(sourceTp.getTpId().toString());
78 sb.append(HEAD_TAIL_STRING);
79 sb.append(destTp.getTpId().toString());
80 return new LinkKey(new LinkId(sb.toString()));