blob: 10f9e5e70c2c43e23721e93e91dbd1a4ab86b44d [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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 Lidab72562016-04-12 14:10:32 -070018import com.fasterxml.jackson.databind.JsonNode;
Jian Li1ef82db2016-03-03 14:43:21 -080019import com.fasterxml.jackson.databind.node.ObjectNode;
Cem Türker3baff672017-10-12 15:09:01 +030020import com.google.common.collect.Maps;
Jian Lidab72562016-04-12 14:10:32 -070021import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050023import org.onlab.packet.EthType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070024import org.onlab.packet.IpAddress;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.MplsLabel;
Hyunsun Moonfab29502015-08-25 13:39:16 -070027import org.onlab.packet.TpPort;
Ray Milkeyd43fe452015-05-29 09:35:12 -070028import org.onlab.packet.VlanId;
Yafit Hadar5796d972015-10-15 13:16:11 +030029import org.onlab.util.HexString;
Frank Wang74ce2c12018-04-11 20:26:45 +080030import org.onlab.util.ImmutableByteSequence;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070031import org.onosproject.codec.CodecContext;
Jian Lice8c5602016-03-03 21:43:24 -080032import org.onosproject.core.GroupId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070033import org.onosproject.net.ChannelSpacing;
Jian Lidab72562016-04-12 14:10:32 -070034import org.onosproject.net.Device;
35import org.onosproject.net.DeviceId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070036import org.onosproject.net.GridType;
Ray Milkeyd43fe452015-05-29 09:35:12 -070037import org.onosproject.net.OchSignal;
Yafit Hadar5796d972015-10-15 13:16:11 +030038import org.onosproject.net.OduSignalId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070039import org.onosproject.net.PortNumber;
Jian Lidab72562016-04-12 14:10:32 -070040import org.onosproject.net.device.DeviceService;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070041import org.onosproject.net.flow.ExtensionTreatmentCodec;
Cem Türker3baff672017-10-12 15:09:01 +030042import org.onosproject.net.flow.StatTriggerField;
43import org.onosproject.net.flow.StatTriggerFlag;
Jian Lidab72562016-04-12 14:10:32 -070044import org.onosproject.net.flow.instructions.ExtensionTreatment;
Ray Milkeyd43fe452015-05-29 09:35:12 -070045import org.onosproject.net.flow.instructions.Instruction;
46import org.onosproject.net.flow.instructions.Instructions;
47import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Yafit Hadar5796d972015-10-15 13:16:11 +030048import org.onosproject.net.flow.instructions.L1ModificationInstruction;
Ray Milkeyd43fe452015-05-29 09:35:12 -070049import org.onosproject.net.flow.instructions.L2ModificationInstruction;
50import org.onosproject.net.flow.instructions.L3ModificationInstruction;
Hyunsun Moonfab29502015-08-25 13:39:16 -070051import org.onosproject.net.flow.instructions.L4ModificationInstruction;
Jian Li47b26232016-03-07 09:59:59 -080052import org.onosproject.net.meter.MeterId;
Frank Wang74ce2c12018-04-11 20:26:45 +080053import org.onosproject.net.pi.model.PiActionId;
54import org.onosproject.net.pi.model.PiActionParamId;
55import org.onosproject.net.pi.runtime.PiAction;
Frank Wang74ce2c12018-04-11 20:26:45 +080056import org.onosproject.net.pi.runtime.PiActionParam;
Carmelo Casconecb4327a2018-09-11 15:17:23 -070057import org.onosproject.net.pi.runtime.PiActionProfileGroupId;
58import org.onosproject.net.pi.runtime.PiActionProfileMemberId;
Frank Wang74ce2c12018-04-11 20:26:45 +080059import org.onosproject.net.pi.runtime.PiTableAction;
Jian Lidab72562016-04-12 14:10:32 -070060import org.slf4j.Logger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070061
Cem Türker3baff672017-10-12 15:09:01 +030062import java.util.Map;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050063import java.util.regex.Matcher;
64import java.util.regex.Pattern;
65
Jian Li1ef82db2016-03-03 14:43:21 -080066import static org.onlab.util.Tools.nullIsIllegal;
Cem Türker3baff672017-10-12 15:09:01 +030067import static org.onosproject.codec.impl.InstructionCodec.STAT_PACKET_COUNT;
Jian Lidab72562016-04-12 14:10:32 -070068import static org.slf4j.LoggerFactory.getLogger;
Ray Milkeyd43fe452015-05-29 09:35:12 -070069
70/**
71 * Decoding portion of the instruction codec.
72 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070073public final class DecodeInstructionCodecHelper {
Ray Milkey9c9cde42018-01-12 14:22:06 -080074 private static final Logger log = getLogger(DecodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070075 private final ObjectNode json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070076 private final CodecContext context;
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -050077 private static final Pattern ETHTYPE_PATTERN = Pattern.compile("0x([0-9a-fA-F]{4})");
Ray Milkeyd43fe452015-05-29 09:35:12 -070078
79 /**
80 * Creates a decode instruction codec object.
81 *
82 * @param json JSON object to decode
Ray Milkeyef794342016-11-09 16:20:29 -080083 * @param context codec context
Ray Milkeyd43fe452015-05-29 09:35:12 -070084 */
Jonathan Harte3bcfc32016-08-16 17:12:49 -070085 public DecodeInstructionCodecHelper(ObjectNode json, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070086 this.json = json;
Jonathan Harte3bcfc32016-08-16 17:12:49 -070087 this.context = context;
Ray Milkeyd43fe452015-05-29 09:35:12 -070088 }
89
90 /**
91 * Decodes a Layer 2 instruction.
92 *
93 * @return instruction object decoded from the JSON
94 * @throws IllegalArgumentException if the JSON is invalid
95 */
96 private Instruction decodeL2() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +053097 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
98 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -070099
100 if (subType.equals(L2ModificationInstruction.L2SubType.ETH_SRC.name())) {
101 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
102 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
103 return Instructions.modL2Src(MacAddress.valueOf(mac));
104 } else if (subType.equals(L2ModificationInstruction.L2SubType.ETH_DST.name())) {
105 String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
106 InstructionCodec.MAC + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
107 return Instructions.modL2Dst(MacAddress.valueOf(mac));
108 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_ID.name())) {
109 short vlanId = (short) nullIsIllegal(json.get(InstructionCodec.VLAN_ID),
110 InstructionCodec.VLAN_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
111 return Instructions.modVlanId(VlanId.vlanId(vlanId));
112 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PCP.name())) {
113 byte vlanPcp = (byte) nullIsIllegal(json.get(InstructionCodec.VLAN_PCP),
114 InstructionCodec.VLAN_PCP + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
115 return Instructions.modVlanPcp(vlanPcp);
116 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_LABEL.name())) {
117 int label = nullIsIllegal(json.get(InstructionCodec.MPLS_LABEL),
118 InstructionCodec.MPLS_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
119 return Instructions.modMplsLabel(MplsLabel.mplsLabel(label));
120 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_PUSH.name())) {
121 return Instructions.pushMpls();
122 } else if (subType.equals(L2ModificationInstruction.L2SubType.MPLS_POP.name())) {
Sivachidambaram Subramaniandab7f4b2017-05-15 12:28:12 +0530123 if (json.has(InstructionCodec.ETHERNET_TYPE)) {
124 return Instructions.popMpls(getEthType());
125 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700126 return Instructions.popMpls();
127 } else if (subType.equals(L2ModificationInstruction.L2SubType.DEC_MPLS_TTL.name())) {
128 return Instructions.decMplsTtl();
129 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_POP.name())) {
130 return Instructions.popVlan();
131 } else if (subType.equals(L2ModificationInstruction.L2SubType.VLAN_PUSH.name())) {
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500132 if (json.has(InstructionCodec.ETHERNET_TYPE)) {
133 return Instructions.pushVlan(getEthType());
134 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700135 return Instructions.pushVlan();
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700136 } else if (subType.equals(L2ModificationInstruction.L2SubType.TUNNEL_ID.name())) {
137 long tunnelId = nullIsIllegal(json.get(InstructionCodec.TUNNEL_ID),
138 InstructionCodec.TUNNEL_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
139 return Instructions.modTunnelId(tunnelId);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700140 }
141 throw new IllegalArgumentException("L2 Instruction subtype "
142 + subType + " is not supported");
143 }
144
145 /**
146 * Decodes a Layer 3 instruction.
147 *
148 * @return instruction object decoded from the JSON
149 * @throws IllegalArgumentException if the JSON is invalid
150 */
151 private Instruction decodeL3() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530152 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
153 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700154
155 if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_SRC.name())) {
156 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
157 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
158 return Instructions.modL3Src(ip);
159 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_DST.name())) {
160 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
161 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
162 return Instructions.modL3Dst(ip);
163 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_SRC.name())) {
164 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
165 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
166 return Instructions.modL3IPv6Src(ip);
167 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_DST.name())) {
168 IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
169 InstructionCodec.IP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
170 return Instructions.modL3IPv6Dst(ip);
171 } else if (subType.equals(L3ModificationInstruction.L3SubType.IPV6_FLABEL.name())) {
172 int flowLabel = nullIsIllegal(json.get(InstructionCodec.FLOW_LABEL),
173 InstructionCodec.FLOW_LABEL + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
174 return Instructions.modL3IPv6FlowLabel(flowLabel);
Jonathan Hartcc962d82016-08-09 16:52:22 -0700175 } else if (subType.equals(L3ModificationInstruction.L3SubType.TTL_IN.name())) {
176 return Instructions.copyTtlIn();
177 } else if (subType.equals(L3ModificationInstruction.L3SubType.TTL_OUT.name())) {
178 return Instructions.copyTtlOut();
179 } else if (subType.equals(L3ModificationInstruction.L3SubType.DEC_TTL.name())) {
180 return Instructions.decNwTtl();
Rory Savagedaeb9492020-02-18 14:35:50 -0500181 } else if (subType.equals(L3ModificationInstruction.L3SubType.IP_DSCP.name())) {
182 int ipDscp = nullIsIllegal(json.get(InstructionCodec.IP_DSCP),
183 InstructionCodec.IP_DSCP + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
184 if ((ipDscp < Byte.MIN_VALUE) || (ipDscp > Byte.MAX_VALUE)) {
185 throw new IllegalArgumentException("Value " + ipDscp + " must be single byte");
186 }
187 return Instructions.modIpDscp((byte) ipDscp);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700188 }
189 throw new IllegalArgumentException("L3 Instruction subtype "
190 + subType + " is not supported");
191 }
192
193 /**
194 * Decodes a Layer 0 instruction.
195 *
196 * @return instruction object decoded from the JSON
197 * @throws IllegalArgumentException if the JSON is invalid
198 */
199 private Instruction decodeL0() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530200 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
201 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700202
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800203 if (subType.equals(L0ModificationInstruction.L0SubType.OCH.name())) {
Ray Milkeyd43fe452015-05-29 09:35:12 -0700204 String gridTypeString = nullIsIllegal(json.get(InstructionCodec.GRID_TYPE),
205 InstructionCodec.GRID_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
206 GridType gridType = GridType.valueOf(gridTypeString);
207 if (gridType == null) {
208 throw new IllegalArgumentException("Unknown grid type "
209 + gridTypeString);
210 }
211 String channelSpacingString = nullIsIllegal(json.get(InstructionCodec.CHANNEL_SPACING),
212 InstructionCodec.CHANNEL_SPACING + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
213 ChannelSpacing channelSpacing = ChannelSpacing.valueOf(channelSpacingString);
214 if (channelSpacing == null) {
215 throw new IllegalArgumentException("Unknown channel spacing "
216 + channelSpacingString);
217 }
218 int spacingMultiplier = nullIsIllegal(json.get(InstructionCodec.SPACING_MULTIPLIER),
219 InstructionCodec.SPACING_MULTIPLIER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
220 int slotGranularity = nullIsIllegal(json.get(InstructionCodec.SLOT_GRANULARITY),
221 InstructionCodec.SLOT_GRANULARITY + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
222 return Instructions.modL0Lambda(new OchSignal(gridType, channelSpacing,
223 spacingMultiplier, slotGranularity));
224 }
225 throw new IllegalArgumentException("L0 Instruction subtype "
226 + subType + " is not supported");
227 }
228
229 /**
Yafit Hadar5796d972015-10-15 13:16:11 +0300230 * Decodes a Layer 1 instruction.
231 *
232 * @return instruction object decoded from the JSON
233 * @throws IllegalArgumentException if the JSON is invalid
234 */
235 private Instruction decodeL1() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530236 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
237 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Yafit Hadar5796d972015-10-15 13:16:11 +0300238 if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) {
239 int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER),
240 InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
241 int tributarySlotLen = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_LEN),
242 InstructionCodec.TRIBUTARY_SLOT_LEN + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
243 byte[] tributarySlotBitmap = null;
244 tributarySlotBitmap = HexString.fromHexString(
245 nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_SLOT_BITMAP),
246 InstructionCodec.TRIBUTARY_SLOT_BITMAP + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
247 return Instructions.modL1OduSignalId(OduSignalId.oduSignalId(tributaryPortNumber, tributarySlotLen,
248 tributarySlotBitmap));
249 }
250 throw new IllegalArgumentException("L1 Instruction subtype "
251 + subType + " is not supported");
252 }
253
254 /**
Hyunsun Moonfab29502015-08-25 13:39:16 -0700255 * Decodes a Layer 4 instruction.
256 *
257 * @return instruction object decoded from the JSON
258 * @throws IllegalArgumentException if the JSON is invalid
259 */
260 private Instruction decodeL4() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530261 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
262 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700263
264 if (subType.equals(L4ModificationInstruction.L4SubType.TCP_DST.name())) {
265 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
266 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
267 return Instructions.modTcpDst(tcpPort);
268 } else if (subType.equals(L4ModificationInstruction.L4SubType.TCP_SRC.name())) {
269 TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
270 InstructionCodec.TCP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
271 return Instructions.modTcpSrc(tcpPort);
272 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_DST.name())) {
273 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
274 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
275 return Instructions.modUdpDst(udpPort);
276 } else if (subType.equals(L4ModificationInstruction.L4SubType.UDP_SRC.name())) {
277 TpPort udpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.UDP_PORT),
278 InstructionCodec.UDP_PORT + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
279 return Instructions.modUdpSrc(udpPort);
280 }
281 throw new IllegalArgumentException("L4 Instruction subtype "
282 + subType + " is not supported");
283 }
284
Frank Wang74ce2c12018-04-11 20:26:45 +0800285 /**
286 * Decodes a protocol-independent instruction.
287 *
288 * @return instruction object decoded from the JSON
289 * @throws IllegalArgumentException if the JSON is invalid
290 */
291 private Instruction decodePi() {
292 String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
293 InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
294
295 if (subType.equals(PiTableAction.Type.ACTION.name())) {
296 PiActionId piActionId = PiActionId.of(nullIsIllegal(
297 json.get(InstructionCodec.PI_ACTION_ID),
298 InstructionCodec.PI_ACTION_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asText());
299 JsonNode params = json.get(InstructionCodec.PI_ACTION_PARAMS);
300
301 PiAction.Builder builder = PiAction.builder();
302 PiActionParam piActionParam;
303 PiActionParamId piActionParamId;
304 if (params != null) {
305 for (Map.Entry<String, String> param : ((Map<String, String>)
306 (context.mapper().convertValue(params, Map.class))).entrySet()) {
307 piActionParamId = PiActionParamId.of(param.getKey());
308 piActionParam = new PiActionParam(piActionParamId,
309 ImmutableByteSequence.copyFrom(
310 HexString.fromHexString(param.getValue(), null)));
311 builder.withParameter(piActionParam);
312 }
313 }
314
315 return Instructions.piTableAction(builder.withId(piActionId).build());
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700316 } else if (subType.equals(PiTableAction.Type.ACTION_PROFILE_GROUP_ID.name())) {
317 PiActionProfileGroupId piActionGroupId = PiActionProfileGroupId.of(nullIsIllegal(
318 json.get(InstructionCodec.PI_ACTION_PROFILE_GROUP_ID),
319 InstructionCodec.PI_ACTION_PROFILE_GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Frank Wang74ce2c12018-04-11 20:26:45 +0800320
321 return Instructions.piTableAction(piActionGroupId);
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700322 } else if (subType.equals(PiTableAction.Type.ACTION_PROFILE_MEMBER_ID.name())) {
323 PiActionProfileMemberId piActionProfileMemberId = PiActionProfileMemberId.of(nullIsIllegal(
324 json.get(InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID),
325 InstructionCodec.PI_ACTION_PROFILE_MEMBER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Frank Wang74ce2c12018-04-11 20:26:45 +0800326
Carmelo Casconecb4327a2018-09-11 15:17:23 -0700327 return Instructions.piTableAction(piActionProfileMemberId);
Frank Wang74ce2c12018-04-11 20:26:45 +0800328 }
329 throw new IllegalArgumentException("Protocol-independent Instruction subtype "
330 + subType + " is not supported");
331 }
332
Cem Türker3baff672017-10-12 15:09:01 +0300333 private Instruction decodeStatTrigger() {
334 String statTriggerFlag = nullIsIllegal(json.get(InstructionCodec.STAT_TRIGGER_FLAG),
335 InstructionCodec.STAT_TRIGGER_FLAG + InstructionCodec.ERROR_MESSAGE).asText();
336
337 StatTriggerFlag flag = null;
338
339 if (statTriggerFlag.equals(StatTriggerFlag.ONLY_FIRST.name())) {
340 flag = StatTriggerFlag.ONLY_FIRST;
341 } else if (statTriggerFlag.equals(StatTriggerFlag.PERIODIC.name())) {
342 flag = StatTriggerFlag.PERIODIC;
343 } else {
344 throw new IllegalArgumentException("statTriggerFlag "
345 + statTriggerFlag + " is not supported");
346 }
347 if (!json.has(InstructionCodec.STAT_THRESHOLDS)) {
348 throw new IllegalArgumentException("statThreshold is not added");
349 }
350 JsonNode statThresholdsNode = nullIsIllegal(json.get(InstructionCodec.STAT_THRESHOLDS),
351 InstructionCodec.STAT_THRESHOLDS + InstructionCodec.ERROR_MESSAGE);
352 Map<StatTriggerField, Long> statThresholdMap = getStatThreshold(statThresholdsNode);
353 if (statThresholdMap.isEmpty()) {
354 throw new IllegalArgumentException("statThreshold must have at least one property");
355 }
356 return Instructions.statTrigger(statThresholdMap, flag);
357 }
358
359 private Map<StatTriggerField, Long> getStatThreshold(JsonNode statThresholdNode) {
360 Map<StatTriggerField, Long> statThresholdMap = Maps.newEnumMap(StatTriggerField.class);
361 for (JsonNode jsonNode : statThresholdNode) {
362 if (jsonNode.hasNonNull(InstructionCodec.STAT_BYTE_COUNT)) {
363 JsonNode byteCountNode = jsonNode.get(InstructionCodec.STAT_BYTE_COUNT);
364 if (!byteCountNode.isNull() && byteCountNode.isNumber()) {
365 statThresholdMap.put(StatTriggerField.BYTE_COUNT, byteCountNode.asLong());
366 }
367 } else if (jsonNode.hasNonNull(STAT_PACKET_COUNT)) {
368 JsonNode packetCount = jsonNode.get(STAT_PACKET_COUNT);
369 if (!packetCount.isNull() && packetCount.isNumber()) {
370 statThresholdMap.put(StatTriggerField.PACKET_COUNT, packetCount.asLong());
371 }
372 } else if (jsonNode.hasNonNull(InstructionCodec.STAT_DURATION)) {
373 JsonNode duration = jsonNode.get(InstructionCodec.STAT_DURATION);
374 if (!duration.isNull() && duration.isNumber()) {
375 statThresholdMap.put(StatTriggerField.DURATION, duration.asLong());
376 }
377 } else {
378 log.error("Unsupported stat {}", jsonNode.toString());
379 }
380 }
381
382 return statThresholdMap;
383 }
384
Hyunsun Moonfab29502015-08-25 13:39:16 -0700385 /**
Jian Lidab72562016-04-12 14:10:32 -0700386 * Decodes a extension instruction.
387 *
388 * @return extension treatment
389 */
390 private Instruction decodeExtension() {
391 ObjectNode node = (ObjectNode) json.get(InstructionCodec.EXTENSION);
392 if (node != null) {
393 DeviceId deviceId = getDeviceId();
394
395 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
396 DeviceService deviceService = serviceDirectory.get(DeviceService.class);
397 Device device = deviceService.getDevice(deviceId);
398
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700399 if (device == null) {
400 throw new IllegalArgumentException("Device not found");
401 }
402
Jian Lidab72562016-04-12 14:10:32 -0700403 if (device.is(ExtensionTreatmentCodec.class)) {
404 ExtensionTreatmentCodec treatmentCodec = device.as(ExtensionTreatmentCodec.class);
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700405 ExtensionTreatment treatment = treatmentCodec.decode(node, context);
Jian Lidab72562016-04-12 14:10:32 -0700406 return Instructions.extension(treatment, deviceId);
407 } else {
Jonathan Harte3bcfc32016-08-16 17:12:49 -0700408 throw new IllegalArgumentException(
409 "There is no codec to decode extension for device " + deviceId.toString());
Jian Lidab72562016-04-12 14:10:32 -0700410 }
411 }
412 return null;
413 }
414
415 /**
416 * Returns device identifier.
417 *
418 * @return device identifier
419 * @throws IllegalArgumentException if the JSON is invalid
420 */
421 private DeviceId getDeviceId() {
422 JsonNode deviceIdNode = json.get(InstructionCodec.DEVICE_ID);
423 if (deviceIdNode != null) {
424 return DeviceId.deviceId(deviceIdNode.asText());
425 }
426 throw new IllegalArgumentException("Empty device identifier");
427 }
428
429 /**
Jian Li70dffe42016-03-08 22:23:02 -0800430 * Extracts port number of the given json node.
431 *
432 * @param jsonNode json node
433 * @return port number
434 */
435 private PortNumber getPortNumber(ObjectNode jsonNode) {
436 PortNumber portNumber;
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530437 JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT),
438 InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE);
439 if (portNode.isLong() || portNode.isInt()) {
440 portNumber = PortNumber.portNumber(portNode.asLong());
441 } else if (portNode.isTextual()) {
442 portNumber = PortNumber.fromString(portNode.textValue());
Jian Li70dffe42016-03-08 22:23:02 -0800443 } else {
444 throw new IllegalArgumentException("Port value "
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530445 + portNode.toString()
Jian Li70dffe42016-03-08 22:23:02 -0800446 + " is not supported");
447 }
448 return portNumber;
449 }
450
451 /**
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500452 * Returns Ethernet type.
453 *
454 * @return ethernet type
455 * @throws IllegalArgumentException if the JSON is invalid
456 */
457 private EthType getEthType() {
458 String ethTypeStr = nullIsIllegal(json.get(InstructionCodec.ETHERNET_TYPE),
459 InstructionCodec.ETHERNET_TYPE + InstructionCodec.MISSING_MEMBER_MESSAGE).asText();
460 Matcher matcher = ETHTYPE_PATTERN.matcher(ethTypeStr);
461 if (!matcher.matches()) {
462 throw new IllegalArgumentException("ETHERNET_TYPE must be a four digit hex string starting with 0x");
463 }
464 short ethernetType = (short) Integer.parseInt(matcher.group(1), 16);
465 return new EthType(ethernetType);
466 }
467
468 /**
Ray Milkeyd43fe452015-05-29 09:35:12 -0700469 * Decodes the JSON into an instruction object.
470 *
471 * @return Criterion object
472 * @throws IllegalArgumentException if the JSON is invalid
473 */
474 public Instruction decode() {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530475 String type = nullIsIllegal(json.get(InstructionCodec.TYPE),
476 InstructionCodec.TYPE + InstructionCodec.ERROR_MESSAGE).asText();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700477
478 if (type.equals(Instruction.Type.OUTPUT.name())) {
Jian Li70dffe42016-03-08 22:23:02 -0800479 return Instructions.createOutput(getPortNumber(json));
Ray Milkey2be39ed2016-02-22 15:54:19 -0800480 } else if (type.equals(Instruction.Type.NOACTION.name())) {
481 return Instructions.createNoAction();
Jian Li1ef82db2016-03-03 14:43:21 -0800482 } else if (type.equals(Instruction.Type.TABLE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530483 return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID),
484 InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
Jian Lice8c5602016-03-03 21:43:24 -0800485 } else if (type.equals(Instruction.Type.GROUP.name())) {
Seyeon Jeong8188da12020-03-03 12:49:48 -0800486 // a group id should be an unsigned integer
487 Long id = nullIsIllegal(json.get(InstructionCodec.GROUP_ID),
488 InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
489 GroupId groupId = new GroupId(id.intValue());
Jian Lice8c5602016-03-03 21:43:24 -0800490 return Instructions.createGroup(groupId);
Jian Li47b26232016-03-07 09:59:59 -0800491 } else if (type.equals(Instruction.Type.METER.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530492 MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID),
493 InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong());
Jian Li47b26232016-03-07 09:59:59 -0800494 return Instructions.meterTraffic(meterId);
Jian Li70dffe42016-03-08 22:23:02 -0800495 } else if (type.equals(Instruction.Type.QUEUE.name())) {
Jayasree Ghosh8aca6772016-10-04 03:32:11 +0530496 long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID),
497 InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
ke han74702102016-11-29 14:57:29 +0800498 if (json.get(InstructionCodec.PORT) == null ||
499 json.get(InstructionCodec.PORT).isNull()) {
500 return Instructions.setQueue(queueId, null);
501 } else {
502 return Instructions.setQueue(queueId, getPortNumber(json));
503 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700504 } else if (type.equals(Instruction.Type.L0MODIFICATION.name())) {
505 return decodeL0();
Yafit Hadar5796d972015-10-15 13:16:11 +0300506 } else if (type.equals(Instruction.Type.L1MODIFICATION.name())) {
507 return decodeL1();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700508 } else if (type.equals(Instruction.Type.L2MODIFICATION.name())) {
509 return decodeL2();
510 } else if (type.equals(Instruction.Type.L3MODIFICATION.name())) {
511 return decodeL3();
Hyunsun Moonfab29502015-08-25 13:39:16 -0700512 } else if (type.equals(Instruction.Type.L4MODIFICATION.name())) {
513 return decodeL4();
Jian Lidab72562016-04-12 14:10:32 -0700514 } else if (type.equals(Instruction.Type.EXTENSION.name())) {
515 return decodeExtension();
Cem Türker3baff672017-10-12 15:09:01 +0300516 } else if (type.equals(Instruction.Type.STAT_TRIGGER.name())) {
517 return decodeStatTrigger();
Frank Wang74ce2c12018-04-11 20:26:45 +0800518 } else if (type.equals(Instruction.Type.PROTOCOL_INDEPENDENT.name())) {
519 return decodePi();
Ray Milkeyd43fe452015-05-29 09:35:12 -0700520 }
Frank Wang74ce2c12018-04-11 20:26:45 +0800521
Ray Milkeyd43fe452015-05-29 09:35:12 -0700522 throw new IllegalArgumentException("Instruction type "
Frank Wang74ce2c12018-04-11 20:26:45 +0800523 + type + " is not supported");
Ray Milkeyd43fe452015-05-29 09:35:12 -0700524 }
Ray Milkeyd43fe452015-05-29 09:35:12 -0700525}