Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / multipart / MultipartReplyFlowTest.java
1 /*
2  * Copyright (c) 2014 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.openflowjava.protocol.impl.deserialization.factories.multipart;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.math.BigInteger;
14
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.MultipartReplyMessageFactory;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStats;
31
32 /**
33  * @author michal.polkorab
34  *
35  */
36 public class MultipartReplyFlowTest {
37
38     private OFDeserializer<MultipartReplyMessage> factory;
39
40     /**
41      * Initializes deserializer registry and lookups correct deserializer
42      */
43     @Before
44     public void startUp() {
45         DeserializerRegistry registry = new DeserializerRegistryImpl();
46         registry.init();
47         factory = registry.getDeserializer(
48                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 19, MultipartReplyMessage.class));
49     }
50
51     /**
52      * Testing {@link MultipartReplyMessageFactory} for correct translation into POJO
53      */
54     @Test
55     public void testEmptyMultipartReplyFlowBody(){
56         ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 00 00 00");
57         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(factory, bb);
58
59         BufferHelper.checkHeaderV13(builtByFactory);
60         Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
61         Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
62         MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
63         MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
64         Assert.assertEquals("Wrong flow stats size", 0, message.getFlowStats().size());
65     }
66
67     /**
68      * Testing {@link MultipartReplyMessageFactory} for correct translation into POJO
69      */
70     @Test
71     public void testMultipartReplyFlowBody(){
72         ByteBuf bb = BufferHelper.buildBuffer("00 01 00 01 00 00 00 00 "+
73                                               // first flow stat
74                                               "00 48 08 00 "+ // length, tableId, padding
75                                               "00 00 00 09 "+//durationSec
76                                               "00 00 00 07 "+//durationNsec
77                                               "00 0C 00 0E 00 0F 00 1F "+//priority, idleTimeout, hardTimeout, flags
78                                               "00 00 00 00 "+//pad_02
79                                               "FF 01 01 01 01 01 01 01 "+//cookie
80                                               "EF 01 01 01 01 01 01 01 "+//packetCount
81                                               "7F 01 01 01 01 01 01 01 "+//byteCount
82                                               "00 01 00 04 00 00 00 00 "+//empty match
83                                               "00 01 00 08 06 00 00 00 "+
84                                               "00 01 00 08 06 00 00 00 "+
85                                               // second flow stat
86                                               "00 48 08 00 "+ // length, tableId, padding
87                                               "00 00 00 09 "+//durationSec
88                                               "00 00 00 07 "+//durationNsec
89                                               "00 0C 00 0E 00 0F 00 00 "+//priority, idleTimeout, hardTimeout, flags
90                                               "00 00 00 00 "+//pad_02
91                                               "FF 01 01 01 01 01 01 01 "+//cookie
92                                               "EF 01 01 01 01 01 01 01 "+//packetCount
93                                               "7F 01 01 01 01 01 01 01 "+//byteCount
94                                               "00 01 00 04 00 00 00 00 "+//empty match
95                                               "00 01 00 08 06 00 00 00 "+
96                                               "00 01 00 08 06 00 00 00");
97         MultipartReplyMessage builtByFactory = BufferHelper.deserialize(factory, bb);
98
99         BufferHelper.checkHeaderV13(builtByFactory);
100         Assert.assertEquals("Wrong type", 0x01, builtByFactory.getType().getIntValue());
101         Assert.assertEquals("Wrong flag", true, builtByFactory.getFlags().isOFPMPFREQMORE());
102         MultipartReplyFlowCase messageCase = (MultipartReplyFlowCase) builtByFactory.getMultipartReplyBody();
103         MultipartReplyFlow message = messageCase.getMultipartReplyFlow();
104         Assert.assertEquals("Wrong flow stats size", 2, message.getFlowStats().size());
105         FlowStats flowStats1 = message.getFlowStats().get(0);
106         Assert.assertEquals("Wrong tableId", 8, flowStats1.getTableId().intValue());
107         Assert.assertEquals("Wrong durationSec", 9, flowStats1.getDurationSec().intValue());
108         Assert.assertEquals("Wrong durationNsec", 7, flowStats1.getDurationNsec().intValue());
109         Assert.assertEquals("Wrong priority", 12, flowStats1.getPriority().intValue());
110         Assert.assertEquals("Wrong idleTimeOut", 14, flowStats1.getIdleTimeout().intValue());
111         Assert.assertEquals("Wrong hardTimeOut", 15, flowStats1.getHardTimeout().intValue());
112         Assert.assertEquals("Wrong flags", new FlowModFlags(true, true, true, true, true), flowStats1.getFlags());
113         Assert.assertEquals("Wrong cookie", new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01,
114                 0x01, 0x01, 0x01}), flowStats1.getCookie());
115         Assert.assertEquals("Wrong packetCount", new BigInteger(1, new byte[]{(byte) 0xEF, 0x01, 0x01, 0x01,
116                 0x01, 0x01, 0x01, 0x01}), flowStats1.getPacketCount());
117         Assert.assertEquals("Wrong byteCount", new BigInteger(1, new byte[]{(byte) 0x7F, 0x01, 0x01, 0x01,
118                 0x01, 0x01, 0x01, 0x01}), flowStats1.getByteCount());
119         Assert.assertEquals("Wrong match type", OxmMatchType.class, flowStats1.getMatch().getType());
120         flowStats1 = message.getFlowStats().get(1);
121         Assert.assertEquals("Wrong tableId", 8, flowStats1.getTableId().intValue());
122         Assert.assertEquals("Wrong durationSec", 9, flowStats1.getDurationSec().intValue());
123         Assert.assertEquals("Wrong durationNsec", 7, flowStats1.getDurationNsec().intValue());
124         Assert.assertEquals("Wrong priority", 12, flowStats1.getPriority().intValue());
125         Assert.assertEquals("Wrong idleTimeOut", 14, flowStats1.getIdleTimeout().intValue());
126         Assert.assertEquals("Wrong hardTimeOut", 15, flowStats1.getHardTimeout().intValue());
127         Assert.assertEquals("Wrong flags", new FlowModFlags(false, false, false, false, false), flowStats1.getFlags());
128         Assert.assertEquals("Wrong cookie", new BigInteger(1, new byte[]{(byte) 0xFF, 0x01, 0x01, 0x01, 0x01,
129                 0x01, 0x01, 0x01}), flowStats1.getCookie());
130         Assert.assertEquals("Wrong packetCount", new BigInteger(1, new byte[]{(byte) 0xEF, 0x01, 0x01, 0x01,
131                 0x01, 0x01, 0x01, 0x01}), flowStats1.getPacketCount());
132         Assert.assertEquals("Wrong byteCount", new BigInteger(1, new byte[]{(byte) 0x7F, 0x01, 0x01, 0x01,
133                 0x01, 0x01, 0x01, 0x01}), flowStats1.getByteCount());
134         Assert.assertEquals("Wrong match type", OxmMatchType.class, flowStats1.getMatch().getType());
135     }
136 }