a7a7a9ac3ab6dd78a4207699672e98083c81ec84
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / topologymanager / AdSalTopologyMapping.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.topologymanager;
9
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Set;
14
15 import org.opendaylight.controller.sal.compatibility.NodeMapping;
16 import org.opendaylight.controller.sal.core.ConstructionException;
17 import org.opendaylight.controller.sal.core.Edge;
18 import org.opendaylight.controller.sal.core.NodeConnector;
19 import org.opendaylight.controller.sal.core.Property;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Destination;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.Source;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34
35 public class AdSalTopologyMapping {
36     private final InstanceIdentifier<Topology> topologyPath;
37
38     public AdSalTopologyMapping(final TopologyKey topology) {
39         this.topologyPath = InstanceIdentifier.builder(NetworkTopology.class)
40                 .child(Topology.class, topology).toInstance();
41     }
42
43     public InstanceIdentifier<Topology> getTopologyPath() {
44         return topologyPath;
45     }
46
47     public InstanceIdentifier<TerminationPoint> toTerminationPoint(final NodeConnector connector) {
48         return getTopologyPath().builder()
49                 .child(Node.class)
50                 .child(TerminationPoint.class, toTerminationPointKey(connector))
51                 .toInstance();
52     }
53
54     public Map<Edge,Set<Property>> toEdgePropertiesMap(final Iterable<Link> links) {
55         final HashMap<Edge,Set<Property>> ret = new HashMap<>();
56         for (final Link link : links) {
57             try {
58                 ret.put(toEdge(link), toProperties(link));
59             } catch (ConstructionException e) {
60                 throw new IllegalStateException(String.format("Failed to create edge properties for {}", link), e);
61             }
62         }
63         return ret;
64     }
65
66     public static Set<Edge> toEdges(final Iterable<Link> links) throws ConstructionException {
67         final HashSet<Edge> ret = new HashSet<Edge>();
68         for (final Link link : links) {
69             ret.add(toEdge(link));
70         }
71         return ret;
72     }
73
74     public static Edge toEdge(final Link link) throws ConstructionException {
75         final NodeConnector tail = toNodeConnector(link.getSource());
76         final NodeConnector head = toNodeConnector(link.getDestination());
77         return new Edge(tail, head);
78     }
79
80     public static org.opendaylight.controller.sal.core.Node toAdNode(final Node node) throws ConstructionException {
81         return toAdNode(node.getNodeId());
82     }
83
84     public static org.opendaylight.controller.sal.core.Node toAdNode(final NodeId node) throws ConstructionException {
85         final NodeKey key = new NodeKey(
86                 new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId(node));
87         return new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, key);
88     }
89
90     public static NodeConnector toNodeConnector(final Source ref) throws ConstructionException {
91         final org.opendaylight.controller.sal.core.Node adNode = toAdNode(ref.getSourceNode());
92         final NodeConnectorKey key = new NodeConnectorKey(new NodeConnectorId(ref.getSourceTp()));
93         return new NodeConnector(NodeMapping.MD_SAL_TYPE, key, adNode);
94     }
95
96     public static NodeConnector toNodeConnector(final Destination ref) throws ConstructionException {
97         final org.opendaylight.controller.sal.core.Node adNode = toAdNode(ref.getDestNode());
98         final NodeConnectorKey key = new NodeConnectorKey(new NodeConnectorId(ref.getDestTp()));
99         return new NodeConnector(NodeMapping.MD_SAL_TYPE, key, adNode);
100     }
101
102     public TerminationPointKey toTerminationPointKey(final NodeConnector connector) {
103         return null;
104     }
105
106     public Set<Property> toProperties(final Link link) {
107         return null;
108     }
109 }