blob: 8bf44395b0c54492e18c55c98e1ce6eaefecdad6 [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;
20import org.onlab.packet.IpAddress;
21import org.onlab.packet.IpPrefix;
22import 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 /**
147 * Tests the encoding of mod ip instructions.
148 */
149 @Test
150 public void modIPInstructionTest() {
151 final IpAddress ip = IpPrefix.valueOf("1.2.3.4/24").address();
152 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 /**
161 * Tests the encoding of mod MPLS label instructions.
162 */
163 @Test
164 public void modMplsLabelInstructionTest() {
165 final L2ModificationInstruction.ModMplsLabelInstruction instruction =
166 (L2ModificationInstruction.ModMplsLabelInstruction)
167 Instructions.modMplsLabel(99);
168 final ObjectNode instructionJson =
169 instructionCodec.encode(instruction, context);
170 assertThat(instructionJson, matchesInstruction(instruction));
171 }
172
173}