Merge "Bug 2525 : remove zombie registrations of notification listener"
authormichal rehak <mirehak@cisco.com>
Thu, 8 Jan 2015 14:38:04 +0000 (14:38 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 8 Jan 2015 14:38:04 +0000 (14:38 +0000)
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/md/core/HandshakeListener.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/HandshakeManagerImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtil.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImplTest.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl2Test.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImplV13Test.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/util/ByteUtilTest.java

index 32081bc15c299baaf18711da8f99692ace5aafd5..d104b9cabb916ec18249ee79bd4cfb14307f2b8c 100644 (file)
@@ -21,4 +21,10 @@ public interface HandshakeListener {
      */
     void onHandshakeSuccessfull(GetFeaturesOutput featureOutput, Short version);
 
+    /**
+     * This method is called when handshake fails for some reason. It allows
+     * all necessary cleanup operations.
+     */
+    void onHandshakeFailure();
+
 }
index fe063ed1d525f4c97383f418455d318e81d16950..e892f575db90a57bc8be9895d7e17832f1cc351c 100644 (file)
@@ -8,11 +8,11 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
+import com.google.common.util.concurrent.Futures;
 import java.util.concurrent.Future;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
-
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
 import org.opendaylight.openflowplugin.api.OFConstants;
@@ -22,17 +22,16 @@ import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationEnqueuer;
 import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
-import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
-import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper;
 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueKeeper.QueueType;
-import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueProcessor;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.session.PortFeaturesUtil;
+import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.SwitchConfigFlag;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
@@ -50,7 +49,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestDescCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestGroupFeaturesCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestMeterFeaturesCaseBuilder;
@@ -64,15 +62,15 @@ import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.util.concurrent.Futures;
-
 /**
  * @author mirehak
  */
 public class ConnectionConductorImpl implements OpenflowProtocolListener,
-        SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener, NotificationEnqueuer {
+        SystemNotificationsListener, ConnectionConductor, ConnectionReadyListener, HandshakeListener, NotificationEnqueuer, AutoCloseable {
 
-    /** ingress queue limit */
+    /**
+     * ingress queue limit
+     */
     private static final int INGRESS_QUEUE_MAX_SIZE = 200;
 
     protected static final Logger LOG = LoggerFactory
@@ -99,14 +97,14 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     private HandshakeManager handshakeManager;
 
     private boolean firstHelloProcessed;
-    
+
     private PortFeaturesUtil portFeaturesUtils;
 
     private int conductorId;
 
     private int ingressMaxQueueSize;
 
-    
+
     /**
      * @param connectionAdapter
      */
@@ -133,10 +131,10 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     @Override
     public void init() {
         int handshakeThreadLimit = 1;
-        hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit , handshakeThreadLimit, 0L, 
-                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), 
-                "OFHandshake-"+conductorId);
-        
+        hsPool = new ThreadPoolLoggingExecutor(handshakeThreadLimit, handshakeThreadLimit, 0L,
+                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
+                "OFHandshake-" + conductorId);
+
         connectionAdapter.setMessageListener(this);
         connectionAdapter.setSystemListener(this);
         connectionAdapter.setConnectionReadyListener(this);
@@ -178,14 +176,14 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         enqueueMessage(errorMessage);
     }
 
-    
+
     /**
      * @param message
      */
     private void enqueueMessage(OfHeader message) {
         enqueueMessage(message, QueueType.DEFAULT);
     }
-    
+
     @Override
     public void enqueueNotification(NotificationQueueWrapper notification) {
         enqueueMessage(notification);
@@ -213,9 +211,9 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     /**
      * version negotiation happened as per following steps:
      * 1. If HelloMessage version field has same version, continue connection processing.
-     *    If HelloMessage version is lower than supported versions, just disconnect.
+     * If HelloMessage version is lower than supported versions, just disconnect.
      * 2. If HelloMessage contains bitmap and common version found in bitmap
-     *    then continue connection processing. if no common version found, just disconnect.
+     * then continue connection processing. if no common version found, just disconnect.
      * 3. If HelloMessage version is not supported, send HelloMessage with lower supported version.
      * 4. If Hello message received again with not supported version, just disconnect.
      */
@@ -260,7 +258,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         processPortStatusMsg(message);
         enqueueMessage(message);
     }
-    
+
     protected void processPortStatusMsg(PortStatus msg) {
         if (msg.getReason().getIntValue() == 2) {
             updatePort(msg);
@@ -270,22 +268,22 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
             deletePort(msg);
         }
     }
-    
+
     protected void updatePort(PortStatus msg) {
-        Long portNumber = msg.getPortNo();        
+        Long portNumber = msg.getPortNo();
         Boolean portBandwidth = portFeaturesUtils.getPortBandwidth(msg);
-        
-        if(portBandwidth == null) {
+
+        if (portBandwidth == null) {
             LOG.debug("can't get bandwidth info from port: {}, aborting port update", msg.toString());
         } else {
             this.getSessionContext().getPhysicalPorts().put(portNumber, msg);
-            this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);                   
-        }            
+            this.getSessionContext().getPortsBandwidth().put(portNumber, portBandwidth);
+        }
     }
