blob: e8e4af76b31a83f8653c6da1869c24f39a80899a [file] [log] [blame]
Brian O'Connor055fb972015-02-05 11:40:51 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080047 @Override
48 public int hashCode() {
Sho SHIMIZU87114512015-05-08 14:31:00 -070049 return Long.hashCode(hash);
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080050 }
51
52 @Override
53 public abstract boolean equals(Object obj);
54
55 /**
56 * Creates a key based on the provided string.
57 * <p>
58 * Note: Two keys with equal value, but different appId, are not equal.
Luca Pretede10c782017-01-05 17:23:08 -080059 * Warning: it is caller responsibility to make sure the hashed value of
60 * {@code value} is unique.
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080061 * </p>
62 *
63 * @param key the provided string
64 * @param appId application id to associate with this key
65 * @return the key for the string
66 */
Brian O'Connor055fb972015-02-05 11:40:51 -080067 public static Key of(String key, ApplicationId appId) {
68 return new StringKey(key, appId);
69 }
70
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080071 /**
72 * Creates a key based on the provided long.
73 * <p>
74 * Note: Two keys with equal value, but different appId, are not equal.
75 * Also, "10" and 10L are different.
76 * </p>
77 *
78 * @param key the provided long
79 * @param appId application id to associate with this key
80 * @return the key for the long
81 */
Brian O'Connor055fb972015-02-05 11:40:51 -080082 public static Key of(long key, ApplicationId appId) {
83 return new LongKey(key, appId);
84 }
85
Luca Pretede10c782017-01-05 17:23:08 -080086 @Override
87 public ResourceConsumerId consumerId() {
88 return ResourceConsumerId.of(hash(), getClass());
89 }
90
Brian O'Connor4e6c17d2015-02-19 11:40:05 -080091 private static final class StringKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -080092
93 private final ApplicationId appId;
94 private final String key;
95
96 private StringKey(String key, ApplicationId appId) {
97 super(HASH_FN.newHasher()
98 .putShort(appId.id())
99 .putString(key, StandardCharsets.UTF_8)
100 .hash().asLong());
101 this.key = key;
102 this.appId = appId;
103 }
104
105 @Override
106 public String toString() {
107 return key;
108 }
109
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800110 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800111 @Override
112 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800113 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800114 }
115
116 @Override
117 public boolean equals(Object obj) {
118 if (this == obj) {
119 return true;
120 }
121 if (obj == null || getClass() != obj.getClass()) {
122 return false;
123 }
124 final StringKey other = (StringKey) obj;
125 return this.hash() == other.hash() &&
126 Objects.equals(this.appId, other.appId) &&
127 Objects.equals(this.key, other.key);
128 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700129
130 @Override
131 public int compareTo(Key o) {
132 StringKey sk = (StringKey) o;
133 return this.key.compareTo(sk.key);
134 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800135 }
136
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800137 private static final class LongKey extends Key {
Brian O'Connor055fb972015-02-05 11:40:51 -0800138
139 private final ApplicationId appId;
Ray Milkey5ad39662015-02-10 15:12:14 -0800140 private final long key;
Brian O'Connor055fb972015-02-05 11:40:51 -0800141
142 private LongKey(long key, ApplicationId appId) {
143 super(HASH_FN.newHasher()
144 .putShort(appId.id())
145 .putLong(key)
146 .hash().asLong());
147 this.key = key;
148 this.appId = appId;
149 }
150
151 @Override
152 public String toString() {
Ray Milkey3af13862015-02-10 15:23:13 -0800153 return "0x" + Long.toHexString(key);
Brian O'Connor055fb972015-02-05 11:40:51 -0800154 }
155
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800156 // checkstyle requires this
Brian O'Connor055fb972015-02-05 11:40:51 -0800157 @Override
158 public int hashCode() {
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800159 return super.hashCode();
Brian O'Connor055fb972015-02-05 11:40:51 -0800160 }
161
162 @Override
163 public boolean equals(Object obj) {
164 if (this == obj) {
165 return true;
166 }
167 if (obj == null || getClass() != obj.getClass()) {
168 return false;
169 }
170 final LongKey other = (LongKey) obj;
Brian O'Connor4e6c17d2015-02-19 11:40:05 -0800171 return this.hash() == other.hash() &&
172 this.key == other.key &&
173 Objects.equals(this.appId, other.appId);
Brian O'Connor055fb972015-02-05 11:40:51 -0800174 }
Simon Hunte2b6a2b2015-10-27 11:38:51 -0700175
176 @Override
177 public int compareTo(Key o) {
178 Long myKey = key;
179 Long otherKey = ((LongKey) o).key;
180 return myKey.compareTo(otherKey);
181 }
Brian O'Connor055fb972015-02-05 11:40:51 -0800182 }
183}
184
185