Merge "Add GroupMessageSerializer"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / messages / GroupMessageSerializer.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.impl.protocol.serialization.messages;
10
11 import com.google.common.base.MoreObjects;
12 import io.netty.buffer.ByteBuf;
13 import java.util.Comparator;
14 import java.util.Optional;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.openflowplugin.api.OFConstants;
20 import org.opendaylight.openflowplugin.impl.protocol.serialization.util.ActionUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
23
24 /**
25  * Translates GroupMod messages.
26  * OF protocol versions: 1.3.
27  */
28 public class GroupMessageSerializer extends AbstractMessageSerializer<GroupMessage> implements SerializerRegistryInjector {
29     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
30     private static final byte PADDING_IN_BUCKET = 4;
31
32     private static final Comparator<Bucket> COMPARATOR = (bucket1, bucket2) -> {
33         if (bucket1.getBucketId() == null || bucket2.getBucketId() == null) return 0;
34         return bucket1.getBucketId().getValue().compareTo(bucket2.getBucketId().getValue());
35     };
36
37     private SerializerRegistry registry;
38
39     @Override
40     public void serialize(GroupMessage message, ByteBuf outBuffer) {
41         super.serialize(message, outBuffer);
42         outBuffer.writeShort(message.getCommand().getIntValue());
43         outBuffer.writeByte(message.getGroupType().getIntValue());
44         outBuffer.writeZero(PADDING_IN_GROUP_MOD_MESSAGE);
45         outBuffer.writeInt(message.getGroupId().getValue().intValue());
46
47         Optional.ofNullable(message.getBuckets())
48                 .flatMap(b -> Optional.ofNullable(b.getBucket()))
49                 .ifPresent(b -> b.stream()
50                         .sorted(COMPARATOR)
51                         .forEach(bucket -> {
52                             int index = outBuffer.writerIndex();
53                             outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
54                             outBuffer.writeShort(MoreObjects.firstNonNull(bucket.getWeight(), 0));
55                             outBuffer.writeInt(MoreObjects.firstNonNull(bucket.getWatchPort(), OFConstants.OFPG_ANY).intValue());
56                             outBuffer.writeInt(MoreObjects.firstNonNull(bucket.getWatchGroup(), OFConstants.OFPG_ANY).intValue());
57                             outBuffer.writeZero(PADDING_IN_BUCKET);
58
59                             Optional.ofNullable(bucket.getAction()).ifPresent(as -> as.forEach(a ->
60                                     ActionUtil.writeAction(
61                                             a.getAction(),
62                                             OFConstants.OFP_VERSION_1_3,
63                                             registry,
64                                             outBuffer)));
65
66                             outBuffer.setShort(index, outBuffer.writerIndex() - index);
67                         }));
68
69         ByteBufUtils.updateOFHeaderLength(outBuffer);
70     }
71
72     @Override
73     protected byte getMessageType() {
74         return 15;
75     }
76
77     @Override
78     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
79         registry = serializerRegistry;
80     }
81 }