Updated Flow Capable Models & AD SAL Compatibility mapping
[controller.git] / opendaylight / md-sal / sal-compability / src / main / java / org / opendaylight / controller / sal / compability / InventoryAndReadAdapter.xtend
diff --git a/opendaylight/md-sal/sal-compability/src/main/java/org/opendaylight/controller/sal/compability/InventoryAndReadAdapter.xtend b/opendaylight/md-sal/sal-compability/src/main/java/org/opendaylight/controller/sal/compability/InventoryAndReadAdapter.xtend
new file mode 100644 (file)
index 0000000..367e64c
--- /dev/null
@@ -0,0 +1,203 @@
+package org.opendaylight.controller.sal.compability
+
+import org.opendaylight.controller.sal.reader.IPluginInReadService
+import org.opendaylight.controller.sal.core.NodeConnector
+import org.opendaylight.controller.sal.core.Node
+import org.opendaylight.controller.sal.flowprogrammer.Flow
+import org.opendaylight.controller.sal.core.NodeTable
+import org.opendaylight.controller.sal.binding.api.data.DataBrokerService
+
+import static extension org.opendaylight.controller.sal.common.util.Arguments.*
+import static extension org.opendaylight.controller.sal.compability.NodeMapping.*
+import static org.opendaylight.controller.sal.compability.MDFlowMapping.*
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow   .inventory.rev130819.FlowCapableNode
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.OpendaylightFlowStatisticsService
+import org.opendaylight.controller.sal.reader.NodeConnectorStatistics
+import org.opendaylight.controller.sal.reader.FlowOnNode
+import org.opendaylight.controller.sal.reader.NodeDescription
+import org.slf4j.LoggerFactory
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsInputBuilder
+import java.util.ArrayList
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllNodeConnectorStatisticsInputBuilder
+import org.opendaylight.controller.sal.inventory.IPluginInInventoryService
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener
+import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
+import java.util.Collections
+import org.opendaylight.controller.sal.core.UpdateType
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
+import org.opendaylight.yangtools.yang.binding.DataObject
+
+class InventoryAndReadAdapter implements IPluginInReadService, IPluginInInventoryService,OpendaylightInventoryListener {
+
+    private static val LOG = LoggerFactory.getLogger(InventoryAndReadAdapter);
+
+    @Property
+    DataBrokerService dataService;
+
+    @Property
+    OpendaylightFlowStatisticsService flowStatisticsService;
+    
+    @Property
+    IPluginOutInventoryService adInventoryPublisher;
+    
+
+    override getTransmitRate(NodeConnector connector) {
+        val nodeConnector = readFlowCapableNodeConnector(connector.toNodeConnectorRef);
+        return nodeConnector.currentSpeed
+    }
+
+    override readAllFlow(Node node, boolean cached) {
+        val input = new GetAllFlowStatisticsInputBuilder;
+        input.setNode(node.toNodeRef);
+        val result = flowStatisticsService.getAllFlowStatistics(input.build)
+        
+        val statistics = result.get.result;
+        val output = new ArrayList<FlowOnNode>();
+        for(stat : statistics.flowStatistics) {
+            // FIXME: Create FlowOnNode
+        }
+        return output;
+    }
+
+    override readAllNodeConnector(Node node, boolean cached) {
+        val input = new GetAllNodeConnectorStatisticsInputBuilder();
+        input.setNode(node.toNodeRef);
+        val result = flowStatisticsService.getAllNodeConnectorStatistics(input.build());
+        val statistics = result.get.result.nodeConnectorStatistics;
+        val ret = new ArrayList<NodeConnectorStatistics>();
+        for(stat : statistics) {
+            ret.add(stat.toNodeConnectorStatistics())
+        }
+        return ret;
+    }
+
+    override readAllNodeTable(Node node, boolean cached) {
+        throw new UnsupportedOperationException("TODO: auto-generated method stub")
+    }
+
+    override readDescription(Node node, boolean cached) {
+        val capableNode = readFlowCapableNode(node.toNodeRef)
+
+        val it = new NodeDescription()
+        manufacturer = capableNode.manufacturer
+        serialNumber = capableNode.serialNumber
+        software = capableNode.software
+        description = capableNode.description
+
+        return it;
+    }
+
+    override readFlow(Node node, Flow flow, boolean cached) {
+        val input = flowStatisticsInput(node, flow);
+        val output = flowStatisticsService.getFlowStatistics(input);
+
+        try {
+            val statistics = output.get().getResult();
+            if (statistics != null) {
+                val it = new FlowOnNode(flow);
+                byteCount = statistics.byteCount.value.longValue
+                durationNanoseconds = statistics.duration.getNanosecond().getValue().intValue();
+                durationSeconds = statistics.duration.getSecond().getValue().intValue();
+                packetCount = statistics.getPacketCount().getValue().longValue();
+                return it;
+            }
+        } catch (Exception e) {
+             LOG.error("Read flow not processed", e);
+        }
+        return null;
+    }
+
+    override readNodeConnector(NodeConnector connector, boolean cached) {
+
+        val getNodeConnectorStatisticsInput = FromSalConversionsUtils.nodeConnectorStatistics(connector);
+        val future = flowStatisticsService.getNodeConnectorStatistics(getNodeConnectorStatisticsInput);
+        try {
+            val rpcResult = future.get();
+            val output = rpcResult.getResult();
+
+            if (output != null) {
+                return output.toNodeConnectorStatistics;
+            }
+        } catch (Exception e) {
+            LOG.error("Read node connector not processed", e);
+        }
+
+        return null;
+    }
+
+    override onNodeConnectorRemoved(NodeConnectorRemoved update) {
+        // NOOP
+    }
+
+    override onNodeRemoved(NodeRemoved notification) {
+        
+        // NOOP
+    }
+
+    override onNodeConnectorUpdated(NodeConnectorUpdated update) {
+        val properties = Collections.<org.opendaylight.controller.sal.core.Property>emptySet();
+        adInventoryPublisher.updateNodeConnector(update.nodeConnectorRef.toADNodeConnector,UpdateType.CHANGED,properties);
+    }
+
+    override onNodeUpdated(NodeUpdated notification) {
+        val properties = Collections.<org.opendaylight.controller.sal.core.Property>emptySet();
+        adInventoryPublisher.updateNode(notification.nodeRef.toADNode,UpdateType.CHANGED,properties);
+    }
+
+    override  getNodeProps() {
+        // FIXME: Read from MD-SAL inventory service
+        return null;
+    }
+
+    override  getNodeConnectorProps(Boolean refresh) {
+        // FIXME: Read from MD-SAL Invcentory Service
+        return null;
+    }
+
+
+    override readNodeTable(NodeTable table, boolean cached) {
+        throw new UnsupportedOperationException("TODO: auto-generated method stub")
+    }
+
+    private def FlowCapableNode readFlowCapableNode(NodeRef ref) {
+        val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
+        val node = dataObject.checkInstanceOf(
+            org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node);
+        return node.getAugmentation(FlowCapableNode);
+    }
+
+    private def FlowCapableNodeConnector readFlowCapableNodeConnector(NodeConnectorRef ref) {
+        val dataObject = dataService.readOperationalData(ref.value as InstanceIdentifier<? extends DataObject>);
+        val node = dataObject.checkInstanceOf(
+            org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector);
+        return node.getAugmentation(FlowCapableNodeConnector);
+    }
+
+    private static def toNodeConnectorStatistics(
+        org.opendaylight.yang.gen.v1.urn.opendaylight.model.statistics.types.rev130925.NodeConnectorStatistics output) {
+        val it = new NodeConnectorStatistics
+
+        collisionCount = output.getCollisionCount().longValue();
+        receiveCRCErrorCount = output.getReceiveCrcError().longValue();
+        receiveFrameErrorCount = output.getReceiveFrameError().longValue();
+        receiveOverRunErrorCount = output.getReceiveOverRunError().longValue();
+
+        receiveDropCount = output.getReceiveDrops().longValue();
+        receiveErrorCount = output.getReceiveErrors().longValue();
+        receivePacketCount = output.getPackets().getReceived().longValue();
+        receiveByteCount = output.getBytes().getReceived().longValue();
+
+        transmitDropCount = output.getTransmitDrops().longValue();
+        transmitErrorCount = output.getTransmitErrors().longValue();
+        transmitPacketCount = output.getPackets().getTransmitted().longValue();
+        transmitByteCount = output.getBytes().getTransmitted().longValue();
+        return it;
+    }
+}