blob: 78685c1940dff21a626f12a7786de0da3ecfd185 [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;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.instructions.Instruction;
29import org.onosproject.net.flow.instructions.Instructions;
30import org.onosproject.net.flow.instructions.L0ModificationInstruction;
31import org.onosproject.net.flow.instructions.L2ModificationInstruction;
32import org.onosproject.net.flow.instructions.L3ModificationInstruction;
33
34import com.fasterxml.jackson.databind.node.ObjectNode;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.notNullValue;
38import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction;
39
40/**
41 * Unit tests for Instruction codec.
42 */
43public class InstructionCodecTest {
44 CodecContext context;
45 JsonCodec<Instruction> instructionCodec;
46
47 /**
Ray Milkeydb358082015-01-13 16:34:38 -080048 * Sets up for each test. Creates a context and fetches the instruction
Ray Milkeyd03eda02015-01-09 14:58:48 -080049 * codec.
50 */
51 @Before
52 public void setUp() {
53 context = new MockCodecContext();
54 instructionCodec = context.codec(Instruction.class);
55 assertThat(instructionCodec, notNullValue());
56 }
57
58 /**
59 * Tests the encoding of push header instructions.
60 */
61 @Test
62 public void pushHeaderInstructionsTest() {
63 final L2ModificationInstruction.PushHeaderInstructions instruction =
64 (L2ModificationInstruction.PushHeaderInstructions) Instructions.pushMpls();
65 final ObjectNode instructionJson = instructionCodec.encode(instruction, context);
66
67 assertThat(instructionJson, matchesInstruction(instruction));
68 }
69
70 /**
71 * Tests the encoding of drop instructions.
72 */
73 @Test
74 public void dropInstructionTest() {
75 final Instructions.DropInstruction instruction =
76 new Instructions.DropInstruction();
77 final ObjectNode instructionJson =
78 instructionCodec.encode(instruction, context);
79 assertThat(instructionJson, matchesInstruction(instruction));
80 }
81
82 /**
83 * Tests the encoding of output instructions.
84 */
85 @Test
86 public void outputInstructionTest() {
87 final Instructions.OutputInstruction instruction =
88 Instructions.createOutput(PortNumber.portNumber(22));
89 final ObjectNode instructionJson =
90 instructionCodec.encode(instruction, context);
91 assertThat(instructionJson, matchesInstruction(instruction));
92 }
93
94 /**
95 * Tests the encoding of mod lambda instructions.
96 */
97 @Test
98 public void modLambdaInstructionTest() {
99 final L0ModificationInstruction.ModLambdaInstruction instruction =
100 (L0ModificationInstruction.ModLambdaInstruction)
101 Instructions.modL0Lambda((short) 55);
102 final ObjectNode instructionJson =
103 instructionCodec.encode(instruction, context);
104 assertThat(instructionJson, matchesInstruction(instruction));
105 }
106
107 /**
108 * Tests the encoding of mod ether instructions.
109 */
110 @Test
111 public void modEtherInstructionTest() {
112 final L2ModificationInstruction.ModEtherInstruction instruction =
113 (L2ModificationInstruction.ModEtherInstruction)
114 Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66"));
115 final ObjectNode instructionJson =
116 instructionCodec.encode(instruction, context);
117 assertThat(instructionJson, matchesInstruction(instruction));
118 }
119
120 /**
121 * Tests the encoding of mod vlan id instructions.
122 */
123 @Test
124 public void modVlanIdInstructionTest() {
125 final L2ModificationInstruction.ModVlanIdInstruction instruction =
126 (L2ModificationInstruction.ModVlanIdInstruction)
127 Instructions.modVlanId(VlanId.vlanId((short) 12));
128
129 final ObjectNode instructionJson =
130 instructionCodec.encode(instruction, context);
131 assertThat(instructionJson, matchesInstruction(instruction));
132 }
133
134 /**
135 * Tests the encoding of mod vlan pcp instructions.
136 */
137 @Test
138 public void modVlanPcpInstructionTest() {
139 final L2ModificationInstruction.ModVlanPcpInstruction instruction =
140 (L2ModificationInstruction.ModVlanPcpInstruction)
141 Instructions.modVlanPcp((byte) 9);
142 final ObjectNode instructionJson =
143 instructionCodec.encode(instruction, context);
144 assertThat(instructionJson, matchesInstruction(instruction));
145 }
146
147 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800148 * Tests the encoding of mod IPv4 src instructions.
Ray Milkeyd03eda02015-01-09 14:58:48 -0800149 */
150 @Test
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800151 public void modIPSrcInstructionTest() {
152 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
Ray Milkeyd03eda02015-01-09 14:58:48 -0800153 final L3ModificationInstruction.ModIPInstruction instruction =
154 (L3ModificationInstruction.ModIPInstruction)
155 Instructions.modL3Src(ip);
156 final ObjectNode instructionJson =
157 instructionCodec.encode(instruction, context);
158 assertThat(instructionJson, matchesInstruction(instruction));
159 }
160
161 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800162 * Tests the encoding of mod IPv4 dst instructions.
163 */
164 @Test
165 public void modIPDstInstructionTest() {
166 final Ip4Address ip = Ip4Address.valueOf("1.2.3.4");
167 final L3ModificationInstruction.ModIPInstruction instruction =
168 (L3ModificationInstruction.ModIPInstruction)
169 Instructions.modL3Dst(ip);
170 final ObjectNode instructionJson =
171 instructionCodec.encode(instruction, context);
172 assertThat(instructionJson, matchesInstruction(instruction));
173 }
174
175 /**
176 * Tests the encoding of mod IPv6 src instructions.
177 */
178 @Test
179 public void modIPv6SrcInstructionTest() {
180 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
181 final L3ModificationInstruction.ModIPInstruction instruction =
182 (L3ModificationInstruction.ModIPInstruction)
183 Instructions.modL3IPv6Src(ip);
184 final ObjectNode instructionJson =
185 instructionCodec.encode(instruction, context);
186 assertThat(instructionJson, matchesInstruction(instruction));
187 }
188
189 /**
190 * Tests the encoding of mod IPv6 dst instructions.
191 */
192 @Test
193 public void modIPv6DstInstructionTest() {
194 final Ip6Address ip = Ip6Address.valueOf("1111::2222");
195 final L3ModificationInstruction.ModIPInstruction instruction =
196 (L3ModificationInstruction.ModIPInstruction)
197 Instructions.modL3IPv6Dst(ip);
198 final ObjectNode instructionJson =
199 instructionCodec.encode(instruction, context);
200 assertThat(instructionJson, matchesInstruction(instruction));
201 }
202
203 /**
204 * Tests the encoding of mod IPv6 flow label instructions.
205 */
206 @Test
207 public void modIPv6FlowLabelInstructionTest() {
208 final int flowLabel = 0xfffff;
209 final L3ModificationInstruction.ModIPv6FlowLabelInstruction instruction =
210 (L3ModificationInstruction.ModIPv6FlowLabelInstruction)
211 Instructions.modL3IPv6FlowLabel(flowLabel);
212 final ObjectNode instructionJson =
213 instructionCodec.encode(instruction, context);
214 assertThat(instructionJson, matchesInstruction(instruction));
215 }
216
217 /**
Ray Milkeyd03eda02015-01-09 14:58:48 -0800218 * Tests the encoding of mod MPLS label instructions.
219 */
220 @Test
221 public void modMplsLabelInstructionTest() {
222 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
223 (L2ModificationInstruction.ModMplsLabelInstruction)
Michele Santuari4b6019e2014-12-19 11:31:45 +0100224 Instructions.modMplsLabel(MplsLabel.mplsLabel(99));
Ray Milkeyd03eda02015-01-09 14:58:48 -0800225 final ObjectNode instructionJson =
226 instructionCodec.encode(instruction, context);
227 assertThat(instructionJson, matchesInstruction(instruction));
228 }
229
230}