Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.Ip6Address; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 23 | import org.onlab.packet.MplsLabel; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.flow.instructions.Instruction; |
| 29 | import org.onosproject.net.flow.instructions.Instructions; |
| 30 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 31 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 32 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 33 | |
| 34 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 35 | |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.Matchers.notNullValue; |
| 38 | import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction; |
| 39 | |
| 40 | /** |
| 41 | * Unit tests for Instruction codec. |
| 42 | */ |
| 43 | public class InstructionCodecTest { |
| 44 | CodecContext context; |
| 45 | JsonCodec<Instruction> instructionCodec; |
| 46 | |
| 47 | /** |
Ray Milkey | db35808 | 2015-01-13 16:34:38 -0800 | [diff] [blame] | 48 | * Sets up for each test. Creates a context and fetches the instruction |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 49 | * codec. |
| 50 | */ |
| 51 | @Before |
| 52 | public void setUp() { |
| 53 | context = new MockCodecContext(); |
| 54 | instructionCodec = context.codec(Instruction.class); |
| 55 | assertThat(instructionCodec, notNullValue()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tests the encoding of push header instructions. |
| 60 | */ |
| 61 | @Test |
| 62 | public void pushHeaderInstructionsTest() { |
| 63 | final L2ModificationInstruction.PushHeaderInstructions instruction = |
| 64 | (L2ModificationInstruction.PushHeaderInstructions) Instructions.pushMpls(); |
| 65 | final ObjectNode instructionJson = instructionCodec.encode(instruction, context); |
| 66 | |
| 67 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests the encoding of drop instructions. |
| 72 | */ |
| 73 | @Test |
| 74 | public void dropInstructionTest() { |
| 75 | final Instructions.DropInstruction instruction = |
Yuta HIGUCHI | 6a47964 | 2015-02-08 01:28:50 -0800 | [diff] [blame] | 76 | Instructions.createDrop(); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 77 | final ObjectNode instructionJson = |
| 78 | instructionCodec.encode(instruction, context); |
| 79 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests the encoding of output instructions. |
| 84 | */ |
| 85 | @Test |
| 86 | public void outputInstructionTest() { |
| 87 | final Instructions.OutputInstruction instruction = |
| 88 | Instructions.createOutput(PortNumber.portNumber(22)); |
| 89 | final ObjectNode instructionJson = |
| 90 | instructionCodec.encode(instruction, context); |
| 91 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Tests the encoding of mod lambda instructions. |
| 96 | */ |
| 97 | @Test |
| 98 | public void modLambdaInstructionTest() { |
| 99 | final L0ModificationInstruction.ModLambdaInstruction instruction = |
| 100 | (L0ModificationInstruction.ModLambdaInstruction) |
| 101 | Instructions.modL0Lambda((short) 55); |
| 102 | final ObjectNode instructionJson = |
| 103 | instructionCodec.encode(instruction, context); |
| 104 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests the encoding of mod ether instructions. |
| 109 | */ |
| 110 | @Test |
| 111 | public void modEtherInstructionTest() { |
| 112 | final L2ModificationInstruction.ModEtherInstruction instruction = |
| 113 | (L2ModificationInstruction.ModEtherInstruction) |
| 114 | Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66")); |
| 115 | final ObjectNode instructionJson = |
| 116 | instructionCodec.encode(instruction, context); |
| 117 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests the encoding of mod vlan id instructions. |
| 122 | */ |
| 123 | @Test |
| 124 | public void modVlanIdInstructionTest() { |
| 125 | final L2ModificationInstruction.ModVlanIdInstruction instruction = |
| 126 | (L2ModificationInstruction.ModVlanIdInstruction) |
| 127 | Instructions.modVlanId(VlanId.vlanId((short) 12)); |
| 128 | |
| 129 | final ObjectNode instructionJson = |
| 130 | instructionCodec.encode(instruction, context); |
| 131 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests the encoding of mod vlan pcp instructions. |
| 136 | */ |
| 137 | @Test |
| 138 | public void modVlanPcpInstructionTest() { |
| 139 | final L2ModificationInstruction.ModVlanPcpInstruction instruction = |
| 140 | (L2ModificationInstruction.ModVlanPcpInstruction) |
| 141 | Instructions.modVlanPcp((byte) 9); |
| 142 | final ObjectNode instructionJson = |
| 143 | instructionCodec.encode(instruction, context); |
| 144 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 145 | } |
| 146 | |
| 147 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 148 | * Tests the encoding of mod IPv4 src instructions. |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 149 | */ |
| 150 | @Test |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 151 | public void modIPSrcInstructionTest() { |
| 152 | final Ip4Address ip = Ip4Address.valueOf("1.2.3.4"); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 153 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 154 | (L3ModificationInstruction.ModIPInstruction) |
| 155 | Instructions.modL3Src(ip); |
| 156 | final ObjectNode instructionJson = |
| 157 | instructionCodec.encode(instruction, context); |
| 158 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 159 | } |
| 160 | |
| 161 | /** |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 162 | * Tests the encoding of mod IPv4 dst instructions. |
| 163 | */ |
| 164 | @Test |
| 165 | public void modIPDstInstructionTest() { |
| 166 | final Ip4Address ip = Ip4Address.valueOf("1.2.3.4"); |
| 167 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 168 | (L3ModificationInstruction.ModIPInstruction) |
| 169 | Instructions.modL3Dst(ip); |
| 170 | final ObjectNode instructionJson = |
| 171 | instructionCodec.encode(instruction, context); |
| 172 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Tests the encoding of mod IPv6 src instructions. |
| 177 | */ |
| 178 | @Test |
| 179 | public void modIPv6SrcInstructionTest() { |
| 180 | final Ip6Address ip = Ip6Address.valueOf("1111::2222"); |
| 181 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 182 | (L3ModificationInstruction.ModIPInstruction) |
| 183 | Instructions.modL3IPv6Src(ip); |
| 184 | final ObjectNode instructionJson = |
| 185 | instructionCodec.encode(instruction, context); |
| 186 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests the encoding of mod IPv6 dst instructions. |
| 191 | */ |
| 192 | @Test |
| 193 | public void modIPv6DstInstructionTest() { |
| 194 | final Ip6Address ip = Ip6Address.valueOf("1111::2222"); |
| 195 | final L3ModificationInstruction.ModIPInstruction instruction = |
| 196 | (L3ModificationInstruction.ModIPInstruction) |
| 197 | Instructions.modL3IPv6Dst(ip); |
| 198 | final ObjectNode instructionJson = |
| 199 | instructionCodec.encode(instruction, context); |
| 200 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests the encoding of mod IPv6 flow label instructions. |
| 205 | */ |
| 206 | @Test |
| 207 | public void modIPv6FlowLabelInstructionTest() { |
| 208 | final int flowLabel = 0xfffff; |
| 209 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction = |
| 210 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) |
| 211 | Instructions.modL3IPv6FlowLabel(flowLabel); |
| 212 | final ObjectNode instructionJson = |
| 213 | instructionCodec.encode(instruction, context); |
| 214 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 215 | } |
| 216 | |
| 217 | /** |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 218 | * Tests the encoding of mod MPLS label instructions. |
| 219 | */ |
| 220 | @Test |
| 221 | public void modMplsLabelInstructionTest() { |
| 222 | final L2ModificationInstruction.ModMplsLabelInstruction instruction = |
| 223 | (L2ModificationInstruction.ModMplsLabelInstruction) |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 224 | Instructions.modMplsLabel(MplsLabel.mplsLabel(99)); |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 225 | final ObjectNode instructionJson = |
| 226 | instructionCodec.encode(instruction, context); |
| 227 | assertThat(instructionJson, matchesInstruction(instruction)); |
| 228 | } |
| 229 | |
| 230 | } |