blob: ce8945dfb955a7476e0f97d67d356fe5f393e61a [file] [log] [blame]
Jian Li38e4d942018-07-03 22:19:16 +09001/*
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 */
16package org.onosproject.openstackvtap.api;
17
18import java.util.Objects;
19import java.util.UUID;
20
21/**
22 * Immutable representation of an openstack vTap identifier.
23 */
24public final class OpenstackVtapId {
25
26 /**
27 * Represents either no vTap, or an unspecified vTap.
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 /**
53 * Creates a vTap identifier using the supplied UUID.
54 *
55 * @param uuid vTap UUID
56 * @return vTap identifier
57 */
58 public static OpenstackVtapId vTapId(UUID uuid) {
59 return new OpenstackVtapId(uuid);
60 }
61
62 /**
63 * Creates a vTap identifier using the supplied string format of UUID.
64 *
65 * @param uuidString vTap UUID
66 * @return vTap identifier
67 */
68 public static OpenstackVtapId vTapId(String uuidString) {
69 try {
70 return vTapId(UUID.fromString(uuidString));
71 } catch (Exception e) {
72 throw new IllegalArgumentException("Invalid UUID string: " + uuidString);
73 }
74 }
75
76 /**
77 * Creates a OpenstackVtap id using random uuid.
78 *
79 * @return OpenstackVtap identifier
80 */
81 public static OpenstackVtapId vTapId() {
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}