blob: 49b179ed715d5a1b9c061f12033d6bae7b26b3fd [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 OCH:
64 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
Jian Lie0991662016-03-07 11:22:25 -080065 (L0ModificationInstruction.ModOchSignalInstruction) l0Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070066 OchSignal ochSignal = ochSignalInstruction.lambda();
67 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
68 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
69 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
70 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
71 break;
72
73 default:
Jian Lie0991662016-03-07 11:22:25 -080074 log.info("Cannot convert L0 subtype of {}", l0Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -070075 }
76 }
77
78 /**
Yafit Hadar5796d972015-10-15 13:16:11 +030079 * Encode an L1 modification instruction.
80 *
81 * @param result json node that the instruction attributes are added to
Yafit Hadar5796d972015-10-15 13:16:11 +030082 */
83 private void encodeL1(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -080084 L1ModificationInstruction l1Instruction = (L1ModificationInstruction) instruction;
85 result.put(InstructionCodec.SUBTYPE, l1Instruction.subtype().name());
Yafit Hadar5796d972015-10-15 13:16:11 +030086
Jian Lie0991662016-03-07 11:22:25 -080087 switch (l1Instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -080088 case ODU_SIGID:
89 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -080090 (L1ModificationInstruction.ModOduSignalIdInstruction) l1Instruction;
Jian Li47b26232016-03-07 09:59:59 -080091 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
Yafit Hadar5796d972015-10-15 13:16:11 +030092
Jian Li47b26232016-03-07 09:59:59 -080093 ObjectNode child = result.putObject("oduSignalId");
Yafit Hadar5796d972015-10-15 13:16:11 +030094
Jian Li47b26232016-03-07 09:59:59 -080095 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
96 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
97 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP,
98 HexString.toHexString(oduSignalId.tributarySlotBitmap()));
99 break;
100 default:
Jian Lie0991662016-03-07 11:22:25 -0800101 log.info("Cannot convert L1 subtype of {}", l1Instruction.subtype());
Jian Li47b26232016-03-07 09:59:59 -0800102 break;
Yafit Hadar5796d972015-10-15 13:16:11 +0300103 }
104 }
105
106 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700107 * Encode an L2 modification instruction.
108 *
109 * @param result json node that the instruction attributes are added to
110 */
111 private void encodeL2(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800112 L2ModificationInstruction l2Instruction = (L2ModificationInstruction) instruction;
113 result.put(InstructionCodec.SUBTYPE, l2Instruction.subtype().name());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700114
Jian Lie0991662016-03-07 11:22:25 -0800115 switch (l2Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700116 case ETH_SRC:
117 case ETH_DST:
118 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800119 (L2ModificationInstruction.ModEtherInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
121 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700122 case VLAN_ID:
123 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800124 (L2ModificationInstruction.ModVlanIdInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700125 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
126 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 case VLAN_PCP:
128 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800129 (L2ModificationInstruction.ModVlanPcpInstruction) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700130 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
131 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700132 case MPLS_LABEL:
133 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800134 (L2ModificationInstruction.ModMplsLabelInstruction) l2Instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800135 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700136 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700137 case MPLS_PUSH:
138 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
Jian Lie0991662016-03-07 11:22:25 -0800139 (L2ModificationInstruction.PushHeaderInstructions) l2Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700140
alshabib7b808c52015-06-26 14:22:24 -0700141 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800142 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700143 break;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700144 case TUNNEL_ID:
145 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800146 (L2ModificationInstruction.ModTunnelIdInstruction) l2Instruction;
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700147 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
148 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700149 case MPLS_BOS:
150 final L2ModificationInstruction.ModMplsBosInstruction modMplsBosInstruction =
151 (L2ModificationInstruction.ModMplsBosInstruction) l2Instruction;
152 result.put(InstructionCodec.MPLS_BOS, modMplsBosInstruction.mplsBos());
153 case MPLS_POP:
154 case DEC_MPLS_TTL:
155 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700156 default:
Jian Lie0991662016-03-07 11:22:25 -0800157 log.info("Cannot convert L2 subtype of {}", l2Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700158 break;
159 }
160 }
161
162 /**
163 * Encode an L3 modification instruction.
164 *
165 * @param result json node that the instruction attributes are added to
166 */
167 private void encodeL3(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800168 L3ModificationInstruction l3Instruction = (L3ModificationInstruction) instruction;
169 result.put(InstructionCodec.SUBTYPE, l3Instruction.subtype().name());
170 switch (l3Instruction.subtype()) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700171 case IPV4_SRC:
172 case IPV4_DST:
173 case IPV6_SRC:
174 case IPV6_DST:
175 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800176 (L3ModificationInstruction.ModIPInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700177 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
178 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700179 case IPV6_FLABEL:
180 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
181 modFlowLabelInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800182 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) l3Instruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700183 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
184 break;
Charles Chanfbaabae2016-03-18 10:44:44 -0700185 case TTL_IN:
186 case TTL_OUT:
187 case DEC_TTL:
188 break;
Ray Milkeyd43fe452015-05-29 09:35:12 -0700189 default:
Jian Lie0991662016-03-07 11:22:25 -0800190 log.info("Cannot convert L3 subtype of {}", l3Instruction.subtype());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700191 break;
192 }
193 }
194
195 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700196 * Encode a L4 modification instruction.
197 *
198 * @param result json node that the instruction attributes are added to
199 */
200 private void encodeL4(ObjectNode result) {
Jian Lie0991662016-03-07 11:22:25 -0800201 L4ModificationInstruction l4Instruction = (L4ModificationInstruction) instruction;
202 result.put(InstructionCodec.SUBTYPE, l4Instruction.subtype().name());
203 switch (l4Instruction.subtype()) {
Hyunsun Moonfab29502015-08-25 13:39:16 -0700204 case TCP_DST:
205 case TCP_SRC:
206 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800207 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700208 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
209 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700210 case UDP_DST:
211 case UDP_SRC:
212 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
Jian Lie0991662016-03-07 11:22:25 -0800213 (L4ModificationInstruction.ModTransportPortInstruction) l4Instruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700214 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
215 break;
Hyunsun Moonfab29502015-08-25 13:39:16 -0700216 default:
Jian Lie0991662016-03-07 11:22:25 -0800217 log.info("Cannot convert L4 subtype of {}", l4Instruction.subtype());
Hyunsun Moonfab29502015-08-25 13:39:16 -0700218 break;
219 }
220 }
221
222 /**
Charles Chanfbaabae2016-03-18 10:44:44 -0700223 * Encode a extension instruction.
224 *
225 * @param result json node that the instruction attributes are added to
226 */
227 private void encodeExtension(ObjectNode result) {
228 // TODO Support extension in REST API
229 log.info("Cannot convert instruction type of EXTENSION");
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
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700248 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700249 break;
250
Jian Lice8c5602016-03-03 21:43:24 -0800251 case GROUP:
252 final Instructions.GroupInstruction groupInstruction =
253 (Instructions.GroupInstruction) instruction;
254 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
255 break;
256
Jian Li47b26232016-03-07 09:59:59 -0800257 case METER:
258 final Instructions.MeterInstruction meterInstruction =
259 (Instructions.MeterInstruction) instruction;
260 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
261 break;
262
Jian Li70dffe42016-03-08 22:23:02 -0800263 case QUEUE:
264 final Instructions.SetQueueInstruction setQueueInstruction =
265 (Instructions.SetQueueInstruction) instruction;
266 result.put(InstructionCodec.QUEUE_ID, setQueueInstruction.queueId());
267 result.put(InstructionCodec.PORT, setQueueInstruction.port().toString());
268 break;
269
Ray Milkeyd43fe452015-05-29 09:35:12 -0700270 case L0MODIFICATION:
271 encodeL0(result);
272 break;
273
Yafit Hadar5796d972015-10-15 13:16:11 +0300274 case L1MODIFICATION:
275 encodeL1(result);
276 break;
277
Ray Milkeyd43fe452015-05-29 09:35:12 -0700278 case L2MODIFICATION:
279 encodeL2(result);
280 break;
281
282 case L3MODIFICATION:
283 encodeL3(result);
284 break;
285
Hyunsun Moonfab29502015-08-25 13:39:16 -0700286 case L4MODIFICATION:
287 encodeL4(result);
288 break;
289
Charles Chanfbaabae2016-03-18 10:44:44 -0700290 case EXTENSION:
291 encodeExtension(result);
292 break;
293
Ray Milkeyd43fe452015-05-29 09:35:12 -0700294 default:
295 log.info("Cannot convert instruction type of {}", instruction.type());
296 break;
297 }
298 return result;
299 }
300
301}