X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflow-protocol-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowjava%2Fprotocol%2Fimpl%2Fserialization%2Ffactories%2FGroupModInputMessageFactory.java;h=95a3b7db47ee620f33718d0f8960e666ef66c0ed;hb=ce4d2f2c0077fff015c3cde8c144fe543b915cba;hp=b60a7c29327e6b53389f0d6e8e0aa7d6da152caf;hpb=b0c862db7ff00227cc2f1bf23fad7b4c96d08493;p=openflowjava.git diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java index b60a7c29..95a3b7db 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/GroupModInputMessageFactory.java @@ -1,94 +1,67 @@ -/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */ -package org.opendaylight.openflowjava.protocol.impl.serialization.factories; - -import io.netty.buffer.ByteBuf; - -import java.util.List; - -import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer; -import org.opendaylight.openflowjava.protocol.impl.util.ActionsSerializer; -import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.BucketsList; - -/** - * Translates GroupMod messages - * @author timotej.kubas - * @author michal.polkorab - */ -public class GroupModInputMessageFactory implements OFSerializer { - private static final byte MESSAGE_TYPE = 15; - private static final int MESSAGE_LENGTH = 16; - private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1; - private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16; - private static final byte PADDING_IN_BUCKET = 4; - private static GroupModInputMessageFactory instance; - - private GroupModInputMessageFactory() { - // singleton - } - - /** - * @return singleton factory - */ - public static synchronized GroupModInputMessageFactory getInstance() { - if (instance == null) { - instance = new GroupModInputMessageFactory(); - } - return instance; - } - - @Override - public void messageToBuffer(short version, ByteBuf out, - GroupModInput message) { - ByteBufUtils.writeOFHeader(instance, message, out); - out.writeShort(message.getCommand().getIntValue()); - out.writeByte(message.getType().getIntValue()); - ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, out); - out.writeInt(message.getGroupId().intValue()); - encodeBuckets(message.getBucketsList(), out); - } - - @Override - public int computeLength(GroupModInput message) { - return MESSAGE_LENGTH + computeLengthOfBuckets(message.getBucketsList()); - } - - @Override - public byte getMessageType() { - return MESSAGE_TYPE; - } - - private static void encodeBuckets(List buckets, ByteBuf outBuffer) { - if (buckets != null) { - for (BucketsList currentBucket : buckets) { - outBuffer.writeShort(computeLengthOfBucket(currentBucket)); - outBuffer.writeShort(currentBucket.getWeight().shortValue()); - outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue()); - outBuffer.writeInt(currentBucket.getWatchGroup().intValue()); - ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer); - ActionsSerializer.encodeActions(currentBucket.getActionsList(), outBuffer); - } - } - } - - private static int computeLengthOfBucket(BucketsList bucket) { - int lengthOfBuckets = 0; - if (bucket != null) { - lengthOfBuckets = LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(bucket.getActionsList()); - } - return lengthOfBuckets; - } - - private static int computeLengthOfBuckets(List buckets) { - int lengthOfBuckets = 0; - if (buckets != null) { - for (BucketsList currentBucket : buckets) { - lengthOfBuckets += LENGTH_OF_BUCKET_STRUCTURE + ActionsSerializer.computeLengthOfActions(currentBucket.getActionsList()); - } - } - return lengthOfBuckets; - } - - -} +/* + * 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.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer; +import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory; +import org.opendaylight.openflowjava.util.ByteBufUtils; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupMod; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList; + +/** + * Translates GroupMod messages. + * OF protocol versions: 1.3. + * @author timotej.kubas + * @author michal.polkorab + */ +public class GroupModInputMessageFactory implements OFSerializer, 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(GroupMod message, ByteBuf outBuffer) { + ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH); + outBuffer.writeShort(message.getCommand().getIntValue()); + outBuffer.writeByte(message.getType().getIntValue()); + outBuffer.writeZero(PADDING_IN_GROUP_MOD_MESSAGE); + outBuffer.writeInt(message.getGroupId().getValue().intValue()); + serializerBuckets(message.getBucketsList(), outBuffer); + ByteBufUtils.updateOFHeaderLength(outBuffer); + } + + private void serializerBuckets(List 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()); + outBuffer.writeZero(PADDING_IN_BUCKET); + ListSerializer.serializeList(currentBucket.getAction(), TypeKeyMakerFactory + .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer); + outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex); + } + } + } + + @Override + public void injectSerializerRegistry(SerializerRegistry serializerRegistry) { + this.registry = serializerRegistry; + } + +}