blob: 7e7ee109e89fd60bcf571c40399150b4d27ed230 [file] [log] [blame]
Jonathan Hart3c259162015-10-21 21:31:19 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart3c259162015-10-21 21:31:19 -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 */
16
17package org.onosproject.driver.extensions;
18
Jian Lide6eb182019-02-25 21:33:07 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
Frank Wang5733c382017-03-28 10:15:18 +080020import com.google.common.collect.Lists;
21import com.google.common.primitives.Bytes;
Jonathan Hart3c259162015-10-21 21:31:19 -070022import org.onlab.packet.Ip4Address;
Frank Wang5733c382017-03-28 10:15:18 +080023import org.onlab.packet.IpAddress;
Jian Lidab72562016-04-12 14:10:32 -070024import org.onosproject.codec.CodecContext;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053025import org.onosproject.net.NshContextHeader;
26import org.onosproject.net.NshServiceIndex;
27import org.onosproject.net.NshServicePathId;
Jian Lid4163882016-03-30 20:03:14 -070028import org.onosproject.net.PortNumber;
alshabib880b6442015-11-23 22:13:04 -080029import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
Jonathan Hart3c259162015-10-21 21:31:19 -070030import org.onosproject.net.driver.AbstractHandlerBehaviour;
alshabib880b6442015-11-23 22:13:04 -080031import org.onosproject.net.flow.instructions.ExtensionTreatment;
32import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
33import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Jonathan Hart3c259162015-10-21 21:31:19 -070034import org.projectfloodlight.openflow.protocol.OFActionType;
35import org.projectfloodlight.openflow.protocol.OFFactory;
36import org.projectfloodlight.openflow.protocol.action.OFAction;
samueljcc4f4d25a2015-12-07 10:52:28 +080037import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
38import org.projectfloodlight.openflow.protocol.action.OFActionNicira;
Frank Wang5733c382017-03-28 10:15:18 +080039import org.projectfloodlight.openflow.protocol.action.OFActionNiciraCt;
Jian Lide6eb182019-02-25 21:33:07 +090040import org.projectfloodlight.openflow.protocol.action.OFActionNiciraLoad;
samueljcc4f4d25a2015-12-07 10:52:28 +080041import org.projectfloodlight.openflow.protocol.action.OFActionNiciraMove;
Frank Wang5733c382017-03-28 10:15:18 +080042import org.projectfloodlight.openflow.protocol.action.OFActionNiciraNat;
Jian Lid4163882016-03-30 20:03:14 -070043import org.projectfloodlight.openflow.protocol.action.OFActionNiciraResubmit;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053044import org.projectfloodlight.openflow.protocol.action.OFActionNiciraResubmitTable;
Jonathan Hart3c259162015-10-21 21:31:19 -070045import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
46import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053047import org.projectfloodlight.openflow.protocol.oxm.OFOxmEncapEthDst;
48import org.projectfloodlight.openflow.protocol.oxm.OFOxmEncapEthSrc;
49import org.projectfloodlight.openflow.protocol.oxm.OFOxmEncapEthType;
50import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshC1;
51import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshC2;
52import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshC3;
53import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshC4;
54import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshMdtype;
55import org.projectfloodlight.openflow.protocol.oxm.OFOxmNshNp;
56import org.projectfloodlight.openflow.protocol.oxm.OFOxmNsi;
57import org.projectfloodlight.openflow.protocol.oxm.OFOxmNsp;
58import org.projectfloodlight.openflow.protocol.oxm.OFOxmTunGpeNp;
Jonathan Hart3c259162015-10-21 21:31:19 -070059import org.projectfloodlight.openflow.protocol.oxm.OFOxmTunnelIpv4Dst;
60import org.projectfloodlight.openflow.types.IPv4Address;
Frank Wang5733c382017-03-28 10:15:18 +080061import org.projectfloodlight.openflow.types.IPv6Address;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053062import org.projectfloodlight.openflow.types.MacAddress;
63import org.projectfloodlight.openflow.types.U16;
64import org.projectfloodlight.openflow.types.U32;
Jian Lide6eb182019-02-25 21:33:07 +090065import org.projectfloodlight.openflow.types.U64;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053066import org.projectfloodlight.openflow.types.U8;
Jonathan Hart3c259162015-10-21 21:31:19 -070067
Frank Wang5733c382017-03-28 10:15:18 +080068import java.util.ArrayList;
69import java.util.List;
70
Jian Lide6eb182019-02-25 21:33:07 +090071import static com.google.common.base.Preconditions.checkNotNull;
72import static org.onlab.util.Tools.nullIsIllegal;
73
Jonathan Hart3c259162015-10-21 21:31:19 -070074/**
Jonathan Hart26a8d952015-12-02 15:16:35 -080075 * Interpreter for Nicira OpenFlow treatment extensions.
Jonathan Hart3c259162015-10-21 21:31:19 -070076 */
alshabib880b6442015-11-23 22:13:04 -080077public class NiciraExtensionTreatmentInterpreter extends AbstractHandlerBehaviour
78 implements ExtensionTreatmentInterpreter, ExtensionTreatmentResolver {
Jonathan Hart3c259162015-10-21 21:31:19 -070079
samueljcc4f4d25a2015-12-07 10:52:28 +080080 private static final int TYPE_NICIRA = 0x2320;
samueljcc4f4d25a2015-12-07 10:52:28 +080081 private static final int SRC_ARP_SHA = 0x00012206;
82 private static final int SRC_ARP_SPA = 0x00002004;
83 private static final int SRC_ETH = 0x00000406;
84 private static final int SRC_IP = 0x00000e04;
85
Phaneendra Manda8db7d092016-06-04 00:17:24 +053086 private static final int NSH_C1 = 0x0001e604;
87 private static final int NSH_C2 = 0x0001e804;
88 private static final int NSH_C3 = 0x0001ea04;
89 private static final int NSH_C4 = 0x0001ec04;
90 private static final int TUN_IPV4_DST = 0x00014004;
91 private static final int TUN_ID = 0x12008;
92
Frank Wang5733c382017-03-28 10:15:18 +080093 private static final int NAT_RANGE_IPV4_MIN = 0x01;
94 private static final int NAT_RANGE_IPV4_MAX = 0x02;
95 private static final int NAT_RANGE_IPV6_MIN = 0x04;
96 private static final int NAT_RANGE_IPV6_MAX = 0x08;
97 private static final int NAT_RANGE_PROTO_MIN = 0x10;
98 private static final int NAT_RANGE_PROTO_MAX = 0x20;
99
Jian Lid4163882016-03-30 20:03:14 -0700100 private static final int SUB_TYPE_RESUBMIT = 1;
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530101 private static final int SUB_TYPE_RESUBMIT_TABLE = 14;
Jian Lid4163882016-03-30 20:03:14 -0700102 private static final int SUB_TYPE_MOVE = 6;
Jian Lide6eb182019-02-25 21:33:07 +0900103 private static final int SUB_TYPE_LOAD = 7;
Frank Wang5733c382017-03-28 10:15:18 +0800104 private static final int SUB_TYPE_CT = 35;
105 private static final int SUB_TYPE_NAT = 36;
106 private static final int SUB_TYPE_CT_CLEAR = 43;
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530107 private static final int SUB_TYPE_PUSH_NSH = 38;
108 private static final int SUB_TYPE_POP_NSH = 39;
Jian Lid4163882016-03-30 20:03:14 -0700109
Jian Lidab72562016-04-12 14:10:32 -0700110 private static final String TUNNEL_DST = "tunnelDst";
111 private static final String RESUBMIT = "resubmit";
112 private static final String RESUBMIT_TABLE = "resubmitTable";
113 private static final String NICIRA_NSH_SPI = "niciraNshSpi";
114 private static final String NICIRA_NSH_SI = "niciraNshSi";
115 private static final String NICIRA_NSH_CH = "niciraNshCh";
116 private static final String NICIRA_MOVE = "niciraMove";
Jian Lide6eb182019-02-25 21:33:07 +0900117 private static final String NICIRA_LOAD = "niciraLoad";
Jian Lidab72562016-04-12 14:10:32 -0700118
119 private static final String TYPE = "type";
120
121 private static final String MISSING_MEMBER_MESSAGE = " member is required in NiciraExtensionTreatmentInterpreter";
122
Jonathan Hart3c259162015-10-21 21:31:19 -0700123 @Override
alshabib880b6442015-11-23 22:13:04 -0800124 public boolean supported(ExtensionTreatmentType extensionTreatmentType) {
125 if (extensionTreatmentType.equals(
126 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700127 return true;
128 }
alshabib880b6442015-11-23 22:13:04 -0800129 if (extensionTreatmentType.equals(
130 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenryb53ac6c2015-11-16 20:58:58 +0800131 return true;
132 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530133 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
134 return true;
135 }
136 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
137 return true;
138 }
139 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
140 return true;
141 }
142 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
143 return true;
144 }
145 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
146 return true;
147 }
148 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
Phaneendra Mandace66cbc2015-11-26 18:07:48 +0530149 return true;
150 }
BitOhenry74dd7e12015-12-01 09:07:19 +0800151 if (extensionTreatmentType.equals(
152 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
153 return true;
154 }
samueljcc4a527ed2015-12-03 13:59:49 +0800155 if (extensionTreatmentType.equals(
156 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())) {
157 return true;
158 }
159 if (extensionTreatmentType.equals(
160 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())) {
161 return true;
162 }
163 if (extensionTreatmentType.equals(
164 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())) {
165 return true;
166 }
167 if (extensionTreatmentType.equals(
168 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
169 return true;
170 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530171 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_MDTYPE.type())) {
172 return true;
173 }
174 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_NP.type())) {
175 return true;
176 }
177 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_SRC.type())) {
178 return true;
179 }
180 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_DST.type())) {
181 return true;
182 }
183 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_TYPE
184 .type())) {
185 return true;
186 }
187 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_PUSH_NSH.type())) {
188 return true;
189 }
190 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_POP_NSH.type())) {
191 return true;
192 }
193 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_TUN_GPE_NP.type())) {
194 return true;
195 }
196 if (extensionTreatmentType
197 .equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C1_TO_C1.type())) {
198 return true;
199 }
200 if (extensionTreatmentType
201 .equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_C2.type())) {
202 return true;
203 }
204 if (extensionTreatmentType
205 .equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C3_TO_C3.type())) {
206 return true;
207 }
208 if (extensionTreatmentType
209 .equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C4_TO_C4.type())) {
210 return true;
211 }
212 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes
213 .NICIRA_MOV_TUN_IPV4_DST_TO_TUN_IPV4_DST.type())) {
214 return true;
215 }
216 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_TUN_ID_TO_TUN_ID
217 .type())) {
218 return true;
219 }
220 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_TUN_ID
221 .type())) {
222 return true;
223 }
Frank Wang5733c382017-03-28 10:15:18 +0800224 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type())) {
225 return true;
226 }
227 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type())) {
228 return true;
229 }
230 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT_CLEAR.type())) {
231 return true;
232 }
Jian Lide6eb182019-02-25 21:33:07 +0900233 if (extensionTreatmentType.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD.type())) {
234 return true;
235 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700236 return false;
237 }
238
239 @Override
alshabib880b6442015-11-23 22:13:04 -0800240 public OFAction mapInstruction(OFFactory factory, ExtensionTreatment extensionTreatment) {
241 ExtensionTreatmentType type = extensionTreatment.type();
242 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
243 NiciraSetTunnelDst tunnelDst = (NiciraSetTunnelDst) extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700244 return factory.actions().setField(factory.oxms().tunnelIpv4Dst(
245 IPv4Address.of(tunnelDst.tunnelDst().toInt())));
246 }
alshabib880b6442015-11-23 22:13:04 -0800247 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenry5e2233a2015-12-08 09:26:38 +0800248 NiciraResubmit resubmit = (NiciraResubmit) extensionTreatment;
249 return factory.actions().niciraResubmit((int) resubmit.inPort().toLong(),
250 resubmit.table());
BitOhenryb53ac6c2015-11-16 20:58:58 +0800251 }
BitOhenry74dd7e12015-12-01 09:07:19 +0800252 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
BitOhenry5e2233a2015-12-08 09:26:38 +0800253 NiciraResubmitTable resubmitTable = (NiciraResubmitTable) extensionTreatment;
254 return factory.actions().niciraResubmitTable((int) resubmitTable.inPort().toLong(),
255 resubmitTable.table());
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530256 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530257
258 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
259 NiciraSetNshSpi niciraNshSpi = (NiciraSetNshSpi) extensionTreatment;
260 return factory.actions().setField(factory.oxms().nsp(U32.of(niciraNshSpi.nshSpi().servicePathId())));
261 }
262 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
263 NiciraSetNshSi niciraNshSi = (NiciraSetNshSi) extensionTreatment;
264 return factory.actions().setField(factory.oxms().nsi(U8.of(niciraNshSi.nshSi().serviceIndex())));
265 }
266 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
267 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
268 return factory.actions().setField(factory.oxms().nshC1(U32.of(niciraNshch.nshCh().nshContextHeader())));
269 }
270 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
271 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
272 return factory.actions().setField(factory.oxms().nshC2(U32.of(niciraNshch.nshCh().nshContextHeader())));
273 }
274 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
275 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
276 return factory.actions().setField(factory.oxms().nshC3(U32.of(niciraNshch.nshCh().nshContextHeader())));
277 }
278 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
279 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
280 return factory.actions().setField(factory.oxms().nshC4(U32.of(niciraNshch.nshCh().nshContextHeader())));
281 }
282 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_MDTYPE.type())) {
283 NiciraNshMdType niciraNshMdType = (NiciraNshMdType) extensionTreatment;
284 return factory.actions().setField(factory.oxms().nshMdtype(U8.of(niciraNshMdType.nshMdType())));
285 }
286 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_NP.type())) {
287 NiciraNshNp niciraNshNp = (NiciraNshNp) extensionTreatment;
288 return factory.actions().setField(factory.oxms().nshNp(U8.of(niciraNshNp.nshNp())));
289 }
290 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_SRC.type())) {
291 NiciraEncapEthSrc niciraEncapEthSrc = (NiciraEncapEthSrc) extensionTreatment;
292 return factory.actions().setField(factory.oxms().encapEthSrc(MacAddress.of(niciraEncapEthSrc.encapEthSrc()
293 .toBytes())));
294 }
295 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_DST.type())) {
296 NiciraEncapEthDst niciraEncapEthDst = (NiciraEncapEthDst) extensionTreatment;
297 return factory.actions().setField(factory.oxms().encapEthDst(MacAddress.of(niciraEncapEthDst.encapEthDst()
298 .toBytes())));
299 }
300 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_TYPE.type())) {
301 NiciraEncapEthType niciraEncapEthType = (NiciraEncapEthType) extensionTreatment;
302 return factory.actions().setField(factory.oxms().encapEthType(U16.of(niciraEncapEthType.encapEthType())));
303 }
304 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_PUSH_NSH.type())) {
305 return factory.actions().niciraPushNsh();
306 }
307 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_POP_NSH.type())) {
308 return factory.actions().niciraPopNsh();
309 }
310 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_TUN_GPE_NP.type())) {
311 NiciraTunGpeNp niciraTunGpeNp = (NiciraTunGpeNp) extensionTreatment;
312 return factory.actions().setField(factory.oxms().tunGpeNp(U8.of(niciraTunGpeNp.tunGpeNp())));
313 }
samueljcc4f4d25a2015-12-07 10:52:28 +0800314 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())
samueljcc4a527ed2015-12-03 13:59:49 +0800315 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())
316 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530317 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())
318 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C1_TO_C1.type())
319 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_C2.type())
320 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C3_TO_C3.type())
321 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C4_TO_C4.type())
322 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_TUN_IPV4_DST_TO_TUN_IPV4_DST
323 .type())
324 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_TUN_ID_TO_TUN_ID.type())
325 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_TUN_ID.type())) {
samueljcc4f4d25a2015-12-07 10:52:28 +0800326 MoveExtensionTreatment mov = (MoveExtensionTreatment) extensionTreatment;
327 OFActionNiciraMove.Builder action = factory.actions()
328 .buildNiciraMove();
329 action.setDstOfs(mov.dstOffset());
330 action.setSrcOfs(mov.srcOffset());
331 action.setNBits(mov.nBits());
332 action.setSrc(mov.src());
333 action.setDst(mov.dst());
334 return action.build();
samueljcc4a527ed2015-12-03 13:59:49 +0800335 }
Jian Lide6eb182019-02-25 21:33:07 +0900336 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD.type())) {
337 NiciraLoad load = (NiciraLoad) extensionTreatment;
338 OFActionNiciraLoad.Builder action = factory.actions()
339 .buildNiciraLoad();
340 action.setOfsNbits(load.ofsNbits());
341 action.setDst(load.dst());
342 action.setValue(U64.of(load.value()));
343 return action.build();
344 }
Frank Wang5733c382017-03-28 10:15:18 +0800345 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type())) {
346 NiciraCt niciraCt = (NiciraCt) extensionTreatment;
347 OFActionNiciraCt.Builder ctAction = factory.actions().buildNiciraCt();
348 ctAction.setFlags(niciraCt.niciraCtFlags());
349 ctAction.setZoneSrc(niciraCt.niciraCtZoneSrc());
350 ctAction.setZone(niciraCt.niciraCtZone());
351 ctAction.setRecircTable(niciraCt.niciraCtRecircTable());
352 ctAction.setAlg(niciraCt.niciraCtAlg());
353
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700354 List<OFAction> actions = Lists.newArrayList();
Frank Wang5733c382017-03-28 10:15:18 +0800355 for (ExtensionTreatment nestedTreatment : niciraCt.niciraCtNestActions()) {
356 if (nestedTreatment instanceof NiciraNat) {
357 NiciraNat niciraNat = (NiciraNat) nestedTreatment;
358 OFActionNiciraNat.Builder action = factory.actions().buildNiciraNat();
359 action.setFlags(niciraNat.niciraNatFlags());
360
361 int presetFlags = niciraNat.niciraNatPresentFlags();
362 action.setRangePresent(presetFlags);
363
364 List<IPv4Address> ipv4RangeList = Lists.newArrayList();
365 List<IPv6Address> ipv6RangeList = Lists.newArrayList();
366 List<U16> portRangeList = Lists.newArrayList();
367 List<U8> padList = Lists.newArrayList();
368 if ((presetFlags & NAT_RANGE_IPV4_MIN) != 0) {
369 ipv4RangeList.add(IPv4Address.of(niciraNat.niciraNatIpAddressMin().getIp4Address().toString()));
370 }
371 if ((presetFlags & NAT_RANGE_IPV4_MAX) != 0) {
372 ipv4RangeList.add(IPv4Address.of(niciraNat.niciraNatIpAddressMax().getIp4Address().toString()));
373 }
374
375 if ((presetFlags & NAT_RANGE_IPV6_MIN) != 0) {
376 ipv6RangeList.add(IPv6Address.of(niciraNat.niciraNatIpAddressMin().getIp6Address().toString()));
377 }
378 if ((presetFlags & NAT_RANGE_IPV6_MAX) != 0) {
379 ipv6RangeList.add(IPv6Address.of(niciraNat.niciraNatIpAddressMax().getIp6Address().toString()));
380 }
381
382 if ((presetFlags & NAT_RANGE_PROTO_MIN) != 0) {
383 portRangeList.add(U16.of(niciraNat.niciraNatPortMin()));
384 }
385 if ((presetFlags & NAT_RANGE_PROTO_MAX) != 0) {
386 portRangeList.add(U16.of(niciraNat.niciraNatPortMax()));
387 }
388
389 for (; (ipv6RangeList.size() * 16 + ipv4RangeList.size() * 4
390 + portRangeList.size() * 2 + padList.size()) % 8 != 0;) {
391 padList.add(U8.ofRaw((byte) 0));
392 }
393
394 action.setIpv4Range(ipv4RangeList);
395 action.setIpv6Range(ipv6RangeList);
396 action.setPortRange(portRangeList);
397 action.setPad(padList);
398
399 //nat action must be nested in ct action
Frank Wang5733c382017-03-28 10:15:18 +0800400 actions.add(action.build());
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700401 } else if (nestedTreatment instanceof NiciraLoad) {
402 NiciraLoad niciraLoad = (NiciraLoad) nestedTreatment;
403 OFActionNiciraLoad.Builder action = factory.actions().buildNiciraLoad();
404
405 action.setDst(niciraLoad.dst());
406 action.setOfsNbits(niciraLoad.ofsNbits());
407 action.setValue(U64.of(niciraLoad.value()));
408
409 actions.add(action.build());
Frank Wang5733c382017-03-28 10:15:18 +0800410 }
411 }
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700412 ctAction.setActions(actions);
Frank Wang5733c382017-03-28 10:15:18 +0800413 return ctAction.build();
414 }
415 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT_CLEAR.type())) {
416 return factory.actions().niciraCtClear();
417 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700418 return null;
419 }
420
421 @Override
Jian Liffef5002016-04-04 23:27:37 -0700422 public ExtensionTreatment mapAction(OFAction action) throws UnsupportedOperationException {
Jonathan Hart3c259162015-10-21 21:31:19 -0700423 if (action.getType().equals(OFActionType.SET_FIELD)) {
424 OFActionSetField setFieldAction = (OFActionSetField) action;
425 OFOxm<?> oxm = setFieldAction.getField();
426 switch (oxm.getMatchField().id) {
427 case TUNNEL_IPV4_DST:
428 OFOxmTunnelIpv4Dst tunnelIpv4Dst = (OFOxmTunnelIpv4Dst) oxm;
429 return new NiciraSetTunnelDst(Ip4Address.valueOf(tunnelIpv4Dst.getValue().getInt()));
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530430 case NSP:
431 OFOxmNsp nsp = (OFOxmNsp) oxm;
432 return new NiciraSetNshSpi(NshServicePathId.of((nsp.getValue().getRaw())));
433 case NSI:
434 OFOxmNsi nsi = (OFOxmNsi) oxm;
435 return new NiciraSetNshSi(NshServiceIndex.of((nsi.getValue().getRaw())));
436 case NSH_C1:
437 OFOxmNshC1 nshC1 = (OFOxmNshC1) oxm;
438 return new NiciraSetNshContextHeader(NshContextHeader.of((nshC1.getValue().getRaw())),
439 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1
440 .type());
441 case NSH_C2:
442 OFOxmNshC2 nshC2 = (OFOxmNshC2) oxm;
443 return new NiciraSetNshContextHeader(NshContextHeader.of((nshC2.getValue().getRaw())),
444 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2
445 .type());
446 case NSH_C3:
447 OFOxmNshC3 nshC3 = (OFOxmNshC3) oxm;
448 return new NiciraSetNshContextHeader(NshContextHeader.of((nshC3.getValue().getRaw())),
449 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3
450 .type());
451 case NSH_C4:
452 OFOxmNshC4 nshC4 = (OFOxmNshC4) oxm;
453 return new NiciraSetNshContextHeader(NshContextHeader.of((nshC4.getValue().getRaw())),
454 ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4
455 .type());
456 case NSH_MDTYPE:
457 OFOxmNshMdtype nshMdType = (OFOxmNshMdtype) oxm;
458 return new NiciraNshMdType((nshMdType.getValue().getRaw()));
459 case NSH_NP:
460 OFOxmNshNp nshNp = (OFOxmNshNp) oxm;
461 return new NiciraNshNp((nshNp.getValue().getRaw()));
462 case ENCAP_ETH_SRC:
463 OFOxmEncapEthSrc encapEthSrc = (OFOxmEncapEthSrc) oxm;
464 return new NiciraEncapEthSrc(org.onlab.packet.MacAddress.valueOf((encapEthSrc.getValue().getBytes())));
465 case ENCAP_ETH_DST:
466 OFOxmEncapEthDst encapEthDst = (OFOxmEncapEthDst) oxm;
467 return new NiciraEncapEthDst(org.onlab.packet.MacAddress.valueOf((encapEthDst.getValue().getBytes())));
468 case ENCAP_ETH_TYPE:
469 OFOxmEncapEthType encapEthType = (OFOxmEncapEthType) oxm;
470 return new NiciraEncapEthType((encapEthType.getValue().getRaw()));
471 case TUN_GPE_NP:
472 OFOxmTunGpeNp tunGpeNp = (OFOxmTunGpeNp) oxm;
473 return new NiciraTunGpeNp((tunGpeNp.getValue().getRaw()));
Jonathan Hart3c259162015-10-21 21:31:19 -0700474 default:
475 throw new UnsupportedOperationException(
476 "Driver does not support extension type " + oxm.getMatchField().id);
477 }
478 }
samueljcc4f4d25a2015-12-07 10:52:28 +0800479 if (action.getType().equals(OFActionType.EXPERIMENTER)) {
480 OFActionExperimenter experimenter = (OFActionExperimenter) action;
481 if (Long.valueOf(experimenter.getExperimenter())
482 .intValue() == TYPE_NICIRA) {
483 OFActionNicira nicira = (OFActionNicira) experimenter;
Jian Lid4163882016-03-30 20:03:14 -0700484 switch (nicira.getSubtype()) {
485 case SUB_TYPE_MOVE:
486 OFActionNiciraMove moveAction = (OFActionNiciraMove) nicira;
487 switch (Long.valueOf(moveAction.getSrc()).intValue()) {
488 case SRC_ARP_SHA:
489 return NiciraMoveTreatmentFactory
490 .createNiciraMovArpShaToTha();
491 case SRC_ETH:
492 return NiciraMoveTreatmentFactory
493 .createNiciraMovEthSrcToDst();
494 case SRC_IP:
495 return NiciraMoveTreatmentFactory
496 .createNiciraMovIpSrcToDst();
497 case SRC_ARP_SPA:
498 return NiciraMoveTreatmentFactory
499 .createNiciraMovArpSpaToTpa();
Ray Milkeyd6a67c32018-02-02 10:30:35 -0800500 case NSH_C1:
501 return NiciraMoveTreatmentFactory.createNiciraMovNshC1ToC1();
502 case NSH_C2:
503 if (Long.valueOf(moveAction.getDst()).intValue() == TUN_ID) {
504 return NiciraMoveTreatmentFactory.createNiciraMovNshC2ToTunId();
505 }
506 return NiciraMoveTreatmentFactory.createNiciraMovNshC2ToC2();
507 case NSH_C3:
508 return NiciraMoveTreatmentFactory.createNiciraMovNshC3ToC3();
509 case NSH_C4:
510 return NiciraMoveTreatmentFactory.createNiciraMovNshC4ToC4();
511 case TUN_IPV4_DST:
512 return NiciraMoveTreatmentFactory.createNiciraMovTunDstToTunDst();
513 case TUN_ID:
514 return NiciraMoveTreatmentFactory.createNiciraMovTunIdToTunId();
Jian Lid4163882016-03-30 20:03:14 -0700515 default:
516 throw new UnsupportedOperationException("Driver does not support move from "
Ray Milkeyd6a67c32018-02-02 10:30:35 -0800517 + moveAction.getSrc() + " to " + moveAction.getDst() + "of length "
518 + moveAction.getNBits());
Jian Lid4163882016-03-30 20:03:14 -0700519 }
520 case SUB_TYPE_RESUBMIT:
521 OFActionNiciraResubmit resubmitAction = (OFActionNiciraResubmit) nicira;
522 return new NiciraResubmit(PortNumber.portNumber(resubmitAction.getInPort()));
Frank Wang5733c382017-03-28 10:15:18 +0800523 case SUB_TYPE_PUSH_NSH:
524 return new NiciraPushNsh();
525 case SUB_TYPE_POP_NSH:
526 return new NiciraPopNsh();
527 case SUB_TYPE_RESUBMIT_TABLE:
528 OFActionNiciraResubmitTable resubmitTable = (OFActionNiciraResubmitTable) nicira;
529 return new NiciraResubmitTable(PortNumber.portNumber(resubmitTable.getInPort()),
530 resubmitTable.getTable());
531 case SUB_TYPE_CT:
532 OFActionNiciraCt ctAction = (OFActionNiciraCt) nicira;
533 List<OFAction> actions = ctAction.getActions();
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700534
535 List<ExtensionTreatment> extensionActions = new ArrayList<>();
536
Frank Wang5733c382017-03-28 10:15:18 +0800537 for (OFAction act : actions) {
538 OFActionExperimenter ctExperimenter = (OFActionExperimenter) act;
539 if (Long.valueOf(ctExperimenter.getExperimenter()).intValue() == TYPE_NICIRA) {
540 OFActionNicira actionNicira = (OFActionNicira) ctExperimenter;
541 switch (actionNicira.getSubtype()) {
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700542 case SUB_TYPE_LOAD:
543 OFActionNiciraLoad loadAction = (OFActionNiciraLoad) actionNicira;
544 extensionActions.add(new NiciraLoad(loadAction.getOfsNbits(),
545 loadAction.getDst(),
546 loadAction.getValue().getValue()));
547 break;
Frank Wang5733c382017-03-28 10:15:18 +0800548 case SUB_TYPE_NAT:
549 OFActionNiciraNat natAction = (OFActionNiciraNat) actionNicira;
550 int portMin = 0;
551 int portMax = 0;
552 IpAddress ipAddressMin = IpAddress.valueOf(0);
553 IpAddress ipAddressMax = IpAddress.valueOf(0);
554 //FIXME: we need to get ipv6 and port from list<ipv4> temporarily,
555 // becase loxi don't know how to arrange these data to corresonding field.
556 IPv4Address[] arrays = (IPv4Address[]) natAction
557 .getIpv4Range().toArray(new IPv4Address[0]);
558 int index = 0;
559 if ((natAction.getRangePresent() & NAT_RANGE_IPV4_MIN) != 0) {
560 ipAddressMin = IpAddress.valueOf(arrays[index++].toString());
561 }
562 if ((natAction.getRangePresent() & NAT_RANGE_IPV4_MAX) != 0) {
563 ipAddressMax = IpAddress.valueOf(arrays[index++].toString());
564 }
565 if ((natAction.getRangePresent() & NAT_RANGE_IPV6_MIN) != 0) {
566 byte[] bytes = Bytes.concat(arrays[index++].getBytes(),
567 arrays[index++].getBytes(),
568 arrays[index++].getBytes(),
569 arrays[index++].getBytes());
570
571 ipAddressMin = IpAddress.valueOf(IpAddress.Version.INET6, bytes);
572 }
573 if ((natAction.getRangePresent() & NAT_RANGE_IPV6_MAX) != 0) {
574 byte[] bytes = Bytes.concat(arrays[index++].getBytes(),
575 arrays[index++].getBytes(),
576 arrays[index++].getBytes(),
577 arrays[index++].getBytes());
578
579 ipAddressMax = IpAddress.valueOf(IpAddress.Version.INET6, bytes);
580 }
581 if ((natAction.getRangePresent() & NAT_RANGE_PROTO_MIN) != 0) {
582 portMin = arrays[index].getInt() >> 16 & 0x0000ffff;
583 }
584 if ((natAction.getRangePresent() & NAT_RANGE_PROTO_MAX) != 0) {
585 portMax = arrays[index].getInt() & 0x0000ffff;
586 }
Frank Wang5733c382017-03-28 10:15:18 +0800587 NiciraNat natTreatment = new NiciraNat(natAction.getFlags(),
588 natAction.getRangePresent(),
589 portMin, portMax,
590 ipAddressMin, ipAddressMax);
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700591 extensionActions.add(natTreatment);
592 break;
Frank Wang5733c382017-03-28 10:15:18 +0800593 default:
594 throw new UnsupportedOperationException("Driver does not support nested" +
595 " in ct action extension subtype " + actionNicira.getSubtype());
596 }
597 }
598 }
599 return new NiciraCt(ctAction.getFlags(),
600 ctAction.getZoneSrc(),
601 ctAction.getZone(),
602 ctAction.getRecircTable(),
603 ctAction.getAlg(),
Sacheth Hegdedeccfba2019-10-15 12:00:19 -0700604 extensionActions);
Frank Wang5733c382017-03-28 10:15:18 +0800605 case SUB_TYPE_CT_CLEAR:
606 return new NiciraCtClear();
Jian Lide6eb182019-02-25 21:33:07 +0900607 case SUB_TYPE_LOAD:
608 OFActionNiciraLoad loadAction = (OFActionNiciraLoad) nicira;
609 return new NiciraLoad(loadAction.getOfsNbits(),
610 loadAction.getDst(), loadAction.getValue().getValue());
samueljcc4f4d25a2015-12-07 10:52:28 +0800611 default:
Jian Lid4163882016-03-30 20:03:14 -0700612 throw new UnsupportedOperationException("Driver does not support extension subtype "
613 + nicira.getSubtype());
samueljcc4f4d25a2015-12-07 10:52:28 +0800614 }
615 }
616 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700617 return null;
618 }
619
620 @Override
alshabib880b6442015-11-23 22:13:04 -0800621 public ExtensionTreatment getExtensionInstruction(ExtensionTreatmentType type) {
622 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700623 return new NiciraSetTunnelDst();
624 }
alshabib880b6442015-11-23 22:13:04 -0800625 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
BitOhenryb53ac6c2015-11-16 20:58:58 +0800626 return new NiciraResubmit();
627 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530628 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
629 return new NiciraResubmitTable();
630 }
Phaneendra Mandace66cbc2015-11-26 18:07:48 +0530631 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
632 return new NiciraSetNshSpi();
633 }
Phaneendra Mandaab5a7362015-12-02 01:10:01 +0530634 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
635 return new NiciraSetNshSi();
636 }
637 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())
638 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())
639 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())
640 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
641 return new NiciraSetNshContextHeader(type);
BitOhenry74dd7e12015-12-01 09:07:19 +0800642 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530643 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_PUSH_NSH.type())) {
644 return new NiciraPushNsh();
645 }
646 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_POP_NSH.type())) {
647 return new NiciraPopNsh();
648 }
649 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_MDTYPE.type())) {
650 return new NiciraNshMdType();
651 }
652 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NSH_NP.type())) {
653 return new NiciraNshNp();
654 }
655 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_SRC.type())) {
656 return new NiciraEncapEthSrc();
657 }
658 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_DST.type())) {
659 return new NiciraEncapEthDst();
660 }
661 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_ENCAP_ETH_TYPE.type())) {
662 return new NiciraEncapEthType();
663 }
samueljcc4a527ed2015-12-03 13:59:49 +0800664 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())) {
665 return NiciraMoveTreatmentFactory.createNiciraMovArpShaToTha();
666 }
667 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())) {
668 return NiciraMoveTreatmentFactory.createNiciraMovArpSpaToTpa();
669 }
670 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())) {
671 return NiciraMoveTreatmentFactory.createNiciraMovEthSrcToDst();
672 }
673 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
674 return NiciraMoveTreatmentFactory.createNiciraMovIpSrcToDst();
675 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530676 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_TUN_GPE_NP.type())) {
677 return new NiciraTunGpeNp();
678 }
679 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C1_TO_C1.type())) {
680 return NiciraMoveTreatmentFactory.createNiciraMovNshC1ToC1();
681 }
682 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_C2.type())) {
683 return NiciraMoveTreatmentFactory.createNiciraMovNshC2ToC2();
684 }
685 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C3_TO_C3.type())) {
686 return NiciraMoveTreatmentFactory.createNiciraMovNshC3ToC3();
687 }
688 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C4_TO_C4.type())) {
689 return NiciraMoveTreatmentFactory.createNiciraMovNshC4ToC4();
690 }
691 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_TUN_IPV4_DST_TO_TUN_IPV4_DST
692 .type())) {
693 return NiciraMoveTreatmentFactory.createNiciraMovTunDstToTunDst();
694 }
695 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_TUN_ID_TO_TUN_ID.type())) {
696 return NiciraMoveTreatmentFactory.createNiciraMovTunIdToTunId();
697 }
698 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_NSH_C2_TO_TUN_ID.type())) {
699 return NiciraMoveTreatmentFactory.createNiciraMovNshC2ToTunId();
700 }
Frank Wang5733c382017-03-28 10:15:18 +0800701 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT.type())) {
702 return new NiciraCt();
703 }
704 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type())) {
705 return new NiciraNat();
706 }
707 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_CT_CLEAR.type())) {
708 return new NiciraCtClear();
709 }
Jian Lide6eb182019-02-25 21:33:07 +0900710 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD.type())) {
711 return new NiciraLoad();
712 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530713 throw new UnsupportedOperationException("Driver does not support extension type " + type.toString());
Jonathan Hart3c259162015-10-21 21:31:19 -0700714 }
Jian Lidab72562016-04-12 14:10:32 -0700715
716 @Override
717 public ObjectNode encode(ExtensionTreatment extensionTreatment, CodecContext context) {
718 checkNotNull(extensionTreatment, "Extension treatment cannot be null");
719 ExtensionTreatmentType type = extensionTreatment.type();
720 ObjectNode root = context.mapper().createObjectNode();
721
722 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
723 NiciraSetTunnelDst tunnelDst = (NiciraSetTunnelDst) extensionTreatment;
724 root.set(TUNNEL_DST, context.codec(NiciraSetTunnelDst.class).encode(tunnelDst, context));
725 }
726
727 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
728 NiciraResubmit resubmit = (NiciraResubmit) extensionTreatment;
729 root.set(RESUBMIT, context.codec(NiciraResubmit.class).encode(resubmit, context));
730 }
731 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
732 NiciraResubmitTable resubmitTable = (NiciraResubmitTable) extensionTreatment;
733 root.set(RESUBMIT_TABLE, context.codec(NiciraResubmitTable.class).encode(resubmitTable, context));
734 }
735 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
736 NiciraSetNshSpi niciraNshSpi = (NiciraSetNshSpi) extensionTreatment;
737 root.set(NICIRA_NSH_SPI, context.codec(NiciraSetNshSpi.class).encode(niciraNshSpi, context));
738 }
739 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
740 NiciraSetNshSi niciraNshSi = (NiciraSetNshSi) extensionTreatment;
741 root.set(NICIRA_NSH_SI, context.codec(NiciraSetNshSi.class).encode(niciraNshSi, context));
742 }
743 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
744 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
745 root.set(NICIRA_NSH_CH, context.codec(NiciraSetNshContextHeader.class).encode(niciraNshch, context));
746 }
747 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
748 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
749 root.set(NICIRA_NSH_CH, context.codec(NiciraSetNshContextHeader.class).encode(niciraNshch, context));
750 }
751 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
752 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
753 root.set(NICIRA_NSH_CH, context.codec(NiciraSetNshContextHeader.class).encode(niciraNshch, context));
754 }
755 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
756 NiciraSetNshContextHeader niciraNshch = (NiciraSetNshContextHeader) extensionTreatment;
757 root.set(NICIRA_NSH_CH, context.codec(NiciraSetNshContextHeader.class).encode(niciraNshch, context));
758 }
759 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())
760 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())
761 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())
762 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
763 MoveExtensionTreatment mov = (MoveExtensionTreatment) extensionTreatment;
764 root.set(NICIRA_MOVE, context.codec(MoveExtensionTreatment.class).encode(mov, context));
765 }
766
767 return root;
768 }
769
770 @Override
771 public ExtensionTreatment decode(ObjectNode json, CodecContext context) {
772 if (json == null || !json.isObject()) {
773 return null;
774 }
775
776 // parse extension type
777 int typeInt = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asInt();
778 ExtensionTreatmentType type = new ExtensionTreatmentType(typeInt);
779
780 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_TUNNEL_DST.type())) {
781 return context.codec(NiciraSetTunnelDst.class).decode(json, context);
782 }
783
784 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type())) {
785 return context.codec(NiciraResubmit.class).decode(json, context);
786 }
787 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT_TABLE.type())) {
788 return context.codec(NiciraResubmitTable.class).decode(json, context);
789 }
790 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SPI.type())) {
791 return context.codec(NiciraSetNshSpi.class).decode(json, context);
792 }
793 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_SI.type())) {
794 return context.codec(NiciraSetNshSi.class).decode(json, context);
795 }
796 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH1.type())) {
797 return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
798 }
799 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH2.type())) {
800 return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
801 }
802 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH3.type())) {
803 return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
804 }
805 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_SET_NSH_CH4.type())) {
806 return context.codec(NiciraSetNshContextHeader.class).decode(json, context);
807 }
808 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SHA_TO_THA.type())
809 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ARP_SPA_TO_TPA.type())
810 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_ETH_SRC_TO_DST.type())
811 || type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_MOV_IP_SRC_TO_DST.type())) {
812 return context.codec(MoveExtensionTreatment.class).decode(json, context);
813 }
814 throw new UnsupportedOperationException(
815 "Driver does not support extension type " + type.toString());
816 }
Jonathan Hart3c259162015-10-21 21:31:19 -0700817}