Simplify method isMutualExclusive in Subnet.
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / main / java / org / opendaylight / controller / sal / compatibility / InventoryAndReadAdapter.xtend
1 package org.opendaylight.controller.sal.compatibility
2
3 import org.opendaylight.controller.sal.reader.IPluginInReadService
4 import org.opendaylight.controller.sal.core.NodeConnector
5 import org.opendaylight.controller.sal.core.Node
6 import org.opendaylight.controller.sal.flowprogrammer.Flow
7 import org.opendaylight.controller.sal.core.NodeTable
8 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService
9
10 import static extension org.opendaylight.controller.sal.common.util.Arguments.*
11 import static extension org.opendaylight.controller.sal.compatibility.NodeMapping.*
12 import static org.opendaylight.controller.sal.compatibility.MDFlowMapping.*
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService
18 import org.opendaylight.controller.sal.reader.NodeConnectorStatistics
19 import org.opendaylight.controller.sal.reader.FlowOnNode
20 import org.opendaylight.controller.sal.reader.NodeDescription
21 import org.slf4j.LoggerFactory
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsInputBuilder
23 import java.util.ArrayList
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllNodeConnectorStatisticsInputBuilder
25 import org.opendaylight.controller.sal.inventory.IPluginInInventoryService
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener
27 import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
30 import java.util.Collections
31 import org.opendaylight.controller.sal.core.UpdateType
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
35 import org.opendaylight.yangtools.yang.binding.DataObject
36 import org.opendaylight.controller.sal.topology.IPluginOutTopologyService
37 import org.opendaylight.controller.sal.topology.IPluginInTopologyService
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryService
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.FlowTopologyDiscoveryListener
40 import org.opendaylight.controller.sal.core.Edge
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.Link
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkOverutilized
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkRemoved
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkUtilizationNormal
46 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate
47 import org.opendaylight.controller.sal.discovery.IDiscoveryService
48
49 class InventoryAndReadAdapter implements IPluginInTopologyService, IPluginInReadService, IPluginInInventoryService, OpendaylightInventoryListener, FlowTopologyDiscoveryListener {
50
51     private static val LOG = LoggerFactory.getLogger(InventoryAndReadAdapter);
52
53     @Property
54     DataBrokerService dataService;
55
56     @Property
57     OpendaylightFlowStatisticsService flowStatisticsService;
58
59     @Property
60     IPluginOutInventoryService inventoryPublisher;
61
62     @Property
63     IPluginOutTopologyService topologyPublisher;
64     
65     @Property
66     IDiscoveryService discoveryPublisher;
67
68     @Property
69     FlowTopologyDiscoveryService topologyDiscovery;
70
71     override getTransmitRate(NodeConnector connector) {
72         val nodeConnector = readFlowCapableNodeConnector(connector.toNodeConnectorRef);
73         return nodeConnector.currentSpeed
74     }
75
76     override readAllFlow(Node node, boolean cached) {
77         val input = new GetAllFlowStatisticsInputBuilder;
78         input.setNode(node.toNodeRef);
79         val result = flowStatisticsService.getAllFlowStatistics(input.build)
80
81         val statistics = result.get.result;
82         val output = new ArrayList<FlowOnNode>();
83         for (stat : statistics.flowStatistics) {
84             // FIXME: Create FlowOnNode
85         }
86         return output;
87     }
88
89     override readAllNodeConnector(Node node, boolean cached) {
90         val input = new GetAllNodeConnectorStatisticsInputBuilder();
91         input.setNode(node.toNodeRef);
92         val result = flowStatisticsService.getAllNodeConnectorStatistics(input.build());
93         val statistics = result.get.result.nodeConnectorStatistics;
94         val ret = new ArrayList<NodeConnectorStatistics>();
95         for (stat : statistics) {
96             ret.add(stat.toNodeConnectorStatistics())
97         }
98         return ret;
99     }
100
101     override readAllNodeTable(Node node, boolean cached) {
102         throw new UnsupportedOperationException("TODO: auto-generated method stub")
103     }
104
105     override readDescription(Node node, boolean cached) {
106         val capableNode = readFlowCapableNode(node.toNodeRef)
107
108         val it = new NodeDescription()
109         manufacturer = capableNode.manufacturer
110         serialNumber = capableNode.serialNumber
111         software = capableNode.software
112         description = capableNode.description
113
114         return it;
115     }
116
117     override readFlow(Node node, Flow flow, boolean cached) {
118         val input = flowStatisticsInput(node, flow);
119         val output = flowStatisticsService.getFlowStatistics(input);
120
121         try {
122             val statistics = output.get().getResult();
123             if (statistics != null) {
124                 val it = new FlowOnNode(flow);
125                 byteCount = statistics.byteCount.value.longValue
126                 durationNanoseconds = statistics.duration.getNanosecond().getValue().intValue();
127                 durationSeconds = statistics.duration.getSecond().getValue().intValue();
128                 packetCount = statistics.getPacketCount().getValue().longValue();
129                 return it;
130             }
131         } catch (Exception e) {
132             LOG.error("Read flow not processed", e);
133         }
134         return null;
135     }
136
137     override readNodeConnector(NodeConnector connector, boolean cached) {
138
139         val getNodeConnectorStatisticsInput = FromSalConversionsUtils.nodeConnectorStatistics(connector);
140         val future = flowStatisticsService.getNodeConnectorStatistics(getNodeConnectorStatisticsInput);
141         try {
142             val rpcResult = future.get();
143             val output = rpcResult.getResult();
144
145             if (output != null) {
146                 return output.toNodeConnectorStatistics;
147             }
148         } catch (Exception e) {
149             LOG.error("Read node connector not processed", e);
150         }
151
152         return null;
153     }
154
155     override onNodeConnectorRemoved(NodeConnectorRemoved update) {
156         // NOOP
157     }
158
159     override onNodeRemoved(NodeRemoved notification) {
160         // NOOP
161     }
162
163     override onNodeConnectorUpdated(NodeConnectorUpdated update) {
164         val properties = Collections.<org.opendaylight.controller.sal.core.Property>emptySet();
165         inventoryPublisher.updateNodeConnector(update.nodeConnectorRef.toADNodeConnector, UpdateType.CHANGED, properties);
166     }
167
168     override onNodeUpdated(NodeUpdated notification) {
169         val properties = Collections.<org.opendaylight.controller.sal.core.Property>emptySet();
170         inventoryPublisher.updateNode(notification.nodeRef.toADNode, UpdateType.CHANGED, properties);
171     }
172
173     override getNodeProps() {
174
175         // FIXME: Read from MD-SAL inventory service
176         return null;
177     }
178
179     override getNodeConnectorProps(Boolean refresh) {
180
181         // FIXME: Read from MD-SAL Invcentory Service
182         return null;
183     }
184
185     override readNodeTable(NodeTable table, boolean cached) {
186         throw new UnsupportedOperationException("TODO: auto-generated method stub")
187     }
188
189     private def FlowCapableNode readFlowCapableNode(NodeRef ref) {
190         val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
191         val node = dataObject.checkInstanceOf(
192             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node);
193         return node.getAugmentation(FlowCapableNode);
194     }
195
196     private def FlowCapableNodeConnector readFlowCapableNodeConnector(NodeConnectorRef ref) {
197         val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
198         val node = dataObject.checkInstanceOf(
199             org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector);
200         return node.getAugmentation(FlowCapableNodeConnector);
201     }
202
203     private static def toNodeConnectorStatistics(
204         org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.NodeConnectorStatistics output) {
205         val it = new NodeConnectorStatistics
206
207         collisionCount = output.getCollisionCount().longValue();
208         receiveCRCErrorCount = output.getReceiveCrcError().longValue();
209         receiveFrameErrorCount = output.getReceiveFrameError().longValue();
210         receiveOverRunErrorCount = output.getReceiveOverRunError().longValue();
211
212         receiveDropCount = output.getReceiveDrops().longValue();
213         receiveErrorCount = output.getReceiveErrors().longValue();
214         receivePacketCount = output.getPackets().getReceived().longValue();
215         receiveByteCount = output.getBytes().getReceived().longValue();
216
217         transmitDropCount = output.getTransmitDrops().longValue();
218         transmitErrorCount = output.getTransmitErrors().longValue();
219         transmitPacketCount = output.getPackets().getTransmitted().longValue();
220         transmitByteCount = output.getBytes().getTransmitted().longValue();
221         return it;
222     }
223
224     override sollicitRefresh() {
225         topologyDiscovery.solicitRefresh
226     }
227     
228     override onLinkDiscovered(LinkDiscovered notification) {
229         val update = new TopoEdgeUpdate(notification.toADEdge,Collections.emptySet(),UpdateType.ADDED);
230         discoveryPublisher.notifyEdge(notification.toADEdge,UpdateType.ADDED,Collections.emptySet());
231         topologyPublisher.edgeUpdate(Collections.singletonList(update))
232     }
233     
234     override onLinkOverutilized(LinkOverutilized notification) {
235         topologyPublisher.edgeOverUtilized(notification.toADEdge)
236     }
237     
238     override onLinkRemoved(LinkRemoved notification) {
239         val update = new TopoEdgeUpdate(notification.toADEdge,Collections.emptySet(),UpdateType.REMOVED);
240         topologyPublisher.edgeUpdate(Collections.singletonList(update))
241     }
242     
243     override onLinkUtilizationNormal(LinkUtilizationNormal notification) {
244         topologyPublisher.edgeUtilBackToNormal(notification.toADEdge)
245     }
246     
247     
248     def Edge toADEdge(Link link) {
249         new Edge(link.source.toADNodeConnector,link.destination.toADNodeConnector)
250     }
251
252 }