blob: 7c288806af25f0121d94ea2d2706b6602f33683a [file] [log] [blame]
alshabibbc371962015-07-09 22:26:21 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
alshabibbc371962015-07-09 22:26:21 -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 */
alshabib10c810b2015-08-18 16:59:04 -070016package org.onosproject.net.meter;
alshabibbc371962015-07-09 22:26:21 -070017
Jian Lib6d998e2016-02-29 11:41:18 -080018import org.onlab.util.Identifier;
19
alshabibbc371962015-07-09 22:26:21 -070020import static com.google.common.base.Preconditions.checkArgument;
21
22/**
Frank Wangd7e3b4b2017-09-24 13:37:54 +090023 * A representation of a meter cell identifier. Uniquely identifies a meter cell
24 * in the scope of a single device.
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020025 * <p>
Frank Wangd7e3b4b2017-09-24 13:37:54 +090026 * 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.
alshabibbc371962015-07-09 22:26:21 -070033 */
Frank Wangd7e3b4b2017-09-24 13:37:54 +090034public 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.
alshabibbc371962015-07-09 22:26:21 -070043
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020044 /** Flow meters can use any number up to MAX. */
45 public static final long MAX = 0xFFFF0000L;
alshabibbc371962015-07-09 22:26:21 -070046
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020047
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
alshabibbc371962015-07-09 22:26:21 -070056
alshabib7bb05012015-08-05 10:15:09 -070057 private MeterId(long id) {
Jian Lib6d998e2016-02-29 11:41:18 -080058 super(id);
alshabibbc371962015-07-09 22:26:21 -070059 }
60
alshabib58fe6dc2015-08-19 17:16:13 -070061 @Override
62 public String toString() {
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020063 return Long.toHexString(identifier);
alshabib58fe6dc2015-08-19 17:16:13 -070064 }
65
Jian Lib6d998e2016-02-29 11:41:18 -080066 /**
67 * Creates a new meter identifier.
68 *
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020069 * @param id the backing identifier value
Jian Lib6d998e2016-02-29 11:41:18 -080070 * @return meter identifier
71 */
alshabib7bb05012015-08-05 10:15:09 -070072 public static MeterId meterId(long id) {
Jordi Ortiz75f5fb92016-10-21 19:26:16 +020073 checkArgument(id > 0, "id cannot be negative nor 0");
74 checkArgument(id <= MAX, "id cannot be larger than {}", MAX);
alshabibbc371962015-07-09 22:26:21 -070075 return new MeterId(id);
alshabibbc371962015-07-09 22:26:21 -070076 }
Frank Wangd7e3b4b2017-09-24 13:37:54 +090077
78 @Override
79 public MeterCellType type() {
80 return MeterCellType.INDEX;
81 }
alshabibbc371962015-07-09 22:26:21 -070082}