blob: abc2e604f4258a3002abbac0a1c38bc0c1357b50 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.of.flow.impl;
Jonathan Hart86e59352014-10-22 10:42:16 -070017
alshabib346b5b32015-03-06 00:42:16 -080018import com.google.common.collect.Lists;
Jonathan Hart29afca32015-01-20 18:17:39 -080019import org.onlab.packet.Ip4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080020import org.onlab.packet.Ip6Address;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070021import org.onosproject.net.OchSignal;
Yafit Hadar73514612015-11-04 10:14:21 +020022import org.onosproject.net.OduSignalId;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080023import org.onosproject.net.PortNumber;
Jonathan Hart3c259162015-10-21 21:31:19 -070024import org.onosproject.net.driver.DefaultDriverData;
25import org.onosproject.net.driver.DefaultDriverHandler;
26import org.onosproject.net.driver.Driver;
27import org.onosproject.net.driver.DriverService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.TrafficTreatment;
alshabib880b6442015-11-23 22:13:04 -080030import org.onosproject.net.flow.instructions.ExtensionTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.instructions.Instruction;
alshabib9af70072015-02-09 14:34:16 -080032import org.onosproject.net.flow.instructions.Instructions;
sangho8995ac52015-02-04 11:29:03 -080033import org.onosproject.net.flow.instructions.Instructions.GroupInstruction;
alshabib9af70072015-02-09 14:34:16 -080034import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020035import org.onosproject.net.flow.instructions.Instructions.SetQueueInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.instructions.L0ModificationInstruction;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070037import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Yafit Hadar73514612015-11-04 10:14:21 +020038import org.onosproject.net.flow.instructions.L1ModificationInstruction;
39import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.flow.instructions.L2ModificationInstruction;
41import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Saurav Das73a7dd42015-08-19 22:20:31 -070042import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
Jian Li11260a02016-05-19 13:07:22 -070043import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsHeaderInstruction;
Jonathan Hart29afca32015-01-20 18:17:39 -080044import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070045import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
Jian Li11260a02016-05-19 13:07:22 -070046import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanHeaderInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
48import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.flow.instructions.L3ModificationInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080050import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
Jonathan Hart42feabd2016-02-03 17:56:21 -080051import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080052import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
Jonathan Hart42feabd2016-02-03 17:56:21 -080053import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080054import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070055import org.onosproject.net.flow.instructions.L4ModificationInstruction;
56import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib880b6442015-11-23 22:13:04 -080057import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Thomas Vachuska95caba32016-04-04 10:42:05 -070058import org.onosproject.provider.of.flow.util.NoMappingFoundException;
59import org.onosproject.provider.of.flow.util.OpenFlowValueMapper;
Jonathan Hart86e59352014-10-22 10:42:16 -070060import org.projectfloodlight.openflow.protocol.OFFactory;
61import org.projectfloodlight.openflow.protocol.OFFlowAdd;
Jonathan Hart42feabd2016-02-03 17:56:21 -080062import org.projectfloodlight.openflow.protocol.OFFlowDeleteStrict;
Jonathan Hart86e59352014-10-22 10:42:16 -070063import org.projectfloodlight.openflow.protocol.OFFlowMod;
64import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
65import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080066import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080067import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020068import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080069import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070070import org.projectfloodlight.openflow.protocol.match.Match;
71import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
lishuai3cce60b2015-12-01 19:35:16 +080072import org.projectfloodlight.openflow.types.ArpOpcode;
Jonathan Hart86e59352014-10-22 10:42:16 -070073import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080074import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070075import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080076import org.projectfloodlight.openflow.types.IPv6Address;
77import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Thiago Santos4a69ef82018-08-21 21:18:05 -070078import org.projectfloodlight.openflow.types.IpDscp;
Jonathan Hart86e59352014-10-22 10:42:16 -070079import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070080import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070081import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080082import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070083import org.projectfloodlight.openflow.types.OFPort;
84import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Yafit Hadar73514612015-11-04 10:14:21 +020085import org.projectfloodlight.openflow.types.OduSignalID;
alshabib9af70072015-02-09 14:34:16 -080086import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070087import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080088import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070089import org.projectfloodlight.openflow.types.U64;
90import org.projectfloodlight.openflow.types.VlanPcp;
91import org.slf4j.Logger;
92import org.slf4j.LoggerFactory;
93
Jonathan Hart29afca32015-01-20 18:17:39 -080094import java.util.Collections;
95import java.util.LinkedList;
96import java.util.List;
97import java.util.Optional;
98
Jonathan Hart86e59352014-10-22 10:42:16 -070099/**
100 * Flow mod builder for OpenFlow 1.3+.
101 */
102public class FlowModBuilderVer13 extends FlowModBuilder {
103
Cem Türker3baff672017-10-12 15:09:01 +0300104 protected final Logger log = LoggerFactory.getLogger(getClass());
105 protected static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -0700106
Cem Türker3baff672017-10-12 15:09:01 +0300107 protected final TrafficTreatment treatment;
Jonathan Hart86e59352014-10-22 10:42:16 -0700108
109 /**
110 * Constructor for a flow mod builder for OpenFlow 1.3.
111 *
112 * @param flowRule the flow rule to transform into a flow mod
113 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800114 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800115 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700116 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700117 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
118 Optional<DriverService> driverService) {
119 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700120
121 this.treatment = flowRule.treatment();
122 }
123
124 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800125 public OFFlowMod buildFlowAdd() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700126 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800127 List<OFAction> deferredActions = buildActions(treatment.deferred());
128 List<OFAction> immediateActions = buildActions(treatment.immediate());
129 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800130
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700131
alshabib346b5b32015-03-06 00:42:16 -0800132 if (treatment.clearedDeferred()) {
133 instructions.add(factory().instructions().clearActions());
134 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800135 if (!immediateActions.isEmpty()) {
sangho1e575652015-05-14 00:39:53 -0700136 instructions.add(factory().instructions().applyActions(immediateActions));
137 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800138 if (!deferredActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800139 instructions.add(factory().instructions().writeActions(deferredActions));
140 }
141 if (treatment.tableTransition() != null) {
142 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800143 }
Saurav Das86af8f12015-05-25 23:55:33 -0700144 if (treatment.writeMetadata() != null) {
145 instructions.add(buildMetadata(treatment.writeMetadata()));
146 }
chengfane264bef2015-09-28 08:38:19 +0800147 if (treatment.metered() != null) {
148 instructions.add(buildMeter(treatment.metered()));
149 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700150
151 long cookie = flowRule().id().value();
152
Jonathan Hart86e59352014-10-22 10:42:16 -0700153 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800154 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700155 .setCookie(U64.of(cookie))
156 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800157 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700158 .setMatch(match)
159 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
160 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700161 .setTableId(TableId.of(flowRule().tableId()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700162 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700163 .build();
164
165 return fm;
166 }
167
168 @Override
169 public OFFlowMod buildFlowMod() {
170 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800171 List<OFAction> deferredActions = buildActions(treatment.deferred());
172 List<OFAction> immediateActions = buildActions(treatment.immediate());
173 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800174
alshabib346b5b32015-03-06 00:42:16 -0800175
Jon Hallcbd1b392017-01-18 20:15:44 -0800176 if (!immediateActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800177 instructions.add(factory().instructions().applyActions(immediateActions));
178 }
179 if (treatment.clearedDeferred()) {
180 instructions.add(factory().instructions().clearActions());
181 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800182 if (!deferredActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800183 instructions.add(factory().instructions().writeActions(deferredActions));
184 }
185 if (treatment.tableTransition() != null) {
186 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800187 }
Saurav Das86af8f12015-05-25 23:55:33 -0700188 if (treatment.writeMetadata() != null) {
189 instructions.add(buildMetadata(treatment.writeMetadata()));
190 }
alshabib10c810b2015-08-18 16:59:04 -0700191 if (treatment.metered() != null) {
192 instructions.add(buildMeter(treatment.metered()));
193 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700194
195 long cookie = flowRule().id().value();
196
Jonathan Hart86e59352014-10-22 10:42:16 -0700197 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800198 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700199 .setCookie(U64.of(cookie))
200 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800201 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700202 .setMatch(match)
203 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
204 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700205 .setTableId(TableId.of(flowRule().tableId()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700206 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700207 .build();
208
209 return fm;
210 }
211
212 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800213 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700214 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700215
216 long cookie = flowRule().id().value();
217
Jonathan Hart42feabd2016-02-03 17:56:21 -0800218 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800219 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700220 .setCookie(U64.of(cookie))
221 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700222 .setMatch(match)
223 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
224 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700225 .setTableId(TableId.of(flowRule().tableId()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700226 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700227 .build();
228
229 return fm;
230 }
231
alshabib346b5b32015-03-06 00:42:16 -0800232 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700233 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700234 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700235 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700236
237 boolean tableFound = false;
238 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800239 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700240 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700241 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700242 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800243 case L0MODIFICATION:
244 actions.add(buildL0Modification(i));
245 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200246 case L1MODIFICATION:
247 actions.add(buildL1Modification(i));
248 break;
alshabib9af70072015-02-09 14:34:16 -0800249 case L2MODIFICATION:
250 actions.add(buildL2Modification(i));
251 break;
252 case L3MODIFICATION:
253 actions.add(buildL3Modification(i));
254 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700255 case L4MODIFICATION:
256 actions.add(buildL4Modification(i));
257 break;
alshabib9af70072015-02-09 14:34:16 -0800258 case OUTPUT:
259 OutputInstruction out = (OutputInstruction) i;
260 OFActionOutput.Builder action = factory().actions().buildOutput()
261 .setPort(OFPort.of((int) out.port().toLong()));
262 if (out.port().equals(PortNumber.CONTROLLER)) {
263 action.setMaxLen(OFPCML_NO_BUFFER);
264 }
265 actions.add(action.build());
266 break;
267 case GROUP:
268 GroupInstruction group = (GroupInstruction) i;
269 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
270 .setGroup(OFGroup.of(group.groupId().id()));
271 actions.add(groupBuilder.build());
272 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200273 case QUEUE:
274 SetQueueInstruction queue = (SetQueueInstruction) i;
275 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
276 .setQueueId(queue.queueId());
277 actions.add(queueBuilder.build());
278 break;
alshabib10580802015-02-18 18:30:33 -0800279 case TABLE:
280 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800281 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800282 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700283 case EXTENSION:
284 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
285 .extensionInstruction()));
286 break;
alshabib9af70072015-02-09 14:34:16 -0800287 default:
288 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700289 }
290 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800291 if (tableFound && actions.isEmpty()) {
292 // handles the case where there are no actions, but there is
293 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700294 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800295 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700296 return actions;
297 }
298
Cem Türker3baff672017-10-12 15:09:01 +0300299 protected OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
alshabib9af70072015-02-09 14:34:16 -0800300 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700301 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800302 return instruction;
303 }
304
Cem Türker3baff672017-10-12 15:09:01 +0300305 protected OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
Saurav Das86af8f12015-05-25 23:55:33 -0700306 OFInstruction instruction = factory().instructions().writeMetadata(
307 U64.of(m.metadata()), U64.of(m.metadataMask()));
308 return instruction;
309 }
310
Cem Türker3baff672017-10-12 15:09:01 +0300311 protected OFInstruction buildMeter(Instructions.MeterInstruction metered) {
alshabib10c810b2015-08-18 16:59:04 -0700312 return factory().instructions().meter(metered.meterId().id());
313 }
314
315
Cem Türker3baff672017-10-12 15:09:01 +0300316 protected OFAction buildL0Modification(Instruction i) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700317 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200318 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700319 switch (l0m.subtype()) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700320 case OCH:
321 try {
Yafit Hadar73514612015-11-04 10:14:21 +0200322 ModOchSignalInstruction modOchSignalInstruction = (ModOchSignalInstruction) l0m;
323 OchSignal signal = modOchSignalInstruction.lambda();
324 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
325 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
326 oxm = factory().oxms().expOchSigId(
327 new CircuitSignalID(gridType, channelSpacing,
328 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700329 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700330 log.warn(e.getMessage());
331 break;
332 }
Yafit Hadar73514612015-11-04 10:14:21 +0200333 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700334 default:
335 log.warn("Unimplemented action type {}.", l0m.subtype());
336 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700337 }
Yafit Hadar73514612015-11-04 10:14:21 +0200338 if (oxm != null) {
339 return factory().actions().buildSetField().setField(oxm).build();
340 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700341 return null;
342 }
343
Cem Türker3baff672017-10-12 15:09:01 +0300344 protected OFAction buildL1Modification(Instruction i) {
Yafit Hadar73514612015-11-04 10:14:21 +0200345 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
346 OFOxm<?> oxm = null;
347 switch (l1m.subtype()) {
348 case ODU_SIGID:
349 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
350 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
351
352 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
353 (short) oduSignalId.tributarySlotLength(),
354 oduSignalId.tributarySlotBitmap());
355
356 oxm = factory().oxms().expOduSigId(oduSignalID);
357 break;
358 default:
359 log.warn("Unimplemented action type {}.", l1m.subtype());
360 break;
361 }
362
363 if (oxm != null) {
364 return factory().actions().buildSetField().setField(oxm).build();
365 }
366 return null;
367 }
368
Cem Türker3baff672017-10-12 15:09:01 +0300369 protected OFAction buildL2Modification(Instruction i) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700370 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
371 ModEtherInstruction eth;
372 OFOxm<?> oxm = null;
373 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800374 case ETH_DST:
375 eth = (ModEtherInstruction) l2m;
376 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
377 break;
378 case ETH_SRC:
379 eth = (ModEtherInstruction) l2m;
380 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
381 break;
382 case VLAN_ID:
383 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
384 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
385 break;
386 case VLAN_PCP:
387 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
388 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
389 break;
390 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700391 ModMplsHeaderInstruction pushHeaderInstructions =
392 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800393 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700394 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800395 case MPLS_POP:
Jian Li11260a02016-05-19 13:07:22 -0700396 ModMplsHeaderInstruction popHeaderInstructions =
397 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800398 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700399 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800400 case MPLS_LABEL:
401 ModMplsLabelInstruction mplsLabel =
402 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800403 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800404 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700405 case MPLS_BOS:
406 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
407 oxm = factory().oxms()
408 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
409 : OFBooleanValue.FALSE);
410 break;
sangho3f97a17d2015-01-29 22:56:29 -0800411 case DEC_MPLS_TTL:
412 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800413 case VLAN_POP:
414 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800415 case VLAN_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700416 ModVlanHeaderInstruction pushVlanInstruction = (ModVlanHeaderInstruction) l2m;
Jonathan Hart54b406b2015-03-06 16:24:14 -0800417 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700418 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700419 case TUNNEL_ID:
420 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
421 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
422 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800423 default:
424 log.warn("Unimplemented action type {}.", l2m.subtype());
425 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700426 }
427
428 if (oxm != null) {
429 return factory().actions().buildSetField().setField(oxm).build();
430 }
431 return null;
432 }
433
Cem Türker3baff672017-10-12 15:09:01 +0300434 protected OFAction buildL3Modification(Instruction i) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700435 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
436 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800437 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800438 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700439 OFOxm<?> oxm = null;
440 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700441 case IPV4_SRC:
442 ip = (ModIPInstruction) i;
443 ip4 = ip.ip().getIp4Address();
444 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
445 break;
446 case IPV4_DST:
447 ip = (ModIPInstruction) i;
448 ip4 = ip.ip().getIp4Address();
449 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
450 break;
Thiago Santos4a69ef82018-08-21 21:18:05 -0700451 case IP_DSCP:
452 L3ModificationInstruction.ModDscpInstruction dscp = (L3ModificationInstruction.ModDscpInstruction) i;
453 IpDscp ipDscp = IpDscp.of(dscp.dscp());
454 oxm = factory().oxms().ipDscp(ipDscp);
455 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700456 case IPV6_SRC:
457 ip = (ModIPInstruction) i;
458 ip6 = ip.ip().getIp6Address();
459 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
460 break;
461 case IPV6_DST:
462 ip = (ModIPInstruction) i;
463 ip6 = ip.ip().getIp6Address();
464 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
465 break;
466 case IPV6_FLABEL:
467 ModIPv6FlowLabelInstruction flowLabelInstruction =
468 (ModIPv6FlowLabelInstruction) i;
469 int flowLabel = flowLabelInstruction.flowLabel();
470 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
471 break;
lishuai3cce60b2015-12-01 19:35:16 +0800472 case ARP_SPA:
473 ModArpIPInstruction aip = (ModArpIPInstruction) i;
474 ip4 = aip.ip().getIp4Address();
475 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
476 break;
477 case ARP_SHA:
478 ModArpEthInstruction ei = (ModArpEthInstruction) i;
479 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
480 break;
481 case ARP_OP:
482 ModArpOpInstruction oi = (ModArpOpInstruction) i;
483 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
484 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700485 case DEC_TTL:
486 return factory().actions().decNwTtl();
487 case TTL_IN:
488 return factory().actions().copyTtlIn();
489 case TTL_OUT:
490 return factory().actions().copyTtlOut();
491 default:
492 log.warn("Unimplemented action type {}.", l3m.subtype());
493 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700494 }
495
496 if (oxm != null) {
497 return factory().actions().buildSetField().setField(oxm).build();
498 }
499 return null;
500 }
501
Cem Türker3baff672017-10-12 15:09:01 +0300502 protected OFAction buildL4Modification(Instruction i) {
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700503 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
504 ModTransportPortInstruction tp;
505 OFOxm<?> oxm = null;
506 switch (l4m.subtype()) {
507 case TCP_SRC:
508 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700509 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700510 break;
511 case TCP_DST:
512 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700513 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700514 break;
515 case UDP_SRC:
516 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700517 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700518 break;
519 case UDP_DST:
520 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700521 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700522 break;
523 default:
524 log.warn("Unimplemented action type {}.", l4m.subtype());
525 break;
526 }
527
528 if (oxm != null) {
529 return factory().actions().buildSetField().setField(oxm).build();
530 }
531 return null;
532 }
533
Cem Türker3baff672017-10-12 15:09:01 +0300534 protected OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700535 if (!driverService.isPresent()) {
536 log.error("No driver service present");
537 return null;
538 }
539 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800540 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700541 DefaultDriverHandler handler =
542 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800543 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700544 return interpreter.mapInstruction(factory(), i);
545 }
546
547 return null;
548 }
549
Jonathan Hart86e59352014-10-22 10:42:16 -0700550}