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