blob: eae82513adfef96be4923adf2ccdbd86de484441 [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
Jian Lice8c5602016-03-03 21:43:24 -080018import com.fasterxml.jackson.databind.node.ObjectNode;
Yafit Hadar5796d972015-10-15 13:16:11 +030019import org.onlab.util.HexString;
Ray Milkeyd43fe452015-05-29 09:35:12 -070020import org.onosproject.codec.CodecContext;
21import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030022import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onosproject.net.flow.instructions.Instruction;
24import org.onosproject.net.flow.instructions.Instructions;
25import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030026import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070027import org.onosproject.net.flow.instructions.L2ModificationInstruction;
28import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070029import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Ray Milkeyd43fe452015-05-29 09:35:12 -070033/**
34 * JSON encoding of Instructions.
35 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070036public final class EncodeInstructionCodecHelper {
37 protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070038 private final Instruction instruction;
39 private final CodecContext context;
40
41 /**
42 * Creates an instruction object encoder.
43 *
44 * @param instruction instruction to encode
Jian Li47b26232016-03-07 09:59:59 -080045 * @param context codec context for the encoding
Ray Milkeyd43fe452015-05-29 09:35:12 -070046 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070047 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070048 this.instruction = instruction;
49 this.context = context;
50 }
51
52
53 /**
54 * Encode an L0 modification instruction.
55 *
56 * @param result json node that the instruction attributes are added to
57 */
58 private void encodeL0(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080059 L0ModificationInstruction l0Instruction = (L0ModificationInstruction) instruction;
60 result.put(InstructionCodec.SUBTYPE, l0Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -070061
Jian Lie0991662016-03-07 11:22:25 -080062 switch (l0Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070063 case LAMBDA:
64 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
Jian Lie0991662016-03-07 11:22:25 -080065 (L0ModificationInstruction.ModLambdaInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070066 result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda());
67 break;
68
69 case OCH:
70 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
Jian Lie0991662016-03-07 11:22:25 -080071 (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070072 OchSignal ochSignal = ochSignalInstruction.lambda();
73 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
74 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
75 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
76 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
77 break;
78
79 default:
Jian Lie0991662016-03-07 11:22:25 -080080 log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -070081 }
82 }
83
84 /**
Yafit Hadar5796d972015-10-15 13:16:11 +030085 * Encode an L1 modification instruction.
86 *
87 * @param result json node that the instruction attributes are added to
Yafit Hadar5796d972015-10-15 13:16:11 +030088 */
89 private void encodeL1(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080090 L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction;
91 result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name());
Yafit Hadar5796d972015-10-15 13:16:11 +030092
Jian Lie0991662016-03-07 11:22:25 -080093 switch (l1Instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -080094 case ODU_SIGID:
95 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -080096 (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction;
Jian Li47b26232016-03-07 09:59:59 -080097 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
Yafit Hadar5796d972015-10-15 13:16:11 +030098
Jian Li47b26232016-03-07 09:59:59 -080099 ObjectNode child = result.putObject("oduSignalId");
Yafit Hadar5796d972015-10-15 13:16:11 +0300100
Jian Li47b26232016-03-07 09:59:59 -0800101 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
102 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
103 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP,
104 HexString.toHexString(oduSignalId.tributarySlotBitmap()));
105 break;
106 default:
Jian Lie0991662016-03-07 11:22:25 -0800107 log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype());
Jian Li47b26232016-03-07 09:59:59 -0800108 break;
Yafit Hadar5796d972015-10-15 13:16:11 +0300109 }
110 }
111
112 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700113 * Encode an L2 modification instruction.
114 *
115 * @param result json node that the instruction attributes are added to
116 */
117 private void encodeL2(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800118 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
119 result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120
Jian Lie0991662016-03-07 11:22:25 -0800121 switch (l2Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700122 case ETH_SRC:
123 case ETH_DST:
124 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800125 (L2ModificationInstruction.ModEtherInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700126 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
127 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700128 case VLAN_ID:
129 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800130 (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700131 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
132 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700133 case VLAN_PCP:
134 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800135 (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700136 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
137 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700138 case MPLS_LABEL:
139 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800140 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800141 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700142 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 case MPLS_PUSH:
144 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
Jian Lie0991662016-03-07 11:22:25 -0800145 (L2ModificationInstruction.PushHeaderInstructions) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700146
alshabib7b808c52015-06-26 14:22:24 -0700147 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800148 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700149 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700150 case TUNNEL_ID:
151 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800152 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700153 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
154 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700155 case MPLS_BOS:
156 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
157 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
158 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
159 case MPLS_POP:
160 case DEC_MPLS_TTL:
161 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700162 default:
Jian Lie0991662016-03-07 11:22:25 -0800163 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700164 break;
165 }
166 }
167
168 /**
169 * Encode an L3 modification instruction.
170 *
171 * @param result json node that the instruction attributes are added to
172 */
173 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800174 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
175 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
176 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700177 case IPV4_SRC:
178 case IPV4_DST:
179 case IPV6_SRC:
180 case IPV6_DST:
181 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800182 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700183 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
184 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700185 case IPV6_FLABEL:
186 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
187 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800188 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700189 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
190 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700191 case TTL_IN:
192 case TTL_OUT:
193 case DEC_TTL:
194 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700195 default:
Jian Lie0991662016-03-07 11:22:25 -0800196 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700197 break;
198 }
199 }
200
201 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700202 * Encode a L4 modification instruction.
203 *
204 * @param result json node that the instruction attributes are added to
205 */
206 private void encodeL4(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800207 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
208 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
209 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700210 case TCP_DST:
211 case TCP_SRC:
212 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800213 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700214 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
215 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700216 case UDP_DST:
217 case UDP_SRC:
218 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800219 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700220 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
221 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700222 default:
Jian Lie0991662016-03-07 11:22:25 -0800223 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700224 break;
225 }
226 }
227
228 /**
Charles Chanfbaabae2016-03-18 10:44:44 -0700229 * Encode a extension instruction.
230 *
231 * @param result json node that the instruction attributes are added to
232 */
233 private void encodeExtension(ObjectNode result) {
234 // TODO Support extension in REST API
235 log.info("Cannot convert instruction type of EXTENSION");
236 }
237
238 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700239 * Encodes the given instruction into JSON.
240 *
241 * @return JSON object node representing the instruction
242 */
243 public ObjectNode encode() {
244 final ObjectNode result = context.mapper().createObjectNode()
245 .put(InstructionCodec.TYPE, instruction.type().toString());
246
247 switch (instruction.type()) {
248 case OUTPUT:
249 final Instructions.OutputInstruction outputInstruction =
250 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800251 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700252 break;
253
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700254 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700255 break;
256
Jian Lice8c5602016-03-03 21:43:24 -0800257 case GROUP:
258 final Instructions.GroupInstruction groupInstruction =
259 (Instructions.GroupInstruction) instruction;
260 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
261 break;
262
Jian Li47b26232016-03-07 09:59:59 -0800263 case METER:
264 final Instructions.MeterInstruction meterInstruction =
265 (Instructions.MeterInstruction) instruction;
266 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
267 break;
268
Jian Li70dffe42016-03-08 22:23:02 -0800269 case QUEUE:
270 final Instructions.SetQueueInstruction setQueueInstruction =
271 (Instructions.SetQueueInstruction) instruction;
272 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
273 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
274 break;
275
Ray Milkeyd43fe452015-05-29 09:35:12 -0700276 case L0MODIFICATION:
277 encodeL0(result);
278 break;
279
Yafit Hadar5796d972015-10-15 13:16:11 +0300280 case L1MODIFICATION:
281 encodeL1(result);
282 break;
283
Ray Milkeyd43fe452015-05-29 09:35:12 -0700284 case L2MODIFICATION:
285 encodeL2(result);
286 break;
287
288 case L3MODIFICATION:
289 encodeL3(result);
290 break;
291
Hyunsun Moonfab29502015-08-25 13:39:16 -0700292 case L4MODIFICATION:
293 encodeL4(result);
294 break;
295
Charles Chanfbaabae2016-03-18 10:44:44 -0700296 case EXTENSION:
297 encodeExtension(result);
298 break;
299
Ray Milkeyd43fe452015-05-29 09:35:12 -0700300 default:
301 log.info("Cannot convert instruction type of {}", instruction.type());
302 break;
303 }
304 return result;
305 }
306
307}