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