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