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