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