Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
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 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 47 | @Override |
| 48 | public int hashCode() { |
Sho SHIMIZU | 8711451 | 2015-05-08 14:31:00 -0700 | [diff] [blame] | 49 | return Long.hashCode(hash); |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 50 | } |
| 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 Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 59 | * Warning: it is caller responsibility to make sure the hashed value of |
| 60 | * {@code value} is unique. |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 61 | * </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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 67 | public static Key of(String key, ApplicationId appId) { |
| 68 | return new StringKey(key, appId); |
| 69 | } |
| 70 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 71 | /** |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 82 | public static Key of(long key, ApplicationId appId) { |
| 83 | return new LongKey(key, appId); |
| 84 | } |
| 85 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 86 | @Override |
| 87 | public ResourceConsumerId consumerId() { |
| 88 | return ResourceConsumerId.of(hash(), getClass()); |
| 89 | } |
| 90 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 91 | private static final class StringKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 92 | |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 110 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 111 | @Override |
| 112 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 113 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 114 | } |
| 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 Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 129 | |
| 130 | @Override |
| 131 | public int compareTo(Key o) { |
| 132 | StringKey sk = (StringKey) o; |
| 133 | return this.key.compareTo(sk.key); |
| 134 | } |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 137 | private static final class LongKey extends Key { |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 138 | |
| 139 | private final ApplicationId appId; |
Ray Milkey | 5ad3966 | 2015-02-10 15:12:14 -0800 | [diff] [blame] | 140 | private final long key; |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 141 | |
| 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 Milkey | 3af1386 | 2015-02-10 15:23:13 -0800 | [diff] [blame] | 153 | return "0x" + Long.toHexString(key); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 156 | // checkstyle requires this |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 157 | @Override |
| 158 | public int hashCode() { |
Brian O'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 159 | return super.hashCode(); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 160 | } |
| 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'Connor | 4e6c17d | 2015-02-19 11:40:05 -0800 | [diff] [blame] | 171 | return this.hash() == other.hash() && |
| 172 | this.key == other.key && |
| 173 | Objects.equals(this.appId, other.appId); |
Brian O'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 174 | } |
Simon Hunt | e2b6a2b | 2015-10-27 11:38:51 -0700 | [diff] [blame] | 175 | |
| 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'Connor | 055fb97 | 2015-02-05 11:40:51 -0800 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | |