blob: 2b210a1cbd04dbad0571c2780192459c906e8f2d [file] [log] [blame]
Carmelo Cascone326ad2d2017-11-28 18:09:13 -08001/*
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22import org.onosproject.net.DeviceId;
Carmelo Casconee44592f2018-09-12 02:24:47 -070023import org.onosproject.net.pi.model.PiTableId;
24
25import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080026
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
32public final class PiTableEntryHandle extends PiHandle<PiTableEntry> {
33
Carmelo Casconee44592f2018-09-12 02:24:47 -070034 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 Cascone326ad2d2017-11-28 18:09:13 -080056 }
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 Casconee44592f2018-09-12 02:24:47 -070066 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 Cascone326ad2d2017-11-28 18:09:13 -080073 }
74
75 @Override
76 public int hashCode() {
Carmelo Casconee44592f2018-09-12 02:24:47 -070077 return Objects.hashCode(deviceId(), tableId, matchKey);
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080078 }
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 Casconee44592f2018-09-12 02:24:47 -070090 && Objects.equal(this.tableId, other.tableId)
91 && Objects.equal(this.matchKey, other.matchKey);
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080092 }
93
94 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(this)
97 .add("deviceId", deviceId())
Carmelo Casconee44592f2018-09-12 02:24:47 -070098 .add("tableId", tableId)
99 .add("matchKey", matchKey)
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800100 .toString();
101 }
102}