2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.util;
10 import io.netty.buffer.ByteBuf;
12 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.util.EncodeConstants;
19 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
20 import org.opendaylight.openflowjava.protocol.impl.deserialization.action.AbstractActionDeserializer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.EthertypeAction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.GroupIdAction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthAction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MplsTtlAction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.NwTtlAction;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.OxmFieldsAction;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.QueueIdAction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.InPhyPortCase;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author michal.polkorab
39 public class ActionsDeserializerTest {
41 private static final Logger LOGGER = LoggerFactory
42 .getLogger(ActionsDeserializerTest.class);
43 private DeserializerRegistry registry;
46 * Initializes deserializer registry and lookups correct deserializer
49 public void startUp() {
50 registry = new DeserializerRegistryImpl();
55 * Testing actions deserialization
59 ByteBuf message = BufferHelper.buildBuffer("00 00 00 10 00 00 00 01 00 02 00 00 00 00 00 00 "
60 + "00 0B 00 08 00 00 00 00 "
61 + "00 0C 00 08 00 00 00 00 "
62 + "00 0F 00 08 03 00 00 00 "
63 + "00 10 00 08 00 00 00 00 "
64 + "00 11 00 08 00 04 00 00 "
65 + "00 12 00 08 00 00 00 00 "
66 + "00 13 00 08 00 05 00 00 "
67 + "00 14 00 08 00 06 00 00 "
68 + "00 15 00 08 00 00 00 07 "
69 + "00 16 00 08 00 00 00 08 "
70 + "00 17 00 08 09 00 00 00 "
71 + "00 18 00 08 00 00 00 00 "
72 + "00 19 00 10 80 00 02 04 00 00 00 0B 00 00 00 00 "
73 + "00 1A 00 08 00 0A 00 00 "
74 + "00 1B 00 08 00 00 00 00");
76 message.skipBytes(4); // skip XID
77 LOGGER.info("bytes: " + message.readableBytes());
79 CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
80 List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
81 message.readableBytes(), message, keyMaker, registry);
82 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
83 + "openflow.common.action.rev130731.Output", actions.get(0).getType().getName());
84 Assert.assertEquals("Wrong action port", 1,
85 actions.get(0).getAugmentation(PortAction.class).getPort().getValue().intValue());
86 Assert.assertEquals("Wrong action max-length", 2,
87 actions.get(0).getAugmentation(MaxLengthAction.class).getMaxLength().intValue());
88 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
89 + "openflow.common.action.rev130731.CopyTtlOut", actions.get(1).getType().getName());
90 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
91 + "openflow.common.action.rev130731.CopyTtlIn", actions.get(2).getType().getName());
92 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
93 + "openflow.common.action.rev130731.SetMplsTtl", actions.get(3).getType().getName());
94 Assert.assertEquals("Wrong action value", 3,
95 actions.get(3).getAugmentation(MplsTtlAction.class).getMplsTtl().shortValue());
96 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
97 + "openflow.common.action.rev130731.DecMplsTtl", actions.get(4).getType().getName());
98 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
99 + "openflow.common.action.rev130731.PushVlan", actions.get(5).getType().getName());
100 Assert.assertEquals("Wrong action value", 4,
101 actions.get(5).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());
102 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
103 + "openflow.common.action.rev130731.PopVlan", actions.get(6).getType().getName());
104 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
105 + "openflow.common.action.rev130731.PushMpls", actions.get(7).getType().getName());
106 Assert.assertEquals("Wrong action value", 5,
107 actions.get(7).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());
108 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
109 + "openflow.common.action.rev130731.PopMpls", actions.get(8).getType().getName());
110 Assert.assertEquals("Wrong action value", 6,
111 actions.get(8).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());
112 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
113 + "openflow.common.action.rev130731.SetQueue", actions.get(9).getType().getName());
114 Assert.assertEquals("Wrong action value", 7,
115 actions.get(9).getAugmentation(QueueIdAction.class).getQueueId().intValue());
116 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
117 + "openflow.common.action.rev130731.Group", actions.get(10).getType().getName());
118 Assert.assertEquals("Wrong action value", 8,
119 actions.get(10).getAugmentation(GroupIdAction.class).getGroupId().intValue());
120 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
121 + "openflow.common.action.rev130731.SetNwTtl", actions.get(11).getType().getName());
122 Assert.assertEquals("Wrong action value", 9,
123 actions.get(11).getAugmentation(NwTtlAction.class).getNwTtl().intValue());
124 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
125 + "openflow.common.action.rev130731.DecNwTtl", actions.get(12).getType().getName());
126 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
127 + "openflow.common.action.rev130731.SetField", actions.get(13).getType().getName());
128 List<MatchEntry> entries = actions.get(13).getAugmentation(OxmFieldsAction.class).getMatchEntry();
129 Assert.assertEquals("Wrong number of fields", 1, entries.size());
130 Assert.assertEquals("Wrong match entry class", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."
131 + "oxm.rev150225.OpenflowBasicClass", entries.get(0).getOxmClass().getName());
132 Assert.assertEquals("Wrong match entry field", "org.opendaylight.yang.gen.v1.urn.opendaylight.openflow."
133 + "oxm.rev150225.InPhyPort", entries.get(0).getOxmMatchField().getName());
134 Assert.assertEquals("Wrong match entry mask", false, entries.get(0).isHasMask());
135 Assert.assertEquals("Wrong match entry value", 11,
136 ((InPhyPortCase) entries.get(0).getMatchEntryValue()).getInPhyPort().getPortNumber()
137 .getValue().intValue());
138 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
139 + "openflow.common.action.rev130731.PushPbb", actions.get(14).getType().getName());
140 Assert.assertEquals("Wrong action value", 10,
141 actions.get(14).getAugmentation(EthertypeAction.class).getEthertype().getValue().intValue());
142 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
143 + "openflow.common.action.rev130731.PopPbb", actions.get(15).getType().getName());
144 Assert.assertTrue("Unread data in message", message.readableBytes() == 0);
148 * Tests {@link AbstractActionDeserializer#deserializeHeader(ByteBuf)}
151 public void testDeserializeHeader() {
152 ByteBuf message = BufferHelper.buildBuffer("00 00 00 04 00 19 00 04");
154 message.skipBytes(4); // skip XID
155 CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
156 List<Action> actions = ListDeserializer.deserializeHeaders(EncodeConstants.OF13_VERSION_ID,
157 message.readableBytes(), message, keyMaker, registry);
159 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
160 + "openflow.common.action.rev130731.Output", actions.get(0).getType().getName());
161 Assert.assertEquals("Wrong action port", null, actions.get(0).getAugmentation(PortAction.class));
162 Assert.assertEquals("Wrong action max-length", null, actions.get(0).getAugmentation(MaxLengthAction.class));
163 Assert.assertEquals("Wrong action type", "org.opendaylight.yang.gen.v1.urn.opendaylight."
164 + "openflow.common.action.rev130731.SetField", actions.get(1).getType().getName());
165 Assert.assertEquals("Wrong action oxm field", null, actions.get(1).getAugmentation(OxmFieldsAction.class));