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