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