dbb6bdcbae46f033882f03157058912ba387e9e3
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / topology / TopologyMapping.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.compatibility.topology;
9
10 import java.util.Iterator;
11
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;
29
30 import com.google.common.base.Splitter;
31
32 public class TopologyMapping {
33     private static final Splitter QUAD_COLON_SPLITTER = Splitter.on("::::");
34
35     public TopologyMapping(final TopologyKey path, final InstanceIdentifier<Topology> key) {
36         // No-op for now. Multi-instance will require fixing InventoryMapping first.
37     }
38
39     public Edge toAdTopologyEdge(final InstanceIdentifier<Link> identifier) throws ConstructionException {
40         @SuppressWarnings("unchecked")
41         final LinkKey linkKey = ((KeyedInstanceIdentifier<Link, LinkKey>)identifier).getKey();
42
43         final Iterator<String> it = QUAD_COLON_SPLITTER.split(linkKey.getLinkId().getValue()).iterator();
44         final NodeConnector tail = InventoryMapping.nodeConnectorFromId(it.next());
45         final NodeConnector head = InventoryMapping.nodeConnectorFromId(it.next());
46         return new Edge(tail, head);
47     }
48
49     public NodeConnector toAdTopologyNodeConnector(final InstanceIdentifier<TerminationPoint> identifier) {
50         @SuppressWarnings("unchecked")
51         final TerminationPointKey tpKey = ((KeyedInstanceIdentifier<TerminationPoint, TerminationPointKey>)identifier).getKey();
52
53         return InventoryMapping.nodeConnectorFromId(tpKey.getTpId().getValue());
54     }
55
56     public org.opendaylight.controller.sal.core.Node toAdTopologyNode(final InstanceIdentifier<Node> identifier) {
57         @SuppressWarnings("unchecked")
58         final NodeKey nodeKey = ((KeyedInstanceIdentifier<Node, NodeKey>)identifier).getKey();
59
60         return InventoryMapping.nodeFromNodeId(nodeKey.getNodeId().getValue());
61     }
62
63     public NodeKey toTopologyNodeKey(final org.opendaylight.controller.sal.core.Node node) {
64         return new NodeKey(new NodeId(InventoryMapping.toNodeId(node)));
65     }
66
67     public TerminationPointKey toTopologyTerminationPointKey(final NodeConnector nc) {
68         return new TerminationPointKey(new TpId(InventoryMapping.toNodeConnectorId(nc)));
69     }
70
71     public LinkKey toTopologyLinkKey(final Edge edge) {
72         final TerminationPointKey sourceTp = toTopologyTerminationPointKey(edge.getTailNodeConnector());
73         final TerminationPointKey destTp = toTopologyTerminationPointKey(edge.getHeadNodeConnector());
74
75         final StringBuilder sb = new StringBuilder();
76         sb.append(sourceTp.getTpId().toString());
77         sb.append("::::");
78         sb.append(destTp.getTpId().toString());
79         return new LinkKey(new LinkId(sb.toString()));
80     }
81 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.