blob: c42d83a8f739dc5bbd91cf454c7afa251171d095 [file] [log] [blame]
Ray Milkeyd03eda02015-01-09 14:58:48 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyd03eda02015-01-09 14:58:48 -08003 *
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
Jian Li11260a02016-05-19 13:07:22 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd03eda02015-01-09 14:58:48 -080019import org.junit.Before;
20import org.junit.Test;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080021import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip6Address;
Ray Milkeyd03eda02015-01-09 14:58:48 -080023import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010024import org.onlab.packet.MplsLabel;
Ray Milkeyd03eda02015-01-09 14:58:48 -080025import org.onlab.packet.VlanId;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070028import org.onosproject.net.ChannelSpacing;
29import org.onosproject.net.GridType;
30import org.onosproject.net.Lambda;
Yafit Hadar5796d972015-10-15 13:16:11 +030031import org.onosproject.net.OduSignalId;
Ray Milkeyd03eda02015-01-09 14:58:48 -080032import org.onosproject.net.PortNumber;
33import org.onosproject.net.flow.instructions.Instruction;
34import org.onosproject.net.flow.instructions.Instructions;
35import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030036import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -080037import org.onosproject.net.flow.instructions.L2ModificationInstruction;
38import org.onosproject.net.flow.instructions.L3ModificationInstruction;
39
Jian Li11260a02016-05-19 13:07:22 -070040import static org.hamcrest.MatcherAssert.assertThat;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -080043
Ray Milkeyd03eda02015-01-09 14:58:48 -080044/**
45 * Unit tests for Instruction codec.
46 */
47public class InstructionCodecTest {
48 CodecContext context;
49 JsonCodec<Instruction> instructionCodec;
Ray Milkeyd03eda02015-01-09 14:58:48 -080050 /**
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 /**
Jian Li11260a02016-05-19 13:07:22 -070062 * Tests the encoding of push mpls header instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -080063 */
64 @Test
65 public void pushHeaderInstructionsTest() {
Jian Li11260a02016-05-19 13:07:22 -070066 final L2ModificationInstruction.ModMplsHeaderInstruction instruction =
67 (L2ModificationInstruction.ModMplsHeaderInstruction) Instructions.pushMpls();
Ray Milkeyd03eda02015-01-09 14:58:48 -080068 final ObjectNode instructionJson = instructionCodec.encode(instruction, context);
69
70 assertThat(instructionJson, matchesInstruction(instruction));
71 }
72
73 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -080074 * 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 SHIMIZUed7af542015-05-05 19:10:45 -070086 * 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 Hadar5796d972015-10-15 13:16:11 +030099 * Tests the encoding of mod ODU signal ID instructions.
100 */
101 @Test
102 public void modOduSignalIdInstructionTest() {
Jian Li68c4fc42016-01-11 16:07:03 -0800103 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 +0300104 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 Milkeyd03eda02015-01-09 14:58:48 -0800113 * 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800153 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800154 */
155 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800156 public void modIPSrcInstructionTest() {
157 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800158 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 Radoslavovfebe82c2015-02-11 19:08:15 -0800167 * 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 Milkeyd03eda02015-01-09 14:58:48 -0800223 * Tests the encoding of mod MPLS label instructions.
224 */
225 @Test
226 public void modMplsLabelInstructionTest() {
227 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
228 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100229 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800230 final ObjectNode instructionJson =
231 instructionCodec.encode(instruction, context);
232 assertThat(instructionJson, matchesInstruction(instruction));
233 }
234
235}