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