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