blob: b4edb3cd56e555c455ed7aa5c439e176a3e49681 [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
Carmelo Cascone4c289b72019-01-22 15:30:45 -080025import java.util.OptionalInt;
26
Carmelo Casconee44592f2018-09-12 02:24:47 -070027import static com.google.common.base.Preconditions.checkNotNull;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080028
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 Cascone4c289b72019-01-22 15:30:45 -080034public final class PiTableEntryHandle extends PiHandle {
35
36 private static final int NO_PRIORITY = -1;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080037
Carmelo Casconee44592f2018-09-12 02:24:47 -070038 private final PiTableId tableId;
39 private final PiMatchKey matchKey;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080040 private final int priority;
Carmelo Casconee44592f2018-09-12 02:24:47 -070041
Carmelo Cascone4c289b72019-01-22 15:30:45 -080042 private PiTableEntryHandle(DeviceId deviceId, PiTableId tableId, PiMatchKey matchKey,
43 Integer priority) {
Carmelo Casconee44592f2018-09-12 02:24:47 -070044 super(deviceId);
45 this.tableId = tableId;
46 this.matchKey = matchKey;
Carmelo Cascone4c289b72019-01-22 15:30:45 -080047 this.priority = priority;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080048 }
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 Casconee44592f2018-09-12 02:24:47 -070058 checkNotNull(entry);
Carmelo Cascone4c289b72019-01-22 15:30:45 -080059 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 Casconee44592f2018-09-12 02:24:47 -070090 }
91
92 @Override
93 public PiEntityType entityType() {
94 return PiEntityType.TABLE_ENTRY;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080095 }
96
97 @Override
98 public int hashCode() {
Carmelo Cascone4c289b72019-01-22 15:30:45 -080099 return Objects.hashCode(deviceId(), tableId, matchKey, priority().orElse(NO_PRIORITY));
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800100 }
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 Casconee44592f2018-09-12 02:24:47 -0700112 && Objects.equal(this.tableId, other.tableId)
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800113 && Objects.equal(this.matchKey, other.matchKey)
114 && Objects.equal(this.priority(), other.priority());
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800115 }
116
117 @Override
118 public String toString() {
119 return MoreObjects.toStringHelper(this)
120 .add("deviceId", deviceId())
Carmelo Casconee44592f2018-09-12 02:24:47 -0700121 .add("tableId", tableId)
122 .add("matchKey", matchKey)
Carmelo Cascone4c289b72019-01-22 15:30:45 -0800123 .add("priority", priority == NO_PRIORITY ? "N/A" : priority)
Carmelo Cascone326ad2d2017-11-28 18:09:13 -0800124 .toString();
125 }
126}