Update OF header lenght
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactory.java
index 5c55e103078678ac70654d5fae1a495271dbcbc9..03c39887f8e786d5365d37b30848183bd0384613 100644 (file)
-/* 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 synchronized 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
+/*
+ * 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.serialization.factories;
+
+import io.netty.buffer.ByteBuf;
+import java.util.HashMap;
+import java.util.Map;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
+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;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortMod;
+
+/**
+ * Translates PortMod messages.
+ * OF protocol versions: 1.3.
+ * @author timotej.kubas
+ * @author michal.polkorab
+ */
+public class PortModInputMessageFactory implements OFSerializer<PortMod> {
+    private static final byte MESSAGE_TYPE = 16;
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
+    private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
+
+    @Override
+    public void serialize(final PortMod message, final ByteBuf outBuffer) {
+        int index = outBuffer.writerIndex();
+        ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+        outBuffer.writeInt(message.getPortNo().getValue().intValue());
+        outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
+        outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHwAddress()));
+        outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
+        outBuffer.writeInt(createPortConfigBitmask(message.getConfig()));
+        outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
+        outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
+        outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
+        ByteBufUtils.updateOFHeaderLength(outBuffer, index);
+    }
+
+    /**
+     * @param config
+     * @return port config bitmask
+     */
+    private static int createPortConfigBitmask(final PortConfig config) {
+        int configBitmask = 0;
+        Map<Integer, Boolean> portConfigMap = new HashMap<>();
+        portConfigMap.put(0, config.isPortDown());
+        portConfigMap.put(2, config.isNoRecv());
+        portConfigMap.put(5, config.isNoFwd());
+        portConfigMap.put(6, config.isNoPacketIn());
+
+        configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
+        return configBitmask;
+    }
+
+    private static int createPortFeaturesBitmask(final PortFeatures feature) {
+        return ByteBufUtils.fillBitMask(0, feature.is_10mbHd(),
+                feature.is_10mbFd(),
+                feature.is_100mbHd(),
+                feature.is_100mbFd(),
+                feature.is_1gbHd(),
+                feature.is_1gbFd(),
+                feature.is_10gbFd(),
+                feature.is_40gbFd(),
+                feature.is_100gbFd(),
+                feature.is_1tbFd(),
+                feature.isOther(),
+                feature.isCopper(),
+                feature.isFiber(),
+                feature.isAutoneg(),
+                feature.isPause(),
+                feature.isPauseAsym());
+    }
+
+}