blob: 9799b7847e182ab8fb4f748e83885175b3dbd413 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Brian O'Connor9476fa12015-06-25 15:17:17 -040018import com.google.common.annotations.Beta;
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080019
20import static com.google.common.base.Preconditions.checkArgument;
21
Jian Lib6d998e2016-02-29 11:41:18 -080022import org.onlab.util.Identifier;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080023import org.onosproject.net.resource.ResourceConsumer;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070024import org.onosproject.net.resource.ResourceConsumerId;
Brian O'Connor9476fa12015-06-25 15:17:17 -040025
Brian O'Connorb876bf12014-10-02 14:59:37 -070026/**
27 * Intent identifier suitable as an external key.
Thomas Vachuska4b420772014-10-30 16:46:17 -070028 * <p>This class is immutable.</p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070029 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040030@Beta
Jian Lib6d998e2016-02-29 11:41:18 -080031public final class IntentId extends Identifier<Long> implements ResourceConsumer {
Brian O'Connorb876bf12014-10-02 14:59:37 -070032
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080033 private static final String HEX_PREFIX = "0x";
34
Brian O'Connorb876bf12014-10-02 14:59:37 -070035 /**
Brian O'Connorb7baf712015-07-28 23:27:03 -070036 * Creates an intent identifier from the specified long representation.
Brian O'Connorb876bf12014-10-02 14:59:37 -070037 *
Brian O'Connor520c0522014-11-23 23:50:47 -080038 * @param value long value
Brian O'Connorb876bf12014-10-02 14:59:37 -070039 * @return intent identifier
40 */
Brian O'Connor520c0522014-11-23 23:50:47 -080041 public static IntentId valueOf(long value) {
42 return new IntentId(value);
Brian O'Connorb876bf12014-10-02 14:59:37 -070043 }
44
45 /**
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080046 * 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'Connorb876bf12014-10-02 14:59:37 -070057 * Constructor for serializer.
58 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070059 IntentId() {
Jian Lib6d998e2016-02-29 11:41:18 -080060 super(0L);
Brian O'Connorb876bf12014-10-02 14:59:37 -070061 }
62
63 /**
64 * Constructs the ID corresponding to a given long value.
65 *
Brian O'Connor520c0522014-11-23 23:50:47 -080066 * @param value the underlying value of this ID
Brian O'Connorb876bf12014-10-02 14:59:37 -070067 */
Brian O'Connor520c0522014-11-23 23:50:47 -080068 IntentId(long value) {
Jian Lib6d998e2016-02-29 11:41:18 -080069 super(value);
Brian O'Connorb876bf12014-10-02 14:59:37 -070070 }
71
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080072 /**
Brian O'Connor520c0522014-11-23 23:50:47 -080073 * Returns the backing value.
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080074 *
Brian O'Connor520c0522014-11-23 23:50:47 -080075 * @return the value
Thomas Vachuskaedc944c2014-11-04 15:42:25 -080076 */
77 public long fingerprint() {
Jian Lib6d998e2016-02-29 11:41:18 -080078 return identifier;
Brian O'Connorb876bf12014-10-02 14:59:37 -070079 }
80
81 @Override
82 public String toString() {
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080083 return HEX_PREFIX + Long.toHexString(identifier);
Brian O'Connorb876bf12014-10-02 14:59:37 -070084 }
85
Naoki Shiotabd1974c2016-04-29 18:44:17 -070086 @Override
87 public ResourceConsumerId consumerId() {
88 return ResourceConsumerId.of(this);
89 }
Brian O'Connorb876bf12014-10-02 14:59:37 -070090}