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