blob: 912e91523e541eb28a978ec6bb483d1c71f71c26 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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;
37import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -070038import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Yafit Hadar73514612015-11-04 10:14:21 +020039import org.onosproject.net.flow.instructions.L1ModificationInstruction;
40import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080041import org.onosproject.net.flow.instructions.L2ModificationInstruction;
42import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction;
Saurav Das73a7dd42015-08-19 22:20:31 -070043import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsBosInstruction;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
47import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
49import 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;
Jonathan Hart86e59352014-10-22 10:42:16 -070058import org.projectfloodlight.openflow.protocol.OFFactory;
59import org.projectfloodlight.openflow.protocol.OFFlowAdd;
Jonathan Hart42feabd2016-02-03 17:56:21 -080060import org.projectfloodlight.openflow.protocol.OFFlowDeleteStrict;
Jonathan Hart86e59352014-10-22 10:42:16 -070061import org.projectfloodlight.openflow.protocol.OFFlowMod;
62import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
63import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080064import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080065import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020066import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080067import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070068import org.projectfloodlight.openflow.protocol.match.Match;
69import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
lishuai3cce60b2015-12-01 19:35:16 +080070import org.projectfloodlight.openflow.types.ArpOpcode;
Jonathan Hart86e59352014-10-22 10:42:16 -070071import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080072import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070073import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080074import org.projectfloodlight.openflow.types.IPv6Address;
75import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070076import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070077import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070078import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080079import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070080import org.projectfloodlight.openflow.types.OFPort;
81import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Yafit Hadar73514612015-11-04 10:14:21 +020082import org.projectfloodlight.openflow.types.OduSignalID;
alshabib9af70072015-02-09 14:34:16 -080083import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070084import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080085import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070086import org.projectfloodlight.openflow.types.U64;
87import org.projectfloodlight.openflow.types.VlanPcp;
88import org.slf4j.Logger;
89import org.slf4j.LoggerFactory;
90
Jonathan Hart29afca32015-01-20 18:17:39 -080091import java.util.Collections;
92import java.util.LinkedList;
93import java.util.List;
94import java.util.Optional;
95
Jonathan Hart86e59352014-10-22 10:42:16 -070096/**
97 * Flow mod builder for OpenFlow 1.3+.
98 */
99public class FlowModBuilderVer13 extends FlowModBuilder {
100
alshabib10580802015-02-18 18:30:33 -0800101 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +0800102 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -0700103
104 private final TrafficTreatment treatment;
105
106 /**
107 * Constructor for a flow mod builder for OpenFlow 1.3.
108 *
109 * @param flowRule the flow rule to transform into a flow mod
110 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800111 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800112 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700113 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700114 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
115 Optional<DriverService> driverService) {
116 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700117
118 this.treatment = flowRule.treatment();
119 }
120
121 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800122 public OFFlowMod buildFlowAdd() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700123 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800124 List<OFAction> deferredActions = buildActions(treatment.deferred());
125 List<OFAction> immediateActions = buildActions(treatment.immediate());
126 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800127
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700128
alshabib346b5b32015-03-06 00:42:16 -0800129 if (treatment.clearedDeferred()) {
130 instructions.add(factory().instructions().clearActions());
131 }
sangho1e575652015-05-14 00:39:53 -0700132 if (immediateActions.size() > 0) {
133 instructions.add(factory().instructions().applyActions(immediateActions));
134 }
alshabib346b5b32015-03-06 00:42:16 -0800135 if (deferredActions.size() > 0) {
136 instructions.add(factory().instructions().writeActions(deferredActions));
137 }
138 if (treatment.tableTransition() != null) {
139 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800140 }
Saurav Das86af8f12015-05-25 23:55:33 -0700141 if (treatment.writeMetadata() != null) {
142 instructions.add(buildMetadata(treatment.writeMetadata()));
143 }
chengfane264bef2015-09-28 08:38:19 +0800144 if (treatment.metered() != null) {
145 instructions.add(buildMeter(treatment.metered()));
146 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700147
148 long cookie = flowRule().id().value();
149
Jonathan Hart86e59352014-10-22 10:42:16 -0700150 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800151 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700152 .setCookie(U64.of(cookie))
153 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800154 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700155 .setMatch(match)
156 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
157 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700158 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700159 .build();
160
161 return fm;
162 }
163
164 @Override
165 public OFFlowMod buildFlowMod() {
166 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800167 List<OFAction> deferredActions = buildActions(treatment.deferred());
168 List<OFAction> immediateActions = buildActions(treatment.immediate());
169 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800170
alshabib346b5b32015-03-06 00:42:16 -0800171
172 if (immediateActions.size() > 0) {
173 instructions.add(factory().instructions().applyActions(immediateActions));
174 }
175 if (treatment.clearedDeferred()) {
176 instructions.add(factory().instructions().clearActions());
177 }
178 if (deferredActions.size() > 0) {
179 instructions.add(factory().instructions().writeActions(deferredActions));
180 }
181 if (treatment.tableTransition() != null) {
182 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800183 }
Saurav Das86af8f12015-05-25 23:55:33 -0700184 if (treatment.writeMetadata() != null) {
185 instructions.add(buildMetadata(treatment.writeMetadata()));
186 }
alshabib10c810b2015-08-18 16:59:04 -0700187 if (treatment.metered() != null) {
188 instructions.add(buildMeter(treatment.metered()));
189 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700190
191 long cookie = flowRule().id().value();
192
Jonathan Hart86e59352014-10-22 10:42:16 -0700193 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800194 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700195 .setCookie(U64.of(cookie))
196 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800197 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700198 .setMatch(match)
199 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
200 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700201 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700202 .build();
203
204 return fm;
205 }
206
207 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800208 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700209 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700210
211 long cookie = flowRule().id().value();
212
Jonathan Hart42feabd2016-02-03 17:56:21 -0800213 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800214 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700215 .setCookie(U64.of(cookie))
216 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700217 .setMatch(match)
218 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
219 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700220 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700221 .build();
222
223 return fm;
224 }
225
alshabib346b5b32015-03-06 00:42:16 -0800226 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700227 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700228 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700229 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700230
231 boolean tableFound = false;
232 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800233 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700234 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700235 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700236 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800237 case L0MODIFICATION:
238 actions.add(buildL0Modification(i));
239 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200240 case L1MODIFICATION:
241 actions.add(buildL1Modification(i));
242 break;
alshabib9af70072015-02-09 14:34:16 -0800243 case L2MODIFICATION:
244 actions.add(buildL2Modification(i));
245 break;
246 case L3MODIFICATION:
247 actions.add(buildL3Modification(i));
248 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700249 case L4MODIFICATION:
250 actions.add(buildL4Modification(i));
251 break;
alshabib9af70072015-02-09 14:34:16 -0800252 case OUTPUT:
253 OutputInstruction out = (OutputInstruction) i;
254 OFActionOutput.Builder action = factory().actions().buildOutput()
255 .setPort(OFPort.of((int) out.port().toLong()));
256 if (out.port().equals(PortNumber.CONTROLLER)) {
257 action.setMaxLen(OFPCML_NO_BUFFER);
258 }
259 actions.add(action.build());
260 break;
261 case GROUP:
262 GroupInstruction group = (GroupInstruction) i;
263 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
264 .setGroup(OFGroup.of(group.groupId().id()));
265 actions.add(groupBuilder.build());
266 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200267 case QUEUE:
268 SetQueueInstruction queue = (SetQueueInstruction) i;
269 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
270 .setQueueId(queue.queueId());
271 actions.add(queueBuilder.build());
272 break;
alshabib10580802015-02-18 18:30:33 -0800273 case TABLE:
274 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800275 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800276 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700277 case EXTENSION:
278 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
279 .extensionInstruction()));
280 break;
alshabib9af70072015-02-09 14:34:16 -0800281 default:
282 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700283 }
284 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800285 if (tableFound && actions.isEmpty()) {
286 // handles the case where there are no actions, but there is
287 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700288 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800289 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700290 return actions;
291 }
292
alshabib9af70072015-02-09 14:34:16 -0800293 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
294 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700295 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800296 return instruction;
297 }
298
Saurav Das86af8f12015-05-25 23:55:33 -0700299 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
300 OFInstruction instruction = factory().instructions().writeMetadata(
301 U64.of(m.metadata()), U64.of(m.metadataMask()));
302 return instruction;
303 }
304
alshabib10c810b2015-08-18 16:59:04 -0700305 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
306 return factory().instructions().meter(metered.meterId().id());
307 }
308
309
Jonathan Hart86e59352014-10-22 10:42:16 -0700310 private OFAction buildL0Modification(Instruction i) {
311 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200312 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700313 switch (l0m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700314 case LAMBDA:
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700315 return buildModLambdaInstruction((ModLambdaInstruction) i);
316 case OCH:
317 try {
Yafit Hadar73514612015-11-04 10:14:21 +0200318 ModOchSignalInstruction modOchSignalInstruction = (ModOchSignalInstruction) l0m;
319 OchSignal signal = modOchSignalInstruction.lambda();
320 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
321 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
322 oxm = factory().oxms().expOchSigId(
323 new CircuitSignalID(gridType, channelSpacing,
324 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700325 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700326 log.warn(e.getMessage());
327 break;
328 }
Yafit Hadar73514612015-11-04 10:14:21 +0200329 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700330 default:
331 log.warn("Unimplemented action type {}.", l0m.subtype());
332 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700333 }
Yafit Hadar73514612015-11-04 10:14:21 +0200334 if (oxm != null) {
335 return factory().actions().buildSetField().setField(oxm).build();
336 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700337 return null;
338 }
339
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700340 private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) {
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800341 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700342 new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1)));
343 }
344
345 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
346 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700347 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
348 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700349
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800350 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700351 new CircuitSignalID(gridType, channelSpacing,
352 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
353 ));
354 }
355
Yafit Hadar73514612015-11-04 10:14:21 +0200356 private OFAction buildL1Modification(Instruction i) {
357 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
358 OFOxm<?> oxm = null;
359 switch (l1m.subtype()) {
360 case ODU_SIGID:
361 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
362 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
363
364 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
365 (short) oduSignalId.tributarySlotLength(),
366 oduSignalId.tributarySlotBitmap());
367
368 oxm = factory().oxms().expOduSigId(oduSignalID);
369 break;
370 default:
371 log.warn("Unimplemented action type {}.", l1m.subtype());
372 break;
373 }
374
375 if (oxm != null) {
376 return factory().actions().buildSetField().setField(oxm).build();
377 }
378 return null;
379 }
380
Jonathan Hart86e59352014-10-22 10:42:16 -0700381 private OFAction buildL2Modification(Instruction i) {
382 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
383 ModEtherInstruction eth;
384 OFOxm<?> oxm = null;
385 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800386 case ETH_DST:
387 eth = (ModEtherInstruction) l2m;
388 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
389 break;
390 case ETH_SRC:
391 eth = (ModEtherInstruction) l2m;
392 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
393 break;
394 case VLAN_ID:
395 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
396 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
397 break;
398 case VLAN_PCP:
399 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
400 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
401 break;
402 case MPLS_PUSH:
403 PushHeaderInstructions pushHeaderInstructions =
404 (PushHeaderInstructions) l2m;
405 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700406 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800407 case MPLS_POP:
Jonathan Hart67fc0972015-03-19 15:21:20 -0700408 PushHeaderInstructions popHeaderInstructions =
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800409 (PushHeaderInstructions) l2m;
410 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700411 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800412 case MPLS_LABEL:
413 ModMplsLabelInstruction mplsLabel =
414 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800415 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800416 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700417 case MPLS_BOS:
418 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
419 oxm = factory().oxms()
420 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
421 : OFBooleanValue.FALSE);
422 break;
sangho3f97a17d2015-01-29 22:56:29 -0800423 case DEC_MPLS_TTL:
424 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800425 case VLAN_POP:
426 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800427 case VLAN_PUSH:
428 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
429 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700430 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700431 case TUNNEL_ID:
432 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
433 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
434 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800435 default:
436 log.warn("Unimplemented action type {}.", l2m.subtype());
437 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700438 }
439
440 if (oxm != null) {
441 return factory().actions().buildSetField().setField(oxm).build();
442 }
443 return null;
444 }
445
446 private OFAction buildL3Modification(Instruction i) {
447 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
448 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800449 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800450 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700451 OFOxm<?> oxm = null;
452 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700453 case IPV4_SRC:
454 ip = (ModIPInstruction) i;
455 ip4 = ip.ip().getIp4Address();
456 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
457 break;
458 case IPV4_DST:
459 ip = (ModIPInstruction) i;
460 ip4 = ip.ip().getIp4Address();
461 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
462 break;
463 case IPV6_SRC:
464 ip = (ModIPInstruction) i;
465 ip6 = ip.ip().getIp6Address();
466 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
467 break;
468 case IPV6_DST:
469 ip = (ModIPInstruction) i;
470 ip6 = ip.ip().getIp6Address();
471 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
472 break;
473 case IPV6_FLABEL:
474 ModIPv6FlowLabelInstruction flowLabelInstruction =
475 (ModIPv6FlowLabelInstruction) i;
476 int flowLabel = flowLabelInstruction.flowLabel();
477 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
478 break;
lishuai3cce60b2015-12-01 19:35:16 +0800479 case ARP_SPA:
480 ModArpIPInstruction aip = (ModArpIPInstruction) i;
481 ip4 = aip.ip().getIp4Address();
482 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
483 break;
484 case ARP_SHA:
485 ModArpEthInstruction ei = (ModArpEthInstruction) i;
486 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
487 break;
488 case ARP_OP:
489 ModArpOpInstruction oi = (ModArpOpInstruction) i;
490 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
491 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700492 case DEC_TTL:
493 return factory().actions().decNwTtl();
494 case TTL_IN:
495 return factory().actions().copyTtlIn();
496 case TTL_OUT:
497 return factory().actions().copyTtlOut();
498 default:
499 log.warn("Unimplemented action type {}.", l3m.subtype());
500 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700501 }
502
503 if (oxm != null) {
504 return factory().actions().buildSetField().setField(oxm).build();
505 }
506 return null;
507 }
508
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700509 private OFAction buildL4Modification(Instruction i) {
510 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
511 ModTransportPortInstruction tp;
512 OFOxm<?> oxm = null;
513 switch (l4m.subtype()) {
514 case TCP_SRC:
515 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700516 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700517 break;
518 case TCP_DST:
519 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700520 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700521 break;
522 case UDP_SRC:
523 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700524 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700525 break;
526 case UDP_DST:
527 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700528 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700529 break;
530 default:
531 log.warn("Unimplemented action type {}.", l4m.subtype());
532 break;
533 }
534
535 if (oxm != null) {
536 return factory().actions().buildSetField().setField(oxm).build();
537 }
538 return null;
539 }
540
alshabib880b6442015-11-23 22:13:04 -0800541 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700542 if (!driverService.isPresent()) {
543 log.error("No driver service present");
544 return null;
545 }
546 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800547 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700548 DefaultDriverHandler handler =
549 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800550 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700551 return interpreter.mapInstruction(factory(), i);
552 }
553
554 return null;
555 }
556
Jonathan Hart86e59352014-10-22 10:42:16 -0700557}