alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
alshabib | bc37196 | 2015-07-09 22:26:21 -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 | */ |
alshabib | 10c810b | 2015-08-18 16:59:04 -0700 | [diff] [blame] | 16 | package org.onosproject.net.meter; |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 17 | |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 18 | import org.onlab.util.Identifier; |
| 19 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | |
| 22 | /** |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 23 | * A representation of a meter cell identifier. Uniquely identifies a meter cell |
| 24 | * in the scope of a single device. |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 25 | * <p> |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 26 | * This ID uniquely identifies a meter cell within in a switch that maintains |
| 27 | * only one meter instance. If a switch supports multiple meter instances (like |
| 28 | * in P4), then {@link org.onosproject.net.pi.runtime.PiMeterCellId} should be |
| 29 | * used. In this case, meter cells are defined starting with id=1 up to the |
| 30 | * maximum number of cells that the switch can support. The OpenFlow protocol |
| 31 | * also defines some additional virtual meter cells that can not be associated |
| 32 | * with flows. |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 33 | */ |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 34 | public final class MeterId extends Identifier<Long> implements MeterCellId { |
| 35 | |
| 36 | // TODO: should rename this class to SimpleMeterCellId to distinguish it |
| 37 | // from PiMeterId and PiMeterCellId. From ONOS-7051, to follow the P4 |
| 38 | // abstraction there can be multiple instances of a meter in a data plane, |
| 39 | // each meter instance is made of multiple cells. This class is based on the |
| 40 | // OpenFlow abstraction where, following P4 terminology, the data plane |
| 41 | // maintains only one meter instance. What is described here as a MeterId is |
| 42 | // indeed the identifier of a meter cell. |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 43 | |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 44 | /** Flow meters can use any number up to MAX. */ |
| 45 | public static final long MAX = 0xFFFF0000L; |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 46 | |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 47 | |
| 48 | /* The following are virtual meters as defined in openflow-spec-1.3 P. 58 */ |
| 49 | /** Meter for slow datapath, if any. */ |
| 50 | public static final MeterId SLOWPATH = new MeterId(0xFFFFFFFDL); |
| 51 | /** Meter for controller connection. */ |
| 52 | public static final MeterId CONTROLLER = new MeterId(0xFFFFFFFEL); |
| 53 | /** Represents all meters for stat requests commands. */ |
| 54 | public static final MeterId ALL = new MeterId(0xFFFFFFFFL); |
| 55 | |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 56 | |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 57 | private MeterId(long id) { |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 58 | super(id); |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 59 | } |
| 60 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public String toString() { |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 63 | return Long.toHexString(identifier); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Creates a new meter identifier. |
| 68 | * |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 69 | * @param id the backing identifier value |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 70 | * @return meter identifier |
| 71 | */ |
alshabib | 7bb0501 | 2015-08-05 10:15:09 -0700 | [diff] [blame] | 72 | public static MeterId meterId(long id) { |
Jordi Ortiz | 75f5fb9 | 2016-10-21 19:26:16 +0200 | [diff] [blame] | 73 | checkArgument(id > 0, "id cannot be negative nor 0"); |
| 74 | checkArgument(id <= MAX, "id cannot be larger than {}", MAX); |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 75 | return new MeterId(id); |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 76 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public MeterCellType type() { |
| 80 | return MeterCellType.INDEX; |
| 81 | } |
alshabib | bc37196 | 2015-07-09 22:26:21 -0700 | [diff] [blame] | 82 | } |