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 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Global identifier of a PI table entry applied on a device, uniquely defined |
| 29 | * by a device ID, table ID and match key. |
| 30 | */ |
| 31 | @Beta |
| 32 | public final class PiTableEntryHandle extends PiHandle<PiTableEntry> { |
| 33 | |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 34 | private final PiTableId tableId; |
| 35 | private final PiMatchKey matchKey; |
| 36 | |
| 37 | private PiTableEntryHandle(DeviceId deviceId, PiTableId tableId, PiMatchKey matchKey) { |
| 38 | super(deviceId); |
| 39 | this.tableId = tableId; |
| 40 | this.matchKey = matchKey; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Creates a new handle for the given device ID, PI table ID, and match |
| 45 | * key. |
| 46 | * |
| 47 | * @param deviceId device ID |
| 48 | * @param tableId table ID |
| 49 | * @param matchKey match key |
| 50 | * @return PI table entry handle |
| 51 | */ |
| 52 | public static PiTableEntryHandle of(DeviceId deviceId, PiTableId tableId, PiMatchKey matchKey) { |
| 53 | checkNotNull(tableId); |
| 54 | checkNotNull(matchKey); |
| 55 | return new PiTableEntryHandle(deviceId, tableId, matchKey); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Creates a new handle for the given PI table entry and device ID. |
| 60 | * |
| 61 | * @param deviceId device ID |
| 62 | * @param entry PI table entry |
| 63 | * @return PI table entry handle |
| 64 | */ |
| 65 | public static PiTableEntryHandle of(DeviceId deviceId, PiTableEntry entry) { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 66 | checkNotNull(entry); |
| 67 | return PiTableEntryHandle.of(deviceId, entry.table(), entry.matchKey()); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public PiEntityType entityType() { |
| 72 | return PiEntityType.TABLE_ENTRY; |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public int hashCode() { |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 77 | return Objects.hashCode(deviceId(), tableId, matchKey); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean equals(Object obj) { |
| 82 | if (this == obj) { |
| 83 | return true; |
| 84 | } |
| 85 | if (obj == null || getClass() != obj.getClass()) { |
| 86 | return false; |
| 87 | } |
| 88 | final PiTableEntryHandle other = (PiTableEntryHandle) obj; |
| 89 | return Objects.equal(this.deviceId(), other.deviceId()) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 90 | && Objects.equal(this.tableId, other.tableId) |
| 91 | && Objects.equal(this.matchKey, other.matchKey); |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public String toString() { |
| 96 | return MoreObjects.toStringHelper(this) |
| 97 | .add("deviceId", deviceId()) |
Carmelo Cascone | e44592f | 2018-09-12 02:24:47 -0700 | [diff] [blame] | 98 | .add("tableId", tableId) |
| 99 | .add("matchKey", matchKey) |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 100 | .toString(); |
| 101 | } |
| 102 | } |