3bb61441545551d41a15156bdedccb0ee116903b
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowRemovedMessageFactoryTest.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 java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
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.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPhyPort;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpEcn;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.IpEcnCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.in.phy.port._case.InPhyPortBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.ip.ecn._case.IpEcnBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
40
41 /**
42  * Unit tests for FlowRemovedMessageFactory.
43  *
44  * @author giuseppex.petralia@intel.com
45  */
46 public class FlowRemovedMessageFactoryTest {
47     private OFSerializer<FlowRemovedMessage> factory;
48     private static final byte MESSAGE_TYPE = 11;
49
50     @Before
51     public void startUp() {
52         SerializerRegistry registry = new SerializerRegistryImpl();
53         registry.init();
54         factory = registry
55                 .getSerializer(new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, FlowRemovedMessage.class));
56     }
57
58     @Test
59     public void testSerialize() throws Exception {
60         FlowRemovedMessageBuilder builder = new FlowRemovedMessageBuilder();
61         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
62         builder.setCookie(BigInteger.valueOf(1234L));
63         builder.setPriority(1234);
64         builder.setReason(FlowRemovedReason.forValue(2));
65         builder.setTableId(new TableId(65L));
66         builder.setDurationSec(1234L);
67         builder.setDurationNsec(1234L);
68         builder.setIdleTimeout(1234);
69         builder.setHardTimeout(1234);
70         builder.setPacketCount(BigInteger.valueOf(1234L));
71         builder.setByteCount(BigInteger.valueOf(1234L));
72         MatchBuilder matchBuilder = new MatchBuilder();
73         matchBuilder.setType(OxmMatchType.class);
74         final List<MatchEntry> entries = new ArrayList<>();
75         MatchEntryBuilder entriesBuilder = new MatchEntryBuilder();
76         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
77         entriesBuilder.setOxmMatchField(InPhyPort.class);
78         entriesBuilder.setHasMask(false);
79         InPhyPortCaseBuilder inPhyPortCaseBuilder = new InPhyPortCaseBuilder();
80         InPhyPortBuilder inPhyPortBuilder = new InPhyPortBuilder();
81         inPhyPortBuilder.setPortNumber(new PortNumber(42L));
82         inPhyPortCaseBuilder.setInPhyPort(inPhyPortBuilder.build());
83         entriesBuilder.setMatchEntryValue(inPhyPortCaseBuilder.build());
84         entries.add(entriesBuilder.build());
85         entriesBuilder.setOxmClass(OpenflowBasicClass.class);
86         entriesBuilder.setOxmMatchField(IpEcn.class);
87         entriesBuilder.setHasMask(false);
88         IpEcnCaseBuilder ipEcnCaseBuilder = new IpEcnCaseBuilder();
89         IpEcnBuilder ipEcnBuilder = new IpEcnBuilder();
90         ipEcnBuilder.setEcn((short) 4);
91         ipEcnCaseBuilder.setIpEcn(ipEcnBuilder.build());
92         entriesBuilder.setMatchEntryValue(ipEcnCaseBuilder.build());
93         entries.add(entriesBuilder.build());
94         matchBuilder.setMatchEntry(entries);
95         builder.setMatch(matchBuilder.build());
96         final FlowRemovedMessage message = builder.build();
97         ByteBuf serializedBuffer = UnpooledByteBufAllocator.DEFAULT.buffer();
98
99         // simulate parent message
100         serializedBuffer.writeInt(1);
101         serializedBuffer.writeZero(2);
102         serializedBuffer.writeShort(3);
103
104         factory.serialize(message, serializedBuffer);
105
106         // read parent message
107         serializedBuffer.readInt();
108         serializedBuffer.skipBytes(2);
109         serializedBuffer.readShort();
110
111         BufferHelper.checkHeaderV13(serializedBuffer, MESSAGE_TYPE, 72);
112         Assert.assertEquals("Wrong cookie", message.getCookie().longValue(), serializedBuffer.readLong());
113         Assert.assertEquals("Wrong priority", message.getPriority().intValue(), serializedBuffer.readShort());
114         Assert.assertEquals("Wrong reason", message.getReason().getIntValue(), serializedBuffer.readByte());
115         Assert.assertEquals("Wrong Table ID", message.getTableId().getValue().intValue(),
116                 serializedBuffer.readUnsignedByte());
117         Assert.assertEquals("Wrong duration sec", message.getDurationSec().intValue(), serializedBuffer.readInt());
118         Assert.assertEquals("Wrong duration nsec", message.getDurationNsec().intValue(), serializedBuffer.readInt());
119         Assert.assertEquals("Wrong Idle timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort());
120         Assert.assertEquals("Wrong Hard timeout", message.getIdleTimeout().intValue(), serializedBuffer.readShort());
121         Assert.assertEquals("Wrong Packet count", message.getPacketCount().longValue(), serializedBuffer.readLong());
122         Assert.assertEquals("Wrong Byte count", message.getByteCount().longValue(), serializedBuffer.readLong());
123         Assert.assertEquals("Wrong match type", 1, serializedBuffer.readUnsignedShort());
124         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
125         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
126         short fieldAndMask = serializedBuffer.readUnsignedByte();
127         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
128         Assert.assertEquals("Wrong oxm field", 1, fieldAndMask >> 1);
129         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
130         Assert.assertEquals("Wrong oxm value", 42, serializedBuffer.readUnsignedInt());
131         Assert.assertEquals("Wrong oxm class", 0x8000, serializedBuffer.readUnsignedShort());
132         fieldAndMask = serializedBuffer.readUnsignedByte();
133         Assert.assertEquals("Wrong oxm hasMask", 0, fieldAndMask & 1);
134         Assert.assertEquals("Wrong oxm field", 9, fieldAndMask >> 1);
135         serializedBuffer.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
136         Assert.assertEquals("Wrong oxm value", 4, serializedBuffer.readUnsignedByte());
137         serializedBuffer.skipBytes(7);
138     }
139
140 }