blob: 3d1ed2ff9c8475516eb3abbad9958889726c1f98 [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;
Jian Lidab72562016-04-12 14:10:32 -070023import org.onosproject.codec.ExtensionTreatmentCodec;
24import 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
38import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070039
Ray Milkeyd43fe452015-05-29 09:35:12 -070040/**
41 * JSON encoding of Instructions.
42 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070043public final class EncodeInstructionCodecHelper {
Jian Lidab72562016-04-12 14:10:32 -070044 protected static final Logger log = getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070045 private final Instruction instruction;
46 private final CodecContext context;
47
48 /**
49 * Creates an instruction object encoder.
50 *
51 * @param instruction instruction to encode
Jian Li47b26232016-03-07 09:59:59 -080052 * @param context codec context for the encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070053 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070054 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070055 this.instruction = instruction;
56 this.context = context;
57 }
58
59
60 /**
61 * Encode an L0 modification instruction.
62 *
63 * @param result json node that the instruction attributes are added to
64 */
65 private void encodeL0(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080066 L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction;
67 result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -070068
Jian Lie0991662016-03-07 11:22:25 -080069 switch (l0Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070070 case OCH:
71 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
Jian Lie0991662016-03-07 11:22:25 -080072 (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070073 OchSignal ochSignal = ochSignalInstruction.lambda();
74 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
75 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
76 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
77 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
78 break;
79
80 default:
Jian Lie0991662016-03-07 11:22:25 -080081 log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -070082 }
83 }
84
85 /**
Yafit Hadar5796d972015-10-15 13:16:11 +030086 * Encode an L1 modification instruction.
87 *
88 * @param result json node that the instruction attributes are added to
Yafit Hadar5796d972015-10-15 13:16:11 +030089 */
90 private void encodeL1(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080091 L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction;
92 result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name());
Yafit Hadar5796d972015-10-15 13:16:11 +030093
Jian Lie0991662016-03-07 11:22:25 -080094 switch (l1Instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -080095 case ODU_SIGID:
96 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -080097 (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction;
Jian Li47b26232016-03-07 09:59:59 -080098 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
Yafit Hadar5796d972015-10-15 13:16:11 +030099
Jian Li47b26232016-03-07 09:59:59 -0800100 ObjectNode child = result.putObject("oduSignalId");
Yafit Hadar5796d972015-10-15 13:16:11 +0300101
Jian Li47b26232016-03-07 09:59:59 -0800102 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
103 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
104 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP,
105 HexString.toHexString(oduSignalId.tributarySlotBitmap()));
106 break;
107 default:
Jian Lie0991662016-03-07 11:22:25 -0800108 log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype());
Jian Li47b26232016-03-07 09:59:59 -0800109 break;
Yafit Hadar5796d972015-10-15 13:16:11 +0300110 }
111 }
112
113 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700114 * Encode an L2 modification instruction.
115 *
116 * @param result json node that the instruction attributes are added to
117 */
118 private void encodeL2(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800119 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
120 result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700121
Jian Lie0991662016-03-07 11:22:25 -0800122 switch (l2Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700123 case ETH_SRC:
124 case ETH_DST:
125 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800126 (L2ModificationInstruction.ModEtherInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
128 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700129 case VLAN_ID:
130 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800131 (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700132 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
133 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700134 case VLAN_PCP:
135 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800136 (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700137 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
138 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700139 case MPLS_LABEL:
140 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800141 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800142 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700144 case MPLS_PUSH:
145 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
Jian Lie0991662016-03-07 11:22:25 -0800146 (L2ModificationInstruction.PushHeaderInstructions) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700147
alshabib7b808c52015-06-26 14:22:24 -0700148 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800149 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700150 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700151 case TUNNEL_ID:
152 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800153 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700154 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
155 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700156 case MPLS_BOS:
157 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
158 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
159 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
160 case MPLS_POP:
161 case DEC_MPLS_TTL:
162 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700163 default:
Jian Lie0991662016-03-07 11:22:25 -0800164 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700165 break;
166 }
167 }
168
169 /**
170 * Encode an L3 modification instruction.
171 *
172 * @param result json node that the instruction attributes are added to
173 */
174 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800175 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
176 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
177 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700178 case IPV4_SRC:
179 case IPV4_DST:
180 case IPV6_SRC:
181 case IPV6_DST:
182 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800183 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700184 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
185 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700186 case IPV6_FLABEL:
187 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
188 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800189 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700190 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
191 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700192 case TTL_IN:
193 case TTL_OUT:
194 case DEC_TTL:
195 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700196 default:
Jian Lie0991662016-03-07 11:22:25 -0800197 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700198 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) {
Jian Lie0991662016-03-07 11:22:25 -0800208 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
209 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
210 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700211 case TCP_DST:
212 case TCP_SRC:
213 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800214 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700215 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
216 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700217 case UDP_DST:
218 case UDP_SRC:
219 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800220 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700221 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
222 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700223 default:
Jian Lie0991662016-03-07 11:22:25 -0800224 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700225 break;
226 }
227 }
228
Jian Lidab72562016-04-12 14:10:32 -0700229
Hyunsun Moonfab29502015-08-25 13:39:16 -0700230 /**
Jian Lidab72562016-04-12 14:10:32 -0700231 * Encodes a extension instruction.
Charles Chanfbaabae2016-03-18 10:44:44 -0700232 *
233 * @param result json node that the instruction attributes are added to
234 */
235 private void encodeExtension(ObjectNode result) {
Jian Lidab72562016-04-12 14:10:32 -0700236 final Instructions.ExtensionInstructionWrapper extensionInstruction =
237 (Instructions.ExtensionInstructionWrapper) instruction;
238
239 DeviceId deviceId = extensionInstruction.deviceId();
240
241 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
242 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
243 Device device = deviceService.getDevice(deviceId);
244
245 if (device.is(ExtensionTreatmentCodec.class)) {
246 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
247 ObjectNode node = treatmentCodec.encode(extensionInstruction.extensionInstruction(), context);
248 result.set(InstructionCodec.EXTENSION, node);
249 } else {
250 log.warn("There is no codec to encode extension for device {}", deviceId.toString());
251 }
Charles Chanfbaabae2016-03-18 10:44:44 -0700252 }
253
254 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700255 * Encodes the given instruction into JSON.
256 *
257 * @return JSON object node representing the instruction
258 */
259 public ObjectNode encode() {
260 final ObjectNode result = context.mapper().createObjectNode()
261 .put(InstructionCodec.TYPE, instruction.type().toString());
262
263 switch (instruction.type()) {
264 case OUTPUT:
265 final Instructions.OutputInstruction outputInstruction =
266 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800267 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700268 break;
269
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700270 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700271 break;
272
Jian Lice8c5602016-03-03 21:43:24 -0800273 case GROUP:
274 final Instructions.GroupInstruction groupInstruction =
275 (Instructions.GroupInstruction) instruction;
276 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
277 break;
278
Jian Li47b26232016-03-07 09:59:59 -0800279 case METER:
280 final Instructions.MeterInstruction meterInstruction =
281 (Instructions.MeterInstruction) instruction;
282 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
283 break;
284
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -0500285 case TABLE:
286 final Instructions.TableTypeTransition tableTransitionInstruction =
287 (Instructions.TableTypeTransition) instruction;
288 result.put(InstructionCodec.TABLE_ID, tableTransitionInstruction.tableId().toString());
289 break;
290
Jian Li70dffe42016-03-08 22:23:02 -0800291 case QUEUE:
292 final Instructions.SetQueueInstruction setQueueInstruction =
293 (Instructions.SetQueueInstruction) instruction;
294 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
295 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
296 break;
297
Ray Milkeyd43fe452015-05-29 09:35:12 -0700298 case L0MODIFICATION:
299 encodeL0(result);
300 break;
301
Yafit Hadar5796d972015-10-15 13:16:11 +0300302 case L1MODIFICATION:
303 encodeL1(result);
304 break;
305
Ray Milkeyd43fe452015-05-29 09:35:12 -0700306 case L2MODIFICATION:
307 encodeL2(result);
308 break;
309
310 case L3MODIFICATION:
311 encodeL3(result);
312 break;
313
Hyunsun Moonfab29502015-08-25 13:39:16 -0700314 case L4MODIFICATION:
315 encodeL4(result);
316 break;
317
Charles Chanfbaabae2016-03-18 10:44:44 -0700318 case EXTENSION:
319 encodeExtension(result);
320 break;
321
Ray Milkeyd43fe452015-05-29 09:35:12 -0700322 default:
323 log.info("Cannot convert instruction type of {}", instruction.type());
324 break;
325 }
326 return result;
327 }
328
329}