-/* 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.List;\r
-\r
-import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;\r
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.BucketsList;\r
-\r
-/**\r
- * Translates GroupMod messages\r
- * @author timotej.kubas\r
- * @author michal.polkorab\r
- */\r
-public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {\r
- private static final byte MESSAGE_TYPE = 15;\r
- private static final int MESSAGE_LENGTH = 16;\r
- private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;\r
- private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16;\r
- private static final byte PADDING_IN_BUCKET = 4;\r
- private static GroupModInputMessageFactory instance;\r
-\r
- private GroupModInputMessageFactory() {\r
- // singleton\r
- }\r
- \r
- /**\r
- * @return singleton factory\r
- */\r
- public static synchronized GroupModInputMessageFactory getInstance() {\r
- if (instance == null) {\r
- instance = new GroupModInputMessageFactory();\r
- }\r
- return instance;\r
- }\r
- \r
- @Override\r
- public void messageToBuffer(short version, ByteBuf out,\r
- GroupModInput message) {\r
- ByteBufUtils.writeOFHeader(instance, message, out);\r
- out.writeShort(message.getCommand().getIntValue());\r
- out.writeByte(message.getType().getIntValue());\r
- ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);\r
- out.writeInt(message.getGroupId().getValue().intValue());\r
- encodeBuckets(message.getBucketsList(), out);\r
- }\r
-\r
- @Override\r
- public int computeLength(GroupModInput message) {\r
- return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList());\r
- }\r
-\r
- @Override\r
- public byte getMessageType() {\r
- return MESSAGE_TYPE;\r
- }\r
- \r
- private static void encodeBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {\r
- if (buckets != null) {\r
- for (BucketsList currentBucket : buckets) {\r
- outBuffer.writeShort(computeLengthOfBucket(currentBucket));\r
- outBuffer.writeShort(currentBucket.getWeight().shortValue());\r
- outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());\r
- outBuffer.writeInt(currentBucket.getWatchGroup().intValue());\r
- ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);\r
- ActionsSerializer.encodeActions(currentBucket.getActionsList(), outBuffer);\r
- }\r
- }\r
- }\r
- \r
- private static int computeLengthOfBucket(BucketsList bucket) {\r
- int lengthOfBuckets = 0;\r
- if (bucket != null) {\r
- lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(bucket.getActionsList());\r
- }\r
- return lengthOfBuckets;\r
- }\r
- \r
- private static int computeLengthOfBuckets(List<BucketsList> buckets) {\r
- int lengthOfBuckets = 0;\r
- if (buckets != null) {\r
- for (BucketsList currentBucket : buckets) {\r
- lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(currentBucket.getActionsList());\r
- }\r
- }\r
- return lengthOfBuckets;\r
- }\r
-\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.List;
+
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
+import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.util.EnhancedTypeKeyMakerFactory;
+import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
+
+/**
+ * Translates GroupMod messages
+ * @author timotej.kubas
+ * @author michal.polkorab
+ */
+public class GroupModInputMessageFactory implements OFSerializer<GroupModInput>, SerializerRegistryInjector {
+ private static final byte MESSAGE_TYPE = 15;
+ private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
+ private static final byte PADDING_IN_BUCKET = 4;
+ private SerializerRegistry registry;
+
+ @Override
+ public void serialize(GroupModInput message, ByteBuf outBuffer) {
+ ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
+ outBuffer.writeShort(message.getCommand().getIntValue());
+ outBuffer.writeByte(message.getType().getIntValue());
+ ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, outBuffer);
+ outBuffer.writeInt(message.getGroupId().getValue().intValue());
+ serializerBuckets(message.getBucketsList(), outBuffer);
+ ByteBufUtils.updateOFHeaderLength(outBuffer);
+ }
+
+ private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
+ if (buckets != null) {
+ for (BucketsList currentBucket : buckets) {
+ int bucketLengthIndex = outBuffer.writerIndex();
+ outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
+ outBuffer.writeShort(currentBucket.getWeight().shortValue());
+ outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
+ outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
+ ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
+ ListSerializer.serializeList(currentBucket.getAction(), EnhancedTypeKeyMakerFactory
+ .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer);
+ outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex);
+ }
+ }
+ }
+
+ @Override
+ public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
+ this.registry = serializerRegistry;
+ }
+
+}