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