blob: a5ec08e2e236eff2d73df1abb8f23b5d5996b6c0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.packet;
alshabibf6d77962014-09-12 16:25:21 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.net.flow.DefaultTrafficTreatment;
19import org.onosproject.net.flow.TrafficTreatment;
20import org.onosproject.net.flow.TrafficTreatment.Builder;
alshabib1d4cace2014-09-13 19:16:26 -070021
Jonathan Hartcc36be82015-04-09 11:54:56 -070022import java.util.concurrent.atomic.AtomicBoolean;
alshabibf6d77962014-09-12 16:25:21 -070023
Changhoon Yoon541ef712015-05-23 17:18:34 +090024import static org.onosproject.security.AppGuard.checkPermission;
Changhoon Yoonb856b812015-08-10 03:47:19 +090025import static org.onosproject.security.AppPermission.Type.*;
Changhoon Yoon541ef712015-05-23 17:18:34 +090026
Jonathan Hartcc36be82015-04-09 11:54:56 -070027/**
28 * Default implementation of a packet context.
29 */
alshabibf6d77962014-09-12 16:25:21 -070030public abstract class DefaultPacketContext implements PacketContext {
31
32 private final long time;
33 private final InboundPacket inPkt;
34 private final OutboundPacket outPkt;
alshabib1d4cace2014-09-13 19:16:26 -070035 private final TrafficTreatment.Builder builder;
36
alshabib63d5afe2014-09-15 09:40:24 -070037 private final AtomicBoolean block;
alshabibf6d77962014-09-12 16:25:21 -070038
Jonathan Hartcc36be82015-04-09 11:54:56 -070039 /**
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 */
alshabibf6d77962014-09-12 16:25:21 -070047 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;
alshabib63d5afe2014-09-15 09:40:24 -070053 this.block = new AtomicBoolean(block);
tom9a693fd2014-10-03 11:32:19 -070054 this.builder = DefaultTrafficTreatment.builder();
alshabibf6d77962014-09-12 16:25:21 -070055 }
56
57 @Override
58 public long time() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090059 checkPermission(PACKET_READ);
alshabibf6d77962014-09-12 16:25:21 -070060 return time;
61 }
62
63 @Override
64 public InboundPacket inPacket() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090065 checkPermission(PACKET_READ);
alshabibf6d77962014-09-12 16:25:21 -070066 return inPkt;
67 }
68
69 @Override
70 public OutboundPacket outPacket() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090071 checkPermission(PACKET_READ);
alshabibf6d77962014-09-12 16:25:21 -070072 return outPkt;
73 }
74
75 @Override
alshabib1d4cace2014-09-13 19:16:26 -070076 public Builder treatmentBuilder() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090077 checkPermission(PACKET_READ);
alshabib1d4cace2014-09-13 19:16:26 -070078 return builder;
79 }
80
81 @Override
alshabibf6d77962014-09-12 16:25:21 -070082 public abstract void send();
83
84 @Override
alshabib030111e2014-09-15 15:56:42 -070085 public boolean block() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090086 checkPermission(PACKET_WRITE);
alshabib63d5afe2014-09-15 09:40:24 -070087 return this.block.getAndSet(true);
alshabibf6d77962014-09-12 16:25:21 -070088 }
89
90 @Override
91 public boolean isHandled() {
Changhoon Yoonb856b812015-08-10 03:47:19 +090092 checkPermission(PACKET_READ);
alshabib63d5afe2014-09-15 09:40:24 -070093 return this.block.get();
alshabibf6d77962014-09-12 16:25:21 -070094 }
Changhoon Yoon541ef712015-05-23 17:18:34 +090095}