Fix yangtools reference
[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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetNwDstCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.dst._case.SetNwDstActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.src._case.SetTpSrcActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10Builder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
39 import org.opendaylight.yangtools.yang.common.Uint16;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.opendaylight.yangtools.yang.common.Uint8;
43
44 /**
45  * Unit tests for OF10FlowModInputMessageFactory.
46  *
47  * @author michal.polkorab
48  */
49 public class OF10FlowModInputMessageFactoryTest {
50
51     private SerializerRegistry registry;
52     private OFSerializer<FlowModInput> flowModFactory;
53
54     /**
55      * Initializes serializer registry and stores correct factory in field.
56      */
57     @Before
58     public void startUp() {
59         registry = new SerializerRegistryImpl();
60         registry.init();
61         flowModFactory = registry.getSerializer(
62                 new MessageTypeKey<>(EncodeConstants.OF_VERSION_1_0, FlowModInput.class));
63     }
64
65     /**
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(Uint8.ZERO);
75         matchBuilder.setNwDstMask(Uint8.ZERO);
76         matchBuilder.setInPort(Uint16.valueOf(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(Uint16.valueOf(18));
80         matchBuilder.setDlVlanPcp(Uint8.valueOf(5));
81         matchBuilder.setDlType(Uint16.valueOf(42));
82         matchBuilder.setNwTos(Uint8.valueOf(4));
83         matchBuilder.setNwProto(Uint8.valueOf(7));
84         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
85         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
86         matchBuilder.setTpSrc(Uint16.valueOf(6653));
87         matchBuilder.setTpDst(Uint16.valueOf(6633));
88         builder.setMatchV10(matchBuilder.build());
89         final byte[] cookie = new byte[]{(byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01};
90         builder.setCookie(Uint64.valueOf(new BigInteger(1, cookie)));
91         builder.setCommand(FlowModCommand.forValue(0));
92         builder.setIdleTimeout(Uint16.valueOf(12));
93         builder.setHardTimeout(Uint16.valueOf(16));
94         builder.setPriority(Uint16.ONE);
95         builder.setBufferId(Uint32.TWO);
96         builder.setOutPort(new PortNumber(Uint32.valueOf(4422)));
97         builder.setFlagsV10(new FlowModFlagsV10(true, false, true));
98         final 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(Uint32.valueOf(42)));
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.assertArrayEquals("Wrong dlSrc", new byte[] { 01, 01, 01, 01, 01, 01 }, dlSrc);
125         byte[] dlDst = new byte[6];
126         out.readBytes(dlDst);
127         Assert.assertArrayEquals("Wrong dlDst", new byte[] { -1, -1, -1, -1, -1, -1 }, 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 }