-    
+
     protected void deletePort(PortGrouping port) {
         Long portNumber = port.getPortNo();
-        
+
         this.getSessionContext().getPhysicalPorts().remove(portNumber);
         this.getSessionContext().getPortsBandwidth().remove(portNumber);
     }
@@ -339,8 +337,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     }
 
     /**
-     * @param conductorState
-     *            the connectionState to set
+     * @param conductorState the connectionState to set
      */
     @Override
     public void setConductorState(CONDUCTOR_STATE conductorState) {
@@ -366,6 +363,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     public void onDisconnectEvent(DisconnectEvent arg0) {
         SessionManager sessionManager = OFSessionUtil.getSessionManager();
         sessionManager.invalidateOnDisconnect(this);
+        close();
     }
 
     @Override
@@ -384,7 +382,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
             LOG.debug("connection already disconnected");
             result = Futures.immediateFuture(true);
         }
-
+        close();
         return result;
     }
 
@@ -416,7 +414,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
     @Override
     public void onConnectionReady() {
         LOG.debug("connection is ready-to-use");
-        if (! firstHelloProcessed) {
+        if (!firstHelloProcessed) {
             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
                     null, handshakeManager, connectionAdapter);
             hsPool.execute(handshakeStepWrapper);
@@ -428,26 +426,33 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
 
     @Override
     public void onHandshakeSuccessfull(GetFeaturesOutput featureOutput,
-            Short negotiatedVersion) {
+                                       Short negotiatedVersion) {
         postHandshakeBasic(featureOutput, negotiatedVersion);
-        
+
         // post-handshake actions
-        if(version == OFConstants.OFP_VERSION_1_3){
+        if (version == OFConstants.OFP_VERSION_1_3) {
             requestPorts();
             requestGroupFeatures();
             requestMeterFeatures();
         }
-        
+
         requestDesc();
     }
 
+    @Override
+    public void onHandshakeFailure() {
+        LOG.info("OF handshake failed, doing cleanup.");
+        close();
+    }
+
     /**
      * used by tests
+     *
      * @param featureOutput
      * @param negotiatedVersion
      */
     protected void postHandshakeBasic(GetFeaturesOutput featureOutput,
-            Short negotiatedVersion) {
+                                      Short negotiatedVersion) {
         version = negotiatedVersion;
         if (version == OFConstants.OFP_VERSION_1_0) {
             //  Because the GetFeaturesOutput contains information about the port
@@ -457,7 +462,7 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
             // BUG-1988 - this must be the first item in queue in order not to get behind link-up message
             enqueueMessage(featureOutput);
         }
-        
+
         OFSessionUtil.registerSession(this, featureOutput, negotiatedVersion);
         hsPool.shutdown();
         hsPool.purge();
@@ -487,22 +492,24 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         builder.setXid(getSessionContext().getNextXid());
         getConnectionAdapter().multipartRequest(builder.build());
     }
-    private void requestGroupFeatures(){
+
+    private void requestGroupFeatures() {
         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
         mprInput.setType(MultipartType.OFPMPGROUPFEATURES);
         mprInput.setVersion(getVersion());
         mprInput.setFlags(new MultipartRequestFlags(false));
         mprInput.setXid(getSessionContext().getNextXid());
 
-        MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild = 
+        MultipartRequestGroupFeaturesCaseBuilder mprGroupFeaturesBuild =
                 new MultipartRequestGroupFeaturesCaseBuilder();
         mprInput.setMultipartRequestBody(mprGroupFeaturesBuild.build());
 
-        LOG.debug("Send group features statistics request :{}",mprGroupFeaturesBuild);
+        LOG.debug("Send group features statistics request :{}", mprGroupFeaturesBuild);
         getConnectionAdapter().multipartRequest(mprInput.build());
-        
+
     }
-    private void requestMeterFeatures(){
+
+    private void requestMeterFeatures() {
         MultipartRequestInputBuilder mprInput = new MultipartRequestInputBuilder();
         mprInput.setType(MultipartType.OFPMPMETERFEATURES);
         mprInput.setVersion(getVersion());
@@ -513,10 +520,11 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
                 new MultipartRequestMeterFeaturesCaseBuilder();
         mprInput.setMultipartRequestBody(mprMeterFeaturesBuild.build());
 
-        LOG.debug("Send meter features statistics request :{}",mprMeterFeaturesBuild);
+        LOG.debug("Send meter features statistics request :{}", mprMeterFeaturesBuild);
         getConnectionAdapter().multipartRequest(mprInput.build());
-        
+
     }
+
     /**
      * @param isBitmapNegotiationEnable the isBitmapNegotiationEnable to set
      */
@@ -529,9 +537,27 @@ public class ConnectionConductorImpl implements OpenflowProtocolListener,
         hsPool.shutdownNow();
         LOG.debug("pool is terminated: {}", hsPool.isTerminated());
     }
-    
+
+    protected void shutdownPoolPolitely() {
+        hsPool.shutdown();
+        try {
+            hsPool.awaitTermination(1, TimeUnit.SECONDS);
+        } catch (InterruptedException e) {
+            LOG.info("Error while awaiting termination on pool. Will use shutdownNow method.");
+            shutdownPool();
+        }
+        hsPool.purge();
+        LOG.debug("pool is terminated: {}", hsPool.isTerminated());
+    }
+
     @Override
     public void setId(int conductorId) {
         this.conductorId = conductorId;
     }
+
+    @Override
+    public void close() {
+        shutdownPoolPolitely();
+        conductorState = CONDUCTOR_STATE.RIP;
+    }
 }
index dd20aafb2dace6f93a5995ea0a93258ca9630545..2174fb112d60ab44f07fad9ca95a275d44b5e754 100644 (file)
@@ -119,6 +119,7 @@ public class HandshakeManagerImpl implements HandshakeManager {
         } catch (Exception ex) {
             errorHandler.handleException(ex, null);
             connectionAdapter.disconnect();
+            handshakeListener.onHandshakeFailure();
             LOG.trace("ret - shake fail: {}", ex.getMessage());
         }
     }
@@ -294,6 +295,7 @@ public class HandshakeManagerImpl implements HandshakeManager {
             if (LOG.isTraceEnabled()) {
                 LOG.trace("FIRST HELLO sent.", e);
             }
+            handshakeListener.onHandshakeFailure();
             throw new ConnectionException("FIRST HELLO sending failed because of connection issue.");
         }
     }
@@ -336,11 +338,13 @@ public class HandshakeManagerImpl implements HandshakeManager {
             // handshake failed
             LOG.warn("issuing disconnect during handshake, reason: future expired", e);
             connectionAdapter.disconnect();
+            handshakeListener.onHandshakeFailure();
             throw e;
         } catch (Exception e) {
             // handshake failed
             LOG.warn("issuing disconnect during handshake, reason - RPC: {}", e.getMessage(), e);
             connectionAdapter.disconnect();
+            handshakeListener.onHandshakeFailure();
             throw e;
         }
         
index 3b73311e9a9b0b1ecd5450c55a36cd1bf3039f37..ef88ccfaf8fd4b990832365f678c70ddc8bbb538 100644 (file)
@@ -971,7 +971,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                     pbbBuilder.setPbbIsid(isidMatchEntry.getIsid());
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
                     if (maskMatchEntry != null) {
-                        pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedInt(maskMatchEntry.getMask()));
+                        pbbBuilder.setPbbMask(ByteUtil.bytesToUnsignedMedium(maskMatchEntry.getMask()));
                     }
                     protocolMatchFieldsBuilder.setPbb(pbbBuilder.build());
                     matchBuilder.setProtocolMatchFields(protocolMatchFieldsBuilder.build());
