Merge "Removed enumeration ALL from FlowWildCardsV10"
[openflowjava.git] / 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
14 import java.math.BigInteger;
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
21 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
22 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
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.augments.rev131002.IpAddressAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwDst;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpSrc;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.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.rev130731.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  * @author michal.polkorab
44  *
45  */
46 public class OF10FlowModInputMessageFactoryTest {
47
48     /**
49      * @throws Exception 
50      * Testing of {@link OF10FlowModInputMessageFactory} for correct translation from POJO
51      */
52     @Test
53     public void testFlowModInputMessageFactory() throws Exception {
54         FlowModInputBuilder builder = new FlowModInputBuilder();
55         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
56         MatchV10Builder matchBuilder = new MatchV10Builder();
57         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
58         matchBuilder.setNwSrcMask((short) 0);
59         matchBuilder.setNwDstMask((short) 0);
60         matchBuilder.setInPort(58);
61         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
62         matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
63         matchBuilder.setDlVlan(18);
64         matchBuilder.setDlVlanPcp((short) 5);
65         matchBuilder.setDlType(42);
66         matchBuilder.setNwTos((short) 4);
67         matchBuilder.setNwProto((short) 7);
68         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
69         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
70         matchBuilder.setTpSrc(6653);
71         matchBuilder.setTpDst(6633);
72         builder.setMatchV10(matchBuilder.build());
73         byte[] cookie = new byte[]{0x00, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
74         builder.setCookie(new BigInteger(cookie));
75         builder.setCommand(FlowModCommand.forValue(0));
76         builder.setIdleTimeout(12);
77         builder.setHardTimeout(16);
78         builder.setPriority(1);
79         builder.setBufferId(2L);
80         builder.setOutPort(new PortNumber(4422L));
81         builder.setFlagsV10(new FlowModFlagsV10(true, false, true));
82         List<ActionsList> actions = new ArrayList<>();
83         ActionsListBuilder actionsListBuilder = new ActionsListBuilder();
84         ActionBuilder actionBuilder = new ActionBuilder();
85         actionBuilder.setType(SetNwDst.class);
86         IpAddressActionBuilder ipBuilder = new IpAddressActionBuilder();
87         ipBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));
88         actionBuilder.addAugmentation(IpAddressAction.class, ipBuilder.build());
89         actionsListBuilder.setAction(actionBuilder.build());
90         actions.add(actionsListBuilder.build());
91         actionBuilder = new ActionBuilder();
92         actionBuilder.setType(SetTpSrc.class);
93         PortActionBuilder portBuilder = new PortActionBuilder();
94         portBuilder.setPort(new PortNumber(42L));
95         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
96         actionsListBuilder.setAction(actionBuilder.build());
97         actions.add(actionsListBuilder.build());
98         builder.setActionsList(actions);
99         FlowModInput message = builder.build();
100         
101         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
102         OF10FlowModInputMessageFactory factory = OF10FlowModInputMessageFactory.getInstance();
103         factory.messageToBuffer(EncodeConstants.OF10_VERSION_ID, out, message);
104         
105         BufferHelper.checkHeaderV10(out, factory.getMessageType(), factory.computeLength(message));
106         Assert.assertEquals("Wrong wildcards", 3678463, out.readUnsignedInt());
107         Assert.assertEquals("Wrong inPort", 58, out.readUnsignedShort());
108         byte[] dlSrc = new byte[6];
109         out.readBytes(dlSrc);
110         Assert.assertEquals("Wrong dlSrc", "01:01:01:01:01:01", ByteBufUtils.macAddressToString(dlSrc));
111         byte[] dlDst = new byte[6];
112         out.readBytes(dlDst);
113         Assert.assertEquals("Wrong dlDst", "FF:FF:FF:FF:FF:FF", ByteBufUtils.macAddressToString(dlDst));
114         Assert.assertEquals("Wrong dlVlan", 18, out.readUnsignedShort());
115         Assert.assertEquals("Wrong dlVlanPcp", 5, out.readUnsignedByte());
116         out.skipBytes(1);
117         Assert.assertEquals("Wrong dlType", 42, out.readUnsignedShort());
118         Assert.assertEquals("Wrong nwTos", 4, out.readUnsignedByte());
119         Assert.assertEquals("Wrong nwProto", 7, out.readUnsignedByte());
120         out.skipBytes(2);
121         Assert.assertEquals("Wrong nwSrc", 134744072, out.readUnsignedInt());
122         Assert.assertEquals("Wrong nwDst", 269488144, out.readUnsignedInt());
123         Assert.assertEquals("Wrong tpSrc", 6653, out.readUnsignedShort());
124         Assert.assertEquals("Wrong tpDst", 6633, out.readUnsignedShort());
125         byte[] cookieRead = new byte[8];
126         out.readBytes(cookieRead);
127         Assert.assertArrayEquals("Wrong cookie", cookie, cookieRead);
128         Assert.assertEquals("Wrong command", 0, out.readUnsignedShort());
129         Assert.assertEquals("Wrong idleTimeOut", 12, out.readUnsignedShort());
130         Assert.assertEquals("Wrong hardTimeOut", 16, out.readUnsignedShort());
131         Assert.assertEquals("Wrong priority", 1, out.readUnsignedShort());
132         Assert.assertEquals("Wrong bufferId", 2, out.readUnsignedInt());
133         Assert.assertEquals("Wrong outPort", 4422, out.readUnsignedShort());
134         Assert.assertEquals("Wrong flags", 3, out.readUnsignedShort());
135         Assert.assertEquals("Wrong action - type", 7, out.readUnsignedShort());
136         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
137         Assert.assertEquals("Wrong flags", 33686018, out.readUnsignedInt());
138         Assert.assertEquals("Wrong action - type", 9, out.readUnsignedShort());
139         Assert.assertEquals("Wrong action - length", 8, out.readUnsignedShort());
140         Assert.assertEquals("Wrong flags", 42, out.readUnsignedShort());
141         out.skipBytes(2);
142     }
143
144 }