300e02001dd5117c0995c41f6d65d23ac2d6a89a
[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.util.ByteBufUtils;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.util.EnhancedTypeKeyMakerFactory;
21 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
24
25 /**
26  * Translates GroupMod messages
27  * @author timotej.kubas
28  * @author michal.polkorab
29  */
30 public class GroupModInputMessageFactory implements OFSerializer<GroupModInput>, SerializerRegistryInjector {
31     private static final byte MESSAGE_TYPE = 15;
32     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
33     private static final byte PADDING_IN_BUCKET = 4;
34     private SerializerRegistry registry;
35
36     @Override
37     public void serialize(GroupModInput message, ByteBuf outBuffer) {
38         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
39         outBuffer.writeShort(message.getCommand().getIntValue());
40         outBuffer.writeByte(message.getType().getIntValue());
41         ByteBufUtils.padBuffer(PADDING_IN_GROUP_MOD_MESSAGE, outBuffer);
42         outBuffer.writeInt(message.getGroupId().getValue().intValue());
43         serializerBuckets(message.getBucketsList(), outBuffer);
44         ByteBufUtils.updateOFHeaderLength(outBuffer);
45     }
46
47     private void serializerBuckets(List<BucketsList> buckets, ByteBuf outBuffer) {
48         if (buckets != null) {
49             for (BucketsList currentBucket : buckets) {
50                 int bucketLengthIndex = outBuffer.writerIndex();
51                 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
52                 outBuffer.writeShort(currentBucket.getWeight().shortValue());
53                 outBuffer.writeInt(currentBucket.getWatchPort().getValue().intValue());
54                 outBuffer.writeInt(currentBucket.getWatchGroup().intValue());
55                 ByteBufUtils.padBuffer(PADDING_IN_BUCKET, outBuffer);
56                 ListSerializer.serializeList(currentBucket.getAction(), EnhancedTypeKeyMakerFactory
57                         .createActionKeyMaker(EncodeConstants.OF13_VERSION_ID), registry, outBuffer);
58                 outBuffer.setShort(bucketLengthIndex, outBuffer.writerIndex() - bucketLengthIndex);
59             }
60         }
61     }
62
63     @Override
64     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
65         this.registry = serializerRegistry;
66     }
67
68 }