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; |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 17 | |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 20 | /** |
| 21 | * Abstraction of an inbound packet processor. |
| 22 | */ |
| 23 | public interface PacketProcessor { |
| 24 | |
Sho SHIMIZU | e2952e4 | 2015-09-11 17:11:21 -0700 | [diff] [blame] | 25 | int ADVISOR_MAX = Integer.MAX_VALUE / 3; |
| 26 | int DIRECTOR_MAX = (Integer.MAX_VALUE / 3) * 2; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 27 | int OBSERVER_MAX = Integer.MAX_VALUE; |
alshabib | 369d294 | 2014-09-12 17:59:35 -0700 | [diff] [blame] | 28 | |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 29 | /** |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 30 | * Returns a priority in the ADVISOR range, where processors can take early action and |
| 31 | * influence the packet context. However, they cannot handle the packet (i.e. call send() or block()). |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 32 | * The valid range is from 1 to ADVISOR_MAX. |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 33 | * Processors in this range get to see the packet first. |
| 34 | * |
| 35 | * @param priority priority within ADVISOR range |
| 36 | * @return overall priority |
| 37 | */ |
| 38 | static int advisor(int priority) { |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 39 | int overallPriority = priority + 1; |
| 40 | checkArgument(overallPriority > 0 && overallPriority <= ADVISOR_MAX, |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 41 | "Priority not within ADVISOR range"); |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 42 | return overallPriority; |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Returns a priority in the DIRECTOR range, where processors can handle the packet. |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 47 | * The valid range is from ADVISOR_MAX+1 to DIRECTOR_MAX. |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 48 | * Processors in this range get to see the packet second, after ADVISORS. |
| 49 | * |
| 50 | * @param priority priority within the DIRECTOR range |
| 51 | * @return overall priority |
| 52 | */ |
| 53 | static int director(int priority) { |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 54 | int overallPriority = ADVISOR_MAX + priority + 1; |
| 55 | checkArgument(overallPriority > ADVISOR_MAX && overallPriority <= DIRECTOR_MAX, |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 56 | "Priority not within DIRECTOR range"); |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 57 | return overallPriority; |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Returns a priority in the OBSERVER range, where processors cannot take any action, |
| 62 | * but can observe what action has been taken until then. |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 63 | * The valid range is from DIRECTOR_MAX+1 to OBSERVER_MAX. |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 64 | * Processors in this range get to see the packet last, after ADVISORS and DIRECTORS. |
| 65 | * |
| 66 | * @param priority priority within the OBSERVER range |
| 67 | * @return overall priority |
| 68 | */ |
| 69 | static int observer(int priority) { |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 70 | int overallPriority = DIRECTOR_MAX + priority + 1; |
Thomas Vachuska | 7f171b2 | 2015-08-21 12:49:08 -0700 | [diff] [blame] | 71 | checkArgument(overallPriority > DIRECTOR_MAX, |
| 72 | "Priority not within OBSERVER range"); |
samanwita pal | 14150eb | 2015-07-30 11:27:37 -0700 | [diff] [blame] | 73 | return overallPriority; |
samanwita pal | f528636 | 2015-07-29 12:59:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
tom | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 77 | * Processes the inbound packet as specified in the given context. |
| 78 | * |
| 79 | * @param context packet processing context |
| 80 | */ |
| 81 | void process(PacketContext context); |
| 82 | |
| 83 | } |