Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 19 | package org.onlab.onos.provider.of.packet.impl; |
| 20 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 21 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.flow.instructions.Instruction; |
| 23 | import org.onlab.onos.net.flow.instructions.Instruction.Type; |
| 24 | import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 25 | import org.onlab.onos.net.packet.DefaultPacketContext; |
| 26 | import org.onlab.onos.net.packet.InboundPacket; |
| 27 | import org.onlab.onos.net.packet.OutboundPacket; |
tom | 9c94c5b | 2014-09-17 13:14:42 -0700 | [diff] [blame] | 28 | import org.onlab.onos.openflow.controller.OpenFlowPacketContext; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.projectfloodlight.openflow.types.OFPort; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 31 | |
| 32 | import java.util.List; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 33 | |
| 34 | public class OpenFlowCorePacketContext extends DefaultPacketContext { |
| 35 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 36 | private final OpenFlowPacketContext ofPktCtx; |
| 37 | |
| 38 | protected OpenFlowCorePacketContext(long time, InboundPacket inPkt, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 39 | OutboundPacket outPkt, boolean block, |
| 40 | OpenFlowPacketContext ofPktCtx) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 41 | super(time, inPkt, outPkt, block); |
| 42 | this.ofPktCtx = ofPktCtx; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void send() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 47 | if (!this.block()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 48 | if (outPacket() == null) { |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 49 | sendPacket(null); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 50 | } else { |
| 51 | Ethernet eth = new Ethernet(); |
| 52 | eth.deserialize(outPacket().data().array(), 0, |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 53 | outPacket().data().array().length); |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 54 | sendPacket(eth); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 55 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 56 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 60 | private void sendPacket(Ethernet eth) { |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 61 | List<Instruction> ins = treatmentBuilder().build().instructions(); |
| 62 | OFPort p = null; |
alshabib | 8f1cf4a | 2014-09-17 14:44:48 -0700 | [diff] [blame] | 63 | //TODO: support arbitrary list of treatments must be supported in ofPacketContext |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 64 | for (Instruction i : ins) { |
| 65 | if (i.type() == Type.OUTPUT) { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 66 | p = buildPort(((OutputInstruction) i).port()); |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 67 | break; //for now... |
| 68 | } |
| 69 | } |
alshabib | e9d3a32 | 2014-09-23 15:18:33 -0700 | [diff] [blame] | 70 | if (eth == null) { |
| 71 | ofPktCtx.build(p); |
| 72 | } else { |
| 73 | ofPktCtx.build(eth, p); |
| 74 | } |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 75 | ofPktCtx.send(); |
| 76 | } |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 77 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 78 | private OFPort buildPort(PortNumber port) { |
| 79 | return OFPort.of((int) port.toLong()); |
| 80 | } |
| 81 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 82 | } |