blob: bafbc0f10661cc16d2f4c4f646658e7462d87dc9 [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;
Ray Milkey85571eb2015-06-25 09:45:46 -070029import org.onosproject.net.IndexedLambda;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070030import org.onosproject.net.Lambda;
Ray Milkeyd03eda02015-01-09 14:58:48 -080031import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.instructions.Instruction;
33import org.onosproject.net.flow.instructions.Instructions;
34import org.onosproject.net.flow.instructions.L0ModificationInstruction;
35import org.onosproject.net.flow.instructions.L2ModificationInstruction;
36import org.onosproject.net.flow.instructions.L3ModificationInstruction;
37
38import com.fasterxml.jackson.databind.node.ObjectNode;
39
40import static org.hamcrest.MatcherAssert.assertThat;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
43
44/**
45 * Unit tests for Instruction codec.
46 */
47public class InstructionCodecTest {
48 CodecContext context;
49 JsonCodec<Instruction> instructionCodec;
50
51 /**
Ray Milkeydb358082015-01-13 16:34:38 -080052 * Sets up for each test. Creates a context and fetches the instruction
Ray Milkeyd03eda02015-01-09 14:58:48 -080053 * 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 HIGUCHI6a479642015-02-08 01:28:50 -080080 Instructions.createDrop();
Ray Milkeyd03eda02015-01-09 14:58:48 -080081 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 Milkey85571eb2015-06-25 09:45:46 -0700105 Instructions.modL0Lambda(new IndexedLambda(55));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800106 final ObjectNode instructionJson =
107 instructionCodec.encode(instruction, context);
108 assertThat(instructionJson, matchesInstruction(instruction));
109 }
110
111 /**
Sho SHIMIZUed7af542015-05-05 19:10:45 -0700112 * 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 Milkeyd03eda02015-01-09 14:58:48 -0800125 * 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800165 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800166 */
167 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800168 public void modIPSrcInstructionTest() {
169 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800170 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800179 * 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 Milkeyd03eda02015-01-09 14:58:48 -0800235 * Tests the encoding of mod MPLS label instructions.
236 */
237 @Test
238 public void modMplsLabelInstructionTest() {
239 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
240 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100241 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800242 final ObjectNode instructionJson =
243 instructionCodec.encode(instruction, context);
244 assertThat(instructionJson, matchesInstruction(instruction));
245 }
246
247}