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