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