Bug 2756 - Action model update
[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.common.action.rev150203.action.grouping.action.choice.SetNwDstCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.dst._case.SetNwDstActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.src._case.SetTpSrcActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
43
44 /**
45  * @author michal.polkorab
46  *
47  */
48 public class OF10FlowModInputMessageFactoryTest {
49
50     private SerializerRegistry registry;
51     private OFSerializer<FlowModInput> flowModFactory;
52
53     /**
54      * Initializes serializer registry and stores correct factory in field
55      */
56     @Before
57     public void startUp() {
58         registry = new SerializerRegistryImpl();
59         registry.init();
60         flowModFactory = registry.getSerializer(
61                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, FlowModInput.class));
62     }
63
64     /**
65      * @throws Exception
66      * Testing of {@link OF10FlowModInputMessageFactory} for correct translation from POJO
67      */
68     @Test
69     public void testFlowModInputMessageFactory() throws Exception {
70         FlowModInputBuilder builder = new FlowModInputBuilder();
71         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
72         MatchV10Builder matchBuilder = new MatchV10Builder();
73         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
74         matchBuilder.setNwSrcMask((short) 0);
75         matchBuilder.setNwDstMask((short) 0);
76         matchBuilder.setInPort(58);
77         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
78         matchBuilder.setDlDst(new MacAddress("ff:ff:ff:ff:ff:ff"));
79         matchBuilder.setDlVlan(18);
80         matchBuilder.setDlVlanPcp((short) 5);
81         matchBuilder.setDlType(42);
82         matchBuilder.setNwTos((short) 4);
83         matchBuilder.setNwProto((short) 7);
84         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
85         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
86         matchBuilder.setTpSrc(6653);
87         matchBuilder.setTpDst(6633);
88         builder.setMatchV10(matchBuilder.build());
89         byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
90         builder.setCookie(new BigInteger(1, cookie));
91         builder.setCommand(FlowModCommand.forValue(0));
92         builder.setIdleTimeout(12);
93         builder.setHardTimeout(16);
94         builder.setPriority(1);
95         builder.setBufferId(2L);
96         builder.setOutPort(new PortNumber(4422L));
97         builder.setFlagsV10(new FlowModFlagsV10(true, false, true));
98         List<Action> actions = new ArrayList<>();
99         ActionBuilder actionBuilder = new ActionBuilder();
100         SetNwDstCaseBuilder nwDstCaseBuilder = new SetNwDstCaseBuilder();
101         SetNwDstActionBuilder nwDstBuilder = new SetNwDstActionBuilder();
102         nwDstBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));
103         nwDstCaseBuilder.setSetNwDstAction(nwDstBuilder.build());
104         actionBuilder.setActionChoice(nwDstCaseBuilder.build());
105         actions.add(actionBuilder.build());
106         actionBuilder = new ActionBuilder();
107         SetTpSrcCaseBuilder tpSrcCaseBuilder = new SetTpSrcCaseBuilder();
108         SetTpSrcActionBuilder tpSrcBuilder = new SetTpSrcActionBuilder();
109         tpSrcBuilder.setPort(new PortNumber(42L));
110         tpSrcCaseBuilder.setSetTpSrcAction(tpSrcBuilder.build());
111         actionBuilder.setActionChoice(tpSrcCaseBuilder.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 }