blob: e63fa698574b4fcf2da710bf3be97414c3c7bbc7 [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 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800134 if (!immediateActions.isEmpty()) {
sangho1e575652015-05-14 00:39:53 -0700135 instructions.add(factory().instructions().applyActions(immediateActions));
136 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800137 if (!deferredActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800138 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()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700161 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700162 .build();
163
164 return fm;
165 }
166
167 @Override
168 public OFFlowMod buildFlowMod() {
169 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800170 List<OFAction> deferredActions = buildActions(treatment.deferred());
171 List<OFAction> immediateActions = buildActions(treatment.immediate());
172 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800173
alshabib346b5b32015-03-06 00:42:16 -0800174
Jon Hallcbd1b392017-01-18 20:15:44 -0800175 if (!immediateActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800176 instructions.add(factory().instructions().applyActions(immediateActions));
177 }
178 if (treatment.clearedDeferred()) {
179 instructions.add(factory().instructions().clearActions());
180 }
Jon Hallcbd1b392017-01-18 20:15:44 -0800181 if (!deferredActions.isEmpty()) {
alshabib346b5b32015-03-06 00:42:16 -0800182 instructions.add(factory().instructions().writeActions(deferredActions));
183 }
184 if (treatment.tableTransition() != null) {
185 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800186 }
Saurav Das86af8f12015-05-25 23:55:33 -0700187 if (treatment.writeMetadata() != null) {
188 instructions.add(buildMetadata(treatment.writeMetadata()));
189 }
alshabib10c810b2015-08-18 16:59:04 -0700190 if (treatment.metered() != null) {
191 instructions.add(buildMeter(treatment.metered()));
192 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700193
194 long cookie = flowRule().id().value();
195
Jonathan Hart86e59352014-10-22 10:42:16 -0700196 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800197 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700198 .setCookie(U64.of(cookie))
199 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800200 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700201 .setMatch(match)
202 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
203 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700204 .setTableId(TableId.of(flowRule().tableId()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700205 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700206 .build();
207
208 return fm;
209 }
210
211 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800212 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700213 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700214
215 long cookie = flowRule().id().value();
216
Jonathan Hart42feabd2016-02-03 17:56:21 -0800217 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800218 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700219 .setCookie(U64.of(cookie))
220 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700221 .setMatch(match)
222 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
223 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700224 .setTableId(TableId.of(flowRule().tableId()))
Murat Parlakisikc6759e82016-06-29 03:22:22 -0700225 .setHardTimeout(flowRule().hardTimeout())
Jonathan Hart86e59352014-10-22 10:42:16 -0700226 .build();
227
228 return fm;
229 }
230
alshabib346b5b32015-03-06 00:42:16 -0800231 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700232 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700233 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700234 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700235
236 boolean tableFound = false;
237 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800238 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700239 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700240 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700241 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800242 case L0MODIFICATION:
243 actions.add(buildL0Modification(i));
244 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200245 case L1MODIFICATION:
246 actions.add(buildL1Modification(i));
247 break;
alshabib9af70072015-02-09 14:34:16 -0800248 case L2MODIFICATION:
249 actions.add(buildL2Modification(i));
250 break;
251 case L3MODIFICATION:
252 actions.add(buildL3Modification(i));
253 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700254 case L4MODIFICATION:
255 actions.add(buildL4Modification(i));
256 break;
alshabib9af70072015-02-09 14:34:16 -0800257 case OUTPUT:
258 OutputInstruction out = (OutputInstruction) i;
259 OFActionOutput.Builder action = factory().actions().buildOutput()
260 .setPort(OFPort.of((int) out.port().toLong()));
261 if (out.port().equals(PortNumber.CONTROLLER)) {
262 action.setMaxLen(OFPCML_NO_BUFFER);
263 }
264 actions.add(action.build());
265 break;
266 case GROUP:
267 GroupInstruction group = (GroupInstruction) i;
268 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
269 .setGroup(OFGroup.of(group.groupId().id()));
270 actions.add(groupBuilder.build());
271 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200272 case QUEUE:
273 SetQueueInstruction queue = (SetQueueInstruction) i;
274 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
275 .setQueueId(queue.queueId());
276 actions.add(queueBuilder.build());
277 break;
alshabib10580802015-02-18 18:30:33 -0800278 case TABLE:
279 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800280 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800281 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700282 case EXTENSION:
283 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
284 .extensionInstruction()));
285 break;
alshabib9af70072015-02-09 14:34:16 -0800286 default:
287 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700288 }
289 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800290 if (tableFound && actions.isEmpty()) {
291 // handles the case where there are no actions, but there is
292 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700293 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800294 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700295 return actions;
296 }
297
alshabib9af70072015-02-09 14:34:16 -0800298 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
299 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700300 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800301 return instruction;
302 }
303
Saurav Das86af8f12015-05-25 23:55:33 -0700304 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
305 OFInstruction instruction = factory().instructions().writeMetadata(
306 U64.of(m.metadata()), U64.of(m.metadataMask()));
307 return instruction;
308 }
309
alshabib10c810b2015-08-18 16:59:04 -0700310 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
311 return factory().instructions().meter(metered.meterId().id());
312 }
313
314
Jonathan Hart86e59352014-10-22 10:42:16 -0700315 private OFAction buildL0Modification(Instruction i) {
316 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200317 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700318 switch (l0m.subtype()) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700319 case OCH:
320 try {
Yafit Hadar73514612015-11-04 10:14:21 +0200321 ModOchSignalInstruction modOchSignalInstruction = (ModOchSignalInstruction) l0m;
322 OchSignal signal = modOchSignalInstruction.lambda();
323 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
324 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
325 oxm = factory().oxms().expOchSigId(
326 new CircuitSignalID(gridType, channelSpacing,
327 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700328 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700329 log.warn(e.getMessage());
330 break;
331 }
Yafit Hadar73514612015-11-04 10:14:21 +0200332 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700333 default:
334 log.warn("Unimplemented action type {}.", l0m.subtype());
335 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700336 }
Yafit Hadar73514612015-11-04 10:14:21 +0200337 if (oxm != null) {
338 return factory().actions().buildSetField().setField(oxm).build();
339 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700340 return null;
341 }
342
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700343 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
344 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700345 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
346 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700347
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800348 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700349 new CircuitSignalID(gridType, channelSpacing,
350 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
351 ));
352 }
353
Yafit Hadar73514612015-11-04 10:14:21 +0200354 private OFAction buildL1Modification(Instruction i) {
355 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
356 OFOxm<?> oxm = null;
357 switch (l1m.subtype()) {
358 case ODU_SIGID:
359 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
360 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
361
362 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
363 (short) oduSignalId.tributarySlotLength(),
364 oduSignalId.tributarySlotBitmap());
365
366 oxm = factory().oxms().expOduSigId(oduSignalID);
367 break;
368 default:
369 log.warn("Unimplemented action type {}.", l1m.subtype());
370 break;
371 }
372
373 if (oxm != null) {
374 return factory().actions().buildSetField().setField(oxm).build();
375 }
376 return null;
377 }
378
Jonathan Hart86e59352014-10-22 10:42:16 -0700379 private OFAction buildL2Modification(Instruction i) {
380 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
381 ModEtherInstruction eth;
382 OFOxm<?> oxm = null;
383 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800384 case ETH_DST:
385 eth = (ModEtherInstruction) l2m;
386 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
387 break;
388 case ETH_SRC:
389 eth = (ModEtherInstruction) l2m;
390 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
391 break;
392 case VLAN_ID:
393 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
394 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
395 break;
396 case VLAN_PCP:
397 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
398 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
399 break;
400 case MPLS_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700401 ModMplsHeaderInstruction pushHeaderInstructions =
402 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800403 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700404 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800405 case MPLS_POP:
Jian Li11260a02016-05-19 13:07:22 -0700406 ModMplsHeaderInstruction popHeaderInstructions =
407 (ModMplsHeaderInstruction) l2m;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800408 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700409 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800410 case MPLS_LABEL:
411 ModMplsLabelInstruction mplsLabel =
412 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800413 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800414 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700415 case MPLS_BOS:
416 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
417 oxm = factory().oxms()
418 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
419 : OFBooleanValue.FALSE);
420 break;
sangho3f97a17d2015-01-29 22:56:29 -0800421 case DEC_MPLS_TTL:
422 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800423 case VLAN_POP:
424 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800425 case VLAN_PUSH:
Jian Li11260a02016-05-19 13:07:22 -0700426 ModVlanHeaderInstruction pushVlanInstruction = (ModVlanHeaderInstruction) l2m;
Jonathan Hart54b406b2015-03-06 16:24:14 -0800427 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700428 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700429 case TUNNEL_ID:
430 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
431 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
432 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800433 default:
434 log.warn("Unimplemented action type {}.", l2m.subtype());
435 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700436 }
437
438 if (oxm != null) {
439 return factory().actions().buildSetField().setField(oxm).build();
440 }
441 return null;
442 }
443
444 private OFAction buildL3Modification(Instruction i) {
445 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
446 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800447 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800448 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700449 OFOxm<?> oxm = null;
450 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700451 case IPV4_SRC:
452 ip = (ModIPInstruction) i;
453 ip4 = ip.ip().getIp4Address();
454 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
455 break;
456 case IPV4_DST:
457 ip = (ModIPInstruction) i;
458 ip4 = ip.ip().getIp4Address();
459 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
460 break;
461 case IPV6_SRC:
462 ip = (ModIPInstruction) i;
463 ip6 = ip.ip().getIp6Address();
464 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
465 break;
466 case IPV6_DST:
467 ip = (ModIPInstruction) i;
468 ip6 = ip.ip().getIp6Address();
469 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
470 break;
471 case IPV6_FLABEL:
472 ModIPv6FlowLabelInstruction flowLabelInstruction =
473 (ModIPv6FlowLabelInstruction) i;
474 int flowLabel = flowLabelInstruction.flowLabel();
475 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
476 break;
lishuai3cce60b2015-12-01 19:35:16 +0800477 case ARP_SPA:
478 ModArpIPInstruction aip = (ModArpIPInstruction) i;
479 ip4 = aip.ip().getIp4Address();
480 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
481 break;
482 case ARP_SHA:
483 ModArpEthInstruction ei = (ModArpEthInstruction) i;
484 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
485 break;
486 case ARP_OP:
487 ModArpOpInstruction oi = (ModArpOpInstruction) i;
488 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
489 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700490 case DEC_TTL:
491 return factory().actions().decNwTtl();
492 case TTL_IN:
493 return factory().actions().copyTtlIn();
494 case TTL_OUT:
495 return factory().actions().copyTtlOut();
496 default:
497 log.warn("Unimplemented action type {}.", l3m.subtype());
498 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700499 }
500
501 if (oxm != null) {
502 return factory().actions().buildSetField().setField(oxm).build();
503 }
504 return null;
505 }
506
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700507 private OFAction buildL4Modification(Instruction i) {
508 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
509 ModTransportPortInstruction tp;
510 OFOxm<?> oxm = null;
511 switch (l4m.subtype()) {
512 case TCP_SRC:
513 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700514 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700515 break;
516 case TCP_DST:
517 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700518 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700519 break;
520 case UDP_SRC:
521 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700522 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700523 break;
524 case UDP_DST:
525 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700526 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700527 break;
528 default:
529 log.warn("Unimplemented action type {}.", l4m.subtype());
530 break;
531 }
532
533 if (oxm != null) {
534 return factory().actions().buildSetField().setField(oxm).build();
535 }
536 return null;
537 }
538
alshabib880b6442015-11-23 22:13:04 -0800539 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700540 if (!driverService.isPresent()) {
541 log.error("No driver service present");
542 return null;
543 }
544 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800545 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700546 DefaultDriverHandler handler =
547 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800548 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700549 return interpreter.mapInstruction(factory(), i);
550 }
551
552 return null;
553 }
554
Jonathan Hart86e59352014-10-22 10:42:16 -0700555}