blob: 3fd9a97445d42eb1fd6c729e932db2dca27c3951 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
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 */
alshabib1d4cace2014-09-13 19:16:26 -070019package org.onlab.onos.provider.of.packet.impl;
20
alshabib63d5afe2014-09-15 09:40:24 -070021import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -070022import org.onlab.onos.net.flow.instructions.Instruction;
23import org.onlab.onos.net.flow.instructions.Instruction.Type;
24import org.onlab.onos.net.flow.instructions.Instructions.OutputInstruction;
alshabib1d4cace2014-09-13 19:16:26 -070025import org.onlab.onos.net.packet.DefaultPacketContext;
26import org.onlab.onos.net.packet.InboundPacket;
27import org.onlab.onos.net.packet.OutboundPacket;
tom9c94c5b2014-09-17 13:14:42 -070028import org.onlab.onos.openflow.controller.OpenFlowPacketContext;
alshabib1d4cace2014-09-13 19:16:26 -070029import org.onlab.packet.Ethernet;
30import org.projectfloodlight.openflow.types.OFPort;
tom1679e182014-10-09 13:50:45 -070031
32import java.util.List;
alshabib1d4cace2014-09-13 19:16:26 -070033
34public class OpenFlowCorePacketContext extends DefaultPacketContext {
35
alshabib1d4cace2014-09-13 19:16:26 -070036 private final OpenFlowPacketContext ofPktCtx;
37
38 protected OpenFlowCorePacketContext(long time, InboundPacket inPkt,
tom1679e182014-10-09 13:50:45 -070039 OutboundPacket outPkt, boolean block,
40 OpenFlowPacketContext ofPktCtx) {
alshabib1d4cace2014-09-13 19:16:26 -070041 super(time, inPkt, outPkt, block);
42 this.ofPktCtx = ofPktCtx;
43 }
44
45 @Override
46 public void send() {
alshabib030111e2014-09-15 15:56:42 -070047 if (!this.block()) {
alshabib1d4cace2014-09-13 19:16:26 -070048 if (outPacket() == null) {
alshabibe9d3a322014-09-23 15:18:33 -070049 sendPacket(null);
alshabib1d4cace2014-09-13 19:16:26 -070050 } else {
51 Ethernet eth = new Ethernet();
52 eth.deserialize(outPacket().data().array(), 0,
tom1679e182014-10-09 13:50:45 -070053 outPacket().data().array().length);
alshabibe9d3a322014-09-23 15:18:33 -070054 sendPacket(eth);
alshabib1d4cace2014-09-13 19:16:26 -070055 }
alshabib63d5afe2014-09-15 09:40:24 -070056
alshabib1d4cace2014-09-13 19:16:26 -070057 }
58 }
59
alshabibe9d3a322014-09-23 15:18:33 -070060 private void sendPacket(Ethernet eth) {
alshabib63d5afe2014-09-15 09:40:24 -070061 List<Instruction> ins = treatmentBuilder().build().instructions();
62 OFPort p = null;
alshabib8f1cf4a2014-09-17 14:44:48 -070063 //TODO: support arbitrary list of treatments must be supported in ofPacketContext
alshabib63d5afe2014-09-15 09:40:24 -070064 for (Instruction i : ins) {
65 if (i.type() == Type.OUTPUT) {
alshabib030111e2014-09-15 15:56:42 -070066 p = buildPort(((OutputInstruction) i).port());
alshabib63d5afe2014-09-15 09:40:24 -070067 break; //for now...
68 }
69 }
alshabibe9d3a322014-09-23 15:18:33 -070070 if (eth == null) {
71 ofPktCtx.build(p);
72 } else {
73 ofPktCtx.build(eth, p);
74 }
alshabib63d5afe2014-09-15 09:40:24 -070075 ofPktCtx.send();
76 }
tom1679e182014-10-09 13:50:45 -070077
alshabib63d5afe2014-09-15 09:40:24 -070078 private OFPort buildPort(PortNumber port) {
79 return OFPort.of((int) port.toLong());
80 }
81
alshabib1d4cace2014-09-13 19:16:26 -070082}