blob: b227b9212e88e8e84209e94934575591d9c6a8c4 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Changhoon Yoon541ef712015-05-23 17:18:34 +090018import org.onosproject.core.Permission;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import org.onosproject.net.flow.DefaultTrafficTreatment;
20import org.onosproject.net.flow.TrafficTreatment;
21import org.onosproject.net.flow.TrafficTreatment.Builder;
alshabib1d4cace2014-09-13 19:16:26 -070022
Jonathan Hartcc36be82015-04-09 11:54:56 -070023import java.util.concurrent.atomic.AtomicBoolean;
alshabibf6d77962014-09-12 16:25:21 -070024
Changhoon Yoon541ef712015-05-23 17:18:34 +090025import static org.onosproject.security.AppGuard.checkPermission;
26
27
Jonathan Hartcc36be82015-04-09 11:54:56 -070028/**
29 * Default implementation of a packet context.
30 */
alshabibf6d77962014-09-12 16:25:21 -070031public abstract class DefaultPacketContext implements PacketContext {
32
33 private final long time;
34 private final InboundPacket inPkt;
35 private final OutboundPacket outPkt;
alshabib1d4cace2014-09-13 19:16:26 -070036 private final TrafficTreatment.Builder builder;
37
alshabib63d5afe2014-09-15 09:40:24 -070038 private final AtomicBoolean block;
alshabibf6d77962014-09-12 16:25:21 -070039
Jonathan Hartcc36be82015-04-09 11:54:56 -070040 /**
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 */
alshabibf6d77962014-09-12 16:25:21 -070048 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;
alshabib63d5afe2014-09-15 09:40:24 -070054 this.block = new AtomicBoolean(block);
tom9a693fd2014-10-03 11:32:19 -070055 this.builder = DefaultTrafficTreatment.builder();
alshabibf6d77962014-09-12 16:25:21 -070056 }
57
58 @Override
59 public long time() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090060 checkPermission(Permission.PACKET_READ);
61
alshabibf6d77962014-09-12 16:25:21 -070062 return time;
63 }
64
65 @Override
66 public InboundPacket inPacket() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090067 checkPermission(Permission.PACKET_READ);
68
alshabibf6d77962014-09-12 16:25:21 -070069 return inPkt;
70 }
71
72 @Override
73 public OutboundPacket outPacket() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090074 checkPermission(Permission.PACKET_READ);
75
alshabibf6d77962014-09-12 16:25:21 -070076 return outPkt;
77 }
78
79 @Override
alshabib1d4cace2014-09-13 19:16:26 -070080 public Builder treatmentBuilder() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090081 checkPermission(Permission.PACKET_READ);
82
alshabib1d4cace2014-09-13 19:16:26 -070083 return builder;
84 }
85
86 @Override
alshabibf6d77962014-09-12 16:25:21 -070087 public abstract void send();
88
89 @Override
alshabib030111e2014-09-15 15:56:42 -070090 public boolean block() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090091 checkPermission(Permission.PACKET_WRITE);
92
alshabib63d5afe2014-09-15 09:40:24 -070093 return this.block.getAndSet(true);
alshabibf6d77962014-09-12 16:25:21 -070094 }
95
96 @Override
97 public boolean isHandled() {
Changhoon Yoon541ef712015-05-23 17:18:34 +090098 checkPermission(Permission.PACKET_READ);
99
alshabib63d5afe2014-09-15 09:40:24 -0700100 return this.block.get();
alshabibf6d77962014-09-12 16:25:21 -0700101 }
Changhoon Yoon541ef712015-05-23 17:18:34 +0900102}