Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.of.flow.impl; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 17 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip6Address; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 21 | import org.onosproject.net.OchSignal; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 22 | import org.onosproject.net.PortNumber; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.FlowRule; |
| 24 | import org.onosproject.net.flow.TrafficTreatment; |
| 25 | import org.onosproject.net.flow.instructions.Instruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.Instructions; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.instructions.Instructions.GroupInstruction; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 28 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 30 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction; |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 33 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModEtherInstruction; |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModMplsLabelInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
| 36 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanPcpInstruction; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.PushHeaderInstructions; |
| 38 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 39 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 40 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 42 | import org.projectfloodlight.openflow.protocol.OFFlowAdd; |
| 43 | import org.projectfloodlight.openflow.protocol.OFFlowDelete; |
| 44 | import org.projectfloodlight.openflow.protocol.OFFlowMod; |
| 45 | import org.projectfloodlight.openflow.protocol.OFFlowModFlags; |
| 46 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 47 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 48 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 49 | import org.projectfloodlight.openflow.protocol.instruction.OFInstruction; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 50 | import org.projectfloodlight.openflow.protocol.match.Match; |
| 51 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 52 | import org.projectfloodlight.openflow.types.CircuitSignalID; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 53 | import org.projectfloodlight.openflow.types.EthType; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.types.IPv4Address; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 55 | import org.projectfloodlight.openflow.types.IPv6Address; |
| 56 | import org.projectfloodlight.openflow.types.IPv6FlowLabel; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 57 | import org.projectfloodlight.openflow.types.MacAddress; |
| 58 | import org.projectfloodlight.openflow.types.OFBufferId; |
sangho | 8995ac5 | 2015-02-04 11:29:03 -0800 | [diff] [blame] | 59 | import org.projectfloodlight.openflow.types.OFGroup; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 60 | import org.projectfloodlight.openflow.types.OFPort; |
| 61 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 62 | import org.projectfloodlight.openflow.types.TableId; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 63 | import org.projectfloodlight.openflow.types.U32; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 64 | import org.projectfloodlight.openflow.types.U64; |
| 65 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 66 | import org.slf4j.Logger; |
| 67 | import org.slf4j.LoggerFactory; |
| 68 | |
Jonathan Hart | 29afca3 | 2015-01-20 18:17:39 -0800 | [diff] [blame] | 69 | import java.util.Collections; |
| 70 | import java.util.LinkedList; |
| 71 | import java.util.List; |
| 72 | import java.util.Optional; |
| 73 | |
Sho SHIMIZU | c15ce51 | 2015-05-26 16:54:08 -0700 | [diff] [blame] | 74 | import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertChannelSpacing; |
| 75 | import static org.onosproject.provider.of.flow.impl.FlowModBuilderHelper.convertGridType; |
| 76 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Flow mod builder for OpenFlow 1.3+. |
| 79 | */ |
| 80 | public class FlowModBuilderVer13 extends FlowModBuilder { |
| 81 | |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 82 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Charles M.C. Chan | fe42181 | 2015-01-12 18:20:51 +0800 | [diff] [blame] | 83 | private static final int OFPCML_NO_BUFFER = 0xffff; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 84 | |
| 85 | private final TrafficTreatment treatment; |
| 86 | |
| 87 | /** |
| 88 | * Constructor for a flow mod builder for OpenFlow 1.3. |
| 89 | * |
| 90 | * @param flowRule the flow rule to transform into a flow mod |
| 91 | * @param factory the OpenFlow factory to use to build the flow mod |
Pavlin Radoslavov | 119fd5c | 2014-11-25 19:08:19 -0800 | [diff] [blame] | 92 | * @param xid the transaction ID |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 93 | */ |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 94 | protected FlowModBuilderVer13(FlowRule flowRule, OFFactory factory, Optional<Long> xid) { |
| 95 | super(flowRule, factory, xid); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 96 | |
| 97 | this.treatment = flowRule.treatment(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public OFFlowAdd buildFlowAdd() { |
| 102 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 103 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 104 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 105 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 106 | |
Jonathan Hart | d4a8bba | 2014-10-28 12:44:20 -0700 | [diff] [blame] | 107 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 108 | if (treatment.clearedDeferred()) { |
| 109 | instructions.add(factory().instructions().clearActions()); |
| 110 | } |
sangho | 1e57565 | 2015-05-14 00:39:53 -0700 | [diff] [blame] | 111 | if (immediateActions.size() > 0) { |
| 112 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 113 | } |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 114 | if (deferredActions.size() > 0) { |
| 115 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 116 | } |
| 117 | if (treatment.tableTransition() != null) { |
| 118 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 119 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame^] | 120 | if (treatment.writeMetadata() != null) { |
| 121 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 122 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 123 | |
| 124 | long cookie = flowRule().id().value(); |
| 125 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 126 | OFFlowAdd fm = factory().buildFlowAdd() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 127 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 128 | .setCookie(U64.of(cookie)) |
| 129 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 130 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 131 | .setMatch(match) |
| 132 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 133 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 134 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 135 | .build(); |
| 136 | |
| 137 | return fm; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public OFFlowMod buildFlowMod() { |
| 142 | Match match = buildMatch(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 143 | List<OFAction> deferredActions = buildActions(treatment.deferred()); |
| 144 | List<OFAction> immediateActions = buildActions(treatment.immediate()); |
| 145 | List<OFInstruction> instructions = Lists.newLinkedList(); |
Jonathan Hart | eda3387 | 2015-02-18 10:22:32 -0800 | [diff] [blame] | 146 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 147 | |
| 148 | if (immediateActions.size() > 0) { |
| 149 | instructions.add(factory().instructions().applyActions(immediateActions)); |
| 150 | } |
| 151 | if (treatment.clearedDeferred()) { |
| 152 | instructions.add(factory().instructions().clearActions()); |
| 153 | } |
| 154 | if (deferredActions.size() > 0) { |
| 155 | instructions.add(factory().instructions().writeActions(deferredActions)); |
| 156 | } |
| 157 | if (treatment.tableTransition() != null) { |
| 158 | instructions.add(buildTableGoto(treatment.tableTransition())); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 159 | } |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame^] | 160 | if (treatment.writeMetadata() != null) { |
| 161 | instructions.add(buildMetadata(treatment.writeMetadata())); |
| 162 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 163 | |
| 164 | long cookie = flowRule().id().value(); |
| 165 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 166 | OFFlowMod fm = factory().buildFlowModify() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 167 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 168 | .setCookie(U64.of(cookie)) |
| 169 | .setBufferId(OFBufferId.NO_BUFFER) |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 170 | .setInstructions(instructions) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 171 | .setMatch(match) |
| 172 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 173 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 174 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 175 | .build(); |
| 176 | |
| 177 | return fm; |
| 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public OFFlowDelete buildFlowDel() { |
| 182 | Match match = buildMatch(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 183 | |
| 184 | long cookie = flowRule().id().value(); |
| 185 | |
| 186 | OFFlowDelete fm = factory().buildFlowDelete() |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 187 | .setXid(xid) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 188 | .setCookie(U64.of(cookie)) |
| 189 | .setBufferId(OFBufferId.NO_BUFFER) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 190 | .setMatch(match) |
| 191 | .setFlags(Collections.singleton(OFFlowModFlags.SEND_FLOW_REM)) |
| 192 | .setPriority(flowRule().priority()) |
alshabib | 08d9898 | 2015-04-21 16:25:50 -0700 | [diff] [blame] | 193 | .setTableId(TableId.of(flowRule().tableId())) |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 194 | .build(); |
| 195 | |
| 196 | return fm; |
| 197 | } |
| 198 | |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 199 | private List<OFAction> buildActions(List<Instruction> treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 200 | if (treatment == null) { |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 201 | return Collections.emptyList(); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 202 | } |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 203 | |
| 204 | boolean tableFound = false; |
| 205 | List<OFAction> actions = new LinkedList<>(); |
alshabib | 346b5b3 | 2015-03-06 00:42:16 -0800 | [diff] [blame] | 206 | for (Instruction i : treatments) { |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 207 | switch (i.type()) { |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 208 | case DROP: |
Sho SHIMIZU | b35ed36 | 2015-04-28 11:05:19 -0700 | [diff] [blame] | 209 | return Collections.emptyList(); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 210 | case L0MODIFICATION: |
| 211 | actions.add(buildL0Modification(i)); |
| 212 | break; |
| 213 | case L2MODIFICATION: |
| 214 | actions.add(buildL2Modification(i)); |
| 215 | break; |
| 216 | case L3MODIFICATION: |
| 217 | actions.add(buildL3Modification(i)); |
| 218 | break; |
| 219 | case OUTPUT: |
| 220 | OutputInstruction out = (OutputInstruction) i; |
| 221 | OFActionOutput.Builder action = factory().actions().buildOutput() |
| 222 | .setPort(OFPort.of((int) out.port().toLong())); |
| 223 | if (out.port().equals(PortNumber.CONTROLLER)) { |
| 224 | action.setMaxLen(OFPCML_NO_BUFFER); |
| 225 | } |
| 226 | actions.add(action.build()); |
| 227 | break; |
| 228 | case GROUP: |
| 229 | GroupInstruction group = (GroupInstruction) i; |
| 230 | OFActionGroup.Builder groupBuilder = factory().actions().buildGroup() |
| 231 | .setGroup(OFGroup.of(group.groupId().id())); |
| 232 | actions.add(groupBuilder.build()); |
| 233 | break; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 234 | case TABLE: |
| 235 | //FIXME: should not occur here. |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 236 | tableFound = true; |
alshabib | 1058080 | 2015-02-18 18:30:33 -0800 | [diff] [blame] | 237 | break; |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 238 | default: |
| 239 | log.warn("Instruction type {} not yet implemented.", i.type()); |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 242 | if (tableFound && actions.isEmpty()) { |
| 243 | // handles the case where there are no actions, but there is |
| 244 | // a goto instruction for the next table |
Sho SHIMIZU | 6601dd7 | 2015-05-11 19:00:29 -0700 | [diff] [blame] | 245 | return Collections.emptyList(); |
Saurav Das | cbe6de3 | 2015-03-01 18:30:46 -0800 | [diff] [blame] | 246 | } |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 247 | return actions; |
| 248 | } |
| 249 | |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 250 | private OFInstruction buildTableGoto(Instructions.TableTypeTransition i) { |
| 251 | OFInstruction instruction = factory().instructions().gotoTable( |
alshabib | d17abc2 | 2015-04-21 18:26:35 -0700 | [diff] [blame] | 252 | TableId.of(i.tableId())); |
alshabib | 9af7007 | 2015-02-09 14:34:16 -0800 | [diff] [blame] | 253 | return instruction; |
| 254 | } |
| 255 | |
Saurav Das | 86af8f1 | 2015-05-25 23:55:33 -0700 | [diff] [blame^] | 256 | private OFInstruction buildMetadata(Instructions.MetadataInstruction m) { |
| 257 | OFInstruction instruction = factory().instructions().writeMetadata( |
| 258 | U64.of(m.metadata()), U64.of(m.metadataMask())); |
| 259 | return instruction; |
| 260 | } |
| 261 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 262 | private OFAction buildL0Modification(Instruction i) { |
| 263 | L0ModificationInstruction l0m = (L0ModificationInstruction) i; |
| 264 | switch (l0m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 265 | case LAMBDA: |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 266 | return buildModLambdaInstruction((ModLambdaInstruction) i); |
| 267 | case OCH: |
| 268 | try { |
| 269 | return buildModOchSignalInstruction((ModOchSignalInstruction) i); |
| 270 | } catch (UnsupportedGridTypeException | UnsupportedChannelSpacingException e) { |
| 271 | log.warn(e.getMessage()); |
| 272 | break; |
| 273 | } |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 274 | default: |
| 275 | log.warn("Unimplemented action type {}.", l0m.subtype()); |
| 276 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 277 | } |
| 278 | return null; |
| 279 | } |
| 280 | |
Sho SHIMIZU | e397ffe | 2015-05-12 16:24:23 -0700 | [diff] [blame] | 281 | private OFAction buildModLambdaInstruction(ModLambdaInstruction instruction) { |
| 282 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 283 | new CircuitSignalID((byte) 1, (byte) 2, instruction.lambda(), (short) 1))); |
| 284 | } |
| 285 | |
| 286 | private OFAction buildModOchSignalInstruction(ModOchSignalInstruction instruction) { |
| 287 | OchSignal signal = instruction.lambda(); |
| 288 | byte gridType = convertGridType(signal.gridType()); |
| 289 | byte channelSpacing = convertChannelSpacing(signal.channelSpacing()); |
| 290 | |
| 291 | return factory().actions().circuit(factory().oxms().ochSigidBasic( |
| 292 | new CircuitSignalID(gridType, channelSpacing, |
| 293 | (short) signal.spacingMultiplier(), (short) signal.slotGranularity()) |
| 294 | )); |
| 295 | } |
| 296 | |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 297 | private OFAction buildL2Modification(Instruction i) { |
| 298 | L2ModificationInstruction l2m = (L2ModificationInstruction) i; |
| 299 | ModEtherInstruction eth; |
| 300 | OFOxm<?> oxm = null; |
| 301 | switch (l2m.subtype()) { |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 302 | case ETH_DST: |
| 303 | eth = (ModEtherInstruction) l2m; |
| 304 | oxm = factory().oxms().ethDst(MacAddress.of(eth.mac().toLong())); |
| 305 | break; |
| 306 | case ETH_SRC: |
| 307 | eth = (ModEtherInstruction) l2m; |
| 308 | oxm = factory().oxms().ethSrc(MacAddress.of(eth.mac().toLong())); |
| 309 | break; |
| 310 | case VLAN_ID: |
| 311 | ModVlanIdInstruction vlanId = (ModVlanIdInstruction) l2m; |
| 312 | oxm = factory().oxms().vlanVid(OFVlanVidMatch.ofVlan(vlanId.vlanId().toShort())); |
| 313 | break; |
| 314 | case VLAN_PCP: |
| 315 | ModVlanPcpInstruction vlanPcp = (ModVlanPcpInstruction) l2m; |
| 316 | oxm = factory().oxms().vlanPcp(VlanPcp.of(vlanPcp.vlanPcp())); |
| 317 | break; |
| 318 | case MPLS_PUSH: |
| 319 | PushHeaderInstructions pushHeaderInstructions = |
| 320 | (PushHeaderInstructions) l2m; |
| 321 | return factory().actions().pushMpls(EthType.of(pushHeaderInstructions |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 322 | .ethernetType())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 323 | case MPLS_POP: |
Jonathan Hart | 67fc097 | 2015-03-19 15:21:20 -0700 | [diff] [blame] | 324 | PushHeaderInstructions popHeaderInstructions = |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 325 | (PushHeaderInstructions) l2m; |
| 326 | return factory().actions().popMpls(EthType.of(popHeaderInstructions |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 327 | .ethernetType())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 328 | case MPLS_LABEL: |
| 329 | ModMplsLabelInstruction mplsLabel = |
| 330 | (ModMplsLabelInstruction) l2m; |
| 331 | oxm = factory().oxms().mplsLabel(U32.of(mplsLabel.label() |
| 332 | .longValue())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 333 | break; |
sangho | 3f97a17d | 2015-01-29 22:56:29 -0800 | [diff] [blame] | 334 | case DEC_MPLS_TTL: |
| 335 | return factory().actions().decMplsTtl(); |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 336 | case VLAN_POP: |
| 337 | return factory().actions().popVlan(); |
Jonathan Hart | 54b406b | 2015-03-06 16:24:14 -0800 | [diff] [blame] | 338 | case VLAN_PUSH: |
| 339 | PushHeaderInstructions pushVlanInstruction = (PushHeaderInstructions) l2m; |
| 340 | return factory().actions().pushVlan( |
| 341 | EthType.of(pushVlanInstruction.ethernetType())); |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 342 | default: |
| 343 | log.warn("Unimplemented action type {}.", l2m.subtype()); |
| 344 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (oxm != null) { |
| 348 | return factory().actions().buildSetField().setField(oxm).build(); |
| 349 | } |
| 350 | return null; |
| 351 | } |
| 352 | |
| 353 | private OFAction buildL3Modification(Instruction i) { |
| 354 | L3ModificationInstruction l3m = (L3ModificationInstruction) i; |
| 355 | ModIPInstruction ip; |
Pavlin Radoslavov | 23e398d | 2014-11-05 15:17:57 -0800 | [diff] [blame] | 356 | Ip4Address ip4; |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 357 | Ip6Address ip6; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 358 | OFOxm<?> oxm = null; |
| 359 | switch (l3m.subtype()) { |
Sho SHIMIZU | 5cd7ce9 | 2015-05-11 19:10:38 -0700 | [diff] [blame] | 360 | case IPV4_SRC: |
| 361 | ip = (ModIPInstruction) i; |
| 362 | ip4 = ip.ip().getIp4Address(); |
| 363 | oxm = factory().oxms().ipv4Src(IPv4Address.of(ip4.toInt())); |
| 364 | break; |
| 365 | case IPV4_DST: |
| 366 | ip = (ModIPInstruction) i; |
| 367 | ip4 = ip.ip().getIp4Address(); |
| 368 | oxm = factory().oxms().ipv4Dst(IPv4Address.of(ip4.toInt())); |
| 369 | break; |
| 370 | case IPV6_SRC: |
| 371 | ip = (ModIPInstruction) i; |
| 372 | ip6 = ip.ip().getIp6Address(); |
| 373 | oxm = factory().oxms().ipv6Src(IPv6Address.of(ip6.toOctets())); |
| 374 | break; |
| 375 | case IPV6_DST: |
| 376 | ip = (ModIPInstruction) i; |
| 377 | ip6 = ip.ip().getIp6Address(); |
| 378 | oxm = factory().oxms().ipv6Dst(IPv6Address.of(ip6.toOctets())); |
| 379 | break; |
| 380 | case IPV6_FLABEL: |
| 381 | ModIPv6FlowLabelInstruction flowLabelInstruction = |
| 382 | (ModIPv6FlowLabelInstruction) i; |
| 383 | int flowLabel = flowLabelInstruction.flowLabel(); |
| 384 | oxm = factory().oxms().ipv6Flabel(IPv6FlowLabel.of(flowLabel)); |
| 385 | break; |
| 386 | case DEC_TTL: |
| 387 | return factory().actions().decNwTtl(); |
| 388 | case TTL_IN: |
| 389 | return factory().actions().copyTtlIn(); |
| 390 | case TTL_OUT: |
| 391 | return factory().actions().copyTtlOut(); |
| 392 | default: |
| 393 | log.warn("Unimplemented action type {}.", l3m.subtype()); |
| 394 | break; |
Jonathan Hart | 86e5935 | 2014-10-22 10:42:16 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (oxm != null) { |
| 398 | return factory().actions().buildSetField().setField(oxm).build(); |
| 399 | } |
| 400 | return null; |
| 401 | } |
| 402 | |
| 403 | } |