Merge "DeviceState implementation"
authormichal rehak <mirehak@cisco.com>
Tue, 31 Mar 2015 09:25:45 +0000 (09:25 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 31 Mar 2015 09:25:46 +0000 (09:25 +0000)
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceState.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImpl.java [new file with mode: 0644]
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceStateImplTest.java [new file with mode: 0644]

index 09036fd364089a92fadeb0a7e63ba519ba3a8bfc..d5e926d8d85d210ed8d1bb27b3f572a17bce4c10 100644 (file)
@@ -52,7 +52,7 @@ public interface DeviceState {
      *
      * @return The Map of bandwidths for all OFPorts
      */
-    Map<Long, Boolean> getPortsBandwidth();
+    Map<Long, Long> 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<PortGrouping> 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 (file)
index 0000000..8ed7c24
--- /dev/null
@@ -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
+ * <p/>
+ * DeviceState is builded from {@link FeaturesReply} and {@link NodeId}. Both values are inside
+ * {@link org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext}
+ *
+ * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
+ *         <p/>
+ *         Created: Mar 29, 2015
+ */
+class DeviceStateImpl implements DeviceState {
+
+    private final GetFeaturesOutput featuresOutput;
+    private final Map<Long, PortGrouping> portGrouping;
+    private final Map<Long, Long> 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<Long, PortGrouping> getPhysicalPorts() {
+        return portGrouping;
+    }
+
+    @Override
+    public Map<Long, Long> getPortsBandwidth() {
+        return portsBandwidth;
+    }
+
+    @Override
+    public Set<Long> 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<PortGrouping> getEnabledPorts() {
+        return new ArrayList<PortGrouping>(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 (file)
index 0000000..de04101
--- /dev/null
@@ -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 <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
+ *
+ * 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<PhyPort> 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<Long, PortGrouping> 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<Long, Long> 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<Long> 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<PortGrouping> getEnabledPort = deviceState.getEnabledPorts();
+        Assert.assertNotNull(getEnabledPort);
+        Assert.assertTrue(getEnabledPort.contains(pPort.get(0)));
+    }
+
+}