--- /dev/null
+/**
+ * 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();
+ }
+
+}
--- /dev/null
+/**
+ * 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)));
+ }
+
+}