Merge "Bug 499: Added support for old DOM Broker APIs."
[controller.git] / opendaylight / md-sal / compatibility / inventory-topology-compatibility / src / main / java / org / opendaylight / controller / md / compatibility / topology / TopologyReader.xtend
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.ArrayList
11 import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider
12 import org.opendaylight.controller.sal.core.Edge
13 import org.opendaylight.controller.sal.core.NodeConnector
14 import org.opendaylight.controller.switchmanager.ISwitchManager
15 import org.opendaylight.controller.topologymanager.ITopologyManager
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.DestinationBuilder
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.link.attributes.SourceBuilder
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkBuilder
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder
29 import org.opendaylight.yangtools.yang.binding.DataObject
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
31
32 class TopologyReader implements RuntimeDataProvider {
33
34     @Property
35     var ISwitchManager switchManager;
36
37     @Property
38     var ITopologyManager topologyManager;
39
40     @Property
41     val TopologyKey topologyKey;
42
43     @Property
44     val InstanceIdentifier<Topology> topologyPath;
45
46     @Property
47     val extension TopologyMapping mapping;
48
49     new() {
50         _topologyKey = new TopologyKey(new TopologyId("compatibility:ad-sal"));
51         _topologyPath = InstanceIdentifier.builder(NetworkTopology).child(Topology, topologyKey).toInstance;
52         _mapping = new TopologyMapping(topologyKey, topologyPath);
53     }
54
55     override readConfigurationData(InstanceIdentifier<? extends DataObject> path) {
56
57         // Topology and Inventory are operational only
58         return null;
59     }
60
61     override readOperationalData(InstanceIdentifier<? extends DataObject> path) {
62         val type = path.targetType;
63         var DataObject data = null;
64         if (false == topologyPath.contains(path)) {
65             return null;
66         }
67         switch (type) {
68             case Topology:
69                 data = readTopology(path as InstanceIdentifier<Topology>)
70             case Node:
71                 data = readNode(path as InstanceIdentifier<Node>)
72             case TerminationPoint:
73                 data = readTerminationPoint(path as InstanceIdentifier<TerminationPoint>)
74             case Link:
75                 data = readLink(path as InstanceIdentifier<Link>)
76         }
77         return data;
78     }
79
80     def DataObject readLink(InstanceIdentifier<Link> identifier) {
81         val edge = identifier.toAdTopologyEdge();
82         val properties = topologyManager?.edges?.get(edge);
83
84         return constructLink(edge);
85     }
86
87     def DataObject readTerminationPoint(InstanceIdentifier<TerminationPoint> identifier) {
88         val nodeConnector = identifier.toAdTopologyNodeConnector();
89         return constructTerminationPoint(nodeConnector)
90     }
91
92     def DataObject readNode(InstanceIdentifier<Node> identifier) {
93         val node = identifier.toAdTopologyNode();
94         return constructNode(node);
95     }
96
97     def DataObject readTopology(InstanceIdentifier<Topology> identifier) {
98
99         //val nodeConnectors = switchManager.
100         val nodes = switchManager.nodes
101         val edges = topologyManager.edges
102
103         val nodeList = new ArrayList<Node>(nodes.size)
104         for (node : nodes) {
105             nodeList.add(constructNode(node))
106         }
107
108         val linkList = new ArrayList<Link>(edges.size)
109         for (edge : edges.keySet) {
110             linkList.add(constructLink(edge))
111         }
112
113         val it = new TopologyBuilder();
114         key = topologyKey
115         node = nodeList
116         link = linkList
117         return build()
118     }
119
120     def constructLink(Edge edge) {
121         val sourceNc = edge.tailNodeConnector
122         val destNc = edge.headNodeConnector
123
124         val it = new LinkBuilder()
125         key = edge.toTopologyLinkKey();
126         source = new SourceBuilder().setSourceNode(sourceNc.node.toTopologyNodeKey.nodeId).setSourceTp(
127             sourceNc.toTopologyTerminationPointKey.tpId).build()
128         destination = new DestinationBuilder().setDestNode(destNc.node.toTopologyNodeKey.nodeId).setDestTp(
129             destNc.toTopologyTerminationPointKey.tpId).build
130         return build()
131     }
132
133     def Node constructNode(org.opendaylight.controller.sal.core.Node node) {
134         val connectors = switchManager.getNodeConnectors(node)
135
136         val tpList = new ArrayList<TerminationPoint>(connectors.size)
137         for (connector : connectors) {
138             tpList.add(constructTerminationPoint(connector));
139         }
140
141         val it = new NodeBuilder()
142         key = node.toTopologyNodeKey();
143         terminationPoint = tpList
144         return build();
145     }
146
147     def TerminationPoint constructTerminationPoint(NodeConnector connector) {
148         val it = new TerminationPointBuilder()
149         key = connector.toTopologyTerminationPointKey
150         return build();
151     }
152
153 }