Fix checkstyle violations in openflow-protocol-impl - part 12
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FlowRemovedMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.math.BigInteger;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.openflowjava.util.ByteBufUtils;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
30
31 /**
32  * Unit tests for OF10FlowRemovedMessageFactory.
33  *
34  * @author giuseppex.petralia@intel.com
35  */
36 public class OF10FlowRemovedMessageFactoryTest {
37     private OFSerializer<FlowRemovedMessage> factory;
38     private static final byte MESSAGE_TYPE = 11;
39
40     @Before
41     public void startUp() {
42         SerializerRegistry registry = new SerializerRegistryImpl();
43         registry.init();
44         factory = registry
45                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, FlowRemovedMessage.class));
46     }
47
48     @Test
49     public void testSerialize() throws Exception {
50         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
51         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
52         MatchV10Builder matchBuilder = new MatchV10Builder();
53         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
54         matchBuilder.setNwSrcMask((short) 0);
55         matchBuilder.setNwDstMask((short) 0);
56         matchBuilder.setInPort(58);
57         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
58         matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
59         matchBuilder.setDlVlan(18);
60         matchBuilder.setDlVlanPcp((short) 5);
61         matchBuilder.setDlType(42);
62         matchBuilder.setNwTos((short) 4);
63         matchBuilder.setNwProto((short) 7);
64         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
65         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
66         matchBuilder.setTpSrc(6653);
67         matchBuilder.setTpDst(6633);
68         builder.setMatchV10(matchBuilder.build());
69         byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x01, 0x01, 0x04, 0x01 };
70         builder.setCookie(new BigInteger(1, cookie));
71         builder.setPriority(1);
72         builder.setReason(FlowRemovedReason.forValue(1));
73         builder.setDurationSec(1L);
74         builder.setDurationNsec(1L);
75         builder.setIdleTimeout(12);
76         builder.setPacketCount(BigInteger.valueOf(1L));
77         builder.setByteCount(BigInteger.valueOf(2L));
78         FlowRemovedMessage message = builder.build();
79
80         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
81         factory.serialize(message, serializedBuffer);
82         BufferHelper.checkHeaderV10(serializedBuffer, MESSAGE_TYPE, 88);
83         Assert.assertEquals("Wrong wildcards", 3678463, serializedBuffer.readUnsignedInt());
84         Assert.assertEquals("Wrong inPort", 58, serializedBuffer.readUnsignedShort());
85         byte[] dlSrc = new byte[6];
86         serializedBuffer.readBytes(dlSrc);
87         Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
88         byte[] dlDst = new byte[6];
89         serializedBuffer.readBytes(dlDst);
90         Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
91         Assert.assertEquals("Wrong dlVlan", 18, serializedBuffer.readUnsignedShort());
92         Assert.assertEquals("Wrong dlVlanPcp", 5, serializedBuffer.readUnsignedByte());
93         serializedBuffer.skipBytes(1);
94         Assert.assertEquals("Wrong dlType", 42, serializedBuffer.readUnsignedShort());
95         Assert.assertEquals("Wrong nwTos", 4, serializedBuffer.readUnsignedByte());
96         Assert.assertEquals("Wrong nwProto", 7, serializedBuffer.readUnsignedByte());
97         serializedBuffer.skipBytes(2);
98         Assert.assertEquals("Wrong nwSrc", 134744072, serializedBuffer.readUnsignedInt());
99         Assert.assertEquals("Wrong nwDst", 269488144, serializedBuffer.readUnsignedInt());
100         Assert.assertEquals("Wrong tpSrc", 6653, serializedBuffer.readUnsignedShort());
101         Assert.assertEquals("Wrong tpDst", 6633, serializedBuffer.readUnsignedShort());
102         byte[] cookieRead = new byte[8];
103         serializedBuffer.readBytes(cookieRead);
104         Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);
105         Assert.assertEquals("Wrong priority", 1, serializedBuffer.readUnsignedShort());
106         Assert.assertEquals("Wrong reason", 1, serializedBuffer.readUnsignedByte());
107         serializedBuffer.skipBytes(1);
108         Assert.assertEquals("Wrong duration", 1L, serializedBuffer.readUnsignedInt());
109         Assert.assertEquals("Wrong duration nsec", 1L, serializedBuffer.readUnsignedInt());
110         Assert.assertEquals("Wrong idle timeout", 12, serializedBuffer.readUnsignedShort());
111         serializedBuffer.skipBytes(2);
112         Assert.assertEquals("Wrong packet count", 1L, serializedBuffer.readLong());
113         Assert.assertEquals("Wrong byte count", 2L, serializedBuffer.readLong());
114     }
115 }