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