blob: 70b7e684609866d1c55e4537fea7d655fb8c718b [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;
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;
Jonathan Hart29afca32015-01-20 18:17:39 -080043import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction;
Jonathan Hart3c259162015-10-21 21:31:19 -070044import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModTunnelIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction;
46import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions;
48import org.onosproject.net.flow.instructions.L3ModificationInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080049import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
Jonathan Hart42feabd2016-02-03 17:56:21 -080050import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080051import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
Jonathan Hart42feabd2016-02-03 17:56:21 -080052import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080053import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070054import org.onosproject.net.flow.instructions.L4ModificationInstruction;
55import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib880b6442015-11-23 22:13:04 -080056import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
Jonathan Hart86e59352014-10-22 10:42:16 -070057import org.projectfloodlight.openflow.protocol.OFFactory;
58import org.projectfloodlight.openflow.protocol.OFFlowAdd;
Jonathan Hart42feabd2016-02-03 17:56:21 -080059import org.projectfloodlight.openflow.protocol.OFFlowDeleteStrict;
Jonathan Hart86e59352014-10-22 10:42:16 -070060import org.projectfloodlight.openflow.protocol.OFFlowMod;
61import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
62import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080063import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080064import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020065import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080066import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070067import org.projectfloodlight.openflow.protocol.match.Match;
68import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
lishuai3cce60b2015-12-01 19:35:16 +080069import org.projectfloodlight.openflow.types.ArpOpcode;
Jonathan Hart86e59352014-10-22 10:42:16 -070070import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080071import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070072import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080073import org.projectfloodlight.openflow.types.IPv6Address;
74import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070075import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070076import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070077import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080078import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070079import org.projectfloodlight.openflow.types.OFPort;
80import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Yafit Hadar73514612015-11-04 10:14:21 +020081import org.projectfloodlight.openflow.types.OduSignalID;
alshabib9af70072015-02-09 14:34:16 -080082import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070083import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080084import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070085import org.projectfloodlight.openflow.types.U64;
86import org.projectfloodlight.openflow.types.VlanPcp;
87import org.slf4j.Logger;
88import org.slf4j.LoggerFactory;
89
Jonathan Hart29afca32015-01-20 18:17:39 -080090import java.util.Collections;
91import java.util.LinkedList;
92import java.util.List;
93import java.util.Optional;
94
Jonathan Hart86e59352014-10-22 10:42:16 -070095/**
96 * Flow mod builder for OpenFlow 1.3+.
97 */
98public class FlowModBuilderVer13 extends FlowModBuilder {
99
alshabib10580802015-02-18 18:30:33 -0800100 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +0800101 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -0700102
103 private final TrafficTreatment treatment;
104
105 /**
106 * Constructor for a flow mod builder for OpenFlow 1.3.
107 *
108 * @param flowRule the flow rule to transform into a flow mod
109 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800110 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800111 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700112 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700113 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
114 Optional<DriverService> driverService) {
115 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700116
117 this.treatment = flowRule.treatment();
118 }
119
120 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800121 public OFFlowMod buildFlowAdd() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700122 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800123 List<OFAction> deferredActions = buildActions(treatment.deferred());
124 List<OFAction> immediateActions = buildActions(treatment.immediate());
125 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800126
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700127
alshabib346b5b32015-03-06 00:42:16 -0800128 if (treatment.clearedDeferred()) {
129 instructions.add(factory().instructions().clearActions());
130 }
sangho1e575652015-05-14 00:39:53 -0700131 if (immediateActions.size() > 0) {
132 instructions.add(factory().instructions().applyActions(immediateActions));
133 }
alshabib346b5b32015-03-06 00:42:16 -0800134 if (deferredActions.size() > 0) {
135 instructions.add(factory().instructions().writeActions(deferredActions));
136 }
137 if (treatment.tableTransition() != null) {
138 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800139 }
Saurav Das86af8f12015-05-25 23:55:33 -0700140 if (treatment.writeMetadata() != null) {
141 instructions.add(buildMetadata(treatment.writeMetadata()));
142 }
chengfane264bef2015-09-28 08:38:19 +0800143 if (treatment.metered() != null) {
144 instructions.add(buildMeter(treatment.metered()));
145 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700146
147 long cookie = flowRule().id().value();
148
Jonathan Hart86e59352014-10-22 10:42:16 -0700149 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800150 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700151 .setCookie(U64.of(cookie))
152 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800153 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700154 .setMatch(match)
155 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
156 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700157 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700158 .build();
159
160 return fm;
161 }
162
163 @Override
164 public OFFlowMod buildFlowMod() {
165 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800166 List<OFAction> deferredActions = buildActions(treatment.deferred());
167 List<OFAction> immediateActions = buildActions(treatment.immediate());
168 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800169
alshabib346b5b32015-03-06 00:42:16 -0800170
171 if (immediateActions.size() > 0) {
172 instructions.add(factory().instructions().applyActions(immediateActions));
173 }
174 if (treatment.clearedDeferred()) {
175 instructions.add(factory().instructions().clearActions());
176 }
177 if (deferredActions.size() > 0) {
178 instructions.add(factory().instructions().writeActions(deferredActions));
179 }
180 if (treatment.tableTransition() != null) {
181 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800182 }
Saurav Das86af8f12015-05-25 23:55:33 -0700183 if (treatment.writeMetadata() != null) {
184 instructions.add(buildMetadata(treatment.writeMetadata()));
185 }
alshabib10c810b2015-08-18 16:59:04 -0700186 if (treatment.metered() != null) {
187 instructions.add(buildMeter(treatment.metered()));
188 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700189
190 long cookie = flowRule().id().value();
191
Jonathan Hart86e59352014-10-22 10:42:16 -0700192 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800193 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700194 .setCookie(U64.of(cookie))
195 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800196 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700197 .setMatch(match)
198 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
199 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700200 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700201 .build();
202
203 return fm;
204 }
205
206 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800207 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700208 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700209
210 long cookie = flowRule().id().value();
211
Jonathan Hart42feabd2016-02-03 17:56:21 -0800212 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800213 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700214 .setCookie(U64.of(cookie))
215 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700216 .setMatch(match)
217 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
218 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700219 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700220 .build();
221
222 return fm;
223 }
224
alshabib346b5b32015-03-06 00:42:16 -0800225 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700226 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700227 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700228 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700229
230 boolean tableFound = false;
231 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800232 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700233 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700234 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700235 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800236 case L0MODIFICATION:
237 actions.add(buildL0Modification(i));
238 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200239 case L1MODIFICATION:
240 actions.add(buildL1Modification(i));
241 break;
alshabib9af70072015-02-09 14:34:16 -0800242 case L2MODIFICATION:
243 actions.add(buildL2Modification(i));
244 break;
245 case L3MODIFICATION:
246 actions.add(buildL3Modification(i));
247 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700248 case L4MODIFICATION:
249 actions.add(buildL4Modification(i));
250 break;
alshabib9af70072015-02-09 14:34:16 -0800251 case OUTPUT:
252 OutputInstruction out = (OutputInstruction) i;
253 OFActionOutput.Builder action = factory().actions().buildOutput()
254 .setPort(OFPort.of((int) out.port().toLong()));
255 if (out.port().equals(PortNumber.CONTROLLER)) {
256 action.setMaxLen(OFPCML_NO_BUFFER);
257 }
258 actions.add(action.build());
259 break;
260 case GROUP:
261 GroupInstruction group = (GroupInstruction) i;
262 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
263 .setGroup(OFGroup.of(group.groupId().id()));
264 actions.add(groupBuilder.build());
265 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200266 case QUEUE:
267 SetQueueInstruction queue = (SetQueueInstruction) i;
268 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
269 .setQueueId(queue.queueId());
270 actions.add(queueBuilder.build());
271 break;
alshabib10580802015-02-18 18:30:33 -0800272 case TABLE:
273 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800274 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800275 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700276 case EXTENSION:
277 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
278 .extensionInstruction()));
279 break;
alshabib9af70072015-02-09 14:34:16 -0800280 default:
281 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700282 }
283 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800284 if (tableFound && actions.isEmpty()) {
285 // handles the case where there are no actions, but there is
286 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700287 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800288 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700289 return actions;
290 }
291
alshabib9af70072015-02-09 14:34:16 -0800292 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
293 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700294 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800295 return instruction;
296 }
297
Saurav Das86af8f12015-05-25 23:55:33 -0700298 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
299 OFInstruction instruction = factory().instructions().writeMetadata(
300 U64.of(m.metadata()), U64.of(m.metadataMask()));
301 return instruction;
302 }
303
alshabib10c810b2015-08-18 16:59:04 -0700304 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
305 return factory().instructions().meter(metered.meterId().id());
306 }
307
308
Jonathan Hart86e59352014-10-22 10:42:16 -0700309 private OFAction buildL0Modification(Instruction i) {
310 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200311 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700312 switch (l0m.subtype()) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700313 case OCH:
314 try {
Yafit Hadar73514612015-11-04 10:14:21 +0200315 ModOchSignalInstruction modOchSignalInstruction = (ModOchSignalInstruction) l0m;
316 OchSignal signal = modOchSignalInstruction.lambda();
317 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
318 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
319 oxm = factory().oxms().expOchSigId(
320 new CircuitSignalID(gridType, channelSpacing,
321 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700322 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700323 log.warn(e.getMessage());
324 break;
325 }
Yafit Hadar73514612015-11-04 10:14:21 +0200326 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700327 default:
328 log.warn("Unimplemented action type {}.", l0m.subtype());
329 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700330 }
Yafit Hadar73514612015-11-04 10:14:21 +0200331 if (oxm != null) {
332 return factory().actions().buildSetField().setField(oxm).build();
333 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700334 return null;
335 }
336
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700337 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
338 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700339 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
340 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700341
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800342 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700343 new CircuitSignalID(gridType, channelSpacing,
344 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
345 ));
346 }
347
Yafit Hadar73514612015-11-04 10:14:21 +0200348 private OFAction buildL1Modification(Instruction i) {
349 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
350 OFOxm<?> oxm = null;
351 switch (l1m.subtype()) {
352 case ODU_SIGID:
353 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
354 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
355
356 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
357 (short) oduSignalId.tributarySlotLength(),
358 oduSignalId.tributarySlotBitmap());
359
360 oxm = factory().oxms().expOduSigId(oduSignalID);
361 break;
362 default:
363 log.warn("Unimplemented action type {}.", l1m.subtype());
364 break;
365 }
366
367 if (oxm != null) {
368 return factory().actions().buildSetField().setField(oxm).build();
369 }
370 return null;
371 }
372
Jonathan Hart86e59352014-10-22 10:42:16 -0700373 private OFAction buildL2Modification(Instruction i) {
374 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
375 ModEtherInstruction eth;
376 OFOxm<?> oxm = null;
377 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800378 case ETH_DST:
379 eth = (ModEtherInstruction) l2m;
380 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
381 break;
382 case ETH_SRC:
383 eth = (ModEtherInstruction) l2m;
384 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
385 break;
386 case VLAN_ID:
387 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
388 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
389 break;
390 case VLAN_PCP:
391 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
392 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
393 break;
394 case MPLS_PUSH:
395 PushHeaderInstructions pushHeaderInstructions =
396 (PushHeaderInstructions) l2m;
397 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700398 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800399 case MPLS_POP:
Jonathan Hart67fc0972015-03-19 15:21:20 -0700400 PushHeaderInstructions popHeaderInstructions =
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800401 (PushHeaderInstructions) l2m;
402 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700403 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800404 case MPLS_LABEL:
405 ModMplsLabelInstruction mplsLabel =
406 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800407 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800408 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700409 case MPLS_BOS:
410 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
411 oxm = factory().oxms()
412 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
413 : OFBooleanValue.FALSE);
414 break;
sangho3f97a17d2015-01-29 22:56:29 -0800415 case DEC_MPLS_TTL:
416 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800417 case VLAN_POP:
418 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800419 case VLAN_PUSH:
420 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
421 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700422 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700423 case TUNNEL_ID:
424 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
425 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
426 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800427 default:
428 log.warn("Unimplemented action type {}.", l2m.subtype());
429 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700430 }
431
432 if (oxm != null) {
433 return factory().actions().buildSetField().setField(oxm).build();
434 }
435 return null;
436 }
437
438 private OFAction buildL3Modification(Instruction i) {
439 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
440 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800441 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800442 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700443 OFOxm<?> oxm = null;
444 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700445 case IPV4_SRC:
446 ip = (ModIPInstruction) i;
447 ip4 = ip.ip().getIp4Address();
448 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
449 break;
450 case IPV4_DST:
451 ip = (ModIPInstruction) i;
452 ip4 = ip.ip().getIp4Address();
453 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
454 break;
455 case IPV6_SRC:
456 ip = (ModIPInstruction) i;
457 ip6 = ip.ip().getIp6Address();
458 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
459 break;
460 case IPV6_DST:
461 ip = (ModIPInstruction) i;
462 ip6 = ip.ip().getIp6Address();
463 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
464 break;
465 case IPV6_FLABEL:
466 ModIPv6FlowLabelInstruction flowLabelInstruction =
467 (ModIPv6FlowLabelInstruction) i;
468 int flowLabel = flowLabelInstruction.flowLabel();
469 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
470 break;
lishuai3cce60b2015-12-01 19:35:16 +0800471 case ARP_SPA:
472 ModArpIPInstruction aip = (ModArpIPInstruction) i;
473 ip4 = aip.ip().getIp4Address();
474 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
475 break;
476 case ARP_SHA:
477 ModArpEthInstruction ei = (ModArpEthInstruction) i;
478 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
479 break;
480 case ARP_OP:
481 ModArpOpInstruction oi = (ModArpOpInstruction) i;
482 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
483 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700484 case DEC_TTL:
485 return factory().actions().decNwTtl();
486 case TTL_IN:
487 return factory().actions().copyTtlIn();
488 case TTL_OUT:
489 return factory().actions().copyTtlOut();
490 default:
491 log.warn("Unimplemented action type {}.", l3m.subtype());
492 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700493 }
494
495 if (oxm != null) {
496 return factory().actions().buildSetField().setField(oxm).build();
497 }
498 return null;
499 }
500
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700501 private OFAction buildL4Modification(Instruction i) {
502 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
503 ModTransportPortInstruction tp;
504 OFOxm<?> oxm = null;
505 switch (l4m.subtype()) {
506 case TCP_SRC:
507 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700508 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700509 break;
510 case TCP_DST:
511 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700512 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700513 break;
514 case UDP_SRC:
515 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700516 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700517 break;
518 case UDP_DST:
519 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700520 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700521 break;
522 default:
523 log.warn("Unimplemented action type {}.", l4m.subtype());
524 break;
525 }
526
527 if (oxm != null) {
528 return factory().actions().buildSetField().setField(oxm).build();
529 }
530 return null;
531 }
532
alshabib880b6442015-11-23 22:13:04 -0800533 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700534 if (!driverService.isPresent()) {
535 log.error("No driver service present");
536 return null;
537 }
538 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800539 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700540 DefaultDriverHandler handler =
541 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800542 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700543 return interpreter.mapInstruction(factory(), i);
544 }
545
546 return null;
547 }
548
Jonathan Hart86e59352014-10-22 10:42:16 -0700549}