Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 18 | import com.google.common.annotations.Beta; |
Yuta HIGUCHI | fadb9a3 | 2017-01-13 09:33:10 -0800 | [diff] [blame] | 19 | |
| 20 | import static com.google.common.base.Preconditions.checkArgument; |
| 21 | |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 22 | import org.onlab.util.Identifier; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 23 | import org.onosproject.net.resource.ResourceConsumer; |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 24 | import org.onosproject.net.resource.ResourceConsumerId; |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 25 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 26 | /** |
| 27 | * Intent identifier suitable as an external key. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 28 | * <p>This class is immutable.</p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 29 | */ |
Brian O'Connor | 9476fa1 | 2015-06-25 15:17:17 -0400 | [diff] [blame] | 30 | @Beta |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 31 | public final class IntentId extends Identifier<Long> implements ResourceConsumer { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | |
Yuta HIGUCHI | fadb9a3 | 2017-01-13 09:33:10 -0800 | [diff] [blame] | 33 | private static final String HEX_PREFIX = "0x"; |
| 34 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 35 | /** |
Brian O'Connor | b7baf71 | 2015-07-28 23:27:03 -0700 | [diff] [blame] | 36 | * Creates an intent identifier from the specified long representation. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 37 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 38 | * @param value long value |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 39 | * @return intent identifier |
| 40 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 41 | public static IntentId valueOf(long value) { |
| 42 | return new IntentId(value); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** |
Yuta HIGUCHI | fadb9a3 | 2017-01-13 09:33:10 -0800 | [diff] [blame] | 46 | * Creates an intent identifier from the specified String representation. |
| 47 | * |
| 48 | * @param id hexadecimal String prefixed with 0x |
| 49 | * @return intent identifier |
| 50 | */ |
| 51 | public static IntentId valueOf(String id) { |
| 52 | checkArgument(id.startsWith(HEX_PREFIX), "Invalid id: %s", id); |
| 53 | return valueOf(Long.parseUnsignedLong(id.substring(2), 16)); |
| 54 | } |
| 55 | |
| 56 | /** |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 57 | * Constructor for serializer. |
| 58 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 59 | IntentId() { |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 60 | super(0L); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Constructs the ID corresponding to a given long value. |
| 65 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 66 | * @param value the underlying value of this ID |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 67 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 68 | IntentId(long value) { |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 69 | super(value); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 72 | /** |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 73 | * Returns the backing value. |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 74 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 75 | * @return the value |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 76 | */ |
| 77 | public long fingerprint() { |
Jian Li | b6d998e | 2016-02-29 11:41:18 -0800 | [diff] [blame] | 78 | return identifier; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public String toString() { |
Yuta HIGUCHI | fadb9a3 | 2017-01-13 09:33:10 -0800 | [diff] [blame] | 83 | return HEX_PREFIX + Long.toHexString(identifier); |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Naoki Shiota | bd1974c | 2016-04-29 18:44:17 -0700 | [diff] [blame] | 86 | @Override |
| 87 | public ResourceConsumerId consumerId() { |
| 88 | return ResourceConsumerId.of(this); |
| 89 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 90 | } |