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