Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.packet; |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 17 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 18 | import org.onosproject.core.Permission; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 20 | import org.onosproject.net.flow.TrafficTreatment; |
| 21 | import org.onosproject.net.flow.TrafficTreatment.Builder; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 22 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicBoolean; |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 24 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 25 | import static org.onosproject.security.AppGuard.checkPermission; |
| 26 | |
| 27 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Default implementation of a packet context. |
| 30 | */ |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 31 | public abstract class DefaultPacketContext implements PacketContext { |
| 32 | |
| 33 | private final long time; |
| 34 | private final InboundPacket inPkt; |
| 35 | private final OutboundPacket outPkt; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 36 | private final TrafficTreatment.Builder builder; |
| 37 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 38 | private final AtomicBoolean block; |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | cc36be8 | 2015-04-09 11:54:56 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Creates a new packet context. |
| 42 | * |
| 43 | * @param time creation time |
| 44 | * @param inPkt inbound packet |
| 45 | * @param outPkt outbound packet |
| 46 | * @param block whether the context is blocked or not |
| 47 | */ |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 48 | protected DefaultPacketContext(long time, InboundPacket inPkt, |
| 49 | OutboundPacket outPkt, boolean block) { |
| 50 | super(); |
| 51 | this.time = time; |
| 52 | this.inPkt = inPkt; |
| 53 | this.outPkt = outPkt; |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 54 | this.block = new AtomicBoolean(block); |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 55 | this.builder = DefaultTrafficTreatment.builder(); |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public long time() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 60 | checkPermission(Permission.PACKET_READ); |
| 61 | |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 62 | return time; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public InboundPacket inPacket() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 67 | checkPermission(Permission.PACKET_READ); |
| 68 | |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 69 | return inPkt; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public OutboundPacket outPacket() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 74 | checkPermission(Permission.PACKET_READ); |
| 75 | |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 76 | return outPkt; |
| 77 | } |
| 78 | |
| 79 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 80 | public Builder treatmentBuilder() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 81 | checkPermission(Permission.PACKET_READ); |
| 82 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 83 | return builder; |
| 84 | } |
| 85 | |
| 86 | @Override |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 87 | public abstract void send(); |
| 88 | |
| 89 | @Override |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 90 | public boolean block() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 91 | checkPermission(Permission.PACKET_WRITE); |
| 92 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 93 | return this.block.getAndSet(true); |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean isHandled() { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 98 | checkPermission(Permission.PACKET_READ); |
| 99 | |
alshabib | 63d5afe | 2014-09-15 09:40:24 -0700 | [diff] [blame] | 100 | return this.block.get(); |
alshabib | f6d7796 | 2014-09-12 16:25:21 -0700 | [diff] [blame] | 101 | } |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 102 | } |