blob: 65a6665c96627443657b36cb19630e467bf4825c [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyd43fe452015-05-29 09:35:12 -07003 *
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
Jian Lice8c5602016-03-03 21:43:24 -080018import com.fasterxml.jackson.databind.node.ObjectNode;
Jian Lidab72562016-04-12 14:10:32 -070019import org.onlab.osgi.DefaultServiceDirectory;
20import org.onlab.osgi.ServiceDirectory;
Yafit Hadar5796d972015-10-15 13:16:11 +030021import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070022import org.onosproject.codec.CodecContext;
Andrea Campanella2cfe8ef2017-07-13 19:45:15 +020023import org.onosproject.net.flow.ExtensionTreatmentCodec;
Jian Lidab72562016-04-12 14:10:32 -070024import org.onosproject.net.Device;
25import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070026import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030027import org.onosproject.net.OduSignalId;
Jian Lidab72562016-04-12 14:10:32 -070028import org.onosproject.net.device.DeviceService;
Ray Milkeyd43fe452015-05-29 09:35:12 -070029import org.onosproject.net.flow.instructions.Instruction;
30import org.onosproject.net.flow.instructions.Instructions;
31import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030032import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070033import org.onosproject.net.flow.instructions.L2ModificationInstruction;
34import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070035import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070036import org.slf4j.Logger;
Jian Lidab72562016-04-12 14:10:32 -070037
Jonathan Hartcc962d82016-08-09 16:52:22 -070038import static org.onlab.util.Tools.toHexWithPrefix;
Jian Lidab72562016-04-12 14:10:32 -070039import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070040
Ray Milkeyd43fe452015-05-29 09:35:12 -070041/**
42 * JSON encoding of Instructions.
43 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070044public final class EncodeInstructionCodecHelper {
Ray Milkey9c9cde42018-01-12 14:22:06 -080045 private static final Logger log = getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070046 private final Instruction instruction;
47 private final CodecContext context;
48
49 /**
50 * Creates an instruction object encoder.
51 *
52 * @param instruction instruction to encode
Jian Li47b26232016-03-07 09:59:59 -080053 * @param context codec context for the encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070054 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070055 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070056 this.instruction = instruction;
57 this.context = context;
58 }
59
60
61 /**
62 * Encode an L0 modification instruction.
63 *
64 * @param result json node that the instruction attributes are added to
65 */
66 private void encodeL0(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080067 L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction;
68 result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -070069
Jian Lie0991662016-03-07 11:22:25 -080070 switch (l0Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070071 case OCH:
72 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
Jian Lie0991662016-03-07 11:22:25 -080073 (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070074 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:
Jian Lie0991662016-03-07 11:22:25 -080082 log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -070083 }
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) {
Jian Lie0991662016-03-07 11:22:25 -080092 L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction;
93 result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name());
Yafit Hadar5796d972015-10-15 13:16:11 +030094
Jian Lie0991662016-03-07 11:22:25 -080095 switch (l1Instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -080096 case ODU_SIGID:
97 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -080098 (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction;
Jian Li47b26232016-03-07 09:59:59 -080099 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
Yafit Hadar5796d972015-10-15 13:16:11 +0300100
Jian Li47b26232016-03-07 09:59:59 -0800101 ObjectNode child = result.putObject("oduSignalId");
Yafit Hadar5796d972015-10-15 13:16:11 +0300102
Jian Li47b26232016-03-07 09:59:59 -0800103 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
104 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
105 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP,
106 HexString.toHexString(oduSignalId.tributarySlotBitmap()));
107 break;
108 default:
Jian Lie0991662016-03-07 11:22:25 -0800109 log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype());
Jian Li47b26232016-03-07 09:59:59 -0800110 break;
Yafit Hadar5796d972015-10-15 13:16:11 +0300111 }
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) {
Jian Lie0991662016-03-07 11:22:25 -0800120 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
121 result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700122
Jian Lie0991662016-03-07 11:22:25 -0800123 switch (l2Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700124 case ETH_SRC:
125 case ETH_DST:
126 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800127 (L2ModificationInstruction.ModEtherInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
129 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700130 case VLAN_ID:
131 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800132 (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700133 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
134 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700135 case VLAN_PCP:
136 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800137 (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700138 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
139 break;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500140 case VLAN_PUSH:
141 final L2ModificationInstruction.ModVlanHeaderInstruction pushVlanInstruction =
142 (L2ModificationInstruction.ModVlanHeaderInstruction) l2Instruction;
143 result.put(InstructionCodec.ETHERNET_TYPE, pushVlanInstruction.ethernetType().toString());
144 break;
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700145 case VLAN_POP:
146 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700147 case MPLS_LABEL:
148 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800149 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800150 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700151 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700152 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700153 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstructions =
154 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
alshabib7b808c52015-06-26 14:22:24 -0700155 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800156 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700157 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700158 case TUNNEL_ID:
159 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800160 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700161 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
162 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700163 case MPLS_BOS:
164 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
165 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
166 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700167 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700168 case MPLS_POP:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700169 final L2ModificationInstruction.ModMplsHeaderInstruction popHeaderInstruction =
170 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
171 result.put(InstructionCodec.ETHERNET_TYPE,
172 toHexWithPrefix(popHeaderInstruction.ethernetType().toShort()));
Jonghwan Hyun360b02f2017-08-11 14:00:07 -0700173 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700174 case DEC_MPLS_TTL:
175 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700176 default:
Jian Lie0991662016-03-07 11:22:25 -0800177 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700178 break;
179 }
180 }
181
182 /**
183 * Encode an L3 modification instruction.
184 *
185 * @param result json node that the instruction attributes are added to
186 */
187 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800188 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
189 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
190 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700191 case IPV4_SRC:
192 case IPV4_DST:
193 case IPV6_SRC:
194 case IPV6_DST:
195 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800196 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700197 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
198 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700199 case IPV6_FLABEL:
200 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
201 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800202 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700203 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
204 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700205 case TTL_IN:
206 case TTL_OUT:
207 case DEC_TTL:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700208 // These instructions have no values to be encoded
Charles Chanfbaabae2016-03-18 10:44:44 -0700209 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700210 default:
Jian Lie0991662016-03-07 11:22:25 -0800211 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700212 break;
213 }
214 }
215
216 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700217 * Encode a L4 modification instruction.
218 *
219 * @param result json node that the instruction attributes are added to
220 */
221 private void encodeL4(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800222 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
223 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
224 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700225 case TCP_DST:
226 case TCP_SRC:
227 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800228 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700229 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
230 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700231 case UDP_DST:
232 case UDP_SRC:
233 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800234 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700235 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
236 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700237 default:
Jian Lie0991662016-03-07 11:22:25 -0800238 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700239 break;
240 }
241 }
242
Jian Lidab72562016-04-12 14:10:32 -0700243
Hyunsun Moonfab29502015-08-25 13:39:16 -0700244 /**
Jian Lidab72562016-04-12 14:10:32 -0700245 * Encodes a extension instruction.
Charles Chanfbaabae2016-03-18 10:44:44 -0700246 *
247 * @param result json node that the instruction attributes are added to
248 */
249 private void encodeExtension(ObjectNode result) {
Jian Lidab72562016-04-12 14:10:32 -0700250 final Instructions.ExtensionInstructionWrapper extensionInstruction =
251 (Instructions.ExtensionInstructionWrapper) instruction;
252
253 DeviceId deviceId = extensionInstruction.deviceId();
254
255 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
256 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
257 Device device = deviceService.getDevice(deviceId);
258
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700259 if (device == null) {
260 throw new IllegalArgumentException("Device not found");
261 }
262
Jian Lidab72562016-04-12 14:10:32 -0700263 if (device.is(ExtensionTreatmentCodec.class)) {
264 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
265 ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
266 result.set(InstructionCodec.EXTENSION, node);
267 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700268 throw new IllegalArgumentException(
269 "There is no codec to encode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700270 }
Charles Chanfbaabae2016-03-18 10:44:44 -0700271 }
272
273 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700274 * Encodes the given instruction into JSON.
275 *
276 * @return JSON object node representing the instruction
277 */
278 public ObjectNode encode() {
279 final ObjectNode result = context.mapper().createObjectNode()
280 .put(InstructionCodec.TYPE, instruction.type().toString());
281
282 switch (instruction.type()) {
283 case OUTPUT:
284 final Instructions.OutputInstruction outputInstruction =
285 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800286 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700287 break;
288
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700289 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700290 break;
291
Jian Lice8c5602016-03-03 21:43:24 -0800292 case GROUP:
293 final Instructions.GroupInstruction groupInstruction =
294 (Instructions.GroupInstruction) instruction;
295 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
296 break;
297
Jian Li47b26232016-03-07 09:59:59 -0800298 case METER:
299 final Instructions.MeterInstruction meterInstruction =
300 (Instructions.MeterInstruction) instruction;
301 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
302 break;
303
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -0500304 case TABLE:
305 final Instructions.TableTypeTransition tableTransitionInstruction =
306 (Instructions.TableTypeTransition) instruction;
307 result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString());
308 break;
309
Jian Li70dffe42016-03-08 22:23:02 -0800310 case QUEUE:
311 final Instructions.SetQueueInstruction setQueueInstruction =
312 (Instructions.SetQueueInstruction) instruction;
313 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
ke han215f2452017-04-28 13:36:05 +0800314 if (setQueueInstruction.port() != null) {
315 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
316 }
Jian Li70dffe42016-03-08 22:23:02 -0800317 break;
318
Ray Milkeyd43fe452015-05-29 09:35:12 -0700319 case L0MODIFICATION:
320 encodeL0(result);
321 break;
322
Yafit Hadar5796d972015-10-15 13:16:11 +0300323 case L1MODIFICATION:
324 encodeL1(result);
325 break;
326
Ray Milkeyd43fe452015-05-29 09:35:12 -0700327 case L2MODIFICATION:
328 encodeL2(result);
329 break;
330
331 case L3MODIFICATION:
332 encodeL3(result);
333 break;
334
Hyunsun Moonfab29502015-08-25 13:39:16 -0700335 case L4MODIFICATION:
336 encodeL4(result);
337 break;
338
Charles Chanfbaabae2016-03-18 10:44:44 -0700339 case EXTENSION:
340 encodeExtension(result);
341 break;
342
Ray Milkeyd43fe452015-05-29 09:35:12 -0700343 default:
344 log.info("Cannot convert instruction type of {}", instruction.type());
345 break;
346 }
347 return result;
348 }
349
350}