blob: 268d110cf02c38a0fb2d086d1da95dfe16a0ba3f [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
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
Yafit Hadar5796d972015-10-15 13:16:11 +030018import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070019import org.onosproject.codec.CodecContext;
20import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030021import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070022import org.onosproject.net.flow.instructions.Instruction;
23import org.onosproject.net.flow.instructions.Instructions;
24import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030025import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070026import org.onosproject.net.flow.instructions.L2ModificationInstruction;
27import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070028import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
31
32import com.fasterxml.jackson.databind.node.ObjectNode;
33
34/**
35 * JSON encoding of Instructions.
36 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070037public final class EncodeInstructionCodecHelper {
38 protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070039 private final Instruction instruction;
40 private final CodecContext context;
41
42 /**
43 * Creates an instruction object encoder.
44 *
45 * @param instruction instruction to encode
46 * @param context codec context for the encoding
47 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070048 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070049 this.instruction = instruction;
50 this.context = context;
51 }
52
53
54 /**
55 * Encode an L0 modification instruction.
56 *
57 * @param result json node that the instruction attributes are added to
58 */
59 private void encodeL0(ObjectNode result) {
60 L0ModificationInstruction instruction =
61 (L0ModificationInstruction) this.instruction;
62 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
63
64 switch (instruction.subtype()) {
65 case LAMBDA:
66 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
67 (L0ModificationInstruction.ModLambdaInstruction) instruction;
68 result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda());
69 break;
70
71 case OCH:
72 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
73 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
74 OchSignal ochSignal = ochSignalInstruction.lambda();
75 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
76 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
77 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
78 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
79 break;
80
81 default:
82 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
83 }
84 }
85
86 /**
Yafit Hadar5796d972015-10-15 13:16:11 +030087 * Encode an L1 modification instruction.
88 *
89 * @param result json node that the instruction attributes are added to
Yafit Hadar5796d972015-10-15 13:16:11 +030090 */
91 private void encodeL1(ObjectNode result) {
92 L1ModificationInstruction instruction =
93 (L1ModificationInstruction) this.instruction;
94 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
95
96 switch (instruction.subtype()) {
97 case ODU_SIGID:
98 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
99 (L1ModificationInstruction.ModOduSignalIdInstruction) instruction;
100 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
101
102 ObjectNode child = result.putObject("oduSignalId");
103
104 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
105 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
106 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
107 break;
108 default:
109 log.info("Cannot convert L1 subtype of {}", instruction.subtype());
110 break;
111 }
112 }
113
114 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700115 * Encode an L2 modification instruction.
116 *
117 * @param result json node that the instruction attributes are added to
118 */
119 private void encodeL2(ObjectNode result) {
120 L2ModificationInstruction instruction =
121 (L2ModificationInstruction) this.instruction;
122 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
123
124 switch (instruction.subtype()) {
125 case ETH_SRC:
126 case ETH_DST:
127 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
128 (L2ModificationInstruction.ModEtherInstruction) instruction;
129 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
130 break;
131
132 case VLAN_ID:
133 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
134 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
135 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
136 break;
137
138 case VLAN_PCP:
139 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
140 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
141 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
142 break;
143
144 case MPLS_LABEL:
145 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
146 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
HIGUCHI Yuta04b49fc2015-08-28 09:58:58 -0700147 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.mplsLabel().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700148 break;
149
150 case MPLS_PUSH:
151 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
152 (L2ModificationInstruction.PushHeaderInstructions) instruction;
153
alshabib7b808c52015-06-26 14:22:24 -0700154 result.put(InstructionCodec.ETHERNET_TYPE,
155 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700156 break;
157
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700158 case TUNNEL_ID:
159 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
160 (L2ModificationInstruction.ModTunnelIdInstruction) instruction;
161 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
162 break;
163
Ray Milkeyd43fe452015-05-29 09:35:12 -0700164 default:
165 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
166 break;
167 }
168 }
169
170 /**
171 * Encode an L3 modification instruction.
172 *
173 * @param result json node that the instruction attributes are added to
174 */
175 private void encodeL3(ObjectNode result) {
176 L3ModificationInstruction instruction =
177 (L3ModificationInstruction) this.instruction;
178 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
179 switch (instruction.subtype()) {
180 case IPV4_SRC:
181 case IPV4_DST:
182 case IPV6_SRC:
183 case IPV6_DST:
184 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
185 (L3ModificationInstruction.ModIPInstruction) instruction;
186 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
187 break;
188
189 case IPV6_FLABEL:
190 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
191 modFlowLabelInstruction =
192 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
193 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
194 break;
195
196 default:
197 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
198 break;
199 }
200 }
201
202 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700203 * Encode a L4 modification instruction.
204 *
205 * @param result json node that the instruction attributes are added to
206 */
207 private void encodeL4(ObjectNode result) {
208 L4ModificationInstruction instruction =
209 (L4ModificationInstruction) this.instruction;
210 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
211 switch (instruction.subtype()) {
212 case TCP_DST:
213 case TCP_SRC:
214 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
215 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
216 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
217 break;
218
219 case UDP_DST:
220 case UDP_SRC:
221 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
222 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
223 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
224 break;
225
226 default:
227 log.info("Cannot convert L4 subtype of {}", instruction.subtype());
228 break;
229 }
230 }
231
232 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700233 * Encodes the given instruction into JSON.
234 *
235 * @return JSON object node representing the instruction
236 */
237 public ObjectNode encode() {
238 final ObjectNode result = context.mapper().createObjectNode()
239 .put(InstructionCodec.TYPE, instruction.type().toString());
240
241 switch (instruction.type()) {
242 case OUTPUT:
243 final Instructions.OutputInstruction outputInstruction =
244 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800245 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700246 break;
247
248 case DROP:
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700249 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700250 break;
251
252 case L0MODIFICATION:
253 encodeL0(result);
254 break;
255
Yafit Hadar5796d972015-10-15 13:16:11 +0300256 case L1MODIFICATION:
257 encodeL1(result);
258 break;
259
Ray Milkeyd43fe452015-05-29 09:35:12 -0700260 case L2MODIFICATION:
261 encodeL2(result);
262 break;
263
264 case L3MODIFICATION:
265 encodeL3(result);
266 break;
267
Hyunsun Moonfab29502015-08-25 13:39:16 -0700268 case L4MODIFICATION:
269 encodeL4(result);
270 break;
271
Ray Milkeyd43fe452015-05-29 09:35:12 -0700272 default:
273 log.info("Cannot convert instruction type of {}", instruction.type());
274 break;
275 }
276 return result;
277 }
278
279}