blob: 8f5fc74e2be530d48a574bc38173c468a3ba3787 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 {
Jian Lidab72562016-04-12 14:10:32 -070045 protected 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;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700145 case MPLS_LABEL:
146 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800147 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800148 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700149 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700150 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700151 final L2ModificationInstruction.ModMplsHeaderInstruction pushHeaderInstructions =
152 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
alshabib7b808c52015-06-26 14:22:24 -0700153 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800154 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700155 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700156 case TUNNEL_ID:
157 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800158 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700159 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
160 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700161 case MPLS_BOS:
162 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
163 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
164 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
165 case MPLS_POP:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700166 final L2ModificationInstruction.ModMplsHeaderInstruction popHeaderInstruction =
167 (L2ModificationInstruction.ModMplsHeaderInstruction) l2Instruction;
168 result.put(InstructionCodec.ETHERNET_TYPE,
169 toHexWithPrefix(popHeaderInstruction.ethernetType().toShort()));
Charles Chanfbaabae2016-03-18 10:44:44 -0700170 case DEC_MPLS_TTL:
171 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700172 default:
Jian Lie0991662016-03-07 11:22:25 -0800173 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700174 break;
175 }
176 }
177
178 /**
179 * Encode an L3 modification instruction.
180 *
181 * @param result json node that the instruction attributes are added to
182 */
183 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800184 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
185 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
186 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700187 case IPV4_SRC:
188 case IPV4_DST:
189 case IPV6_SRC:
190 case IPV6_DST:
191 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800192 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700193 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
194 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700195 case IPV6_FLABEL:
196 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
197 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800198 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700199 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
200 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700201 case TTL_IN:
202 case TTL_OUT:
203 case DEC_TTL:
Jonathan Hartcc962d82016-08-09 16:52:22 -0700204 // These instructions have no values to be encoded
Charles Chanfbaabae2016-03-18 10:44:44 -0700205 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700206 default:
Jian Lie0991662016-03-07 11:22:25 -0800207 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700208 break;
209 }
210 }
211
212 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700213 * Encode a L4 modification instruction.
214 *
215 * @param result json node that the instruction attributes are added to
216 */
217 private void encodeL4(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800218 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
219 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
220 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700221 case TCP_DST:
222 case TCP_SRC:
223 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800224 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700225 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
226 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700227 case UDP_DST:
228 case UDP_SRC:
229 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800230 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700231 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
232 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700233 default:
Jian Lie0991662016-03-07 11:22:25 -0800234 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700235 break;
236 }
237 }
238
Jian Lidab72562016-04-12 14:10:32 -0700239
Hyunsun Moonfab29502015-08-25 13:39:16 -0700240 /**
Jian Lidab72562016-04-12 14:10:32 -0700241 * Encodes a extension instruction.
Charles Chanfbaabae2016-03-18 10:44:44 -0700242 *
243 * @param result json node that the instruction attributes are added to
244 */
245 private void encodeExtension(ObjectNode result) {
Jian Lidab72562016-04-12 14:10:32 -0700246 final Instructions.ExtensionInstructionWrapper extensionInstruction =
247 (Instructions.ExtensionInstructionWrapper) instruction;
248
249 DeviceId deviceId = extensionInstruction.deviceId();
250
251 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
252 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
253 Device device = deviceService.getDevice(deviceId);
254
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700255 if (device == null) {
256 throw new IllegalArgumentException("Device not found");
257 }
258
Jian Lidab72562016-04-12 14:10:32 -0700259 if (device.is(ExtensionTreatmentCodec.class)) {
260 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
261 ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
262 result.set(InstructionCodec.EXTENSION, node);
263 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700264 throw new IllegalArgumentException(
265 "There is no codec to encode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700266 }
Charles Chanfbaabae2016-03-18 10:44:44 -0700267 }
268
269 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700270 * Encodes the given instruction into JSON.
271 *
272 * @return JSON object node representing the instruction
273 */
274 public ObjectNode encode() {
275 final ObjectNode result = context.mapper().createObjectNode()
276 .put(InstructionCodec.TYPE, instruction.type().toString());
277
278 switch (instruction.type()) {
279 case OUTPUT:
280 final Instructions.OutputInstruction outputInstruction =
281 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800282 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700283 break;
284
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700285 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700286 break;
287
Jian Lice8c5602016-03-03 21:43:24 -0800288 case GROUP:
289 final Instructions.GroupInstruction groupInstruction =
290 (Instructions.GroupInstruction) instruction;
291 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
292 break;
293
Jian Li47b26232016-03-07 09:59:59 -0800294 case METER:
295 final Instructions.MeterInstruction meterInstruction =
296 (Instructions.MeterInstruction) instruction;
297 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
298 break;
299
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -0500300 case TABLE:
301 final Instructions.TableTypeTransition tableTransitionInstruction =
302 (Instructions.TableTypeTransition) instruction;
303 result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString());
304 break;
305
Jian Li70dffe42016-03-08 22:23:02 -0800306 case QUEUE:
307 final Instructions.SetQueueInstruction setQueueInstruction =
308 (Instructions.SetQueueInstruction) instruction;
309 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
ke han215f2452017-04-28 13:36:05 +0800310 if (setQueueInstruction.port() != null) {
311 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
312 }
Jian Li70dffe42016-03-08 22:23:02 -0800313 break;
314
Ray Milkeyd43fe452015-05-29 09:35:12 -0700315 case L0MODIFICATION:
316 encodeL0(result);
317 break;
318
Yafit Hadar5796d972015-10-15 13:16:11 +0300319 case L1MODIFICATION:
320 encodeL1(result);
321 break;
322
Ray Milkeyd43fe452015-05-29 09:35:12 -0700323 case L2MODIFICATION:
324 encodeL2(result);
325 break;
326
327 case L3MODIFICATION:
328 encodeL3(result);
329 break;
330
Hyunsun Moonfab29502015-08-25 13:39:16 -0700331 case L4MODIFICATION:
332 encodeL4(result);
333 break;
334
Charles Chanfbaabae2016-03-18 10:44:44 -0700335 case EXTENSION:
336 encodeExtension(result);
337 break;
338
Ray Milkeyd43fe452015-05-29 09:35:12 -0700339 default:
340 log.info("Cannot convert instruction type of {}", instruction.type());
341 break;
342 }
343 return result;
344 }
345
346}