Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 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 | package org.onosproject.net.intent; |
| 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 19 | import com.google.common.hash.HashFunction; |
| 20 | import com.google.common.hash.Hashing; |
| 21 | import org.onosproject.core.ApplicationId; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 22 | import org.onosproject.net.resource.ResourceConsumer; |
| 23 | import org.onosproject.net.resource.ResourceConsumerId; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 24 | |
| 25 | import java.nio.charset.StandardCharsets; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | /** |
| 29 | * Key class for Intents. |
| 30 | */ |
| 31 | // TODO maybe pull this up to utils |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 32 | @Beta |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 33 | public abstract class Key implements Comparable<Key>, ResourceConsumer { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 34 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 35 | //TODO consider making this a HashCode object (worry about performance) |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 36 | private final long hash; |
| 37 | private static final HashFunction HASH_FN = Hashing.md5(); |
| 38 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 39 | protected Key(long hash) { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 40 | this.hash = hash; |
| 41 | } |
| 42 | |
| 43 | public long hash() { |
| 44 | return hash; |
| 45 | } |
| 46 | |
Yuta HIGUCHI | e7e71a8 | 2018-05-18 16:36:43 -0700 | [diff] [blame] | 47 | @SuppressWarnings("checkstyle:EqualsHashCode") |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 48 | @Override |
| 49 | public int hashCode() { |
Sho SHIMIZU | 8711451 | 2015-05-08 14:31:00 -0700 | [diff] [blame] | 50 | return Long.hashCode(hash); |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 51 | } |
| 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 Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 60 | * Warning: it is caller responsibility to make sure the hashed value of |
| 61 | * {@code value} is unique. |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 62 | * </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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 68 | public static Key of(String key, ApplicationId appId) { |
| 69 | return new StringKey(key, appId); |
| 70 | } |
| 71 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 72 | /** |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 83 | public static Key of(long key, ApplicationId appId) { |
| 84 | return new LongKey(key, appId); |
| 85 | } |
| 86 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 87 | @Override |
| 88 | public ResourceConsumerId consumerId() { |
| 89 | return ResourceConsumerId.of(hash(), getClass()); |
| 90 | } |
| 91 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 92 | private static final class StringKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 93 | |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 111 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 112 | @Override |
| 113 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 114 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 115 | } |
| 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 Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 130 | |
| 131 | @Override |
| 132 | public int compareTo(Key o) { |
| 133 | StringKey sk = (StringKey) o; |
| 134 | return this.key.compareTo(sk.key); |
| 135 | } |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 138 | private static final class LongKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 139 | |
| 140 | private final ApplicationId appId; |
Ray Milkey | 5ad3966 | 2015-02-10 15:12:14 -0800 | [diff] [blame] | 141 | private final long key; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 142 | |
| 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 Milkey | 3af1386 | 2015-02-10 15:23:13 -0800 | [diff] [blame] | 154 | return "0x" + Long.toHexString(key); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 157 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 158 | @Override |
| 159 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 160 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 161 | } |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 172 | return this.hash() == other.hash() && |
| 173 | this.key == other.key && |
| 174 | Objects.equals(this.appId, other.appId); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 175 | } |
Simon Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 176 | |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | |