blob: fadf8b92cc437ee8a6487cc196b2bb8f4af1ee56 [file] [log] [blame]
Brian O'Connor055fb972015-02-05 11:40:51 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Brian O'Connor055fb972015-02-05 11:40:51 -08003 *
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 */
16package org.onosproject.net.intent;
17
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Brian O'Connor055fb972015-02-05 11:40:51 -080019import com.google.common.hash.HashFunction;
20import com.google.common.hash.Hashing;
21import org.onosproject.core.ApplicationId;
Luca Pretede10c782017-01-05 17:23:08 -080022import org.onosproject.net.resource.ResourceConsumer;
23import org.onosproject.net.resource.ResourceConsumerId;
Brian O'Connor055fb972015-02-05 11:40:51 -080024
25import java.nio.charset.StandardCharsets;
26import java.util.Objects;
27
28/**
29 * Key class for Intents.
30 */
31// TODO maybe pull this up to utils
Brian O'Connor9476fa12015-06-25 15:17:17 -040032@Beta
Luca Pretede10c782017-01-05 17:23:08 -080033public abstract class Key implements Comparable<Key>, ResourceConsumer {
Brian O'Connor055fb972015-02-05 11:40:51 -080034
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080035 //TODO consider making this a HashCode object (worry about performance)
Brian O'Connor055fb972015-02-05 11:40:51 -080036 private final long hash;
37 private static final HashFunction HASH_FN = Hashing.md5();
38
Ray Milkey5b3717e2015-02-05 11:44:08 -080039 protected Key(long hash) {
Brian O'Connor055fb972015-02-05 11:40:51 -080040 this.hash = hash;
41 }
42
43 public long hash() {
44 return hash;
45 }
46
Yuta HIGUCHIe7e71a82018-05-18 16:36:43 -070047 @SuppressWarnings("checkstyle:EqualsHashCode")
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080048 @Override
49 public int hashCode() {
Sho SHIMIZU87114512015-05-08 14:31:00 -070050 return Long.hashCode(hash);
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080051 }
52
53 @Override
54 public abstract boolean equals(Object obj);
55
56 /**
57 * Creates a key based on the provided string.
58 * <p>
59 * Note: Two keys with equal value, but different appId, are not equal.
Luca Pretede10c782017-01-05 17:23:08 -080060 * Warning: it is caller responsibility to make sure the hashed value of
61 * {@code value} is unique.
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080062 * </p>
63 *
64 * @param key the provided string
65 * @param appId application id to associate with this key
66 * @return the key for the string
67 */
Brian O'Connor055fb972015-02-05 11:40:51 -080068 public static Key of(String key, ApplicationId appId) {
69 return new StringKey(key, appId);
70 }
71
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080072 /**
73 * Creates a key based on the provided long.
74 * <p>
75 * Note: Two keys with equal value, but different appId, are not equal.
76 * Also, "10" and 10L are different.
77 * </p>
78 *
79 * @param key the provided long
80 * @param appId application id to associate with this key
81 * @return the key for the long
82 */
Brian O'Connor055fb972015-02-05 11:40:51 -080083 public static Key of(long key, ApplicationId appId) {
84 return new LongKey(key, appId);
85 }
86
Luca Pretede10c782017-01-05 17:23:08 -080087 @Override
88 public ResourceConsumerId consumerId() {
89 return ResourceConsumerId.of(hash(), getClass());
90 }
91
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080092 private static final class StringKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080093
94 private final ApplicationId appId;
95 private final String key;
96
97 private StringKey(String key, ApplicationId appId) {
98 super(HASH_FN.newHasher()
99 .putShort(appId.id())
100 .putString(key, StandardCharsets.UTF_8)
101 .hash().asLong());
102 this.key = key;
103 this.appId = appId;
104 }
105
106 @Override
107 public String toString() {
108 return key;
109 }
110
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800111 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800112 @Override
113 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800114 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800115 }
116
117 @Override
118 public boolean equals(Object obj) {
119 if (this == obj) {
120 return true;
121 }
122 if (obj == null || getClass() != obj.getClass()) {
123 return false;
124 }
125 final StringKey other = (StringKey) obj;
126 return this.hash() == other.hash() &&
127 Objects.equals(this.appId, other.appId) &&
128 Objects.equals(this.key, other.key);
129 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700130
131 @Override
132 public int compareTo(Key o) {
133 StringKey sk = (StringKey) o;
134 return this.key.compareTo(sk.key);
135 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800136 }
137
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800138 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -0800139
140 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -0800141 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -0800142
143 private LongKey(long key, ApplicationId appId) {
144 super(HASH_FN.newHasher()
145 .putShort(appId.id())
146 .putLong(key)
147 .hash().asLong());
148 this.key = key;
149 this.appId = appId;
150 }
151
152 @Override
153 public String toString() {
Ray Milkey3af13862015-02-10 15:23:13 -0800154 return "0x" + Long.toHexString(key);
Brian O'Connor055fb972015-02-05 11:40:51 -0800155 }
156
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800157 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800158 @Override
159 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800160 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800161 }
162
163 @Override
164 public boolean equals(Object obj) {
165 if (this == obj) {
166 return true;
167 }
168 if (obj == null || getClass() != obj.getClass()) {
169 return false;
170 }
171 final LongKey other = (LongKey) obj;
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800172 return this.hash() == other.hash() &&
173 this.key == other.key &&
174 Objects.equals(this.appId, other.appId);
Brian O'Connor055fb972015-02-05 11:40:51 -0800175 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700176
177 @Override
178 public int compareTo(Key o) {
179 Long myKey = key;
180 Long otherKey = ((LongKey) o).key;
181 return myKey.compareTo(otherKey);
182 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800183 }
184}
185
186