From: Tony Tkacik Date: Fri, 23 May 2014 15:03:27 +0000 (+0000) Subject: Merge "BUG-868: migrate InstanceIdentifer.builder() users" X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~28 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=c52298d11716c990ad681a69b6a96e725fd1c63e;hp=797699ffcb8cf2eabbc1fe3159c5e5c7b5ecbe98 Merge "BUG-868: migrate InstanceIdentifer.builder() users" --- diff --git a/opendaylight/md-sal/samples/l2switch/implementation/src/main/java/org/opendaylight/controller/sample/l2switch/md/util/InstanceIdentifierUtils.java b/opendaylight/md-sal/samples/l2switch/implementation/src/main/java/org/opendaylight/controller/sample/l2switch/md/util/InstanceIdentifierUtils.java index ea08f94ebc..c2be488d93 100644 --- a/opendaylight/md-sal/samples/l2switch/implementation/src/main/java/org/opendaylight/controller/sample/l2switch/md/util/InstanceIdentifierUtils.java +++ b/opendaylight/md-sal/samples/l2switch/implementation/src/main/java/org/opendaylight/controller/sample/l2switch/md/util/InstanceIdentifierUtils.java @@ -28,130 +28,129 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; /* InstanceIdentifierUtils provides utility functions related to InstanceIdentifiers. */ -public class InstanceIdentifierUtils { - - /** - * Creates an Instance Identifier (path) for node with specified id - * - * @param nodeId - * @return - */ - public static final InstanceIdentifier createNodePath(NodeId nodeId) { - return InstanceIdentifier.builder(Nodes.class) // - .child(Node.class, new NodeKey(nodeId)) // - .build(); - } - - /** - * Shorten's node child path to node path. - * - * @param nodeChild child of node, from which we want node path. - * @return - */ - public static final InstanceIdentifier getNodePath(InstanceIdentifier nodeChild) { - return nodeChild.firstIdentifierOf(Node.class); - } - - - /** - * Creates a table path by appending table specific location to node path - * - * @param nodePath - * @param tableKey - * @return - */ - public static final InstanceIdentifier createTablePath(InstanceIdentifier nodePath, TableKey tableKey) { - return InstanceIdentifier.builder(nodePath) - .augmentation(FlowCapableNode.class) - .child(Table.class, tableKey) - .build(); - } - - /** - * Creates a path for particular flow, by appending flow-specific information - * to table path. - * - * @param table - * @param flowKey - * @return - */ - public static InstanceIdentifier createFlowPath(InstanceIdentifier
table, FlowKey flowKey) { - return InstanceIdentifier.builder(table) - .child(Flow.class, flowKey) - .build(); - } - - /** - * Extract table id from table path. - * - * @param tablePath - * @return - */ - public static Short getTableId(InstanceIdentifier
tablePath) { - return tablePath.firstKeyOf(Table.class, TableKey.class).getId(); - } - - /** - * Extracts NodeConnectorKey from node connector path. - */ - public static NodeConnectorKey getNodeConnectorKey(InstanceIdentifier nodeConnectorPath) { - return nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class); - } - - /** - * Extracts NodeKey from node path. - */ - public static NodeKey getNodeKey(InstanceIdentifier nodePath) { - return nodePath.firstKeyOf(Node.class, NodeKey.class); - } - - - // - public static final InstanceIdentifier createNodeConnectorIdentifier(String nodeIdValue, - String nodeConnectorIdValue) { - return InstanceIdentifier.builder(createNodePath(new NodeId(nodeIdValue))) // - .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(nodeConnectorIdValue))) // - .build(); - } - - /** - * @param nodeConnectorRef - * @return - */ - public static InstanceIdentifier generateNodeInstanceIdentifier(NodeConnectorRef nodeConnectorRef) { - return nodeConnectorRef.getValue().firstIdentifierOf(Node.class); - } - - /** - * @param nodeConnectorRef - * @param flowTableKey - * @return - */ - public static InstanceIdentifier
generateFlowTableInstanceIdentifier(NodeConnectorRef nodeConnectorRef, TableKey flowTableKey) { - return InstanceIdentifier.builder(generateNodeInstanceIdentifier(nodeConnectorRef)) - .augmentation(FlowCapableNode.class) - .child(Table.class, flowTableKey) - .build(); - } - - /** - * @param nodeConnectorRef - * @param flowTableKey - * @param flowKey - * @return - */ - public static InstanceIdentifier generateFlowInstanceIdentifier(NodeConnectorRef nodeConnectorRef, - TableKey flowTableKey, - FlowKey flowKey) { - return InstanceIdentifier.builder(generateFlowTableInstanceIdentifier(nodeConnectorRef, flowTableKey)) - .child(Flow.class, flowKey) - .build(); - } - - public static InstanceIdentifier generateTopologyInstanceIdentifier(String topologyId) { - return InstanceIdentifier.builder(NetworkTopology.class) - .child(Topology.class, new TopologyKey(new TopologyId(topologyId))) - .build(); - } +public final class InstanceIdentifierUtils { + + private InstanceIdentifierUtils() { + throw new UnsupportedOperationException("Utility class should never be instantiated"); + } + + /** + * Creates an Instance Identifier (path) for node with specified id + * + * @param nodeId + * @return + */ + public static final InstanceIdentifier createNodePath(final NodeId nodeId) { + return InstanceIdentifier.builder(Nodes.class) // + .child(Node.class, new NodeKey(nodeId)) // + .build(); + } + + /** + * Shorten's node child path to node path. + * + * @param nodeChild child of node, from which we want node path. + * @return + */ + public static final InstanceIdentifier getNodePath(final InstanceIdentifier nodeChild) { + return nodeChild.firstIdentifierOf(Node.class); + } + + + /** + * Creates a table path by appending table specific location to node path + * + * @param nodePath + * @param tableKey + * @return + */ + public static final InstanceIdentifier
createTablePath(final InstanceIdentifier nodePath, final TableKey tableKey) { + return nodePath.builder() + .augmentation(FlowCapableNode.class) + .child(Table.class, tableKey) + .build(); + } + + /** + * Creates a path for particular flow, by appending flow-specific information + * to table path. + * + * @param table + * @param flowKey + * @return + */ + public static InstanceIdentifier createFlowPath(final InstanceIdentifier
table, final FlowKey flowKey) { + return table.child(Flow.class, flowKey); + } + + /** + * Extract table id from table path. + * + * @param tablePath + * @return + */ + public static Short getTableId(final InstanceIdentifier
tablePath) { + return tablePath.firstKeyOf(Table.class, TableKey.class).getId(); + } + + /** + * Extracts NodeConnectorKey from node connector path. + */ + public static NodeConnectorKey getNodeConnectorKey(final InstanceIdentifier nodeConnectorPath) { + return nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class); + } + + /** + * Extracts NodeKey from node path. + */ + public static NodeKey getNodeKey(final InstanceIdentifier nodePath) { + return nodePath.firstKeyOf(Node.class, NodeKey.class); + } + + + // + public static final InstanceIdentifier createNodeConnectorIdentifier(final String nodeIdValue, + final String nodeConnectorIdValue) { + return createNodePath(new NodeId(nodeIdValue)) + .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(nodeConnectorIdValue))); + } + + /** + * @param nodeConnectorRef + * @return + */ + public static InstanceIdentifier generateNodeInstanceIdentifier(final NodeConnectorRef nodeConnectorRef) { + return nodeConnectorRef.getValue().firstIdentifierOf(Node.class); + } + + /** + * @param nodeConnectorRef + * @param flowTableKey + * @return + */ + public static InstanceIdentifier
generateFlowTableInstanceIdentifier(final NodeConnectorRef nodeConnectorRef, final TableKey flowTableKey) { + return generateNodeInstanceIdentifier(nodeConnectorRef).builder() + .augmentation(FlowCapableNode.class) + .child(Table.class, flowTableKey) + .build(); + } + + /** + * @param nodeConnectorRef + * @param flowTableKey + * @param flowKey + * @return + */ + public static InstanceIdentifier generateFlowInstanceIdentifier(final NodeConnectorRef nodeConnectorRef, + final TableKey flowTableKey, + final FlowKey flowKey) { + return generateFlowTableInstanceIdentifier(nodeConnectorRef, flowTableKey).child(Flow.class, flowKey); + } + + public static InstanceIdentifier generateTopologyInstanceIdentifier(final String topologyId) { + return InstanceIdentifier.builder(NetworkTopology.class) + .child(Topology.class, new TopologyKey(new TopologyId(topologyId))) + .build(); + } } diff --git a/opendaylight/md-sal/samples/l2switch/implementation/src/test/java/org/opendaylight/controller/sample/l2switch/md/flow/FlowWriterServiceImplTest.java b/opendaylight/md-sal/samples/l2switch/implementation/src/test/java/org/opendaylight/controller/sample/l2switch/md/flow/FlowWriterServiceImplTest.java index 3520d812d7..50f74cc33f 100644 --- a/opendaylight/md-sal/samples/l2switch/implementation/src/test/java/org/opendaylight/controller/sample/l2switch/md/flow/FlowWriterServiceImplTest.java +++ b/opendaylight/md-sal/samples/l2switch/implementation/src/test/java/org/opendaylight/controller/sample/l2switch/md/flow/FlowWriterServiceImplTest.java @@ -7,11 +7,19 @@ */ package org.opendaylight.controller.sample.l2switch.md.flow; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService; import org.opendaylight.controller.sal.binding.api.data.DataBrokerService; import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction; +import org.opendaylight.controller.sample.l2switch.md.topology.NetworkGraphService; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef; @@ -23,128 +31,110 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - /** */ public class FlowWriterServiceImplTest { - private DataBrokerService dataBrokerService; - private NodeConnectorRef srcNodeConnectorRef; - private NodeConnectorRef destNodeConnectorRef; - private MacAddress destMacAddress; - private MacAddress srcMacAddress; - private DataModificationTransaction dataModificationTransaction; - private NetworkGraphService networkGraphService; + private DataBrokerService dataBrokerService; + private NodeConnectorRef srcNodeConnectorRef; + private NodeConnectorRef destNodeConnectorRef; + private MacAddress destMacAddress; + private MacAddress srcMacAddress; + private DataModificationTransaction dataModificationTransaction; + private NetworkGraphService networkGraphService; - @Before - public void init() { - dataBrokerService = mock(DataBrokerService.class); - networkGraphService = mock(NetworkGraphService.class); - //build source node connector ref - InstanceIdentifier srcNodesInstanceIdentifier - = InstanceIdentifier.builder(Nodes.class) - .build(); - InstanceIdentifier srcNodeInstanceIdentifier - = InstanceIdentifier.builder(srcNodesInstanceIdentifier) - .child(Node.class, new NodeKey(new NodeId("openflow:1"))) - .build(); - InstanceIdentifier srcNodeConnectorInstanceIdentifier - = InstanceIdentifier.builder(srcNodeInstanceIdentifier) - .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:1:2"))) - .build(); - srcNodeConnectorRef = new NodeConnectorRef(srcNodeConnectorInstanceIdentifier); + @Before + public void init() { + dataBrokerService = mock(DataBrokerService.class); + networkGraphService = mock(NetworkGraphService.class); + //build source node connector ref + InstanceIdentifier srcNodesInstanceIdentifier = InstanceIdentifier.create(Nodes.class); + InstanceIdentifier srcNodeInstanceIdentifier = srcNodesInstanceIdentifier + .child(Node.class, new NodeKey(new NodeId("openflow:1"))); + InstanceIdentifier srcNodeConnectorInstanceIdentifier = srcNodeInstanceIdentifier + .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:1:2"))); + srcNodeConnectorRef = new NodeConnectorRef(srcNodeConnectorInstanceIdentifier); - //build dest node connector ref - InstanceIdentifier nodesInstanceIdentifier + //build dest node connector ref + InstanceIdentifier nodesInstanceIdentifier = InstanceIdentifier.builder(Nodes.class) .build(); - InstanceIdentifier nodeInstanceIdentifier - = InstanceIdentifier.builder(nodesInstanceIdentifier) - .child(Node.class, new NodeKey(new NodeId("openflow:2"))) - .build(); - InstanceIdentifier nodeConnectorInstanceIdentifier - = InstanceIdentifier.builder(nodeInstanceIdentifier) - .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:2:2"))) - .build(); - destNodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier); - destMacAddress = new MacAddress("00:0a:95:9d:68:16"); - srcMacAddress = new MacAddress("00:0a:95:8c:97:24"); - dataModificationTransaction = mock(DataModificationTransaction.class); - when(dataBrokerService.beginTransaction()).thenReturn(dataModificationTransaction); - } + InstanceIdentifier nodeInstanceIdentifier = + nodesInstanceIdentifier.child(Node.class, new NodeKey(new NodeId("openflow:2"))); + InstanceIdentifier nodeConnectorInstanceIdentifier = + nodeInstanceIdentifier.child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId("openflow:2:2"))); + destNodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier); + destMacAddress = new MacAddress("00:0a:95:9d:68:16"); + srcMacAddress = new MacAddress("00:0a:95:8c:97:24"); + dataModificationTransaction = mock(DataModificationTransaction.class); + when(dataBrokerService.beginTransaction()).thenReturn(dataModificationTransaction); + } - @Test - public void testFlowWriterServiceImpl_NPEWhenDataBrokerServiceIsNull() throws Exception { - try { - new FlowWriterServiceImpl(null, networkGraphService); - fail("Expected null pointer exception."); - } catch(NullPointerException npe) { - assertEquals("dataBrokerService should not be null.", npe.getMessage()); + @Test + public void testFlowWriterServiceImpl_NPEWhenDataBrokerServiceIsNull() throws Exception { + try { + new FlowWriterServiceImpl(null, networkGraphService); + fail("Expected null pointer exception."); + } catch(NullPointerException npe) { + assertEquals("dataBrokerService should not be null.", npe.getMessage()); + } } - } - @Test - public void testAddMacToMacFlow_NPEWhenNullSourceMacDestMacAndNodeConnectorRef() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - try { - flowWriterService.addMacToMacFlow(null, null, null); - fail("Expected null pointer exception."); - } catch(NullPointerException npe) { - assertEquals("Destination mac address should not be null.", npe.getMessage()); + @Test + public void testAddMacToMacFlow_NPEWhenNullSourceMacDestMacAndNodeConnectorRef() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + try { + flowWriterService.addMacToMacFlow(null, null, null); + fail("Expected null pointer exception."); + } catch(NullPointerException npe) { + assertEquals("Destination mac address should not be null.", npe.getMessage()); + } } - } - @Test - public void testAddMacToMacFlow_NPEWhenSourceMacNullMac() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - try { - flowWriterService.addMacToMacFlow(null, null, destNodeConnectorRef); - fail("Expected null pointer exception."); - } catch(NullPointerException npe) { - assertEquals("Destination mac address should not be null.", npe.getMessage()); + @Test + public void testAddMacToMacFlow_NPEWhenSourceMacNullMac() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + try { + flowWriterService.addMacToMacFlow(null, null, destNodeConnectorRef); + fail("Expected null pointer exception."); + } catch(NullPointerException npe) { + assertEquals("Destination mac address should not be null.", npe.getMessage()); + } } - } - @Test - public void testAddMacToMacFlow_NPEWhenNullSourceMacNodeConnectorRef() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - try { - flowWriterService.addMacToMacFlow(null, destMacAddress, null); - fail("Expected null pointer exception."); - } catch(NullPointerException npe) { - assertEquals("Destination port should not be null.", npe.getMessage()); + @Test + public void testAddMacToMacFlow_NPEWhenNullSourceMacNodeConnectorRef() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + try { + flowWriterService.addMacToMacFlow(null, destMacAddress, null); + fail("Expected null pointer exception."); + } catch(NullPointerException npe) { + assertEquals("Destination port should not be null.", npe.getMessage()); + } } - } - @Test - public void testAddMacToMacFlow_WhenNullSourceMac() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - flowWriterService.addMacToMacFlow(null, destMacAddress, destNodeConnectorRef); - verify(dataBrokerService, times(1)).beginTransaction(); - verify(dataModificationTransaction, times(1)).commit(); - } + @Test + public void testAddMacToMacFlow_WhenNullSourceMac() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + flowWriterService.addMacToMacFlow(null, destMacAddress, destNodeConnectorRef); + verify(dataBrokerService, times(1)).beginTransaction(); + verify(dataModificationTransaction, times(1)).commit(); + } - @Test - public void testAddMacToMacFlow_WhenSrcAndDestMacAreSame() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - flowWriterService.addMacToMacFlow(new MacAddress(destMacAddress.getValue()), destMacAddress, destNodeConnectorRef); - verify(dataBrokerService, never()).beginTransaction(); - verify(dataModificationTransaction, never()).commit(); + @Test + public void testAddMacToMacFlow_WhenSrcAndDestMacAreSame() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + flowWriterService.addMacToMacFlow(new MacAddress(destMacAddress.getValue()), destMacAddress, destNodeConnectorRef); + verify(dataBrokerService, never()).beginTransaction(); + verify(dataModificationTransaction, never()).commit(); - } + } - @Test - public void testAddMacToMacFlow_SunnyDay() throws Exception { - FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); - flowWriterService.addMacToMacFlow(srcMacAddress, destMacAddress, destNodeConnectorRef); - verify(dataBrokerService, times(1)).beginTransaction(); - verify(dataModificationTransaction, times(1)).commit(); - } + @Test + public void testAddMacToMacFlow_SunnyDay() throws Exception { + FlowWriterService flowWriterService = new FlowWriterServiceImpl(dataBrokerService, networkGraphService); + flowWriterService.addMacToMacFlow(srcMacAddress, destMacAddress, destNodeConnectorRef); + verify(dataBrokerService, times(1)).beginTransaction(); + verify(dataModificationTransaction, times(1)).commit(); + } }