OF10ActionsSerializer and OF13ActionsSerializer split into separate actions (to allow...
[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
13 import java.util.List;
14
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
19 import org.opendaylight.openflowjava.protocol.impl.util.CodingUtils;
20 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
23
24 /**
25  * Translates GroupMod messages
26  * @author timotej.kubas
27  * @author michal.polkorab
28  */
29 public class GroupModInputMessageFactory implements OFSerializer<GroupModInput>, 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(GroupModInput 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         ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, outBuffer);
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                 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
55                 CodingUtils.serializeActions(currentBucket.getAction(), registry,
56                         outBuffer, EncodeConstants.OF13_VERSION_ID);
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 }