Merge "Added Inventory Reader for SwitchManager"
authorEd Warnicke <eaw@cisco.com>
Thu, 7 Nov 2013 14:02:52 +0000 (14:02 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 7 Nov 2013 14:08:39 +0000 (14:08 +0000)
opendaylight/md-sal/compatibility/inventory-topology-compatibility/src/main/java/org/opendaylight/controller/md/compatibility/inventory/InventoryReader.xtend [new file with mode: 0644]
opendaylight/md-sal/compatibility/inventory-topology-compatibility/src/main/java/org/opendaylight/controller/md/compatibility/topology/TopologyMapping.xtend
opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryMapping.xtend [new file with mode: 0644]
opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/adsal/FlowServiceAdapter.java
opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/TestFromSalConversionsUtils.java
opendaylight/md-sal/compatibility/sal-compatibility/src/test/java/org/opendaylight/controller/sal/compatibility/test/TestToSalConversionsUtils.java

diff --git a/opendaylight/md-sal/compatibility/inventory-topology-compatibility/src/main/java/org/opendaylight/controller/md/compatibility/inventory/InventoryReader.xtend b/opendaylight/md-sal/compatibility/inventory-topology-compatibility/src/main/java/org/opendaylight/controller/md/compatibility/inventory/InventoryReader.xtend
new file mode 100644 (file)
index 0000000..262e848
--- /dev/null
@@ -0,0 +1,83 @@
+package org.opendaylight.controller.md.compatibility.inventory
+
+import org.opendaylight.controller.switchmanager.ISwitchManager
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
+import org.opendaylight.yangtools.yang.binding.DataObject
+import org.opendaylight.controller.sal.binding.api.data.RuntimeDataProvider
+import java.util.ArrayList
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
+import static extension org.opendaylight.controller.sal.compatibility.InventoryMapping.*;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder
+
+class InventoryReader implements RuntimeDataProvider {
+
+    @Property
+    var ISwitchManager switchManager;
+
+    override readConfigurationData(InstanceIdentifier<? extends DataObject> path) {
+
+        // Topology and Inventory are operational only
+        return null;
+    }
+
+    override readOperationalData(InstanceIdentifier<? extends DataObject> path) {
+        val type = path.targetType;
+        var DataObject data = null;
+        switch (type) {
+            case Nodes:
+                data = readNodes(path as InstanceIdentifier<Nodes>)
+            case Node:
+                data = readNode(path as InstanceIdentifier<Node>)
+            case NodeConnector:
+                data = readNodeConnector(path as InstanceIdentifier<NodeConnector>)
+        }
+        return data;
+    }
+
+    def DataObject readNodeConnector(InstanceIdentifier<NodeConnector> identifier) {
+        val nodeConnector = identifier.toAdNodeConnector();
+        return constructNodeConnector(nodeConnector)
+    }
+
+    def DataObject readNode(InstanceIdentifier<Node> identifier) {
+        val node = identifier.toAdNode();
+        return constructNode(node);
+    }
+
+
+    def Node constructNode(org.opendaylight.controller.sal.core.Node node) {
+        val connectors = switchManager.getNodeConnectors(node)
+
+        val tpList = new ArrayList<NodeConnector>(connectors.size)
+        for (connector : connectors) {
+            tpList.add(constructNodeConnector(connector));
+        }
+
+        val it = new NodeBuilder()
+        key = node.toNodeKey();
+        nodeConnector = tpList
+        return build();
+    }
+
+    def NodeConnector constructNodeConnector(org.opendaylight.controller.sal.core.NodeConnector connector) {
+        val it = new NodeConnectorBuilder()
+        key = connector.toNodeConnectorKey()
+        return build();
+    }
+
+    def readNodes(InstanceIdentifier<Nodes> identifier) {
+        val nodes = switchManager.nodes
+        val nodeList = new ArrayList<Node>(nodes.size)
+        for (node : nodes) {
+            nodeList.add(constructNode(node))
+        }
+        val it = new NodesBuilder();
+        node = nodeList
+        return build()
+
+    }
+}
index 2ae74de..5a4aae3 100644 (file)
@@ -19,7 +19,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev130712.LinkId
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev130712.network.topology.topology.Node
-
+import org.opendaylight.controller.sal.compatibility.InventoryMapping
 class TopologyMapping {
 
     new(TopologyKey path, InstanceIdentifier<Topology> key) {
@@ -29,32 +29,32 @@ class TopologyMapping {
     def Edge toAdTopologyEdge(InstanceIdentifier<Link> identifier) {
         val linkKey = (identifier.path.last as IdentifiableItem<Link,LinkKey>).key;
         val components = linkKey.linkId.value.split("::::");
-        val tail = nodeConnectorFromString(components.get(0).split("::"));
-        val head = nodeConnectorFromString(components.get(1).split("::"));
+        val tail = InventoryMapping.nodeConnectorFromId(components.get(0));
+        val head = InventoryMapping.nodeConnectorFromId(components.get(1));
         return new Edge(tail, head);
     }
 
     def NodeConnector toAdTopologyNodeConnector(InstanceIdentifier<TerminationPoint> identifier) {
         val tpKey = (identifier.path.last as IdentifiableItem<TerminationPoint,TerminationPointKey>).key;
-        return nodeConnectorFromString(tpKey.tpId.value.split("::"));
+        return InventoryMapping.nodeConnectorFromId(tpKey.tpId.value);
     }
 
     def org.opendaylight.controller.sal.core.Node toAdTopologyNode(
         InstanceIdentifier<Node> identifier) {
         val tpKey = (identifier.path.last as IdentifiableItem<Node,NodeKey>).key;
-        return nodeFromString(tpKey.nodeId.value.split("::"));
+        return InventoryMapping.nodeFromNodeId(tpKey.nodeId.value);
     }
     
 
 
     def NodeKey toTopologyNodeKey(org.opendaylight.controller.sal.core.Node node) {
-        val nodeId = new NodeId('''ad-sal:«node.type»::«node.nodeIDString»''')
+        val nodeId = new NodeId(InventoryMapping.toNodeId(node));
         return new NodeKey(nodeId);
     }
 
     def TerminationPointKey toTopologyTerminationPointKey(NodeConnector nc) {
         val node = nc.node;
-        val nodeId = new TpId('''ad-sal:«node.type»::«node.nodeIDString»::«nc.nodeConnectorIDString»''')
+        val nodeId = new TpId(InventoryMapping.toNodeConnectorId(nc))
         return new TerminationPointKey(nodeId);
     }
 
@@ -64,14 +64,4 @@ class TopologyMapping {
         val linkId = new LinkId('''«sourceTp.tpId»::::«destTp.tpId»''')
         return new LinkKey(linkId);
     }
-
-    def NodeConnector nodeConnectorFromString(String[] string) {
-        val node = nodeFromString(string.subList(0,1));
-        return NodeConnector.fromStringNoNode(string.get(2),node);
-    }
-    
-    def org.opendaylight.controller.sal.core.Node nodeFromString(String[] strings) {
-        val type = strings.get(0).substring(6);
-        org.opendaylight.controller.sal.core.Node.fromString(type,strings.get(1))
-    }
 }
diff --git a/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryMapping.xtend b/opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/InventoryMapping.xtend
new file mode 100644 (file)
index 0000000..0ea9918
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.compatibility
+
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
+
+class InventoryMapping {
+
+    static def org.opendaylight.controller.sal.core.NodeConnector toAdNodeConnector(
+        InstanceIdentifier<NodeConnector> identifier) {
+        val tpKey = (identifier.path.last as IdentifiableItem<NodeConnector,NodeConnectorKey>).key;
+        return nodeConnectorFromId(tpKey.id.value);
+    }
+
+    static def org.opendaylight.controller.sal.core.Node toAdNode(InstanceIdentifier<Node> identifier) {
+        val tpKey = (identifier.path.last as IdentifiableItem<Node,NodeKey>).key;
+        return nodeFromNodeId(tpKey.id.value);
+    }
+    
+    
+     static def NodeRef toNodeRef(org.opendaylight.controller.sal.core.Node node) {
+        val nodeId = new NodeKey(new NodeId(node.toNodeId))
+        val path = InstanceIdentifier.builder().node(Nodes).child(Node,nodeId).toInstance;
+        return new NodeRef(path);
+    }
+
+    static def NodeKey toNodeKey(org.opendaylight.controller.sal.core.Node node) {
+        val nodeId = new NodeId(node.toNodeId)
+        return new NodeKey(nodeId);
+    }
+
+    static def NodeConnectorKey toNodeConnectorKey(org.opendaylight.controller.sal.core.NodeConnector nc) {
+        val nodeId = new NodeConnectorId(nc.toNodeConnectorId)
+        return new NodeConnectorKey(nodeId);
+    }
+
+    static def String toNodeId(org.opendaylight.controller.sal.core.Node node) {
+        '''ad-sal:«node.type»::«node.nodeIDString»'''
+    }
+
+    static def String toNodeConnectorId(org.opendaylight.controller.sal.core.NodeConnector nc) {
+        '''«nc.node.toNodeId»::«nc.nodeConnectorIDString»'''
+    }
+
+    static def org.opendaylight.controller.sal.core.Node nodeFromNodeId(String nodeId) {
+        return nodeFromString(nodeId.split("::"))
+    }
+
+    static def nodeConnectorFromId(String invId) {
+        return nodeConnectorFromString(invId.split("::"));
+    }
+
+    private static def org.opendaylight.controller.sal.core.NodeConnector nodeConnectorFromString(String[] string) {
+        val node = nodeFromString(string.subList(0, 1));
+        return org.opendaylight.controller.sal.core.NodeConnector.fromStringNoNode(string.get(2), node);
+    }
+
+    private static def org.opendaylight.controller.sal.core.Node nodeFromString(String[] strings) {
+        val type = strings.get(0).substring(6);
+        org.opendaylight.controller.sal.core.Node.fromString(type, strings.get(1))
+    }
+
+}
index 6267c04..ae42745 100644 (file)
@@ -5,10 +5,10 @@ import java.util.concurrent.Future;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.controller.sal.common.util.Futures;
 import org.opendaylight.controller.sal.common.util.Rpcs;
+import org.opendaylight.controller.sal.compatibility.InventoryMapping;
 import org.opendaylight.controller.sal.compatibility.NodeMapping;
 import org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils;
 import org.opendaylight.controller.sal.core.ConstructionException;
-import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.flowprogrammer.Flow;
 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerListener;
 import org.opendaylight.controller.sal.flowprogrammer.IFlowProgrammerService;
@@ -18,6 +18,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.Flow
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -31,58 +33,52 @@ public class FlowServiceAdapter implements SalFlowService, IFlowProgrammerListen
     private NotificationProviderService publish;
 
     @Override
-    public void flowRemoved(Node node, Flow flow) {
+    public void flowRemoved(org.opendaylight.controller.sal.core.Node node, Flow flow) {
         FlowRemovedBuilder flowRemovedBuilder = new FlowRemovedBuilder();
-        flowRemovedBuilder.setNode(NodeMapping.toNodeRef(node));
+        flowRemovedBuilder.setNode(InventoryMapping.toNodeRef(node));
         publish.publish(flowRemovedBuilder.build());
     }
 
     @Override
-    public void flowErrorReported(Node node, long rid, Object err) {
+    public void flowErrorReported(org.opendaylight.controller.sal.core.Node node, long rid, Object err) {
         // TODO Auto-generated method stub
 
     }
 
     @Override
     public Future<RpcResult<Void>> addFlow(AddFlowInput input) {
-        try {
-            Flow flow = ToSalConversionsUtils.toFlow(input);
-            Node node = NodeMapping.toADNode(input.getNode());
-            Status status = delegate.addFlowAsync(node, flow);
-            Void rpcResultType = null;
-            return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
-        } catch (ConstructionException e) {
-            LOG.error(e.getMessage());
-        }
-        return null;
+
+        Flow flow = ToSalConversionsUtils.toFlow(input);
+        @SuppressWarnings("unchecked")
+        org.opendaylight.controller.sal.core.Node node = InventoryMapping.toAdNode((InstanceIdentifier<Node>) input
+                .getNode().getValue());
+        Status status = delegate.addFlowAsync(node, flow);
+        Void rpcResultType = null;
+        return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
     }
 
     @Override
     public Future<RpcResult<Void>> removeFlow(RemoveFlowInput input) {
-        try {
-            Flow flow = ToSalConversionsUtils.toFlow(input);
-            Node node = NodeMapping.toADNode(input.getNode());
-            Status status = delegate.removeFlowAsync(node, flow);
-            Void rpcResultType = null;
-            return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
-        } catch (ConstructionException e) {
-            LOG.error(e.getMessage());
-        }
-        return null;
+
+        Flow flow = ToSalConversionsUtils.toFlow(input);
+        @SuppressWarnings("unchecked")
+        org.opendaylight.controller.sal.core.Node node = InventoryMapping.toAdNode((InstanceIdentifier<Node>) input
+                .getNode().getValue());
+        Status status = delegate.removeFlowAsync(node, flow);
+        Void rpcResultType = null;
+        return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
+
     }
 
     @Override
     public Future<RpcResult<Void>> updateFlow(UpdateFlowInput input) {
-        try {
-            Node node = NodeMapping.toADNode(input.getNode());
-            Flow originalFlow = ToSalConversionsUtils.toFlow(input.getOriginalFlow());
-            Flow updatedFlow = ToSalConversionsUtils.toFlow(input.getUpdatedFlow());
-            Status status = delegate.modifyFlowAsync(node, originalFlow, updatedFlow);
-            Void rpcResultType = null;
-            return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
-        } catch (ConstructionException e) {
-            LOG.error(e.getMessage());
-        }
-        return null;
+        @SuppressWarnings("unchecked")
+        org.opendaylight.controller.sal.core.Node node = InventoryMapping.toAdNode((InstanceIdentifier<Node>) input
+                .getNode().getValue());
+        Flow originalFlow = ToSalConversionsUtils.toFlow(input.getOriginalFlow());
+        Flow updatedFlow = ToSalConversionsUtils.toFlow(input.getUpdatedFlow());
+        Status status = delegate.modifyFlowAsync(node, originalFlow, updatedFlow);
+        Void rpcResultType = null;
+        return Futures.immediateFuture(Rpcs.getRpcResult(status.isSuccess(), rpcResultType, null));
     }
 }
index 1c8b772..c37cce0 100644 (file)
@@ -1,4 +1,11 @@
-package org.opendaylight.controller.sal.compability;
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+*
+* This program and the accompanying materials are made available under the
+* terms of the Eclipse Public License v1.0 which accompanies this distribution,
+* and is available at http://www.eclipse.org/legal/epl-v10.html
+*/
+package org.opendaylight.controller.sal.compatibility.test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
index 2b344c0..e251dd0 100644 (file)
@@ -1,4 +1,11 @@
-package org.opendaylight.controller.sal.compability;
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.compatibility.test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.