blob: eccbddbf390c9cdee17916a6fdbd3a8807ac8318 [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
45 * @param context codec context for the encoding
46 */
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()) {
96 case ODU_SIGID:
97 final L1ModificationInstruction.ModOduSignalIdInstruction oduSignalIdInstruction =
98 (L1ModificationInstruction.ModOduSignalIdInstruction) instruction;
99 OduSignalId oduSignalId = oduSignalIdInstruction.oduSignalId();
100
101 ObjectNode child = result.putObject("oduSignalId");
102
103 child.put(InstructionCodec.TRIBUTARY_PORT_NUMBER, oduSignalId.tributaryPortNumber());
104 child.put(InstructionCodec.TRIBUTARY_SLOT_LEN, oduSignalId.tributarySlotLength());
105 child.put(InstructionCodec.TRIBUTARY_SLOT_BITMAP, HexString.toHexString(oduSignalId.tributarySlotBitmap()));
106 break;
107 default:
108 log.info("Cannot convert L1 subtype of {}", instruction.subtype());
109 break;
110 }
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) {
119 L2ModificationInstruction instruction =
120 (L2ModificationInstruction) this.instruction;
121 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
122
123 switch (instruction.subtype()) {
124 case ETH_SRC:
125 case ETH_DST:
126 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
127 (L2ModificationInstruction.ModEtherInstruction) instruction;
128 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
129 break;
130
131 case VLAN_ID:
132 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
133 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
134 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
135 break;
136
137 case VLAN_PCP:
138 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
139 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
140 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
141 break;
142
143 case MPLS_LABEL:
144 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
145 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
Ray Milkey125572b2016-02-22 16:48:17 -0800146 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label().toInt());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700147 break;
148
149 case MPLS_PUSH:
150 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
151 (L2ModificationInstruction.PushHeaderInstructions) instruction;
152
alshabib7b808c52015-06-26 14:22:24 -0700153 result.put(InstructionCodec.ETHERNET_TYPE,
154 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700155 break;
156
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700157 case TUNNEL_ID:
158 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
159 (L2ModificationInstruction.ModTunnelIdInstruction) instruction;
160 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
161 break;
162
Ray Milkeyd43fe452015-05-29 09:35:12 -0700163 default:
164 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
165 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) {
175 L3ModificationInstruction instruction =
176 (L3ModificationInstruction) this.instruction;
177 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
178 switch (instruction.subtype()) {
179 case IPV4_SRC:
180 case IPV4_DST:
181 case IPV6_SRC:
182 case IPV6_DST:
183 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
184 (L3ModificationInstruction.ModIPInstruction) instruction;
185 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
186 break;
187
188 case IPV6_FLABEL:
189 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
190 modFlowLabelInstruction =
191 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
192 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
193 break;
194
195 default:
196 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
197 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) {
207 L4ModificationInstruction instruction =
208 (L4ModificationInstruction) this.instruction;
209 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
210 switch (instruction.subtype()) {
211 case TCP_DST:
212 case TCP_SRC:
213 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
214 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
215 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
216 break;
217
218 case UDP_DST:
219 case UDP_SRC:
220 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
221 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
222 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
223 break;
224
225 default:
226 log.info("Cannot convert L4 subtype of {}", instruction.subtype());
227 break;
228 }
229 }
230
231 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700232 * Encodes the given instruction into JSON.
233 *
234 * @return JSON object node representing the instruction
235 */
236 public ObjectNode encode() {
237 final ObjectNode result = context.mapper().createObjectNode()
238 .put(InstructionCodec.TYPE, instruction.type().toString());
239
240 switch (instruction.type()) {
241 case OUTPUT:
242 final Instructions.OutputInstruction outputInstruction =
243 (Instructions.OutputInstruction) instruction;
Andrea Campanella5df35952015-12-08 15:46:49 -0800244 result.put(InstructionCodec.PORT, outputInstruction.port().toString());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700245 break;
246
247 case DROP:
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
Ray Milkeyd43fe452015-05-29 09:35:12 -0700257 case L0MODIFICATION:
258 encodeL0(result);
259 break;
260
Yafit Hadar5796d972015-10-15 13:16:11 +0300261 case L1MODIFICATION:
262 encodeL1(result);
263 break;
264
Ray Milkeyd43fe452015-05-29 09:35:12 -0700265 case L2MODIFICATION:
266 encodeL2(result);
267 break;
268
269 case L3MODIFICATION:
270 encodeL3(result);
271 break;
272
Hyunsun Moonfab29502015-08-25 13:39:16 -0700273 case L4MODIFICATION:
274 encodeL4(result);
275 break;
276
Ray Milkeyd43fe452015-05-29 09:35:12 -0700277 default:
278 log.info("Cannot convert instruction type of {}", instruction.type());
279 break;
280 }
281 return result;
282 }
283
284}