blob: 1168a5b6c14ba4c2d2dd81aaa5fd7e12c6fd187e [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
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;
33import org.onosproject.net.Lambda;
Yafit Hadar5796d972015-10-15 13:16:11 +030034import org.onosproject.net.OduSignalId;
Ray Milkeyd03eda02015-01-09 14:58:48 -080035import org.onosproject.net.PortNumber;
36import org.onosproject.net.flow.instructions.Instruction;
37import org.onosproject.net.flow.instructions.Instructions;
38import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030039import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -080040import org.onosproject.net.flow.instructions.L2ModificationInstruction;
41import org.onosproject.net.flow.instructions.L3ModificationInstruction;
42
43import com.fasterxml.jackson.databind.node.ObjectNode;
44
Ray Milkeyd03eda02015-01-09 14:58:48 -080045/**
46 * Unit tests for Instruction codec.
47 */
48public class InstructionCodecTest {
49 CodecContext context;
50 JsonCodec<Instruction> instructionCodec;
Ray Milkeyd03eda02015-01-09 14:58:48 -080051 /**
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 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -080075 * Tests the encoding of output instructions.
76 */
77 @Test
78 public void outputInstructionTest() {
79 final Instructions.OutputInstruction instruction =
80 Instructions.createOutput(PortNumber.portNumber(22));
81 final ObjectNode instructionJson =
82 instructionCodec.encode(instruction, context);
83 assertThat(instructionJson, matchesInstruction(instruction));
84 }
85
86 /**
Sho SHIMIZUed7af542015-05-05 19:10:45 -070087 * Tests the encoding of mod OCh signal instructions.
88 */
89 @Test
90 public void modOchSignalInstructionTest() {
91 L0ModificationInstruction.ModOchSignalInstruction instruction =
92 (L0ModificationInstruction.ModOchSignalInstruction)
93 Instructions.modL0Lambda(Lambda.ochSignal(GridType.DWDM, ChannelSpacing.CHL_100GHZ, 4, 8));
94 ObjectNode instructionJson =
95 instructionCodec.encode(instruction, context);
96 assertThat(instructionJson, matchesInstruction(instruction));
97 }
98
99 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300100 * Tests the encoding of mod ODU signal ID instructions.
101 */
102 @Test
103 public void modOduSignalIdInstructionTest() {
Jian Li68c4fc42016-01-11 16:07:03 -0800104 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 +0300105 L1ModificationInstruction.ModOduSignalIdInstruction instruction =
106 (L1ModificationInstruction.ModOduSignalIdInstruction)
107 Instructions.modL1OduSignalId(oduSignalId);
108 ObjectNode instructionJson =
109 instructionCodec.encode(instruction, context);
110 assertThat(instructionJson, matchesInstruction(instruction));
111 }
112
113 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800114 * Tests the encoding of mod ether instructions.
115 */
116 @Test
117 public void modEtherInstructionTest() {
118 final L2ModificationInstruction.ModEtherInstruction instruction =
119 (L2ModificationInstruction.ModEtherInstruction)
120 Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
121 final ObjectNode instructionJson =
122 instructionCodec.encode(instruction, context);
123 assertThat(instructionJson, matchesInstruction(instruction));
124 }
125
126 /**
127 * Tests the encoding of mod vlan id instructions.
128 */
129 @Test
130 public void modVlanIdInstructionTest() {
131 final L2ModificationInstruction.ModVlanIdInstruction instruction =
132 (L2ModificationInstruction.ModVlanIdInstruction)
133 Instructions.modVlanId(VlanId.vlanId((short) 12));
134
135 final ObjectNode instructionJson =
136 instructionCodec.encode(instruction, context);
137 assertThat(instructionJson, matchesInstruction(instruction));
138 }
139
140 /**
141 * Tests the encoding of mod vlan pcp instructions.
142 */
143 @Test
144 public void modVlanPcpInstructionTest() {
145 final L2ModificationInstruction.ModVlanPcpInstruction instruction =
146 (L2ModificationInstruction.ModVlanPcpInstruction)
147 Instructions.modVlanPcp((byte) 9);
148 final ObjectNode instructionJson =
149 instructionCodec.encode(instruction, context);
150 assertThat(instructionJson, matchesInstruction(instruction));
151 }
152
153 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800154 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800155 */
156 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800157 public void modIPSrcInstructionTest() {
158 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800159 final L3ModificationInstruction.ModIPInstruction instruction =
160 (L3ModificationInstruction.ModIPInstruction)
161 Instructions.modL3Src(ip);
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 dst instructions.
169 */
170 @Test
171 public void modIPDstInstructionTest() {
172 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
173 final L3ModificationInstruction.ModIPInstruction instruction =
174 (L3ModificationInstruction.ModIPInstruction)
175 Instructions.modL3Dst(ip);
176 final ObjectNode instructionJson =
177 instructionCodec.encode(instruction, context);
178 assertThat(instructionJson, matchesInstruction(instruction));
179 }
180
181 /**
182 * Tests the encoding of mod IPv6 src instructions.
183 */
184 @Test
185 public void modIPv6SrcInstructionTest() {
186 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
187 final L3ModificationInstruction.ModIPInstruction instruction =
188 (L3ModificationInstruction.ModIPInstruction)
189 Instructions.modL3IPv6Src(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 dst instructions.
197 */
198 @Test
199 public void modIPv6DstInstructionTest() {
200 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
201 final L3ModificationInstruction.ModIPInstruction instruction =
202 (L3ModificationInstruction.ModIPInstruction)
203 Instructions.modL3IPv6Dst(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 flow label instructions.
211 */
212 @Test
213 public void modIPv6FlowLabelInstructionTest() {
214 final int flowLabel = 0xfffff;
215 final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction =
216 (L3ModificationInstruction.ModIPv6FlowLabelInstruction)
217 Instructions.modL3IPv6FlowLabel(flowLabel);
218 final ObjectNode instructionJson =
219 instructionCodec.encode(instruction, context);
220 assertThat(instructionJson, matchesInstruction(instruction));
221 }
222
223 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800224 * Tests the encoding of mod MPLS label instructions.
225 */
226 @Test
227 public void modMplsLabelInstructionTest() {
228 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
229 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100230 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800231 final ObjectNode instructionJson =
232 instructionCodec.encode(instruction, context);
233 assertThat(instructionJson, matchesInstruction(instruction));
234 }
235
236}