blob: 1b7a5017da4b8bdf356b700de0083a7f7cee8b0f [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 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -080076 * Tests the encoding of output instructions.
77 */
78 @Test
79 public void outputInstructionTest() {
80 final Instructions.OutputInstruction instruction =
81 Instructions.createOutput(PortNumber.portNumber(22));
82 final ObjectNode instructionJson =
83 instructionCodec.encode(instruction, context);
84 assertThat(instructionJson, matchesInstruction(instruction));
85 }
86
87 /**
88 * Tests the encoding of mod lambda instructions.
89 */
90 @Test
91 public void modLambdaInstructionTest() {
92 final L0ModificationInstruction.ModLambdaInstruction instruction =
93 (L0ModificationInstruction.ModLambdaInstruction)
Ray Milkey85571eb2015-06-25 09:45:46 -070094 Instructions.modL0Lambda(new IndexedLambda(55));
Ray Milkeyd03eda02015-01-09 14:58:48 -080095 final ObjectNode instructionJson =
96 instructionCodec.encode(instruction, context);
97 assertThat(instructionJson, matchesInstruction(instruction));
98 }
99
100 /**
Sho SHIMIZUed7af542015-05-05 19:10:45 -0700101 * Tests the encoding of mod OCh signal instructions.
102 */
103 @Test
104 public void modOchSignalInstructionTest() {
105 L0ModificationInstruction.ModOchSignalInstruction instruction =
106 (L0ModificationInstruction.ModOchSignalInstruction)
107 Instructions.modL0Lambda(Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8));
108 ObjectNode instructionJson =
109 instructionCodec.encode(instruction, context);
110 assertThat(instructionJson, matchesInstruction(instruction));
111 }
112
113 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300114 * Tests the encoding of mod ODU signal ID instructions.
115 */
116 @Test
117 public void modOduSignalIdInstructionTest() {
Jian Li68c4fc42016-01-11 16:07:03 -0800118 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 +0300119 L1ModificationInstruction.ModOduSignalIdInstruction instruction =
120 (L1ModificationInstruction.ModOduSignalIdInstruction)
121 Instructions.modL1OduSignalId(oduSignalId);
122 ObjectNode instructionJson =
123 instructionCodec.encode(instruction, context);
124 assertThat(instructionJson, matchesInstruction(instruction));
125 }
126
127 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800128 * Tests the encoding of mod ether instructions.
129 */
130 @Test
131 public void modEtherInstructionTest() {
132 final L2ModificationInstruction.ModEtherInstruction instruction =
133 (L2ModificationInstruction.ModEtherInstruction)
134 Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
135 final ObjectNode instructionJson =
136 instructionCodec.encode(instruction, context);
137 assertThat(instructionJson, matchesInstruction(instruction));
138 }
139
140 /**
141 * Tests the encoding of mod vlan id instructions.
142 */
143 @Test
144 public void modVlanIdInstructionTest() {
145 final L2ModificationInstruction.ModVlanIdInstruction instruction =
146 (L2ModificationInstruction.ModVlanIdInstruction)
147 Instructions.modVlanId(VlanId.vlanId((short) 12));
148
149 final ObjectNode instructionJson =
150 instructionCodec.encode(instruction, context);
151 assertThat(instructionJson, matchesInstruction(instruction));
152 }
153
154 /**
155 * Tests the encoding of mod vlan pcp instructions.
156 */
157 @Test
158 public void modVlanPcpInstructionTest() {
159 final L2ModificationInstruction.ModVlanPcpInstruction instruction =
160 (L2ModificationInstruction.ModVlanPcpInstruction)
161 Instructions.modVlanPcp((byte) 9);
162 final ObjectNode instructionJson =
163 instructionCodec.encode(instruction, context);
164 assertThat(instructionJson, matchesInstruction(instruction));
165 }
166
167 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800168 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800169 */
170 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800171 public void modIPSrcInstructionTest() {
172 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800173 final L3ModificationInstruction.ModIPInstruction instruction =
174 (L3ModificationInstruction.ModIPInstruction)
175 Instructions.modL3Src(ip);
176 final ObjectNode instructionJson =
177 instructionCodec.encode(instruction, context);
178 assertThat(instructionJson, matchesInstruction(instruction));
179 }
180
181 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800182 * Tests the encoding of mod IPv4 dst instructions.
183 */
184 @Test
185 public void modIPDstInstructionTest() {
186 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
187 final L3ModificationInstruction.ModIPInstruction instruction =
188 (L3ModificationInstruction.ModIPInstruction)
189 Instructions.modL3Dst(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 src instructions.
197 */
198 @Test
199 public void modIPv6SrcInstructionTest() {
200 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
201 final L3ModificationInstruction.ModIPInstruction instruction =
202 (L3ModificationInstruction.ModIPInstruction)
203 Instructions.modL3IPv6Src(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 dst instructions.
211 */
212 @Test
213 public void modIPv6DstInstructionTest() {
214 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
215 final L3ModificationInstruction.ModIPInstruction instruction =
216 (L3ModificationInstruction.ModIPInstruction)
217 Instructions.modL3IPv6Dst(ip);
218 final ObjectNode instructionJson =
219 instructionCodec.encode(instruction, context);
220 assertThat(instructionJson, matchesInstruction(instruction));
221 }
222
223 /**
224 * Tests the encoding of mod IPv6 flow label instructions.
225 */
226 @Test
227 public void modIPv6FlowLabelInstructionTest() {
228 final int flowLabel = 0xfffff;
229 final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction =
230 (L3ModificationInstruction.ModIPv6FlowLabelInstruction)
231 Instructions.modL3IPv6FlowLabel(flowLabel);
232 final ObjectNode instructionJson =
233 instructionCodec.encode(instruction, context);
234 assertThat(instructionJson, matchesInstruction(instruction));
235 }
236
237 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800238 * Tests the encoding of mod MPLS label instructions.
239 */
240 @Test
241 public void modMplsLabelInstructionTest() {
242 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
243 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100244 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800245 final ObjectNode instructionJson =
246 instructionCodec.encode(instruction, context);
247 assertThat(instructionJson, matchesInstruction(instruction));
248 }
249
250}