blob: 5d44c91fb26edbe218c921efd000050ee6ab9d60 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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.packet.impl;
alshabib1d4cace2014-09-13 19:16:26 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.PortNumber;
19import org.onosproject.net.flow.instructions.Instruction;
20import org.onosproject.net.flow.instructions.Instruction.Type;
21import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
22import org.onosproject.net.packet.DefaultPacketContext;
23import org.onosproject.net.packet.InboundPacket;
24import org.onosproject.net.packet.OutboundPacket;
25import org.onosproject.openflow.controller.OpenFlowPacketContext;
alshabib1d4cace2014-09-13 19:16:26 -070026import org.onlab.packet.Ethernet;
27import org.projectfloodlight.openflow.types.OFPort;
tom1679e182014-10-09 13:50:45 -070028
29import java.util.List;
alshabib1d4cace2014-09-13 19:16:26 -070030
Jonathan Hartcc36be82015-04-09 11:54:56 -070031/**
32 * Packet context used with the OpenFlow providers.
33 */
alshabib1d4cace2014-09-13 19:16:26 -070034public class OpenFlowCorePacketContext extends DefaultPacketContext {
35
alshabib1d4cace2014-09-13 19:16:26 -070036 private final OpenFlowPacketContext ofPktCtx;
37
Jonathan Hartcc36be82015-04-09 11:54:56 -070038 /**
39 * Creates a new OpenFlow core packet context.
40 *
41 * @param time creation time
42 * @param inPkt inbound packet
43 * @param outPkt outbound packet
44 * @param block whether the context is blocked or not
45 * @param ofPktCtx OpenFlow packet context
46 */
alshabib1d4cace2014-09-13 19:16:26 -070047 protected OpenFlowCorePacketContext(long time, InboundPacket inPkt,
tom1679e182014-10-09 13:50:45 -070048 OutboundPacket outPkt, boolean block,
49 OpenFlowPacketContext ofPktCtx) {
alshabib1d4cace2014-09-13 19:16:26 -070050 super(time, inPkt, outPkt, block);
51 this.ofPktCtx = ofPktCtx;
52 }
53
54 @Override
55 public void send() {
alshabib030111e2014-09-15 15:56:42 -070056 if (!this.block()) {
alshabib1d4cace2014-09-13 19:16:26 -070057 if (outPacket() == null) {
alshabibe9d3a322014-09-23 15:18:33 -070058 sendPacket(null);
alshabib1d4cace2014-09-13 19:16:26 -070059 } else {
60 Ethernet eth = new Ethernet();
61 eth.deserialize(outPacket().data().array(), 0,
tom1679e182014-10-09 13:50:45 -070062 outPacket().data().array().length);
alshabibe9d3a322014-09-23 15:18:33 -070063 sendPacket(eth);
alshabib1d4cace2014-09-13 19:16:26 -070064 }
alshabib63d5afe2014-09-15 09:40:24 -070065
alshabib1d4cace2014-09-13 19:16:26 -070066 }
67 }
68
alshabibe9d3a322014-09-23 15:18:33 -070069 private void sendPacket(Ethernet eth) {
Ray Milkey42507352015-03-20 15:16:10 -070070 List<Instruction> ins = treatmentBuilder().build().allInstructions();
alshabib63d5afe2014-09-15 09:40:24 -070071 OFPort p = null;
alshabib8f1cf4a2014-09-17 14:44:48 -070072 //TODO: support arbitrary list of treatments must be supported in ofPacketContext
alshabib63d5afe2014-09-15 09:40:24 -070073 for (Instruction i : ins) {
74 if (i.type() == Type.OUTPUT) {
alshabib030111e2014-09-15 15:56:42 -070075 p = buildPort(((OutputInstruction) i).port());
alshabib63d5afe2014-09-15 09:40:24 -070076 break; //for now...
77 }
78 }
alshabibe9d3a322014-09-23 15:18:33 -070079 if (eth == null) {
80 ofPktCtx.build(p);
81 } else {
82 ofPktCtx.build(eth, p);
83 }
alshabib63d5afe2014-09-15 09:40:24 -070084 ofPktCtx.send();
85 }
tom1679e182014-10-09 13:50:45 -070086
alshabib63d5afe2014-09-15 09:40:24 -070087 private OFPort buildPort(PortNumber port) {
88 return OFPort.of((int) port.toLong());
89 }
90
alshabib1d4cace2014-09-13 19:16:26 -070091}