Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | */ |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 16 | package org.onlab.onos.provider.of.packet.impl; |
| 17 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 18 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 20 | import org.onlab.onos.net.flow.instructions.Instruction.Type; |
| 21 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.packet.DefaultPacketContext; |
| 23 | import org.onlab.onos.net.packet.InboundPacket; |
| 24 | import org.onlab.onos.net.packet.OutboundPacket; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 25 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 26 | import org.onlab.packet.Ethernet; |
| 27 | import org.projectfloodlight.openflow.types.OFPort; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 30 | |
| 31 | public class OpenFlowCorePacketContext extends DefaultPacketContext { |
| 32 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 33 | private final OpenFlowPacketContext ofPktCtx; |
| 34 | |
| 35 | protected OpenFlowCorePacketContext(long time, InboundPacket inPkt, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 36 | OutboundPacket outPkt, boolean block, |
| 37 | OpenFlowPacketContext ofPktCtx) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 38 | super(time, inPkt, outPkt, block); |
| 39 | this.ofPktCtx = ofPktCtx; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void send() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 44 | if (!this.block()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 45 | if (outPacket() == null) { |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 46 | sendPacket(null); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 47 | } else { |
| 48 | Ethernet eth = new Ethernet(); |
| 49 | eth.deserialize(outPacket().data().array(), 0, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 50 | outPacket().data().array().length); |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 51 | sendPacket(eth); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 52 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 53 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 57 | private void sendPacket(Ethernet eth) { |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 58 | List<Instruction> ins = treatmentBuilder().build().instructions(); |
| 59 | OFPort p = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 60 | //TODO: support arbitrary list of treatments must be supported in ofPacketContext |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 61 | for (Instruction i : ins) { |
| 62 | if (i.type() == Type.OUTPUT) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 63 | p = buildPort(((OutputInstruction) i).port()); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 64 | break; //for now... |
| 65 | } |
| 66 | } |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 67 | if (eth == null) { |
| 68 | ofPktCtx.build(p); |
| 69 | } else { |
| 70 | ofPktCtx.build(eth, p); |
| 71 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 72 | ofPktCtx.send(); |
| 73 | } |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 74 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 75 | private OFPort buildPort(PortNumber port) { |
| 76 | return OFPort.of((int) port.toLong()); |
| 77 | } |
| 78 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 79 | } |