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