Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.net.pi.runtime; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.onlab.util.Identifier; |
| 21 | |
| 22 | import java.util.Optional; |
| 23 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 24 | import static com.google.common.base.Preconditions.checkArgument; |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Identifier of a table in a protocol-independent pipeline. |
| 29 | */ |
| 30 | @Beta |
| 31 | public final class PiTableId extends Identifier<String> { |
| 32 | |
| 33 | private final String scope; |
| 34 | private final String name; |
| 35 | |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 36 | private PiTableId(String scope, String name) { |
| 37 | super((scope != null ? scope + "." : "") + name); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 38 | this.scope = scope; |
| 39 | this.name = name; |
| 40 | } |
| 41 | |
| 42 | /** |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 43 | * Returns a table identifier for the given table scope and name. |
| 44 | * |
| 45 | * @param scope table scope |
| 46 | * @param name table name |
| 47 | * @return table identifier |
| 48 | */ |
| 49 | public static PiTableId of(String scope, String name) { |
| 50 | checkNotNull(name); |
| 51 | checkNotNull(scope); |
| 52 | checkArgument(!name.isEmpty(), "Name can't be empty"); |
| 53 | checkArgument(!scope.isEmpty(), "Scope can't be empty"); |
| 54 | return new PiTableId(scope, name); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns a table identifier for the given table name. |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 59 | * |
| 60 | * @param name table name |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 61 | * @return table identifier |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 62 | */ |
Carmelo Cascone | 7b82170 | 2017-06-19 11:26:08 +0900 | [diff] [blame] | 63 | public static PiTableId of(String name) { |
| 64 | checkNotNull(name); |
| 65 | checkArgument(!name.isEmpty(), "Name can't be empty"); |
| 66 | return new PiTableId(null, name); |
Carmelo Cascone | 1022a4e | 2017-05-25 00:16:18 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Returns the name of this table. |
| 72 | * |
| 73 | * @return table name |
| 74 | */ |
| 75 | public String name() { |
| 76 | return name; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the scope of this table, if present. |
| 81 | * |
| 82 | * @return optional scope |
| 83 | */ |
| 84 | public Optional<String> scope() { |
| 85 | return Optional.ofNullable(scope); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | } |