blob: 29af34aac0eb1c7ae36ba5696514048b0de93f5a [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
18import org.junit.Before;
19import org.junit.Test;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080020import org.onlab.packet.Ip4Address;
21import org.onlab.packet.Ip6Address;
Ray Milkeyd03eda02015-01-09 14:58:48 -080022import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onlab.packet.MplsLabel;
Ray Milkeyd03eda02015-01-09 14:58:48 -080024import org.onlab.packet.VlanId;
25import org.onosproject.codec.CodecContext;
26import org.onosproject.codec.JsonCodec;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070027import org.onosproject.net.ChannelSpacing;
28import org.onosproject.net.GridType;
29import org.onosproject.net.Lambda;
Ray Milkeyd03eda02015-01-09 14:58:48 -080030import org.onosproject.net.PortNumber;
31import org.onosproject.net.flow.instructions.Instruction;
32import org.onosproject.net.flow.instructions.Instructions;
33import org.onosproject.net.flow.instructions.L0ModificationInstruction;
34import org.onosproject.net.flow.instructions.L2ModificationInstruction;
35import org.onosproject.net.flow.instructions.L3ModificationInstruction;
36
37import com.fasterxml.jackson.databind.node.ObjectNode;
38
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.notNullValue;
41import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
42
43/**
44 * Unit tests for Instruction codec.
45 */
46public class InstructionCodecTest {
47 CodecContext context;
48 JsonCodec<Instruction> instructionCodec;
49
50 /**
Ray Milkeydb358082015-01-13 16:34:38 -080051 * Sets up for each test. Creates a context and fetches the instruction
Ray Milkeyd03eda02015-01-09 14:58:48 -080052 * 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 HIGUCHI6a479642015-02-08 01:28:50 -080079 Instructions.createDrop();
Ray Milkeyd03eda02015-01-09 14:58:48 -080080 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 SHIMIZUed7af542015-05-05 19:10:45 -0700111 * 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 Milkeyd03eda02015-01-09 14:58:48 -0800124 * 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800164 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800165 */
166 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800167 public void modIPSrcInstructionTest() {
168 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800169 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800178 * 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 Milkeyd03eda02015-01-09 14:58:48 -0800234 * Tests the encoding of mod MPLS label instructions.
235 */
236 @Test
237 public void modMplsLabelInstructionTest() {
238 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
239 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100240 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800241 final ObjectNode instructionJson =
242 instructionCodec.encode(instruction, context);
243 assertThat(instructionJson, matchesInstruction(instruction));
244 }
245
246}