2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.BucketsList;
22 * Translates GroupMod messages
23 * @author timotej.kubas
24 * @author michal.polkorab
26 public class GroupModInputMessageFactory implements OFSerializer<GroupModInput> {
27 private static final byte MESSAGE_TYPE = 15;
28 private static final int MESSAGE_LENGTH = 16;
29 private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
30 private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16;
31 private static final byte PADDING_IN_BUCKET = 4;
32 private static GroupModInputMessageFactory instance;
34 private GroupModInputMessageFactory() {
39 * @return singleton factory
41 public static synchronized GroupModInputMessageFactory getInstance() {
42 if (instance == null) {
43 instance = new GroupModInputMessageFactory();
49 public void messageToBuffer(short version, ByteBuf out,
50 GroupModInput message) {
51 ByteBufUtils.writeOFHeader(instance, message, out);
52 out.writeShort(message.getCommand().getIntValue());
53 out.writeByte(message.getType().getIntValue());
54 ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out);
55 out.writeInt(message.getGroupId().getValue().intValue());
56 encodeBuckets(message.getBucketsList(), out);
60 public int computeLength(GroupModInput message) {
61 return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList());
65 public byte getMessageType() {
69 private static void encodeBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
70 if (buckets != null) {
71 for (BucketsList currentBucket : buckets) {
72 outBuffer.writeShort(computeLengthOfBucket(currentBucket));
73 outBuffer.writeShort(currentBucket.getWeight().shortValue());
74 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
75 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
76 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
77 ActionsSerializer.encodeActions(currentBucket.getActionsList(), outBuffer);
82 private static int computeLengthOfBucket(BucketsList bucket) {
83 int lengthOfBuckets = 0;
85 lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(bucket.getActionsList());
87 return lengthOfBuckets;
90 private static int computeLengthOfBuckets(List<BucketsList> buckets) {
91 int lengthOfBuckets = 0;
92 if (buckets != null) {
93 for (BucketsList currentBucket : buckets) {
94 lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(currentBucket.getActionsList());
97 return lengthOfBuckets;