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=18155039ff6604625f84d67d903528f04e5d4061;hb=60ad94454217d7d1f6f9e65f39f72ae111ecc845;hp=6fa791d3f53574761161eb509e6e04c9066c1555;hpb=0d9c30aa4c3e66f92eca0a867d3cf678b07770ca;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 6fa791d3..18155039 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,93 +1,68 @@ -/* 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; - -/** - * @author timotej.kubas - * @author michal.polkorab - */ -public class GroupModInputMessageFactory implements OFSerializer { - private static final byte MESSAGE_TYPE = 15; - private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1; - private static final int MESSAGE_LENGTH = 16; - private static final byte LENGTH_OF_BUCKET_STRUCTURE = 16; - 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) { - final byte PADDING_IN_BUCKET = 4; - 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.util.ByteBufUtils; +import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants; +import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory; +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, 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 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(), TypeKeyMakerFactory + .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer); + outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex); + } + } + } + + @Override + public void injectSerializerRegistry(SerializerRegistry serializerRegistry) { + this.registry = serializerRegistry; + } + +}