blob: cd9647297867b7e776970d3b5305954d193ef09e [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
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;
Jonathan Hart86e59352014-10-22 10:42:16 -070078import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070079import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070080import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080081import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070082import org.projectfloodlight.openflow.types.OFPort;
83import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Yafit Hadar73514612015-11-04 10:14:21 +020084import org.projectfloodlight.openflow.types.OduSignalID;
alshabib9af70072015-02-09 14:34:16 -080085import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070086import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080087import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070088import org.projectfloodlight.openflow.types.U64;
89import org.projectfloodlight.openflow.types.VlanPcp;
90import org.slf4j.Logger;
91import org.slf4j.LoggerFactory;
92
Jonathan Hart29afca32015-01-20 18:17:39 -080093import java.util.Collections;
94import java.util.LinkedList;
95import java.util.List;
96import java.util.Optional;
97
Jonathan Hart86e59352014-10-22 10:42:16 -070098/**
99 * Flow mod builder for OpenFlow 1.3+.
100 */
101public class FlowModBuilderVer13 extends FlowModBuilder {
102
alshabib10580802015-02-18 18:30:33 -0800103 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +0800104 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -0700105
106 private final TrafficTreatment treatment;
107
108 /**
109 * Constructor for a flow mod builder for OpenFlow 1.3.
110 *
111 * @param flowRule the flow rule to transform into a flow mod
112 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800113 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800114 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700115 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700116 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
117 Optional<DriverService> driverService) {
118 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700119
120 this.treatment = flowRule.treatment();
121 }
122
123 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800124 public OFFlowMod buildFlowAdd() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700125 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800126 List<OFAction> deferredActions = buildActions(treatment.deferred());
127 List<OFAction> immediateActions = buildActions(treatment.immediate());
128 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800129
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700130
alshabib346b5b32015-03-06 00:42:16 -0800131 if (treatment.clearedDeferred()) {
132 instructions.add(factory().instructions().clearActions());
133 }
sangho1e575652015-05-14 00:39:53 -0700134 if (immediateActions.size() > 0) {
135 instructions.add(factory().instructions().applyActions(immediateActions));
136 }
alshabib346b5b32015-03-06 00:42:16 -0800137 if (deferredActions.size() > 0) {
138 instructions.add(factory().instructions().writeActions(deferredActions));
139 }
140 if (treatment.tableTransition() != null) {
141 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800142 }
Saurav Das86af8f12015-05-25 23:55:33 -0700143 if (treatment.writeMetadata() != null) {
144 instructions.add(buildMetadata(treatment.writeMetadata()));
145 }
chengfane264bef2015-09-28 08:38:19 +0800146 if (treatment.metered() != null) {
147 instructions.add(buildMeter(treatment.metered()));
148 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700149
150 long cookie = flowRule().id().value();
151
Jonathan Hart86e59352014-10-22 10:42:16 -0700152 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800153 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700154 .setCookie(U64.of(cookie))
155 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800156 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700157 .setMatch(match)
158 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
159 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700160 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700161 .build();
162
163 return fm;
164 }
165
166 @Override
167 public OFFlowMod buildFlowMod() {
168 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800169 List<OFAction> deferredActions = buildActions(treatment.deferred());
170 List<OFAction> immediateActions = buildActions(treatment.immediate());
171 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800172
alshabib346b5b32015-03-06 00:42:16 -0800173
174 if (immediateActions.size() > 0) {
175 instructions.add(factory().instructions().applyActions(immediateActions));
176 }
177 if (treatment.clearedDeferred()) {
178 instructions.add(factory().instructions().clearActions());
179 }
180 if (deferredActions.size() > 0) {
181 instructions.add(factory().instructions().writeActions(deferredActions));
182 }
183 if (treatment.tableTransition() != null) {
184 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800185 }
Saurav Das86af8f12015-05-25 23:55:33 -0700186 if (treatment.writeMetadata() != null) {
187 instructions.add(buildMetadata(treatment.writeMetadata()));
188 }
alshabib10c810b2015-08-18 16:59:04 -0700189 if (treatment.metered() != null) {
190 instructions.add(buildMeter(treatment.metered()));
191 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700192
193 long cookie = flowRule().id().value();
194
Jonathan Hart86e59352014-10-22 10:42:16 -0700195 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800196 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700197 .setCookie(U64.of(cookie))
198 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800199 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700200 .setMatch(match)
201 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
202 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700203 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700204 .build();
205
206 return fm;
207 }
208
209 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800210 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700211 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700212
213 long cookie = flowRule().id().value();
214
Jonathan Hart42feabd2016-02-03 17:56:21 -0800215 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800216 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700217 .setCookie(U64.of(cookie))
218 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700219 .setMatch(match)
220 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
221 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700222 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700223 .build();
224
225 return fm;
226 }
227
alshabib346b5b32015-03-06 00:42:16 -0800228 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700229 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700230 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700231 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700232
233 boolean tableFound = false;
234 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800235 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700236 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700237 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700238 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800239 case L0MODIFICATION:
240 actions.add(buildL0Modification(i));
241 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200242 case L1MODIFICATION:
243 actions.add(buildL1Modification(i));
244 break;
alshabib9af70072015-02-09 14:34:16 -0800245 case L2MODIFICATION:
246 actions.add(buildL2Modification(i));
247 break;
248 case L3MODIFICATION:
249 actions.add(buildL3Modification(i));
250 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700251 case L4MODIFICATION:
252 actions.add(buildL4Modification(i));
253 break;
alshabib9af70072015-02-09 14:34:16 -0800254 case OUTPUT:
255 OutputInstruction out = (OutputInstruction) i;
256 OFActionOutput.Builder action = factory().actions().buildOutput()
257 .setPort(OFPort.of((int) out.port().toLong()));
258 if (out.port().equals(PortNumber.CONTROLLER)) {
259 action.setMaxLen(OFPCML_NO_BUFFER);
260 }
261 actions.add(action.build());
262 break;
263 case GROUP:
264 GroupInstruction group = (GroupInstruction) i;
265 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
266 .setGroup(OFGroup.of(group.groupId().id()));
267 actions.add(groupBuilder.build());
268 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200269 case QUEUE:
270 SetQueueInstruction queue = (SetQueueInstruction) i;
271 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
272 .setQueueId(queue.queueId());
273 actions.add(queueBuilder.build());
274 break;
alshabib10580802015-02-18 18:30:33 -0800275 case TABLE:
276 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800277 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800278 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700279 case EXTENSION:
280 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
281 .extensionInstruction()));
282 break;
alshabib9af70072015-02-09 14:34:16 -0800283 default:
284 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700285 }
286 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800287 if (tableFound && actions.isEmpty()) {
288 // handles the case where there are no actions, but there is
289 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700290 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800291 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700292 return actions;
293 }
294
alshabib9af70072015-02-09 14:34:16 -0800295 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
296 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700297 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800298 return instruction;
299 }
300
Saurav Das86af8f12015-05-25 23:55:33 -0700301 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
302 OFInstruction instruction = factory().instructions().writeMetadata(
303 U64.of(m.metadata()), U64.of(m.metadataMask()));
304 return instruction;
305 }
306
alshabib10c810b2015-08-18 16:59:04 -0700307 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
308 return factory().instructions().meter(metered.meterId().id());
309 }
310
311
Jonathan Hart86e59352014-10-22 10:42:16 -0700312 private OFAction buildL0Modification(Instruction i) {
313 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200314 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700315 switch (l0m.subtype()) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700316 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 buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
341 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700342 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
343 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700344
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800345 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700346 new CircuitSignalID(gridType, channelSpacing,
347 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
348 ));
349 }
350
Yafit Hadar73514612015-11-04 10:14:21 +0200351 private OFAction buildL1Modification(Instruction i) {
352 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
353 OFOxm<?> oxm = null;
354 switch (l1m.subtype()) {
355 case ODU_SIGID:
356 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
357 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
358
359 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
360 (short) oduSignalId.tributarySlotLength(),
361 oduSignalId.tributarySlotBitmap());
362
363 oxm = factory().oxms().expOduSigId(oduSignalID);
364 break;
365 default:
366 log.warn("Unimplemented action type {}.", l1m.subtype());
367 break;
368 }
369
370 if (oxm != null) {
371 return factory().actions().buildSetField().setField(oxm).build();
372 }
373 return null;
374 }
375
Jonathan Hart86e59352014-10-22 10:42:16 -0700376 private OFAction buildL2Modification(Instruction i) {
377 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
378 ModEtherInstruction eth;
379 OFOxm<?> oxm = null;
380 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800381 case ETH_DST:
382 eth = (ModEtherInstruction) l2m;
383 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
384 break;
385 case ETH_SRC:
386 eth = (ModEtherInstruction) l2m;
387 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
388 break;
389 case VLAN_ID:
390 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
391 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
392 break;
393 case VLAN_PCP:
394 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
395 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
396 break;
397 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700398 ModMplsHeaderInstruction pushHeaderInstructions =
399 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800400 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700401 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800402 case MPLS_POP:
Jian Li11260a02016-05-19 13:07:22 -0700403 ModMplsHeaderInstruction popHeaderInstructions =
404 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800405 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700406 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800407 case MPLS_LABEL:
408 ModMplsLabelInstruction mplsLabel =
409 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800410 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800411 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700412 case MPLS_BOS:
413 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
414 oxm = factory().oxms()
415 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
416 : OFBooleanValue.FALSE);
417 break;
sangho3f97a17d2015-01-29 22:56:29 -0800418 case DEC_MPLS_TTL:
419 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800420 case VLAN_POP:
421 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800422 case VLAN_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700423 ModVlanHeaderInstruction pushVlanInstruction = (ModVlanHeaderInstruction) l2m;
Jonathan Hart54b406b2015-03-06 16:24:14 -0800424 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700425 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700426 case TUNNEL_ID:
427 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
428 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
429 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800430 default:
431 log.warn("Unimplemented action type {}.", l2m.subtype());
432 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700433 }
434
435 if (oxm != null) {
436 return factory().actions().buildSetField().setField(oxm).build();
437 }
438 return null;
439 }
440
441 private OFAction buildL3Modification(Instruction i) {
442 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
443 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800444 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800445 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700446 OFOxm<?> oxm = null;
447 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700448 case IPV4_SRC:
449 ip = (ModIPInstruction) i;
450 ip4 = ip.ip().getIp4Address();
451 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
452 break;
453 case IPV4_DST:
454 ip = (ModIPInstruction) i;
455 ip4 = ip.ip().getIp4Address();
456 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
457 break;
458 case IPV6_SRC:
459 ip = (ModIPInstruction) i;
460 ip6 = ip.ip().getIp6Address();
461 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
462 break;
463 case IPV6_DST:
464 ip = (ModIPInstruction) i;
465 ip6 = ip.ip().getIp6Address();
466 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
467 break;
468 case IPV6_FLABEL:
469 ModIPv6FlowLabelInstruction flowLabelInstruction =
470 (ModIPv6FlowLabelInstruction) i;
471 int flowLabel = flowLabelInstruction.flowLabel();
472 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
473 break;
lishuai3cce60b2015-12-01 19:35:16 +0800474 case ARP_SPA:
475 ModArpIPInstruction aip = (ModArpIPInstruction) i;
476 ip4 = aip.ip().getIp4Address();
477 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
478 break;
479 case ARP_SHA:
480 ModArpEthInstruction ei = (ModArpEthInstruction) i;
481 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
482 break;
483 case ARP_OP:
484 ModArpOpInstruction oi = (ModArpOpInstruction) i;
485 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
486 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700487 case DEC_TTL:
488 return factory().actions().decNwTtl();
489 case TTL_IN:
490 return factory().actions().copyTtlIn();
491 case TTL_OUT:
492 return factory().actions().copyTtlOut();
493 default:
494 log.warn("Unimplemented action type {}.", l3m.subtype());
495 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700496 }
497
498 if (oxm != null) {
499 return factory().actions().buildSetField().setField(oxm).build();
500 }
501 return null;
502 }
503
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700504 private OFAction buildL4Modification(Instruction i) {
505 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
506 ModTransportPortInstruction tp;
507 OFOxm<?> oxm = null;
508 switch (l4m.subtype()) {
509 case TCP_SRC:
510 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700511 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700512 break;
513 case TCP_DST:
514 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700515 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700516 break;
517 case UDP_SRC:
518 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700519 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700520 break;
521 case UDP_DST:
522 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700523 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700524 break;
525 default:
526 log.warn("Unimplemented action type {}.", l4m.subtype());
527 break;
528 }
529
530 if (oxm != null) {
531 return factory().actions().buildSetField().setField(oxm).build();
532 }
533 return null;
534 }
535
alshabib880b6442015-11-23 22:13:04 -0800536 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700537 if (!driverService.isPresent()) {
538 log.error("No driver service present");
539 return null;
540 }
541 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800542 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700543 DefaultDriverHandler handler =
544 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800545 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700546 return interpreter.mapInstruction(factory(), i);
547 }
548
549 return null;
550 }
551
Jonathan Hart86e59352014-10-22 10:42:16 -0700552}