blob: 8d45f4ca6aff5cc113e5f8583f17840d6b50be26 [file] [log] [blame]
Ray Milkeyd03eda02015-01-09 14:58:48 -08001/*
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 */
16package org.onosproject.codec.impl;
17
Yafit Hadar5796d972015-10-15 13:16:11 +030018import static org.hamcrest.MatcherAssert.assertThat;
19import static org.hamcrest.Matchers.notNullValue;
20import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
21
Ray Milkeyd03eda02015-01-09 14:58:48 -080022import org.junit.Before;
23import org.junit.Test;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080024import org.onlab.packet.Ip4Address;
25import org.onlab.packet.Ip6Address;
Ray Milkeyd03eda02015-01-09 14:58:48 -080026import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010027import org.onlab.packet.MplsLabel;
Ray Milkeyd03eda02015-01-09 14:58:48 -080028import org.onlab.packet.VlanId;
29import org.onosproject.codec.CodecContext;
30import org.onosproject.codec.JsonCodec;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070031import org.onosproject.net.ChannelSpacing;
32import org.onosproject.net.GridType;
Ray Milkey85571eb2015-06-25 09:45:46 -070033import org.onosproject.net.IndexedLambda;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070034import org.onosproject.net.Lambda;
Yafit Hadar5796d972015-10-15 13:16:11 +030035import org.onosproject.net.OduSignalId;
Ray Milkeyd03eda02015-01-09 14:58:48 -080036import org.onosproject.net.PortNumber;
37import org.onosproject.net.flow.instructions.Instruction;
38import org.onosproject.net.flow.instructions.Instructions;
39import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030040import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -080041import org.onosproject.net.flow.instructions.L2ModificationInstruction;
42import org.onosproject.net.flow.instructions.L3ModificationInstruction;
43
44import com.fasterxml.jackson.databind.node.ObjectNode;
45
Ray Milkeyd03eda02015-01-09 14:58:48 -080046/**
47 * Unit tests for Instruction codec.
48 */
49public class InstructionCodecTest {
50 CodecContext context;
51 JsonCodec<Instruction> instructionCodec;
Ray Milkeyd03eda02015-01-09 14:58:48 -080052 /**
Ray Milkeydb358082015-01-13 16:34:38 -080053 * Sets up for each test. Creates a context and fetches the instruction
Ray Milkeyd03eda02015-01-09 14:58:48 -080054 * codec.
55 */
56 @Before
57 public void setUp() {
58 context = new MockCodecContext();
59 instructionCodec = context.codec(Instruction.class);
60 assertThat(instructionCodec, notNullValue());
61 }
62
63 /**
64 * Tests the encoding of push header instructions.
65 */
66 @Test
67 public void pushHeaderInstructionsTest() {
68 final L2ModificationInstruction.PushHeaderInstructions instruction =
69 (L2ModificationInstruction.PushHeaderInstructions) Instructions.pushMpls();
70 final ObjectNode instructionJson = instructionCodec.encode(instruction, context);
71
72 assertThat(instructionJson, matchesInstruction(instruction));
73 }
74
75 /**
76 * Tests the encoding of drop instructions.
77 */
78 @Test
79 public void dropInstructionTest() {
80 final Instructions.DropInstruction instruction =
Yuta HIGUCHI6a479642015-02-08 01:28:50 -080081 Instructions.createDrop();
Ray Milkeyd03eda02015-01-09 14:58:48 -080082 final ObjectNode instructionJson =
83 instructionCodec.encode(instruction, context);
84 assertThat(instructionJson, matchesInstruction(instruction));
85 }
86
87 /**
88 * Tests the encoding of output instructions.
89 */
90 @Test
91 public void outputInstructionTest() {
92 final Instructions.OutputInstruction instruction =
93 Instructions.createOutput(PortNumber.portNumber(22));
94 final ObjectNode instructionJson =
95 instructionCodec.encode(instruction, context);
96 assertThat(instructionJson, matchesInstruction(instruction));
97 }
98
99 /**
100 * Tests the encoding of mod lambda instructions.
101 */
102 @Test
103 public void modLambdaInstructionTest() {
104 final L0ModificationInstruction.ModLambdaInstruction instruction =
105 (L0ModificationInstruction.ModLambdaInstruction)
Ray Milkey85571eb2015-06-25 09:45:46 -0700106 Instructions.modL0Lambda(new IndexedLambda(55));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800107 final ObjectNode instructionJson =
108 instructionCodec.encode(instruction, context);
109 assertThat(instructionJson, matchesInstruction(instruction));
110 }
111
112 /**
Sho SHIMIZUed7af542015-05-05 19:10:45 -0700113 * Tests the encoding of mod OCh signal instructions.
114 */
115 @Test
116 public void modOchSignalInstructionTest() {
117 L0ModificationInstruction.ModOchSignalInstruction instruction =
118 (L0ModificationInstruction.ModOchSignalInstruction)
119 Instructions.modL0Lambda(Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8));
120 ObjectNode instructionJson =
121 instructionCodec.encode(instruction, context);
122 assertThat(instructionJson, matchesInstruction(instruction));
123 }
124
125 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300126 * Tests the encoding of mod ODU signal ID instructions.
127 */
128 @Test
129 public void modOduSignalIdInstructionTest() {
Jian Li68c4fc42016-01-11 16:07:03 -0800130 OduSignalId oduSignalId = OduSignalId.oduSignalId(1, 8, new byte[] {8, 0, 0, 0, 0, 0, 0, 0, 0, 0});
Yafit Hadar5796d972015-10-15 13:16:11 +0300131 L1ModificationInstruction.ModOduSignalIdInstruction instruction =
132 (L1ModificationInstruction.ModOduSignalIdInstruction)
133 Instructions.modL1OduSignalId(oduSignalId);
134 ObjectNode instructionJson =
135 instructionCodec.encode(instruction, context);
136 assertThat(instructionJson, matchesInstruction(instruction));
137 }
138
139 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800140 * Tests the encoding of mod ether instructions.
141 */
142 @Test
143 public void modEtherInstructionTest() {
144 final L2ModificationInstruction.ModEtherInstruction instruction =
145 (L2ModificationInstruction.ModEtherInstruction)
146 Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
147 final ObjectNode instructionJson =
148 instructionCodec.encode(instruction, context);
149 assertThat(instructionJson, matchesInstruction(instruction));
150 }
151
152 /**
153 * Tests the encoding of mod vlan id instructions.
154 */
155 @Test
156 public void modVlanIdInstructionTest() {
157 final L2ModificationInstruction.ModVlanIdInstruction instruction =
158 (L2ModificationInstruction.ModVlanIdInstruction)
159 Instructions.modVlanId(VlanId.vlanId((short) 12));
160
161 final ObjectNode instructionJson =
162 instructionCodec.encode(instruction, context);
163 assertThat(instructionJson, matchesInstruction(instruction));
164 }
165
166 /**
167 * Tests the encoding of mod vlan pcp instructions.
168 */
169 @Test
170 public void modVlanPcpInstructionTest() {
171 final L2ModificationInstruction.ModVlanPcpInstruction instruction =
172 (L2ModificationInstruction.ModVlanPcpInstruction)
173 Instructions.modVlanPcp((byte) 9);
174 final ObjectNode instructionJson =
175 instructionCodec.encode(instruction, context);
176 assertThat(instructionJson, matchesInstruction(instruction));
177 }
178
179 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800180 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800181 */
182 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800183 public void modIPSrcInstructionTest() {
184 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800185 final L3ModificationInstruction.ModIPInstruction instruction =
186 (L3ModificationInstruction.ModIPInstruction)
187 Instructions.modL3Src(ip);
188 final ObjectNode instructionJson =
189 instructionCodec.encode(instruction, context);
190 assertThat(instructionJson, matchesInstruction(instruction));
191 }
192
193 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800194 * Tests the encoding of mod IPv4 dst instructions.
195 */
196 @Test
197 public void modIPDstInstructionTest() {
198 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
199 final L3ModificationInstruction.ModIPInstruction instruction =
200 (L3ModificationInstruction.ModIPInstruction)
201 Instructions.modL3Dst(ip);
202 final ObjectNode instructionJson =
203 instructionCodec.encode(instruction, context);
204 assertThat(instructionJson, matchesInstruction(instruction));
205 }
206
207 /**
208 * Tests the encoding of mod IPv6 src instructions.
209 */
210 @Test
211 public void modIPv6SrcInstructionTest() {
212 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
213 final L3ModificationInstruction.ModIPInstruction instruction =
214 (L3ModificationInstruction.ModIPInstruction)
215 Instructions.modL3IPv6Src(ip);
216 final ObjectNode instructionJson =
217 instructionCodec.encode(instruction, context);
218 assertThat(instructionJson, matchesInstruction(instruction));
219 }
220
221 /**
222 * Tests the encoding of mod IPv6 dst instructions.
223 */
224 @Test
225 public void modIPv6DstInstructionTest() {
226 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
227 final L3ModificationInstruction.ModIPInstruction instruction =
228 (L3ModificationInstruction.ModIPInstruction)
229 Instructions.modL3IPv6Dst(ip);
230 final ObjectNode instructionJson =
231 instructionCodec.encode(instruction, context);
232 assertThat(instructionJson, matchesInstruction(instruction));
233 }
234
235 /**
236 * Tests the encoding of mod IPv6 flow label instructions.
237 */
238 @Test
239 public void modIPv6FlowLabelInstructionTest() {
240 final int flowLabel = 0xfffff;
241 final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction =
242 (L3ModificationInstruction.ModIPv6FlowLabelInstruction)
243 Instructions.modL3IPv6FlowLabel(flowLabel);
244 final ObjectNode instructionJson =
245 instructionCodec.encode(instruction, context);
246 assertThat(instructionJson, matchesInstruction(instruction));
247 }
248
249 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800250 * Tests the encoding of mod MPLS label instructions.
251 */
252 @Test
253 public void modMplsLabelInstructionTest() {
254 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
255 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100256 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800257 final ObjectNode instructionJson =
258 instructionCodec.encode(instruction, context);
259 assertThat(instructionJson, matchesInstruction(instruction));
260 }
261
262}