Add bundle messages serializers
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GroupModInputMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
18 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
19 import org.opendaylight.openflowjava.util.ByteBufUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupMod;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
22
23 /**
24  * Translates GroupMod messages.
25  * OF protocol versions: 1.3.
26  * @author timotej.kubas
27  * @author michal.polkorab
28  */
29 public class GroupModInputMessageFactory implements OFSerializer<GroupMod>, SerializerRegistryInjector {
30     private static final byte MESSAGE_TYPE = 15;
31     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
32     private static final byte PADDING_IN_BUCKET = 4;
33     private SerializerRegistry registry;
34
35     @Override
36     public void serialize(GroupMod message, ByteBuf outBuffer) {
37         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
38         outBuffer.writeShort(message.getCommand().getIntValue());
39         outBuffer.writeByte(message.getType().getIntValue());
40         outBuffer.writeZero(PADDING_IN_GROUP_MOD_MESSAGE);
41         outBuffer.writeInt(message.getGroupId().getValue().intValue());
42         serializerBuckets(message.getBucketsList(), outBuffer);
43         ByteBufUtils.updateOFHeaderLength(outBuffer);
44     }
45
46     private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
47         if (buckets != null) {
48             for (BucketsList currentBucket : buckets) {
49                 int bucketLengthIndex = outBuffer.writerIndex();
50                 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
51                 outBuffer.writeShort(currentBucket.getWeight().shortValue());
52                 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
53                 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
54                 outBuffer.writeZero(PADDING_IN_BUCKET);
55                 ListSerializer.serializeList(currentBucket.getAction(), TypeKeyMakerFactory
56                         .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer);
57                 outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex);
58             }
59         }
60     }
61
62     @Override
63     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
64         this.registry = serializerRegistry;
65     }
66
67 }