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