blob: 6d1531036bdbc2d31433ae91a2a3970f7997e847 [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
Jonathan Hart2a655752015-04-07 16:46:33 -070018import org.onlab.packet.DeserializationException;
19import org.onlab.packet.Ethernet;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.net.PortNumber;
21import org.onosproject.net.flow.instructions.Instruction;
22import org.onosproject.net.flow.instructions.Instruction.Type;
23import org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
24import org.onosproject.net.packet.DefaultPacketContext;
25import org.onosproject.net.packet.InboundPacket;
26import org.onosproject.net.packet.OutboundPacket;
27import org.onosproject.openflow.controller.OpenFlowPacketContext;
alshabib1d4cace2014-09-13 19:16:26 -070028import org.projectfloodlight.openflow.types.OFPort;
Jonathan Hart2a655752015-04-07 16:46:33 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
tom1679e182014-10-09 13:50:45 -070031
32import java.util.List;
alshabib1d4cace2014-09-13 19:16:26 -070033
Jonathan Hartcc36be82015-04-09 11:54:56 -070034/**
35 * Packet context used with the OpenFlow providers.
36 */
alshabib1d4cace2014-09-13 19:16:26 -070037public class OpenFlowCorePacketContext extends DefaultPacketContext {
38
Jonathan Hart2a655752015-04-07 16:46:33 -070039 private static final Logger log = LoggerFactory.getLogger(OpenFlowCorePacketContext.class);
40
alshabib1d4cace2014-09-13 19:16:26 -070041 private final OpenFlowPacketContext ofPktCtx;
42
Jonathan Hartcc36be82015-04-09 11:54:56 -070043 /**
44 * Creates a new OpenFlow core packet context.
45 *
46 * @param time creation time
47 * @param inPkt inbound packet
48 * @param outPkt outbound packet
49 * @param block whether the context is blocked or not
50 * @param ofPktCtx OpenFlow packet context
51 */
alshabib1d4cace2014-09-13 19:16:26 -070052 protected OpenFlowCorePacketContext(long time, InboundPacket inPkt,
tom1679e182014-10-09 13:50:45 -070053 OutboundPacket outPkt, boolean block,
54 OpenFlowPacketContext ofPktCtx) {
alshabib1d4cace2014-09-13 19:16:26 -070055 super(time, inPkt, outPkt, block);
56 this.ofPktCtx = ofPktCtx;
57 }
58
59 @Override
60 public void send() {
alshabib030111e2014-09-15 15:56:42 -070061 if (!this.block()) {
alshabib1d4cace2014-09-13 19:16:26 -070062 if (outPacket() == null) {
alshabibe9d3a322014-09-23 15:18:33 -070063 sendPacket(null);
alshabib1d4cace2014-09-13 19:16:26 -070064 } else {
Jonathan Hart2a655752015-04-07 16:46:33 -070065 try {
66 Ethernet eth = Ethernet.deserializer()
67 .deserialize(outPacket().data().array(), 0,
68 outPacket().data().array().length);
69 sendPacket(eth);
70 } catch (DeserializationException e) {
71 log.warn("Unable to deserialize packet");
72 }
alshabib1d4cace2014-09-13 19:16:26 -070073 }
alshabib1d4cace2014-09-13 19:16:26 -070074 }
75 }
76
alshabibe9d3a322014-09-23 15:18:33 -070077 private void sendPacket(Ethernet eth) {
Ray Milkey42507352015-03-20 15:16:10 -070078 List<Instruction> ins = treatmentBuilder().build().allInstructions();
alshabib63d5afe2014-09-15 09:40:24 -070079 OFPort p = null;
alshabib8f1cf4a2014-09-17 14:44:48 -070080 //TODO: support arbitrary list of treatments must be supported in ofPacketContext
alshabib63d5afe2014-09-15 09:40:24 -070081 for (Instruction i : ins) {
82 if (i.type() == Type.OUTPUT) {
alshabib030111e2014-09-15 15:56:42 -070083 p = buildPort(((OutputInstruction) i).port());
alshabib63d5afe2014-09-15 09:40:24 -070084 break; //for now...
85 }
86 }
alshabibe9d3a322014-09-23 15:18:33 -070087 if (eth == null) {
88 ofPktCtx.build(p);
89 } else {
90 ofPktCtx.build(eth, p);
91 }
alshabib63d5afe2014-09-15 09:40:24 -070092 ofPktCtx.send();
93 }
tom1679e182014-10-09 13:50:45 -070094
alshabib63d5afe2014-09-15 09:40:24 -070095 private OFPort buildPort(PortNumber port) {
96 return OFPort.of((int) port.toLong());
97 }
98
alshabib1d4cace2014-09-13 19:16:26 -070099}