blob: cafa7dffcecc34d71225587ac54d160b3183d1c5 [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;
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;
Thomas Vachuska95caba32016-04-04 10:42:05 -070057import org.onosproject.provider.of.flow.util.NoMappingFoundException;
58import org.onosproject.provider.of.flow.util.OpenFlowValueMapper;
Jonathan Hart86e59352014-10-22 10:42:16 -070059import org.projectfloodlight.openflow.protocol.OFFactory;
60import org.projectfloodlight.openflow.protocol.OFFlowAdd;
Jonathan Hart42feabd2016-02-03 17:56:21 -080061import org.projectfloodlight.openflow.protocol.OFFlowDeleteStrict;
Jonathan Hart86e59352014-10-22 10:42:16 -070062import org.projectfloodlight.openflow.protocol.OFFlowMod;
63import org.projectfloodlight.openflow.protocol.OFFlowModFlags;
64import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho8995ac52015-02-04 11:29:03 -080065import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
Charles M.C. Chanfe421812015-01-12 18:20:51 +080066import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020067import org.projectfloodlight.openflow.protocol.action.OFActionSetQueue;
alshabib9af70072015-02-09 14:34:16 -080068import org.projectfloodlight.openflow.protocol.instruction.OFInstruction;
Jonathan Hart86e59352014-10-22 10:42:16 -070069import org.projectfloodlight.openflow.protocol.match.Match;
70import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
lishuai3cce60b2015-12-01 19:35:16 +080071import org.projectfloodlight.openflow.types.ArpOpcode;
Jonathan Hart86e59352014-10-22 10:42:16 -070072import org.projectfloodlight.openflow.types.CircuitSignalID;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080073import org.projectfloodlight.openflow.types.EthType;
Jonathan Hart86e59352014-10-22 10:42:16 -070074import org.projectfloodlight.openflow.types.IPv4Address;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080075import org.projectfloodlight.openflow.types.IPv6Address;
76import org.projectfloodlight.openflow.types.IPv6FlowLabel;
Jonathan Hart86e59352014-10-22 10:42:16 -070077import org.projectfloodlight.openflow.types.MacAddress;
Saurav Das73a7dd42015-08-19 22:20:31 -070078import org.projectfloodlight.openflow.types.OFBooleanValue;
Jonathan Hart86e59352014-10-22 10:42:16 -070079import org.projectfloodlight.openflow.types.OFBufferId;
sangho8995ac52015-02-04 11:29:03 -080080import org.projectfloodlight.openflow.types.OFGroup;
Jonathan Hart86e59352014-10-22 10:42:16 -070081import org.projectfloodlight.openflow.types.OFPort;
82import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Yafit Hadar73514612015-11-04 10:14:21 +020083import org.projectfloodlight.openflow.types.OduSignalID;
alshabib9af70072015-02-09 14:34:16 -080084import org.projectfloodlight.openflow.types.TableId;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070085import org.projectfloodlight.openflow.types.TransportPort;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080086import org.projectfloodlight.openflow.types.U32;
Jonathan Hart86e59352014-10-22 10:42:16 -070087import org.projectfloodlight.openflow.types.U64;
88import org.projectfloodlight.openflow.types.VlanPcp;
89import org.slf4j.Logger;
90import org.slf4j.LoggerFactory;
91
Jonathan Hart29afca32015-01-20 18:17:39 -080092import java.util.Collections;
93import java.util.LinkedList;
94import java.util.List;
95import java.util.Optional;
96
Jonathan Hart86e59352014-10-22 10:42:16 -070097/**
98 * Flow mod builder for OpenFlow 1.3+.
99 */
100public class FlowModBuilderVer13 extends FlowModBuilder {
101
alshabib10580802015-02-18 18:30:33 -0800102 private final Logger log = LoggerFactory.getLogger(getClass());
Charles M.C. Chanfe421812015-01-12 18:20:51 +0800103 private static final int OFPCML_NO_BUFFER = 0xffff;
Jonathan Hart86e59352014-10-22 10:42:16 -0700104
105 private final TrafficTreatment treatment;
106
107 /**
108 * Constructor for a flow mod builder for OpenFlow 1.3.
109 *
110 * @param flowRule the flow rule to transform into a flow mod
111 * @param factory the OpenFlow factory to use to build the flow mod
Pavlin Radoslavov119fd5c2014-11-25 19:08:19 -0800112 * @param xid the transaction ID
Charles Chan30ba4002015-11-05 14:45:16 -0800113 * @param driverService the device driver service
Jonathan Hart86e59352014-10-22 10:42:16 -0700114 */
Jonathan Hart3c259162015-10-21 21:31:19 -0700115 protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid,
116 Optional<DriverService> driverService) {
117 super(flowRule, factory, xid, driverService);
Jonathan Hart86e59352014-10-22 10:42:16 -0700118
119 this.treatment = flowRule.treatment();
120 }
121
122 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800123 public OFFlowMod buildFlowAdd() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700124 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800125 List<OFAction> deferredActions = buildActions(treatment.deferred());
126 List<OFAction> immediateActions = buildActions(treatment.immediate());
127 List<OFInstruction> instructions = Lists.newLinkedList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800128
Jonathan Hartd4a8bba2014-10-28 12:44:20 -0700129
alshabib346b5b32015-03-06 00:42:16 -0800130 if (treatment.clearedDeferred()) {
131 instructions.add(factory().instructions().clearActions());
132 }
sangho1e575652015-05-14 00:39:53 -0700133 if (immediateActions.size() > 0) {
134 instructions.add(factory().instructions().applyActions(immediateActions));
135 }
alshabib346b5b32015-03-06 00:42:16 -0800136 if (deferredActions.size() > 0) {
137 instructions.add(factory().instructions().writeActions(deferredActions));
138 }
139 if (treatment.tableTransition() != null) {
140 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800141 }
Saurav Das86af8f12015-05-25 23:55:33 -0700142 if (treatment.writeMetadata() != null) {
143 instructions.add(buildMetadata(treatment.writeMetadata()));
144 }
chengfane264bef2015-09-28 08:38:19 +0800145 if (treatment.metered() != null) {
146 instructions.add(buildMeter(treatment.metered()));
147 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700148
149 long cookie = flowRule().id().value();
150
Jonathan Hart86e59352014-10-22 10:42:16 -0700151 OFFlowAdd fm = factory().buildFlowAdd()
Brian O'Connor427a1762014-11-19 18:40:32 -0800152 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700153 .setCookie(U64.of(cookie))
154 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800155 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700156 .setMatch(match)
157 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
158 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700159 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700160 .build();
161
162 return fm;
163 }
164
165 @Override
166 public OFFlowMod buildFlowMod() {
167 Match match = buildMatch();
alshabib346b5b32015-03-06 00:42:16 -0800168 List<OFAction> deferredActions = buildActions(treatment.deferred());
169 List<OFAction> immediateActions = buildActions(treatment.immediate());
170 List<OFInstruction> instructions = Lists.newLinkedList();
Jonathan Harteda33872015-02-18 10:22:32 -0800171
alshabib346b5b32015-03-06 00:42:16 -0800172
173 if (immediateActions.size() > 0) {
174 instructions.add(factory().instructions().applyActions(immediateActions));
175 }
176 if (treatment.clearedDeferred()) {
177 instructions.add(factory().instructions().clearActions());
178 }
179 if (deferredActions.size() > 0) {
180 instructions.add(factory().instructions().writeActions(deferredActions));
181 }
182 if (treatment.tableTransition() != null) {
183 instructions.add(buildTableGoto(treatment.tableTransition()));
Saurav Dascbe6de32015-03-01 18:30:46 -0800184 }
Saurav Das86af8f12015-05-25 23:55:33 -0700185 if (treatment.writeMetadata() != null) {
186 instructions.add(buildMetadata(treatment.writeMetadata()));
187 }
alshabib10c810b2015-08-18 16:59:04 -0700188 if (treatment.metered() != null) {
189 instructions.add(buildMeter(treatment.metered()));
190 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700191
192 long cookie = flowRule().id().value();
193
Jonathan Hart86e59352014-10-22 10:42:16 -0700194 OFFlowMod fm = factory().buildFlowModify()
Brian O'Connor427a1762014-11-19 18:40:32 -0800195 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700196 .setCookie(U64.of(cookie))
197 .setBufferId(OFBufferId.NO_BUFFER)
alshabib9af70072015-02-09 14:34:16 -0800198 .setInstructions(instructions)
Jonathan Hart86e59352014-10-22 10:42:16 -0700199 .setMatch(match)
200 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
201 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700202 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700203 .build();
204
205 return fm;
206 }
207
208 @Override
Jonathan Hart42feabd2016-02-03 17:56:21 -0800209 public OFFlowMod buildFlowDel() {
Jonathan Hart86e59352014-10-22 10:42:16 -0700210 Match match = buildMatch();
Jonathan Hart86e59352014-10-22 10:42:16 -0700211
212 long cookie = flowRule().id().value();
213
Jonathan Hart42feabd2016-02-03 17:56:21 -0800214 OFFlowDeleteStrict fm = factory().buildFlowDeleteStrict()
Brian O'Connor427a1762014-11-19 18:40:32 -0800215 .setXid(xid)
Jonathan Hart86e59352014-10-22 10:42:16 -0700216 .setCookie(U64.of(cookie))
217 .setBufferId(OFBufferId.NO_BUFFER)
Jonathan Hart86e59352014-10-22 10:42:16 -0700218 .setMatch(match)
219 .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM))
220 .setPriority(flowRule().priority())
alshabib08d98982015-04-21 16:25:50 -0700221 .setTableId(TableId.of(flowRule().tableId()))
Jonathan Hart86e59352014-10-22 10:42:16 -0700222 .build();
223
224 return fm;
225 }
226
alshabib346b5b32015-03-06 00:42:16 -0800227 private List<OFAction> buildActions(List<Instruction> treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700228 if (treatment == null) {
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700229 return Collections.emptyList();
Jonathan Hart86e59352014-10-22 10:42:16 -0700230 }
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700231
232 boolean tableFound = false;
233 List<OFAction> actions = new LinkedList<>();
alshabib346b5b32015-03-06 00:42:16 -0800234 for (Instruction i : treatments) {
Jonathan Hart86e59352014-10-22 10:42:16 -0700235 switch (i.type()) {
Charles Chan7efabeb2015-09-28 15:12:19 -0700236 case NOACTION:
Sho SHIMIZUb35ed362015-04-28 11:05:19 -0700237 return Collections.emptyList();
alshabib9af70072015-02-09 14:34:16 -0800238 case L0MODIFICATION:
239 actions.add(buildL0Modification(i));
240 break;
Yafit Hadar73514612015-11-04 10:14:21 +0200241 case L1MODIFICATION:
242 actions.add(buildL1Modification(i));
243 break;
alshabib9af70072015-02-09 14:34:16 -0800244 case L2MODIFICATION:
245 actions.add(buildL2Modification(i));
246 break;
247 case L3MODIFICATION:
248 actions.add(buildL3Modification(i));
249 break;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700250 case L4MODIFICATION:
251 actions.add(buildL4Modification(i));
252 break;
alshabib9af70072015-02-09 14:34:16 -0800253 case OUTPUT:
254 OutputInstruction out = (OutputInstruction) i;
255 OFActionOutput.Builder action = factory().actions().buildOutput()
256 .setPort(OFPort.of((int) out.port().toLong()));
257 if (out.port().equals(PortNumber.CONTROLLER)) {
258 action.setMaxLen(OFPCML_NO_BUFFER);
259 }
260 actions.add(action.build());
261 break;
262 case GROUP:
263 GroupInstruction group = (GroupInstruction) i;
264 OFActionGroup.Builder groupBuilder = factory().actions().buildGroup()
265 .setGroup(OFGroup.of(group.groupId().id()));
266 actions.add(groupBuilder.build());
267 break;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200268 case QUEUE:
269 SetQueueInstruction queue = (SetQueueInstruction) i;
270 OFActionSetQueue.Builder queueBuilder = factory().actions().buildSetQueue()
271 .setQueueId(queue.queueId());
272 actions.add(queueBuilder.build());
273 break;
alshabib10580802015-02-18 18:30:33 -0800274 case TABLE:
275 //FIXME: should not occur here.
Saurav Dascbe6de32015-03-01 18:30:46 -0800276 tableFound = true;
alshabib10580802015-02-18 18:30:33 -0800277 break;
Jonathan Hart3c259162015-10-21 21:31:19 -0700278 case EXTENSION:
279 actions.add(buildExtensionAction(((Instructions.ExtensionInstructionWrapper) i)
280 .extensionInstruction()));
281 break;
alshabib9af70072015-02-09 14:34:16 -0800282 default:
283 log.warn("Instruction type {} not yet implemented.", i.type());
Jonathan Hart86e59352014-10-22 10:42:16 -0700284 }
285 }
Saurav Dascbe6de32015-03-01 18:30:46 -0800286 if (tableFound && actions.isEmpty()) {
287 // handles the case where there are no actions, but there is
288 // a goto instruction for the next table
Sho SHIMIZU6601dd72015-05-11 19:00:29 -0700289 return Collections.emptyList();
Saurav Dascbe6de32015-03-01 18:30:46 -0800290 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700291 return actions;
292 }
293
alshabib9af70072015-02-09 14:34:16 -0800294 private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) {
295 OFInstruction instruction = factory().instructions().gotoTable(
alshabibd17abc22015-04-21 18:26:35 -0700296 TableId.of(i.tableId()));
alshabib9af70072015-02-09 14:34:16 -0800297 return instruction;
298 }
299
Saurav Das86af8f12015-05-25 23:55:33 -0700300 private OFInstruction buildMetadata(Instructions.MetadataInstruction m) {
301 OFInstruction instruction = factory().instructions().writeMetadata(
302 U64.of(m.metadata()), U64.of(m.metadataMask()));
303 return instruction;
304 }
305
alshabib10c810b2015-08-18 16:59:04 -0700306 private OFInstruction buildMeter(Instructions.MeterInstruction metered) {
307 return factory().instructions().meter(metered.meterId().id());
308 }
309
310
Jonathan Hart86e59352014-10-22 10:42:16 -0700311 private OFAction buildL0Modification(Instruction i) {
312 L0ModificationInstruction l0m = (L0ModificationInstruction) i;
Yafit Hadar73514612015-11-04 10:14:21 +0200313 OFOxm<?> oxm = null;
Jonathan Hart86e59352014-10-22 10:42:16 -0700314 switch (l0m.subtype()) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700315 case OCH:
316 try {
Yafit Hadar73514612015-11-04 10:14:21 +0200317 ModOchSignalInstruction modOchSignalInstruction = (ModOchSignalInstruction) l0m;
318 OchSignal signal = modOchSignalInstruction.lambda();
319 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
320 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
321 oxm = factory().oxms().expOchSigId(
322 new CircuitSignalID(gridType, channelSpacing,
323 (short) signal.spacingMultiplier(), (short) signal.slotGranularity()));
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700324 } catch (NoMappingFoundException e) {
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700325 log.warn(e.getMessage());
326 break;
327 }
Yafit Hadar73514612015-11-04 10:14:21 +0200328 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700329 default:
330 log.warn("Unimplemented action type {}.", l0m.subtype());
331 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700332 }
Yafit Hadar73514612015-11-04 10:14:21 +0200333 if (oxm != null) {
334 return factory().actions().buildSetField().setField(oxm).build();
335 }
Jonathan Hart86e59352014-10-22 10:42:16 -0700336 return null;
337 }
338
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700339 private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) {
340 OchSignal signal = instruction.lambda();
Sho SHIMIZUc17042d2015-05-28 12:07:23 -0700341 byte gridType = OpenFlowValueMapper.lookupGridType(signal.gridType());
342 byte channelSpacing = OpenFlowValueMapper.lookupChannelSpacing(signal.channelSpacing());
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700343
Marc De Leenheerc2999f32015-11-20 15:52:16 -0800344 return factory().actions().circuit(factory().oxms().expOchSigId(
Sho SHIMIZUe397ffe2015-05-12 16:24:23 -0700345 new CircuitSignalID(gridType, channelSpacing,
346 (short) signal.spacingMultiplier(), (short) signal.slotGranularity())
347 ));
348 }
349
Yafit Hadar73514612015-11-04 10:14:21 +0200350 private OFAction buildL1Modification(Instruction i) {
351 L1ModificationInstruction l1m = (L1ModificationInstruction) i;
352 OFOxm<?> oxm = null;
353 switch (l1m.subtype()) {
354 case ODU_SIGID:
355 ModOduSignalIdInstruction modOduSignalIdInstruction = (ModOduSignalIdInstruction) l1m;
356 OduSignalId oduSignalId = modOduSignalIdInstruction.oduSignalId();
357
358 OduSignalID oduSignalID = new OduSignalID((short) oduSignalId.tributaryPortNumber(),
359 (short) oduSignalId.tributarySlotLength(),
360 oduSignalId.tributarySlotBitmap());
361
362 oxm = factory().oxms().expOduSigId(oduSignalID);
363 break;
364 default:
365 log.warn("Unimplemented action type {}.", l1m.subtype());
366 break;
367 }
368
369 if (oxm != null) {
370 return factory().actions().buildSetField().setField(oxm).build();
371 }
372 return null;
373 }
374
Jonathan Hart86e59352014-10-22 10:42:16 -0700375 private OFAction buildL2Modification(Instruction i) {
376 L2ModificationInstruction l2m = (L2ModificationInstruction) i;
377 ModEtherInstruction eth;
378 OFOxm<?> oxm = null;
379 switch (l2m.subtype()) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800380 case ETH_DST:
381 eth = (ModEtherInstruction) l2m;
382 oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong()));
383 break;
384 case ETH_SRC:
385 eth = (ModEtherInstruction) l2m;
386 oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong()));
387 break;
388 case VLAN_ID:
389 ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m;
390 oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort()));
391 break;
392 case VLAN_PCP:
393 ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m;
394 oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp()));
395 break;
396 case MPLS_PUSH:
397 PushHeaderInstructions pushHeaderInstructions =
398 (PushHeaderInstructions) l2m;
399 return factory().actions().pushMpls(EthType.of(pushHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700400 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800401 case MPLS_POP:
Jonathan Hart67fc0972015-03-19 15:21:20 -0700402 PushHeaderInstructions popHeaderInstructions =
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800403 (PushHeaderInstructions) l2m;
404 return factory().actions().popMpls(EthType.of(popHeaderInstructions
alshabib7b808c52015-06-26 14:22:24 -0700405 .ethernetType().toShort()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800406 case MPLS_LABEL:
407 ModMplsLabelInstruction mplsLabel =
408 (ModMplsLabelInstruction) l2m;
Ray Milkey125572b2016-02-22 16:48:17 -0800409 oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label().toInt()));
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800410 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700411 case MPLS_BOS:
412 ModMplsBosInstruction mplsBos = (ModMplsBosInstruction) l2m;
413 oxm = factory().oxms()
414 .mplsBos(mplsBos.mplsBos() ? OFBooleanValue.TRUE
415 : OFBooleanValue.FALSE);
416 break;
sangho3f97a17d2015-01-29 22:56:29 -0800417 case DEC_MPLS_TTL:
418 return factory().actions().decMplsTtl();
Saurav Dasfbe25c52015-03-04 11:12:00 -0800419 case VLAN_POP:
420 return factory().actions().popVlan();
Jonathan Hart54b406b2015-03-06 16:24:14 -0800421 case VLAN_PUSH:
422 PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m;
423 return factory().actions().pushVlan(
alshabib7b808c52015-06-26 14:22:24 -0700424 EthType.of(pushVlanInstruction.ethernetType().toShort()));
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700425 case TUNNEL_ID:
426 ModTunnelIdInstruction tunnelId = (ModTunnelIdInstruction) l2m;
427 oxm = factory().oxms().tunnelId(U64.of(tunnelId.tunnelId()));
428 break;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800429 default:
430 log.warn("Unimplemented action type {}.", l2m.subtype());
431 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700432 }
433
434 if (oxm != null) {
435 return factory().actions().buildSetField().setField(oxm).build();
436 }
437 return null;
438 }
439
440 private OFAction buildL3Modification(Instruction i) {
441 L3ModificationInstruction l3m = (L3ModificationInstruction) i;
442 ModIPInstruction ip;
Pavlin Radoslavov23e398d2014-11-05 15:17:57 -0800443 Ip4Address ip4;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800444 Ip6Address ip6;
Jonathan Hart86e59352014-10-22 10:42:16 -0700445 OFOxm<?> oxm = null;
446 switch (l3m.subtype()) {
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700447 case IPV4_SRC:
448 ip = (ModIPInstruction) i;
449 ip4 = ip.ip().getIp4Address();
450 oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt()));
451 break;
452 case IPV4_DST:
453 ip = (ModIPInstruction) i;
454 ip4 = ip.ip().getIp4Address();
455 oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt()));
456 break;
457 case IPV6_SRC:
458 ip = (ModIPInstruction) i;
459 ip6 = ip.ip().getIp6Address();
460 oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets()));
461 break;
462 case IPV6_DST:
463 ip = (ModIPInstruction) i;
464 ip6 = ip.ip().getIp6Address();
465 oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets()));
466 break;
467 case IPV6_FLABEL:
468 ModIPv6FlowLabelInstruction flowLabelInstruction =
469 (ModIPv6FlowLabelInstruction) i;
470 int flowLabel = flowLabelInstruction.flowLabel();
471 oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel));
472 break;
lishuai3cce60b2015-12-01 19:35:16 +0800473 case ARP_SPA:
474 ModArpIPInstruction aip = (ModArpIPInstruction) i;
475 ip4 = aip.ip().getIp4Address();
476 oxm = factory().oxms().arpSpa(IPv4Address.of(ip4.toInt()));
477 break;
478 case ARP_SHA:
479 ModArpEthInstruction ei = (ModArpEthInstruction) i;
480 oxm = factory().oxms().arpSha(MacAddress.of(ei.mac().toLong()));
481 break;
482 case ARP_OP:
483 ModArpOpInstruction oi = (ModArpOpInstruction) i;
484 oxm = factory().oxms().arpOp(ArpOpcode.of((int) oi.op()));
485 break;
Sho SHIMIZU5cd7ce92015-05-11 19:10:38 -0700486 case DEC_TTL:
487 return factory().actions().decNwTtl();
488 case TTL_IN:
489 return factory().actions().copyTtlIn();
490 case TTL_OUT:
491 return factory().actions().copyTtlOut();
492 default:
493 log.warn("Unimplemented action type {}.", l3m.subtype());
494 break;
Jonathan Hart86e59352014-10-22 10:42:16 -0700495 }
496
497 if (oxm != null) {
498 return factory().actions().buildSetField().setField(oxm).build();
499 }
500 return null;
501 }
502
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700503 private OFAction buildL4Modification(Instruction i) {
504 L4ModificationInstruction l4m = (L4ModificationInstruction) i;
505 ModTransportPortInstruction tp;
506 OFOxm<?> oxm = null;
507 switch (l4m.subtype()) {
508 case TCP_SRC:
509 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700510 oxm = factory().oxms().tcpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700511 break;
512 case TCP_DST:
513 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700514 oxm = factory().oxms().tcpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700515 break;
516 case UDP_SRC:
517 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700518 oxm = factory().oxms().udpSrc(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700519 break;
520 case UDP_DST:
521 tp = (ModTransportPortInstruction) l4m;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700522 oxm = factory().oxms().udpDst(TransportPort.of(tp.port().toInt()));
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700523 break;
524 default:
525 log.warn("Unimplemented action type {}.", l4m.subtype());
526 break;
527 }
528
529 if (oxm != null) {
530 return factory().actions().buildSetField().setField(oxm).build();
531 }
532 return null;
533 }
534
alshabib880b6442015-11-23 22:13:04 -0800535 private OFAction buildExtensionAction(ExtensionTreatment i) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700536 if (!driverService.isPresent()) {
537 log.error("No driver service present");
538 return null;
539 }
540 Driver driver = driverService.get().getDriver(deviceId);
alshabib880b6442015-11-23 22:13:04 -0800541 if (driver.hasBehaviour(ExtensionTreatmentInterpreter.class)) {
Jonathan Hart3c259162015-10-21 21:31:19 -0700542 DefaultDriverHandler handler =
543 new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
alshabib880b6442015-11-23 22:13:04 -0800544 ExtensionTreatmentInterpreter interpreter = handler.behaviour(ExtensionTreatmentInterpreter.class);
Jonathan Hart3c259162015-10-21 21:31:19 -0700545 return interpreter.mapInstruction(factory(), i);
546 }
547
548 return null;
549 }
550
Jonathan Hart86e59352014-10-22 10:42:16 -0700551}