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