From 3237a17a946e95af341d269713e8ade864e074ed Mon Sep 17 00:00:00 2001 From: Martin Bobak Date: Sun, 29 Mar 2015 05:18:44 +0200 Subject: [PATCH] DeviceState implementation Note: DeviceState is a basic object which we need to populate from ConnectionContext and it has to be a input object for DeviceContext. So this implementation has to be before DeviceContext implementation in the chain order. * DeviceState interface - fix getNextXid and getPortsBandwidth methods * impl DeviceStateImpl * impl test suite in DeviceStateImplTest Change-Id: Iadf19754c3373b25a6a320c676118d13338a0097 Signed-off-by: Vaclav Demcak Signed-off-by: Martin Bobak --- .../api/openflow/device/DeviceState.java | 9 +- .../impl/device/DeviceStateImpl.java | 127 ++++++++++++ .../impl/device/DeviceStateImplTest.java | 182 ++++++++++++++++++ 3 files changed, 311 insertions(+), 7 deletions(-) create mode 100644 openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImpl.java create mode 100644 openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImplTest.java diff --git a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceState.java b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceState.java index 09036fd364..d5e926d8d8 100644 --- a/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceState.java +++ b/openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceState.java @@ -52,7 +52,7 @@ public interface DeviceState { * * @return The Map of bandwidths for all OFPorts */ - Map getPortsBandwidth(); + Map getPortsBandwidth(); /** * Returns a Set containing all port IDs of this switch. @@ -75,7 +75,7 @@ public interface DeviceState { * @param portNumber the port ID * @return bandwidth */ - Boolean getPortBandwidth(Long portNumber); + Long getPortBandwidth(Long portNumber); /** * Returns True if the port is enabled, @@ -100,11 +100,6 @@ public interface DeviceState { */ List getEnabledPorts(); - /** - * @return the unique xid for this session - */ - Long getNextXid(); - /** * @return seed value for random operations */ diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImpl.java new file mode 100644 index 0000000000..8ed7c24c32 --- /dev/null +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImpl.java @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2015 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.openflowplugin.impl.device; + +import com.google.common.base.Preconditions; +import org.opendaylight.openflowplugin.api.openflow.device.DeviceState; +import org.opendaylight.openflowplugin.api.openflow.device.XidGenerator; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * openflowplugin-impl + * org.opendaylight.openflowplugin.impl.device + *

+ * DeviceState is builded from {@link FeaturesReply} and {@link NodeId}. Both values are inside + * {@link org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext} + * + * @author Vaclav Demcak + *

+ * Created: Mar 29, 2015 + */ +class DeviceStateImpl implements DeviceState { + + private final GetFeaturesOutput featuresOutput; + private final Map portGrouping; + private final Map portsBandwidth; + private final XidGenerator xidGenerator; + private final NodeId nodeId; + private boolean valid; + + public DeviceStateImpl(@CheckForNull final FeaturesReply featuresReply, @Nonnull final NodeId nodeId) { + Preconditions.checkArgument(featuresReply != null); + Preconditions.checkArgument(featuresReply.getPhyPort() != null); + featuresOutput = new GetFeaturesOutputBuilder(featuresReply).build(); + this.nodeId = Preconditions.checkNotNull(nodeId); + xidGenerator = new XidGenerator(); + portGrouping = new HashMap<>(); + portsBandwidth = new HashMap<>(); + for (final PhyPort port : featuresReply.getPhyPort()) { + portGrouping.put(port.getPortNo(), port); + portsBandwidth.put(port.getPortNo(), port.getMaxSpeed()); + } + } + + @Override + public NodeId getNodeId() { + return nodeId; + } + + @Override + public GetFeaturesOutput getFeatures() { + return featuresOutput; + } + + @Override + public boolean isValid() { + return valid; + } + + @Override + public void setValid(final boolean valid) { + this.valid = valid; + } + + @Override + public Map getPhysicalPorts() { + return portGrouping; + } + + @Override + public Map getPortsBandwidth() { + return portsBandwidth; + } + + @Override + public Set getPorts() { + return portGrouping.keySet(); + } + + @Override + public PortGrouping getPhysicalPort(final Long portNumber) { + return portGrouping.get(portNumber); + } + + @Override + public Long getPortBandwidth(final Long portNumber) { + return portsBandwidth.get(portNumber); + } + + @Override + public boolean isPortEnabled(final long portNumber) { + return portGrouping.containsKey(portNumber); + } + + @Override + public boolean isPortEnabled(final PortGrouping port) { + return portGrouping.containsValue(port); + } + + @Override + public List getEnabledPorts() { + return new ArrayList(portGrouping.values()); + } + + @Override + public int getSeed() { + return hashCode(); + } + +} diff --git a/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImplTest.java b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImplTest.java new file mode 100644 index 0000000000..de04101958 --- /dev/null +++ b/openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImplTest.java @@ -0,0 +1,182 @@ +/** + * Copyright (c) 2015 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.openflowplugin.impl.device; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPortBuilder; + +/** + * openflowplugin-impl + * org.opendaylight.openflowplugin.impl.device + * + * test of {@link DeviceStateImpl} - lightweight version, using basic ways (TDD) + * + * @author Vaclav Demcak + * + * Created: Mar 29, 2015 + */ +@RunWith(MockitoJUnitRunner.class) +public class DeviceStateImplTest { + + private NodeId nodeId; + @Mock + private FeaturesReply featuresReply; + private DeviceStateImpl deviceState; + + private final short version = 13; + private final long portNr = 10L; + private final Long portBandwidth = 1024L; + private final List pPort = Arrays.asList(new PhyPortBuilder() + .setPortNo(portNr).setMaxSpeed(portBandwidth).build()); + + @Before + public void initialization() { + nodeId = new NodeId("test-node-id"); + Mockito.when(featuresReply.getVersion()).thenReturn(version); + Mockito.when(featuresReply.getPhyPort()).thenReturn(pPort); + deviceState = new DeviceStateImpl(featuresReply, nodeId); + } + + /** + * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}. + */ + @Test(expected=NullPointerException.class) + public void testDeviceStateImplNullNodeId(){ + new DeviceStateImpl(featuresReply, null); + } + + /** + * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}. + */ + @Test(expected=IllegalArgumentException.class) + public void testDeviceStateImplNullFeaturesReply(){ + new DeviceStateImpl(null, nodeId); + } + + /** + * Test method for {@link DeviceStateImpl#DeviceStateImpl(FeaturesReply, NodeId)}. + */ + @Test(expected=IllegalArgumentException.class) + public void testDeviceStateImplNullPhyPort(){ + final FeaturesReply emptyFeaturesReply = Mockito.mock(FeaturesReply.class); + Mockito.when(emptyFeaturesReply.getPhyPort()).thenReturn(null); + new DeviceStateImpl(emptyFeaturesReply, nodeId); + } + + /** + * Test method for {@link DeviceStateImpl#getNodeId()}. + */ + @Test + public void testGetNodeId(){ + final NodeId getNodeId = deviceState.getNodeId(); + Assert.assertNotNull(getNodeId); + Assert.assertEquals(nodeId, getNodeId); + } + + /** + * Test method for {@link DeviceStateImpl#getFeatures()}. + */ + @Test + public void testGetFeatures(){ + final GetFeaturesOutputBuilder expetedResult = new GetFeaturesOutputBuilder(featuresReply); + final GetFeaturesOutput getFeatures = deviceState.getFeatures(); + Assert.assertNotNull(getFeatures); + Assert.assertEquals(expetedResult.getVersion(), getFeatures.getVersion()); + Assert.assertEquals(expetedResult.getPhyPort(), getFeatures.getPhyPort()); + } + + /** + * Test method for {@link DeviceStateImpl#getPhysicalPorts()}. + */ + @Test + public void testGetPhysicalPorts(){ + final Map getPhysPort = deviceState.getPhysicalPorts(); + Assert.assertNotNull(getPhysPort); + Assert.assertTrue(getPhysPort.values().contains(pPort.get(0))); + } + + /** + * Test method for {@link DeviceStateImpl#getPortsBandwidth()}. + */ + @Test + public void testGetPortsBandwidth(){ + final Map portBandwidth = deviceState.getPortsBandwidth(); + Assert.assertNotNull(portBandwidth); + Assert.assertTrue(portBandwidth.containsKey(portNr)); + Assert.assertEquals(this.portBandwidth, portBandwidth.get(portNr)); + } + + /** + * Test method for {@link DeviceStateImpl#getPorts()}. + */ + @Test + public void testGetPorts(){ + final Set portNrs = deviceState.getPorts(); + Assert.assertTrue(portNrs.contains(portNr)); + } + + /** + * Test method for {@link DeviceStateImpl#getPhysicalPort(java.lang.Long)}. + */ + @Test + public void testGetPhysicalPort(){ + Assert.assertEquals(pPort.get(0), deviceState.getPhysicalPort(portNr)); + } + + /** + * Test method for {@link DeviceStateImpl#getPortBandwidth(java.lang.Long)}. + */ + @Test + public void testGetPortBandwidth(){ + Assert.assertEquals(portBandwidth, deviceState.getPortBandwidth((portNr))); + } + + /** + * Test method for {@link DeviceStateImpl#isPortEnabled(long)}. + */ + @Test + public void testIsPortEnabledLong(){ + Assert.assertTrue(deviceState.isPortEnabled(portNr)); + } + + /** + * Test method for {@link DeviceStateImpl#isPortEnabled(PortGrouping)}. + */ + @Test + public void testIsPortEnabledPortGrouping(){ + Assert.assertTrue(deviceState.isPortEnabled(pPort.get(0))); + } + + /** + * Test method for {@link DeviceStateImpl#getEnabledPorts()}. + */ + @Test + public void testGetEnabledPorts(){ + final List getEnabledPort = deviceState.getEnabledPorts(); + Assert.assertNotNull(getEnabledPort); + Assert.assertTrue(getEnabledPort.contains(pPort.get(0))); + } + +} -- 2.36.6