Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstackvtap.api; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | import java.util.UUID; |
| 20 | |
| 21 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 22 | * Immutable representation of an openstack vtap identifier. |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 23 | */ |
| 24 | public final class OpenstackVtapId { |
| 25 | |
| 26 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 27 | * Represents either no vtap, or an unspecified vtap. |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 28 | */ |
| 29 | public static final OpenstackVtapId NONE = new OpenstackVtapId(null); |
| 30 | |
| 31 | private final UUID uuid; |
| 32 | |
| 33 | // Public construction is prohibited |
| 34 | private OpenstackVtapId(UUID uuid) { |
| 35 | this.uuid = uuid; |
| 36 | } |
| 37 | |
| 38 | // Default constructor for serialization |
| 39 | private OpenstackVtapId() { |
| 40 | this.uuid = null; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns an unique UUID. |
| 45 | * |
| 46 | * @return UUID |
| 47 | */ |
| 48 | public UUID uuid() { |
| 49 | return uuid; |
| 50 | } |
| 51 | |
| 52 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 53 | * Creates a vtap identifier using the supplied UUID. |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 54 | * |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 55 | * @param uuid vtap UUID |
| 56 | * @return vtap identifier |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 57 | */ |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 58 | public static OpenstackVtapId vtapId(UUID uuid) { |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 59 | return new OpenstackVtapId(uuid); |
| 60 | } |
| 61 | |
| 62 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 63 | * Creates a vtap identifier using the supplied string format of UUID. |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 64 | * |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 65 | * @param uuidString vtap UUID |
| 66 | * @return vtap identifier |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 67 | */ |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 68 | public static OpenstackVtapId vtapId(String uuidString) { |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 69 | try { |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 70 | return vtapId(UUID.fromString(uuidString)); |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 71 | } catch (Exception e) { |
| 72 | throw new IllegalArgumentException("Invalid UUID string: " + uuidString); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 77 | * Creates a openstack vtap id using random uuid. |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 78 | * |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 79 | * @return openstack vtap identifier |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 80 | */ |
Jimo Jung | 14e87bf | 2018-09-03 16:28:13 +0900 | [diff] [blame] | 81 | public static OpenstackVtapId vtapId() { |
Jian Li | 38e4d94 | 2018-07-03 22:19:16 +0900 | [diff] [blame] | 82 | return new OpenstackVtapId(UUID.randomUUID()); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
| 87 | return uuid.toString(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hash(uuid); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) { |
| 98 | return true; |
| 99 | } |
| 100 | if (obj instanceof OpenstackVtapId) { |
| 101 | final OpenstackVtapId other = (OpenstackVtapId) obj; |
| 102 | return Objects.equals(this.uuid, other.uuid); |
| 103 | } |
| 104 | return false; |
| 105 | } |
| 106 | } |