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