Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 20 | import org.hamcrest.MatcherAssert; |
| 21 | import org.junit.Assert; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 24 | import org.onlab.packet.Ip4Address; |
| 25 | import org.onlab.packet.Ip6Address; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 27 | import org.onlab.packet.MplsLabel; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 29 | import org.onlab.util.ImmutableByteSequence; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 30 | import org.onosproject.codec.CodecContext; |
| 31 | import org.onosproject.codec.JsonCodec; |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 32 | import org.onosproject.net.ChannelSpacing; |
| 33 | import org.onosproject.net.GridType; |
| 34 | import org.onosproject.net.Lambda; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 35 | import org.onosproject.net.OduSignalId; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 36 | import org.onosproject.net.PortNumber; |
| 37 | import org.onosproject.net.flow.instructions.Instruction; |
| 38 | import org.onosproject.net.flow.instructions.Instructions; |
| 39 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 40 | import org.onosproject.net.flow.instructions.L1ModificationInstruction; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 41 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 42 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 43 | import org.onosproject.net.flow.instructions.PiInstruction; |
| 44 | import org.onosproject.net.pi.model.PiActionId; |
| 45 | import org.onosproject.net.pi.model.PiActionParamId; |
| 46 | import org.onosproject.net.pi.runtime.PiAction; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 47 | import org.onosproject.net.pi.runtime.PiActionParam; |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiActionProfileGroupId; |
| 49 | import org.onosproject.net.pi.runtime.PiActionProfileMemberId; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 50 | import org.onosproject.net.pi.runtime.PiTableAction; |
| 51 | |
| 52 | import java.io.IOException; |
| 53 | import java.io.InputStream; |
| 54 | import java.util.Collection; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 55 | |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 56 | import static org.hamcrest.MatcherAssert.assertThat; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 57 | import static org.hamcrest.Matchers.is; |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 58 | import static org.hamcrest.Matchers.notNullValue; |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 59 | import static org.onlab.util.ImmutableByteSequence.copyFrom; |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 60 | import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 61 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Unit tests for Instruction codec. |
| 64 | */ |
| 65 | public class InstructionCodecTest { |
| 66 | CodecContext context; |
| 67 | JsonCodec<Instruction> instructionCodec; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 68 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 69 | * Sets up for each test. Creates a context and fetches the instruction |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 70 | * codec. |
| 71 | */ |
| 72 | @Before |
| 73 | public void setUp() { |
| 74 | context = new MockCodecContext(); |
| 75 | instructionCodec = context.codec(Instruction.class); |
| 76 | assertThat(instructionCodec, notNullValue()); |
| 77 | } |
| 78 | |
| 79 | /** |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 80 | * Tests the encoding of push mpls header instructions. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 81 | */ |
| 82 | @Test |
| 83 | public void pushHeaderInstructionsTest() { |
Jian Li | 11260a0 | 2016-05-19 13:07:22 -0700 | [diff] [blame] | 84 | final L2ModificationInstruction.ModMplsHeaderInstruction instruction = |
| 85 | (L2ModificationInstruction.ModMplsHeaderInstruction) Instructions.pushMpls(); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 86 | final ObjectNode instructionJson = instructionCodec.encode(instruction, context); |
| 87 | |
| 88 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 89 | } |
| 90 | |
| 91 | /** |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 92 | * Tests the encoding of output instructions. |
| 93 | */ |
| 94 | @Test |
| 95 | public void outputInstructionTest() { |
| 96 | final Instructions.OutputInstruction instruction = |
| 97 | Instructions.createOutput(PortNumber.portNumber(22)); |
| 98 | final ObjectNode instructionJson = |
| 99 | instructionCodec.encode(instruction, context); |
| 100 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 101 | } |
| 102 | |
| 103 | /** |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 104 | * Tests the encoding of mod OCh signal instructions. |
| 105 | */ |
| 106 | @Test |
| 107 | public void modOchSignalInstructionTest() { |
| 108 | L0ModificationInstruction.ModOchSignalInstruction instruction = |
| 109 | (L0ModificationInstruction.ModOchSignalInstruction) |
| 110 | Instructions.modL0Lambda(Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8)); |
| 111 | ObjectNode instructionJson = |
| 112 | instructionCodec.encode(instruction, context); |
| 113 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 114 | } |
| 115 | |
| 116 | /** |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 117 | * Tests the encoding of mod ODU signal ID instructions. |
| 118 | */ |
| 119 | @Test |
| 120 | public void modOduSignalIdInstructionTest() { |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 121 | OduSignalId oduSignalId = OduSignalId.oduSignalId(1, 8, new byte[] {8, 0, 0, 0, 0, 0, 0, 0, 0, 0}); |
Yafit Hadar | 5796d97 | 2015-10-15 13:16:11 +0300 | [diff] [blame] | 122 | L1ModificationInstruction.ModOduSignalIdInstruction instruction = |
| 123 | (L1ModificationInstruction.ModOduSignalIdInstruction) |
| 124 | Instructions.modL1OduSignalId(oduSignalId); |
| 125 | ObjectNode instructionJson = |
| 126 | instructionCodec.encode(instruction, context); |
| 127 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 128 | } |
| 129 | |
| 130 | /** |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 131 | * Tests the encoding of mod ether instructions. |
| 132 | */ |
| 133 | @Test |
| 134 | public void modEtherInstructionTest() { |
| 135 | final L2ModificationInstruction.ModEtherInstruction instruction = |
| 136 | (L2ModificationInstruction.ModEtherInstruction) |
| 137 | Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66")); |
| 138 | final ObjectNode instructionJson = |
| 139 | instructionCodec.encode(instruction, context); |
| 140 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Tests the encoding of mod vlan id instructions. |
| 145 | */ |
| 146 | @Test |
| 147 | public void modVlanIdInstructionTest() { |
| 148 | final L2ModificationInstruction.ModVlanIdInstruction instruction = |
| 149 | (L2ModificationInstruction.ModVlanIdInstruction) |
| 150 | Instructions.modVlanId(VlanId.vlanId((short) 12)); |
| 151 | |
| 152 | final ObjectNode instructionJson = |
| 153 | instructionCodec.encode(instruction, context); |
| 154 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Tests the encoding of mod vlan pcp instructions. |
| 159 | */ |
| 160 | @Test |
| 161 | public void modVlanPcpInstructionTest() { |
| 162 | final L2ModificationInstruction.ModVlanPcpInstruction instruction = |
| 163 | (L2ModificationInstruction.ModVlanPcpInstruction) |
| 164 | Instructions.modVlanPcp((byte) 9); |
| 165 | final ObjectNode instructionJson = |
| 166 | instructionCodec.encode(instruction, context); |
| 167 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 168 | } |
| 169 | |
| 170 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 171 | * Tests the encoding of mod IPv4 src instructions. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 172 | */ |
| 173 | @Test |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 174 | public void modIPSrcInstructionTest() { |
| 175 | final Ip4Address ip = Ip4Address.valueOf("1.2.3.4"); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 176 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 177 | (L3ModificationInstruction.ModIPInstruction) |
| 178 | Instructions.modL3Src(ip); |
| 179 | final ObjectNode instructionJson = |
| 180 | instructionCodec.encode(instruction, context); |
| 181 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 182 | } |
| 183 | |
| 184 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 185 | * Tests the encoding of mod IPv4 dst instructions. |
| 186 | */ |
| 187 | @Test |
| 188 | public void modIPDstInstructionTest() { |
| 189 | final Ip4Address ip = Ip4Address.valueOf("1.2.3.4"); |
| 190 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 191 | (L3ModificationInstruction.ModIPInstruction) |
| 192 | Instructions.modL3Dst(ip); |
| 193 | final ObjectNode instructionJson = |
| 194 | instructionCodec.encode(instruction, context); |
| 195 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Tests the encoding of mod IPv6 src instructions. |
| 200 | */ |
| 201 | @Test |
| 202 | public void modIPv6SrcInstructionTest() { |
| 203 | final Ip6Address ip = Ip6Address.valueOf("1111::2222"); |
| 204 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 205 | (L3ModificationInstruction.ModIPInstruction) |
| 206 | Instructions.modL3IPv6Src(ip); |
| 207 | final ObjectNode instructionJson = |
| 208 | instructionCodec.encode(instruction, context); |
| 209 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Tests the encoding of mod IPv6 dst instructions. |
| 214 | */ |
| 215 | @Test |
| 216 | public void modIPv6DstInstructionTest() { |
| 217 | final Ip6Address ip = Ip6Address.valueOf("1111::2222"); |
| 218 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 219 | (L3ModificationInstruction.ModIPInstruction) |
| 220 | Instructions.modL3IPv6Dst(ip); |
| 221 | final ObjectNode instructionJson = |
| 222 | instructionCodec.encode(instruction, context); |
| 223 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Tests the encoding of mod IPv6 flow label instructions. |
| 228 | */ |
| 229 | @Test |
| 230 | public void modIPv6FlowLabelInstructionTest() { |
| 231 | final int flowLabel = 0xfffff; |
| 232 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction = |
| 233 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) |
| 234 | Instructions.modL3IPv6FlowLabel(flowLabel); |
| 235 | final ObjectNode instructionJson = |
| 236 | instructionCodec.encode(instruction, context); |
| 237 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 238 | } |
| 239 | |
| 240 | /** |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 241 | * Tests the encoding of mod MPLS label instructions. |
| 242 | */ |
| 243 | @Test |
| 244 | public void modMplsLabelInstructionTest() { |
| 245 | final L2ModificationInstruction.ModMplsLabelInstruction instruction = |
| 246 | (L2ModificationInstruction.ModMplsLabelInstruction) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 247 | Instructions.modMplsLabel(MplsLabel.mplsLabel(99)); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 248 | final ObjectNode instructionJson = |
| 249 | instructionCodec.encode(instruction, context); |
| 250 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 251 | } |
| 252 | |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 253 | /** |
| 254 | * Tests the encoding of protocol-independent instructions. |
| 255 | */ |
| 256 | @Test |
| 257 | public void piInstructionEncodingTest() { |
| 258 | PiActionId actionId = PiActionId.of("set_egress_port"); |
| 259 | PiActionParamId actionParamId = PiActionParamId.of("port"); |
| 260 | PiActionParam actionParam = new PiActionParam(actionParamId, ImmutableByteSequence.copyFrom(10)); |
| 261 | PiTableAction action = PiAction.builder().withId(actionId).withParameter(actionParam).build(); |
| 262 | final PiInstruction actionInstruction = Instructions.piTableAction(action); |
| 263 | final ObjectNode actionInstructionJson = |
| 264 | instructionCodec.encode(actionInstruction, context); |
| 265 | assertThat(actionInstructionJson, matchesInstruction(actionInstruction)); |
| 266 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 267 | PiTableAction actionGroupId = PiActionProfileGroupId.of(10); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 268 | final PiInstruction actionGroupIdInstruction = Instructions.piTableAction(actionGroupId); |
| 269 | final ObjectNode actionGroupIdInstructionJson = |
| 270 | instructionCodec.encode(actionGroupIdInstruction, context); |
| 271 | assertThat(actionGroupIdInstructionJson, matchesInstruction(actionGroupIdInstruction)); |
| 272 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 273 | PiTableAction actionProfileMemberId = PiActionProfileMemberId.of(10); |
| 274 | final PiInstruction actionProfileMemberIdInstruction = Instructions.piTableAction(actionProfileMemberId); |
| 275 | final ObjectNode actionProfileMemberIdInstructionJson = |
| 276 | instructionCodec.encode(actionProfileMemberIdInstruction, context); |
| 277 | assertThat(actionProfileMemberIdInstructionJson, matchesInstruction(actionProfileMemberIdInstruction)); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Tests the decoding of protocol-independent instructions. |
| 282 | */ |
| 283 | @Test |
| 284 | public void piInstructionDecodingTest() throws IOException { |
| 285 | |
| 286 | Instruction actionInstruction = getInstruction("PiActionInstruction.json"); |
| 287 | Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT)); |
| 288 | PiTableAction action = ((PiInstruction) actionInstruction).action(); |
| 289 | Assert.assertThat(action.type(), is(PiTableAction.Type.ACTION)); |
| 290 | Assert.assertThat(((PiAction) action).id().id(), is("set_egress_port")); |
| 291 | Assert.assertThat(((PiAction) action).parameters().size(), is(1)); |
| 292 | Collection<PiActionParam> actionParams = ((PiAction) action).parameters(); |
| 293 | PiActionParam actionParam = actionParams.iterator().next(); |
| 294 | Assert.assertThat(actionParam.id().id(), is("port")); |
| 295 | Assert.assertThat(actionParam.value(), is(copyFrom((byte) 0x1))); |
| 296 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 297 | Instruction actionGroupIdInstruction = getInstruction("PiActionProfileGroupIdInstruction.json"); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 298 | Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT)); |
| 299 | PiTableAction actionGroupId = ((PiInstruction) actionGroupIdInstruction).action(); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 300 | Assert.assertThat(actionGroupId.type(), is(PiTableAction.Type.ACTION_PROFILE_GROUP_ID)); |
| 301 | Assert.assertThat(((PiActionProfileGroupId) actionGroupId).id(), is(100)); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 302 | |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 303 | Instruction actionMemberIdInstruction = getInstruction("PiActionProfileMemberIdInstruction.json"); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 304 | Assert.assertThat(actionInstruction.type(), is(Instruction.Type.PROTOCOL_INDEPENDENT)); |
| 305 | PiTableAction actionMemberId = ((PiInstruction) actionMemberIdInstruction).action(); |
Carmelo Cascone | cb4327a | 2018-09-11 15:17:23 -0700 | [diff] [blame] | 306 | Assert.assertThat(actionMemberId.type(), is(PiTableAction.Type.ACTION_PROFILE_MEMBER_ID)); |
| 307 | Assert.assertThat(((PiActionProfileMemberId) actionMemberId).id(), is(100)); |
Frank Wang | 74ce2c1 | 2018-04-11 20:26:45 +0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Reads in an instruction from the given resource and decodes it. |
| 312 | * |
| 313 | * @param resourceName resource to use to read the JSON for the rule |
| 314 | * @return decoded instruction |
| 315 | * @throws IOException if processing the resource fails |
| 316 | */ |
| 317 | private Instruction getInstruction(String resourceName) throws IOException { |
| 318 | InputStream jsonStream = InstructionCodecTest.class.getResourceAsStream(resourceName); |
| 319 | JsonNode json = context.mapper().readTree(jsonStream); |
| 320 | MatcherAssert.assertThat(json, notNullValue()); |
| 321 | Instruction instruction = instructionCodec.decode((ObjectNode) json, context); |
| 322 | Assert.assertThat(instruction, notNullValue()); |
| 323 | return instruction; |
| 324 | } |
| 325 | |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 326 | } |