@@ -1011,7 +1011,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
         matchEntriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());
         if (pbb.getPbbMask() != null) {
             hasmask = true;
-            addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedIntToBytes(pbb.getPbbMask()));
+            addMaskAugmentation(matchEntriesBuilder, ByteUtil.unsignedMediumToBytes(pbb.getPbbMask()));
         }
         matchEntriesBuilder.setHasMask(hasmask);
         return matchEntriesBuilder.build();
index bf2631fc02666897dec12e087f7d34a10ca27b9f..671c081393b8d91f7262559dea3f166992927674 100644 (file)
@@ -8,7 +8,6 @@
 package org.opendaylight.openflowplugin.openflow.md.util;
 
 import com.google.common.base.Preconditions;
-
 import java.math.BigInteger;
 import java.util.Arrays;
 
@@ -62,7 +61,7 @@ public abstract class ByteUtil {
      * @return a long representing the unsigned int
      */
     public static final long bytesToUnsignedInt(final byte[] bytes) {
-        Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly two bytes long.");
+        Preconditions.checkArgument(bytes.length == 4, "Input byte array must be exactly four bytes long.");
         long unsignedInt = 0;
         unsignedInt |= bytes[0] & 0xFF;
         unsignedInt <<= 8;
@@ -74,6 +73,23 @@ public abstract class ByteUtil {
         return unsignedInt;
     }
 
+    /**
+     * Converts a 3 byte array of unsigned bytes to unsigned int
+     *
+     * @param bytes an array of 4 unsigned bytes
+     * @return a long representing the unsigned int
+     */
+    public static final long bytesToUnsignedMedium(final byte[] bytes) {
+        Preconditions.checkArgument(bytes.length == 3, "Input byte array must be exactly three bytes long.");
+        long unsignedMedium = 0;
+        unsignedMedium |= bytes[0] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[1] & 0xFF;
+        unsignedMedium <<= 8;
+        unsignedMedium |= bytes[2] & 0xFF;
+        return unsignedMedium;
+    }
+
     /**
      * Converts a 2 byte array of unsigned bytes to unsigned short
      *
@@ -104,6 +120,20 @@ public abstract class ByteUtil {
         return bytes;
     }
 
+    /**
+     * Converts unsigned integer to a 3 byte array of unsigned bytes
+     *
+     * @param unsignedInt representing the unsigned integer
+     * @return bytes an array of 3 unsigned bytes
+     */
+    public static byte[] unsignedMediumToBytes(final Long unsignedInt) {
+        byte[] bytes = new byte[3];
+        bytes[2] = (byte) (unsignedInt & 0xFF);
+        bytes[1] = (byte) ((unsignedInt >> 8) & 0xFF);
+        bytes[0] = (byte) ((unsignedInt >> 16) & 0xFF);
+        return bytes;
+    }
+
     /**
      * Converts unsigned short to a 2 byte array of unsigned bytes
      *
index e46a87b46ff878d32e22bd46fe225304cdee884e..5d586ca12ac27e50eba47d574ab537dc0b54f5af 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.openflowplugin.openflow.md.core;
 
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.math.BigInteger;
 import java.util.ArrayList;
@@ -699,6 +700,12 @@ public class ConnectionConductorImplTest {
         Assert.assertEquals(connectionConductor.getSessionContext().getPhysicalPort(portNumberV10), msg);
     }
 
+
+    @Test
+    public void testHandshakeFailOperations(){
+        connectionConductor.onHandshakeFailure();
+        connectionConductor.checkState(ConnectionConductor.CONDUCTOR_STATE.RIP);
+    }
     private void simulateV13PostHandshakeState(ConnectionConductorImpl conductor) {
         GetFeaturesOutputBuilder featureOutput = getFeatureResponseMsg();
         conductor.postHandshakeBasic(featureOutput.build(), OFConstants.OFP_VERSION_1_3);
index d7692f0dd408b9523f427795db9986e98e1c6731..767c61a47639bfce4fd260ac7ad7a4989513dd7b 100644 (file)
@@ -652,9 +652,8 @@ public class MatchConvertorImpl2Test {
         checkEntryHeader(entry, PbbIsid.class, true);\r
         Assert.assertEquals("Wrong pbb isid", 20, entry.getAugmentation(IsidMatchEntry.class)\r
                 .getIsid().intValue());\r
-        // FIXME - fix mask computation (length should be 3 instead of 4)\r
-//        Assert.assertArrayEquals("Wrong pbb isid mask", new byte[]{0, 0, 8},\r
-//                entry.getAugmentation(MaskMatchEntry.class).getMask());\r
+        Assert.assertArrayEquals("Wrong pbb isid mask", new byte[]{0, 0, 8},\r
+                entry.getAugmentation(MaskMatchEntry.class).getMask());\r
         entry = entries.get(7);\r
         checkEntryHeader(entry, TunnelId.class, true);\r
         Assert.assertArrayEquals("Wrong tunnel id", new byte[]{0, 0, 0, 0, 0, 0, 0, 21},\r
index 65622c485fd79c082459c0c9699f476d674cd4ea..b62ed381c0d2ead42aad0cdc698a9b34e4cbd1ad 100644 (file)
@@ -497,18 +497,17 @@ public class MatchConvertorImplV13Test {
         maskBuilder.setMask(new byte[]{(byte) 255, (byte) 255, (byte) 240, 0});\r
         entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());\r
         entries.add(entriesBuilder.build());\r
-        // FIXME - PbbIsid should check for mask length of 3 instead of 2\r
-//        entriesBuilder = new MatchEntriesBuilder();\r
-//        entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
-//        entriesBuilder.setOxmMatchField(PbbIsid.class);\r
-//        entriesBuilder.setHasMask(true);\r
-//        IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();\r
-//        isidBuilder.setIsid(23L);\r
-//        entriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());\r
-//        maskBuilder = new MaskMatchEntryBuilder();\r
-//        maskBuilder.setMask(new byte[]{0, 0, 7});\r
-//        entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());\r
-//        entries.add(entriesBuilder.build());\r
+        entriesBuilder = new MatchEntriesBuilder();\r
+        entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
+        entriesBuilder.setOxmMatchField(PbbIsid.class);\r
+        entriesBuilder.setHasMask(true);\r
+        IsidMatchEntryBuilder isidBuilder = new IsidMatchEntryBuilder();\r
+        isidBuilder.setIsid(23L);\r
+        entriesBuilder.addAugmentation(IsidMatchEntry.class, isidBuilder.build());\r
+        maskBuilder = new MaskMatchEntryBuilder();\r
+        maskBuilder.setMask(new byte[]{0, 0, 7});\r
+        entriesBuilder.addAugmentation(MaskMatchEntry.class, maskBuilder.build());\r
+        entries.add(entriesBuilder.build());\r
         entriesBuilder = new MatchEntriesBuilder();\r
         entriesBuilder.setOxmClass(OpenflowBasicClass.class);\r
         entriesBuilder.setOxmMatchField(TunnelId.class);\r
index e0e3590d236ede3942ca894a48b9902041fc025e..8fdd1a25e8af4ea4dd8a906ff2956f811abb9e23 100644 (file)
@@ -7,13 +7,12 @@
 
 package org.opendaylight.openflowplugin.openflow.md.util;
 
-import org.junit.Test;
-
-import java.math.BigInteger;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.math.BigInteger;
+import org.junit.Test;
+
 /**
  * Created by Martin Bobak mbobak@cisco.com on 6/30/14.
  */
@@ -27,11 +26,19 @@ public class ByteUtilTest {
     private static final byte[] testBytes00 = {0, 0, 0, 0};
     private static final byte[] testBytesFF = {(byte) 255, (byte) 255, (byte) 255, (byte) 255};
 
+    private static final byte[] test3Bytes = {100, 101, 102};
+    private static final byte[] test3Bytes00 = {0, 0, 0};
+    private static final byte[] test3BytesFF = {(byte) 255, (byte) 255, (byte) 255};
+
     private static final BigInteger bigInteger = new BigInteger("1684367103");
     private static final BigInteger bigIntFF = new BigInteger("4294967295");
+
+    private static final Integer mediumInteger = new Integer("6579558");
+    private static final Integer mediumIntegerFF = new Integer("16777215");
     private static final int int00 = 0;
 
     private static final int shortByteLength = 2;
+    private static final int mediumByteLength = 3;
     private static final int intByteLength = 4;
 
     @Test
@@ -81,6 +88,18 @@ public class ByteUtilTest {
         assertEquals(bigIntFF.shiftRight(16).intValue(), unsigned);
     }
 
+    @Test
+    public void testBytesToUnsignedMedium() {
+        long unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes);
+        assertEquals(mediumInteger.longValue(), unsigned);
+
+        unsigned = ByteUtil.bytesToUnsignedMedium(test3Bytes00);
+        assertEquals(0, unsigned);
+
+        unsigned = ByteUtil.bytesToUnsignedMedium(test3BytesFF);
+        assertEquals(mediumIntegerFF.longValue(), unsigned);
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void exceptionTestBytesToUnsignedShort() {
         ByteUtil.bytesToUnsignedShort(testBytes);
@@ -123,4 +142,21 @@ public class ByteUtilTest {
         bytes = ByteUtil.unsignedShortToBytes(intValue);
         assertTrue(bytes.length == shortByteLength);
     }
+
+    @Test
+    public void testUnsignedMediumToBytes() {
+        long intValue = 255;
+        byte[] bytes = ByteUtil.unsignedMediumToBytes(intValue);
+
+        assertTrue(bytes.length == mediumByteLength);
+
+        intValue += 256;
+        bytes = ByteUtil.unsignedMediumToBytes(intValue);
+        assertTrue(bytes.length == mediumByteLength);
+
+        intValue += 256;
+        bytes = ByteUtil.unsignedMediumToBytes(intValue);
+        assertTrue(bytes.length == mediumByteLength);
+    }
+
 }