Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import org.onosproject.net.DeviceId; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 23 | import org.onosproject.net.pi.model.PiTableId; |
| 24 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 25 | import java.util.OptionalInt; |
| 26 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Global identifier of a PI table entry applied on a device, uniquely defined |
| 31 | * by a device ID, table ID and match key. |
| 32 | */ |
| 33 | @Beta |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 34 | public final class PiTableEntryHandle extends PiHandle { |
| 35 | |
| 36 | private static final int NO_PRIORITY = -1; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 37 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 38 | private final PiTableId tableId; |
| 39 | private final PiMatchKey matchKey; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 40 | private final int priority; |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 41 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 42 | private PiTableEntryHandle(DeviceId deviceId, PiTableId tableId, PiMatchKey matchKey, |
| 43 | Integer priority) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 44 | super(deviceId); |
| 45 | this.tableId = tableId; |
| 46 | this.matchKey = matchKey; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 47 | this.priority = priority; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new handle for the given PI table entry and device ID. |
| 52 | * |
| 53 | * @param deviceId device ID |
| 54 | * @param entry PI table entry |
| 55 | * @return PI table entry handle |
| 56 | */ |
| 57 | public static PiTableEntryHandle of(DeviceId deviceId, PiTableEntry entry) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 58 | checkNotNull(entry); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 59 | return new PiTableEntryHandle( |
| 60 | deviceId, entry.table(), entry.matchKey(), |
| 61 | entry.priority().orElse(NO_PRIORITY)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns the table ID associated with this handle. |
| 66 | * |
| 67 | * @return table ID |
| 68 | */ |
| 69 | public PiTableId tableId() { |
| 70 | return tableId; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the match key associated with this handle. |
| 75 | * |
| 76 | * @return match key |
| 77 | */ |
| 78 | public PiMatchKey matchKey() { |
| 79 | return matchKey; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the optional priority associated with this handle. |
| 84 | * |
| 85 | * @return optional priority |
| 86 | */ |
| 87 | public OptionalInt priority() { |
| 88 | return priority == NO_PRIORITY |
| 89 | ? OptionalInt.empty() : OptionalInt.of(priority); |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public PiEntityType entityType() { |
| 94 | return PiEntityType.TABLE_ENTRY; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int hashCode() { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 99 | return Objects.hashCode(deviceId(), tableId, matchKey, priority().orElse(NO_PRIORITY)); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public boolean equals(Object obj) { |
| 104 | if (this == obj) { |
| 105 | return true; |
| 106 | } |
| 107 | if (obj == null || getClass() != obj.getClass()) { |
| 108 | return false; |
| 109 | } |
| 110 | final PiTableEntryHandle other = (PiTableEntryHandle) obj; |
| 111 | return Objects.equal(this.deviceId(), other.deviceId()) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 112 | && Objects.equal(this.tableId, other.tableId) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 113 | && Objects.equal(this.matchKey, other.matchKey) |
| 114 | && Objects.equal(this.priority(), other.priority()); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | return MoreObjects.toStringHelper(this) |
| 120 | .add("deviceId", deviceId()) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 121 | .add("tableId", tableId) |
| 122 | .add("matchKey", matchKey) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 123 | .add("priority", priority == NO_PRIORITY ? "N/A" : priority) |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 124 | .toString(); |
| 125 | } |
| 126 | } |