Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10FlowModInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.deserialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
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.common.action.rev150203.action.grouping.action.choice.SetNwDstCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.SetTpSrcCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.nw.dst._case.SetNwDstActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.set.tp.src._case.SetTpSrcActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlagsV10;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowWildcardsV10;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
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
39 /**
40  * @author giuseppex.petralia@intel.com
41  *
42  */
43 public class OF10FlowModInputMessageFactoryTest {
44     private OFDeserializer<FlowModInput> factory;
45
46     @Before
47     public void startUp() {
48         DeserializerRegistry desRegistry = new DeserializerRegistryImpl();
49         desRegistry.init();
50         factory = desRegistry
51                 .getDeserializer(new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, 14, FlowModInput.class));
52     }
53
54     @Test
55     public void test() {
56         ByteBuf bb = BufferHelper.buildBuffer("00 38 20 ff 00 3a 01 01 01 01 01 01 ff "
57                 + "ff ff ff ff ff 00 12 05 00 00 2a 04 07 00 00 08 08 08 08 10 10 10 10 "
58                 + "19 fd 19 e9 ff 01 04 01 06 00 07 01 00 00 00 0c 00 10 00 01 00 00 00 02 "
59                 + "11 46 00 03 00 07 00 08 02 02 02 02 00 09 00 08 00 2a 00 00 ");
60         FlowModInput deserializedMessage = BufferHelper.deserialize(factory, bb);
61         BufferHelper.checkHeaderV10(deserializedMessage);
62         Assert.assertEquals("Wrong Match", createMatch(), deserializedMessage.getMatchV10());
63         byte[] cookie = new byte[] { (byte) 0xFF, 0x01, 0x04, 0x01, 0x06, 0x00, 0x07, 0x01 };
64         Assert.assertEquals("Wrong cookie", new BigInteger(1, cookie), deserializedMessage.getCookie());
65         Assert.assertEquals("Wrong command", FlowModCommand.forValue(0), deserializedMessage.getCommand());
66         Assert.assertEquals("Idle Timeout", 12, deserializedMessage.getIdleTimeout().intValue());
67         Assert.assertEquals("Wrong Hard Timeout", 16, deserializedMessage.getHardTimeout().intValue());
68         Assert.assertEquals("Wrong priority", 1, deserializedMessage.getPriority().intValue());
69         Assert.assertEquals("Wrong buffer id", 2L, deserializedMessage.getBufferId().longValue());
70         Assert.assertEquals("Wrong out port", new PortNumber(4422L), deserializedMessage.getOutPort());
71         Assert.assertEquals("Wrong flags", new FlowModFlagsV10(true, false, true), deserializedMessage.getFlagsV10());
72         Assert.assertEquals("Wrong actions", createAction(), deserializedMessage.getAction());
73     }
74
75     private static List<Action> createAction() {
76         List<Action> actions = new ArrayList<>();
77         ActionBuilder actionBuilder = new ActionBuilder();
78         SetNwDstCaseBuilder nwDstCaseBuilder = new SetNwDstCaseBuilder();
79         SetNwDstActionBuilder nwDstBuilder = new SetNwDstActionBuilder();
80         nwDstBuilder.setIpAddress(new Ipv4Address("2.2.2.2"));
81         nwDstCaseBuilder.setSetNwDstAction(nwDstBuilder.build());
82         actionBuilder.setActionChoice(nwDstCaseBuilder.build());
83         actions.add(actionBuilder.build());
84         actionBuilder = new ActionBuilder();
85         SetTpSrcCaseBuilder tpSrcCaseBuilder = new SetTpSrcCaseBuilder();
86         SetTpSrcActionBuilder tpSrcBuilder = new SetTpSrcActionBuilder();
87         tpSrcBuilder.setPort(new PortNumber(42L));
88         tpSrcCaseBuilder.setSetTpSrcAction(tpSrcBuilder.build());
89         actionBuilder.setActionChoice(tpSrcCaseBuilder.build());
90         actions.add(actionBuilder.build());
91         return actions;
92     }
93
94     private static MatchV10 createMatch() {
95         MatchV10Builder matchBuilder = new MatchV10Builder();
96         matchBuilder.setWildcards(new FlowWildcardsV10(true, true, true, true, true, true, true, true, true, true));
97         matchBuilder.setNwSrcMask((short) 0);
98         matchBuilder.setNwDstMask((short) 0);
99         matchBuilder.setInPort(58);
100         matchBuilder.setDlSrc(new MacAddress("01:01:01:01:01:01"));
101         matchBuilder.setDlDst(new MacAddress("FF:FF:FF:FF:FF:FF"));
102         matchBuilder.setDlVlan(18);
103         matchBuilder.setDlVlanPcp((short) 5);
104         matchBuilder.setDlType(42);
105         matchBuilder.setNwTos((short) 4);
106         matchBuilder.setNwProto((short) 7);
107         matchBuilder.setNwSrc(new Ipv4Address("8.8.8.8"));
108         matchBuilder.setNwDst(new Ipv4Address("16.16.16.16"));
109         matchBuilder.setTpSrc(6653);
110         matchBuilder.setTpDst(6633);
111         return matchBuilder.build();
112     }
113 }