blob: 409a2f64d8a94906c6e972eca406038f1d4716ec [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) {
59 L0ModificationInstruction instruction =
60 (L0ModificationInstruction) this.instruction;
61 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
62
63 switch (instruction.subtype()) {
64 case LAMBDA:
65 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
66 (L0ModificationInstruction.ModLambdaInstruction) instruction;
67 result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda());
68 break;
69
70 case OCH:
71 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
72 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
73 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:
81 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
82 }
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) {
91 L1ModificationInstruction instruction =
92 (L1ModificationInstruction) this.instruction;
93 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
94
95 switch (instruction.subtype()) {
Jian Li47b26232016-03-07 09:59:59 -080096 case ODU_SIGID:
97 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
98 (L1ModificationInstruction.ModOduSignalIdInstruction) instruction;
99 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:
109 log.info("Cannot convert L1 subtype of {}", instruction.subtype());
110 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) {
120 L2ModificationInstruction instruction =
121 (L2ModificationInstruction) this.instruction;
122 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
123
124 switch (instruction.subtype()) {
125 case ETH_SRC:
126 case ETH_DST:
127 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
128 (L2ModificationInstruction.ModEtherInstruction) instruction;
129 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
130 break;
131
132 case VLAN_ID:
133 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
134 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
135 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
136 break;
137
138 case VLAN_PCP:
139 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
140 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
141 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
142 break;
143
144 case MPLS_LABEL:
145 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
146 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800147 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700148 break;
149
150 case MPLS_PUSH:
151 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
152 (L2ModificationInstruction.PushHeaderInstructions) instruction;
153
alshabib7b808c52015-06-26 14:22:24 -0700154 result.put(InstructionCodec.ETHERNET_TYPE,
Jian Li47b26232016-03-07 09:59:59 -0800155 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700156 break;
157
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700158 case TUNNEL_ID:
159 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
160 (L2ModificationInstruction.ModTunnelIdInstruction) instruction;
161 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
162 break;
163
Ray Milkeyd43fe452015-05-29 09:35:12 -0700164 default:
165 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
166 break;
167 }
168 }
169
170 /**
171 * Encode an L3 modification instruction.
172 *
173 * @param result json node that the instruction attributes are added to
174 */
175 private void encodeL3(ObjectNode result) {
176 L3ModificationInstruction instruction =
177 (L3ModificationInstruction) this.instruction;
178 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
179 switch (instruction.subtype()) {
180 case IPV4_SRC:
181 case IPV4_DST:
182 case IPV6_SRC:
183 case IPV6_DST:
184 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
185 (L3ModificationInstruction.ModIPInstruction) instruction;
186 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
187 break;
188
189 case IPV6_FLABEL:
190 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
191 modFlowLabelInstruction =
192 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
193 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
194 break;
195
196 default:
197 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
198 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) {
208 L4ModificationInstruction instruction =
209 (L4ModificationInstruction) this.instruction;
210 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
211 switch (instruction.subtype()) {
212 case TCP_DST:
213 case TCP_SRC:
214 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
215 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
216 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
217 break;
218
219 case UDP_DST:
220 case UDP_SRC:
221 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
222 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
223 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
224 break;
225
226 default:
227 log.info("Cannot convert L4 subtype of {}", instruction.subtype());
228 break;
229 }
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
248 case DROP:
Brian O'Connor7664e7d2015-10-09 00:57:58 -0700249 case NOACTION:
Ray Milkeyd43fe452015-05-29 09:35:12 -0700250 break;
251
Jian Lice8c5602016-03-03 21:43:24 -0800252 case GROUP:
253 final Instructions.GroupInstruction groupInstruction =
254 (Instructions.GroupInstruction) instruction;
255 result.put(InstructionCodec.GROUP_ID, groupInstruction.groupId().toString());
256 break;
257
Jian Li47b26232016-03-07 09:59:59 -0800258 case METER:
259 final Instructions.MeterInstruction meterInstruction =
260 (Instructions.MeterInstruction) instruction;
261 result.put(InstructionCodec.METER_ID, meterInstruction.meterId().toString());
262 break;
263
Ray Milkeyd43fe452015-05-29 09:35:12 -0700264 case L0MODIFICATION:
265 encodeL0(result);
266 break;
267
Yafit Hadar5796d972015-10-15 13:16:11 +0300268 case L1MODIFICATION:
269 encodeL1(result);
270 break;
271
Ray Milkeyd43fe452015-05-29 09:35:12 -0700272 case L2MODIFICATION:
273 encodeL2(result);
274 break;
275
276 case L3MODIFICATION:
277 encodeL3(result);
278 break;
279
Hyunsun Moonfab29502015-08-25 13:39:16 -0700280 case L4MODIFICATION:
281 encodeL4(result);
282 break;
283
Ray Milkeyd43fe452015-05-29 09:35:12 -0700284 default:
285 log.info("Cannot convert instruction type of {}", instruction.type());
286 break;
287 }
288 return result;
289 }
290
291}