Merge "Remove Optional.ofNullable() antipatterns"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / messages / GroupMessageDeserializerTest.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.deserialization.messages;
10
11 import static org.junit.Assert.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.UnpooledByteBufAllocator;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.ActionConstants;
18 import org.opendaylight.openflowplugin.impl.protocol.deserialization.AbstractDeserializerTest;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCase;
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.GroupTypes;
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 public class GroupMessageDeserializerTest extends AbstractDeserializerTest {
26
27     private static final byte PADDING = 1;
28     private static final byte PADDING_IN_BUCKETS_HEADER = 4;
29
30     private static final int TYPE = 15;
31     private static final int XID = 42;
32     private static final GroupModCommand COMMAND = GroupModCommand.OFPGCADD;
33     private static final GroupTypes GROUP_TYPE = GroupTypes.GroupAll;
34     private static final int GROUP_ID = 26;
35     private static final short WEIGHT = 50;
36     private static final int WATCH_PORT = 22;
37     private static final int WATCH_GROUP = 25;
38
39     private ByteBuf buffer;
40
41     @Override
42     protected void init() {
43         buffer = UnpooledByteBufAllocator.DEFAULT.buffer();
44     }
45
46     @Test
47     public void deserialize() {
48         // Group header
49         buffer.writeByte(TYPE);
50         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
51         buffer.writeInt(XID);
52         buffer.writeShort(COMMAND.getIntValue());
53         buffer.writeByte(GROUP_TYPE.getIntValue());
54         buffer.writeZero(PADDING);
55         buffer.writeInt(GROUP_ID);
56
57         // Buckets header
58         int index = buffer.writerIndex();
59         buffer.writeShort(EncodeConstants.EMPTY_LENGTH);
60         buffer.writeShort(WEIGHT);
61         buffer.writeInt(WATCH_PORT);
62         buffer.writeInt(WATCH_GROUP);
63         buffer.writeZero(PADDING_IN_BUCKETS_HEADER);
64
65         // POP PBB action
66         buffer.writeShort(ActionConstants.POP_PBB_CODE);
67         buffer.writeShort(ActionConstants.GENERAL_ACTION_LENGTH);
68         buffer.writeZero(ActionConstants.PADDING_IN_ACTION_HEADER);
69
70         // Count total length of buckets
71         buffer.setShort(index, buffer.writerIndex() - index);
72
73         // Deserialize and check everything
74         final GroupMessage message = (GroupMessage) getFactory()
75             .deserialize(buffer, EncodeConstants.OF13_VERSION_ID);
76
77         assertEquals(XID, message.getXid().intValue());
78         assertEquals(COMMAND.getIntValue(), message.getCommand().getIntValue());
79         assertEquals(GROUP_TYPE.getIntValue(), message.getGroupType().getIntValue());
80         assertEquals(1, message.getBuckets().getBucket().size());
81
82         final Bucket bucket = message.getBuckets().getBucket().get(0);
83         assertEquals(WEIGHT, bucket.getWeight().shortValue());
84         assertEquals(WATCH_PORT, bucket.getWatchPort().intValue());
85         assertEquals(WATCH_GROUP, bucket.getWatchGroup().intValue());
86         assertEquals(1, bucket.getAction().size());
87         assertEquals(PopPbbActionCase.class, bucket.getAction().get(0).getAction().implementedInterface());
88     }
89
90 }