1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
\r
4 import io.netty.buffer.ByteBuf;
\r
6 import java.util.List;
\r
8 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
\r
9 import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
\r
10 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.BucketsList;
\r
15 * Translates GroupMod messages
\r
16 * @author timotej.kubas
\r
17 * @author michal.polkorab
\r
19 public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {
\r
20 private static final byte MESSAGE_TYPE = 15;
\r
21 private static final int MESSAGE_LENGTH = 16;
\r
22 private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
\r
23 private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16;
\r
24 private static final byte PADDING_IN_BUCKET = 4;
\r
25 private static GroupModInputMessageFactory instance;
\r
27 private GroupModInputMessageFactory() {
\r
32 * @return singleton factory
\r
34 public static synchronized GroupModInputMessageFactory getInstance() {
\r
35 if (instance == null) {
\r
36 instance = new GroupModInputMessageFactory();
\r
42 public void messageToBuffer(short version, ByteBuf out,
\r
43 GroupModInput message) {
\r
44 ByteBufUtils.writeOFHeader(instance, message, out);
\r
45 out.writeShort(message.getCommand().getIntValue());
\r
46 out.writeByte(message.getType().getIntValue());
\r
47 ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);
\r
48 out.writeInt(message.getGroupId().intValue());
\r
49 encodeBuckets(message.getBucketsList(), out);
\r
53 public int computeLength(GroupModInput message) {
\r
54 return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList());
\r
58 public byte getMessageType() {
\r
59 return MESSAGE_TYPE;
\r
62 private static void encodeBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
\r
63 if (buckets != null) {
\r
64 for (BucketsList currentBucket : buckets) {
\r
65 outBuffer.writeShort(computeLengthOfBucket(currentBucket));
\r
66 outBuffer.writeShort(currentBucket.getWeight().shortValue());
\r
67 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
\r
68 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
\r
69 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
\r
70 ActionsSerializer.encodeActions(currentBucket.getActionsList(), outBuffer);
\r
75 private static int computeLengthOfBucket(BucketsList bucket) {
\r
76 int lengthOfBuckets = 0;
\r
77 if (bucket != null) {
\r
78 lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(bucket.getActionsList());
\r
80 return lengthOfBuckets;
\r
83 private static int computeLengthOfBuckets(List<BucketsList> buckets) {
\r
84 int lengthOfBuckets = 0;
\r
85 if (buckets != null) {
\r
86 for (BucketsList currentBucket : buckets) {
\r
87 lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(currentBucket.getActionsList());
\r
90 return lengthOfBuckets;
\r