Merge "OPNFLWPLUG-1000 : Execute reconciliation asynchronously when the user selected...
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10FlowModInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.UnpooledByteBufAllocator;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
21 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
22 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.openflowjava.util.ByteBufUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwDstCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.dst._case.SetNwDstActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.src._case.SetTpSrcActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
41
42 /**
43  * Unit tests for OF10FlowModInputMessageFactory.
44  *
45  * @author michal.polkorab
46  */
47 public class OF10FlowModInputMessageFactoryTest {
48
49     private SerializerRegistry registry;
50     private OFSerializer<FlowModInput> flowModFactory;
51
52     /**
53      * Initializes serializer registry and stores correct factory in field.
54      */
55     @Before
56     public void startUp() {
57         registry = new SerializerRegistryImpl();
58         registry.init();
59         flowModFactory = registry.getSerializer(
60                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, FlowModInput.class));
61     }
62
63     /**
64      * Testing of {@link OF10FlowModInputMessageFactory} for correct translation from POJO.
65      */
66     @Test
67     public void testFlowModInputMessageFactory() throws Exception {
68         FlowModInputBuilder builder = new FlowModInputBuilder();
69         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
70         MatchV10Builder matchBuilder = new MatchV10Builder();
71         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
72         matchBuilder.setNwSrcMask((short) 0);
73         matchBuilder.setNwDstMask((short) 0);
74         matchBuilder.setInPort(58);
75         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
76         matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
77         matchBuilder.setDlVlan(18);
78         matchBuilder.setDlVlanPcp((short) 5);
79         matchBuilder.setDlType(42);
80         matchBuilder.setNwTos((short) 4);
81         matchBuilder.setNwProto((short) 7);
82         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
83         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
84         matchBuilder.setTpSrc(6653);
85         matchBuilder.setTpDst(6633);
86         builder.setMatchV10(matchBuilder.build());
87         byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
88         builder.setCookie(new BigInteger(1, cookie));
89         builder.setCommand(FlowModCommand.forValue(0));
90         builder.setIdleTimeout(12);
91         builder.setHardTimeout(16);
92         builder.setPriority(1);
93         builder.setBufferId(2L);
94         builder.setOutPort(new PortNumber(4422L));
95         builder.setFlagsV10(new FlowModFlagsV10(true, false, true));
96         final List<Action> actions = new ArrayList<>();
97         ActionBuilder actionBuilder = new ActionBuilder();
98         SetNwDstCaseBuilder nwDstCaseBuilder = new SetNwDstCaseBuilder();
99         SetNwDstActionBuilder nwDstBuilder = new SetNwDstActionBuilder();
100         nwDstBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));
101         nwDstCaseBuilder.setSetNwDstAction(nwDstBuilder.build());
102         actionBuilder.setActionChoice(nwDstCaseBuilder.build());
103         actions.add(actionBuilder.build());
104         actionBuilder = new ActionBuilder();
105         SetTpSrcCaseBuilder tpSrcCaseBuilder = new SetTpSrcCaseBuilder();
106         SetTpSrcActionBuilder tpSrcBuilder = new SetTpSrcActionBuilder();
107         tpSrcBuilder.setPort(new PortNumber(42L));
108         tpSrcCaseBuilder.setSetTpSrcAction(tpSrcBuilder.build());
109         actionBuilder.setActionChoice(tpSrcCaseBuilder.build());
110         actions.add(actionBuilder.build());
111         builder.setAction(actions);
112         FlowModInput message = builder.build();
113
114         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
115         flowModFactory.serialize(message, out);
116
117         BufferHelper.checkHeaderV10(out, (byte) 14, 88);
118         Assert.assertEquals("Wrong wildcards", 3678463, out.readUnsignedInt());
119         Assert.assertEquals("Wrong inPort", 58, out.readUnsignedShort());
120         byte[] dlSrc = new byte[6];
121         out.readBytes(dlSrc);
122         Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
123         byte[] dlDst = new byte[6];
124         out.readBytes(dlDst);
125         Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
126         Assert.assertEquals("Wrong dlVlan", 18, out.readUnsignedShort());
127         Assert.assertEquals("Wrong dlVlanPcp", 5, out.readUnsignedByte());
128         out.skipBytes(1);
129         Assert.assertEquals("Wrong dlType", 42, out.readUnsignedShort());
130         Assert.assertEquals("Wrong nwTos", 4, out.readUnsignedByte());
131         Assert.assertEquals("Wrong nwProto", 7, out.readUnsignedByte());
132         out.skipBytes(2);
133         Assert.assertEquals("Wrong nwSrc", 134744072, out.readUnsignedInt());
134         Assert.assertEquals("Wrong nwDst", 269488144, out.readUnsignedInt());
135         Assert.assertEquals("Wrong tpSrc", 6653, out.readUnsignedShort());
136         Assert.assertEquals("Wrong tpDst", 6633, out.readUnsignedShort());
137         byte[] cookieRead = new byte[8];
138         out.readBytes(cookieRead);
139         Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);
140         Assert.assertEquals("Wrong command", 0, out.readUnsignedShort());
141         Assert.assertEquals("Wrong idleTimeOut", 12, out.readUnsignedShort());
142         Assert.assertEquals("Wrong hardTimeOut", 16, out.readUnsignedShort());
143         Assert.assertEquals("Wrong priority", 1, out.readUnsignedShort());
144         Assert.assertEquals("Wrong bufferId", 2, out.readUnsignedInt());
145         Assert.assertEquals("Wrong outPort", 4422, out.readUnsignedShort());
146         Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());
147         Assert.assertEquals("Wrong action - type", 7, out.readUnsignedShort());
148         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
149         Assert.assertEquals("Wrong flags", 33686018, out.readUnsignedInt());
150         Assert.assertEquals("Wrong action - type", 9, out.readUnsignedShort());
151         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
152         Assert.assertEquals("Wrong flags", 42, out.readUnsignedShort());
153         out.skipBytes(2);
154     }
155
156 }