added new serialization factories and their tests
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactory.java
diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/PortModInputMessageFactory.java
new file mode 100644 (file)
index 0000000..f871df3
--- /dev/null
@@ -0,0 +1,104 @@
+/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
+package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;\r
+\r
+/**\r
+ * @author timotej.kubas\r
+ * @author michal.polkorab\r
+ */\r
+public class PortModInputMessageFactory implements OFSerializer<PortModInput> {\r
+    private static final byte MESSAGE_TYPE = 16;\r
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;\r
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;\r
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;\r
+    private static final int MESSAGE_LENGTH = 40;\r
+    private static PortModInputMessageFactory instance;\r
+    \r
+    private PortModInputMessageFactory() {\r
+        // singleton\r
+    }\r
+    \r
+    /**\r
+     * @return singleton factory\r
+     */\r
+    public static PortModInputMessageFactory getInstance() {\r
+        if (instance == null) {\r
+            instance = new PortModInputMessageFactory();\r
+        }\r
+        return instance;\r
+    }\r
+    \r
+    @Override\r
+    public void messageToBuffer(short version, ByteBuf out, PortModInput message) {\r
+        ByteBufUtils.writeOFHeader(instance, message, out);\r
+        out.writeInt(message.getPortNo().getValue().intValue());\r
+        ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, out);\r
+        out.writeBytes(ByteBufUtils.hexStringToBytes(message.getHwAddress().getValue(), false));\r
+        ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, out);\r
+        out.writeInt(createPortConfigBitmask(message.getConfig()));\r
+        out.writeInt(createPortConfigBitmask(message.getMask()));\r
+        out.writeInt(createPortFeaturesBitmask(message.getAdvertise()));\r
+        ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, out);\r
+    }\r
+\r
+    @Override\r
+    public int computeLength() {\r
+        return MESSAGE_LENGTH;\r
+    }\r
+\r
+    @Override\r
+    public byte getMessageType() {\r
+        return MESSAGE_TYPE;\r
+    }\r
+   \r
+    \r
+    /**\r
+     * @param config\r
+     * @return port config bitmask \r
+     */\r
+    private static int createPortConfigBitmask(PortConfig config) {\r
+        int configBitmask = 0;\r
+        Map<Integer, Boolean> portConfigMap = new HashMap<>();\r
+        portConfigMap.put(0, config.isPortDown());\r
+        portConfigMap.put(2, config.isNoRecv());\r
+        portConfigMap.put(5, config.isNoFwd());\r
+        portConfigMap.put(6, config.isNoPacketIn());\r
+        \r
+        configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);\r
+        return configBitmask;\r
+    }\r
+    \r
+    private static int createPortFeaturesBitmask(PortFeatures feature) {\r
+        int configBitmask = 0;\r
+        Map<Integer, Boolean> portFeaturesMap = new HashMap<>();\r
+        portFeaturesMap.put(0, feature.is_10mbHd());\r
+        portFeaturesMap.put(1, feature.is_10mbFd());\r
+        portFeaturesMap.put(2, feature.is_100mbHd());\r
+        portFeaturesMap.put(3, feature.is_100mbFd());\r
+        portFeaturesMap.put(4, feature.is_1gbHd());\r
+        portFeaturesMap.put(5, feature.is_1gbFd());\r
+        portFeaturesMap.put(6, feature.is_10gbFd());\r
+        portFeaturesMap.put(7, feature.is_40gbFd());\r
+        portFeaturesMap.put(8, feature.is_100gbFd());\r
+        portFeaturesMap.put(9, feature.is_1tbFd());\r
+        portFeaturesMap.put(10, feature.isOther());\r
+        portFeaturesMap.put(11, feature.isCopper());\r
+        portFeaturesMap.put(12, feature.isFiber());\r
+        portFeaturesMap.put(13, feature.isAutoneg());\r
+        portFeaturesMap.put(14, feature.isPause());\r
+        portFeaturesMap.put(15, feature.isPauseAsym());\r
+        \r
+        configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);\r
+        return configBitmask;\r
+    }\r
+}\r