Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PortStatusMessageFactory.java
index a87c9a5b75a1d02416f6be42b92a9395169fb3da..2ffc23c28345f0be3cc6f4c5b5dc4ad7e24c5883 100644 (file)
@@ -1,9 +1,18 @@
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.deserialization.factories;
 
 import io.netty.buffer.ByteBuf;
 
-import org.opendaylight.openflowjava.protocol.impl.deserialization.OFDeserializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 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.openflow.common.types.rev130731.PortConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
@@ -19,43 +28,24 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
  */
 public class PortStatusMessageFactory implements OFDeserializer<PortStatusMessage> {
 
-    private static PortStatusMessageFactory instance;
     private static final byte PADDING_IN_PORT_STATUS_HEADER = 7;
     private static final byte PADDING_IN_OFP_PORT_HEADER_1 = 4;
     private static final byte PADDING_IN_OFP_PORT_HEADER_2 = 2;
-    private static final int macAddressLength = 6;
-    
-    private PortStatusMessageFactory() {
-        // Singleton
-    }
-    
-    /**
-     * @return singleton factory
-     */
-    public static synchronized PortStatusMessageFactory getInstance(){
-        if(instance == null){
-            instance = new PortStatusMessageFactory();
-        }
-        return instance;
-    }
-    
+
     @Override
-    public PortStatusMessage bufferToMessage(ByteBuf rawMessage, short version) {
+    public PortStatusMessage deserialize(ByteBuf rawMessage) {
         PortStatusMessageBuilder builder = new PortStatusMessageBuilder(); 
-        builder.setVersion(version);
+        builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
         builder.setXid(rawMessage.readUnsignedInt());
         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
         builder.setPortNo(rawMessage.readUnsignedInt());
         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_1);
-        StringBuffer macToString = new StringBuffer();
-        for(int i=0; i<macAddressLength; i++){
-            short mac = 0;
-            mac = rawMessage.readUnsignedByte();
-            macToString.append(String.format("%02X", mac));
-        }
-        builder.setHwAddr(new MacAddress(macToString.toString()));
+        byte[] hwAddress = new byte[EncodeConstants.MAC_ADDRESS_LENGTH];
+        rawMessage.readBytes(hwAddress);
+        builder.setHwAddr(new MacAddress(ByteBufUtils.macAddressToString(hwAddress)));
         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_2);
+        builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
         builder.setConfig(createPortConfig(rawMessage.readUnsignedInt()));
         builder.setState(createPortState(rawMessage.readUnsignedInt()));
         builder.setCurrentFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
@@ -84,8 +74,8 @@ public class PortStatusMessageFactory implements OFDeserializer<PortStatusMessag
         final Boolean _autoneg = ((input) & (1<<13)) != 0;
         final Boolean _pause = ((input) & (1<<14)) != 0;
         final Boolean _pauseAsym = ((input) & (1<<15)) != 0;
-        return new PortFeatures(_10mbHd, _10mbFd, _100mbHd, _100mbFd, _1gbHd, _1gbFd, _10gbFd,
-                _40gbFd, _100gbFd, _1tbFd, _other, _copper, _fiber, _autoneg, _pause, _pauseAsym);
+        return new PortFeatures(_100gbFd, _100mbFd, _100mbHd, _10gbFd, _10mbFd, _10mbHd, _1gbFd,
+                _1gbHd, _1tbFd, _40gbFd, _autoneg, _copper, _fiber, _other, _pause, _pauseAsym);
     }
     
     private static PortState createPortState(long input){
@@ -102,5 +92,4 @@ public class PortStatusMessageFactory implements OFDeserializer<PortStatusMessag
         final Boolean _noPacketIn = ((input) & (1<<6)) != 0;
         return new PortConfig(_noFwd, _noPacketIn, _noRecv, _portDown);
     }
-    
 }