blob: c70fb03d8c88687e987047e42c3a9085b17199d5 [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/**
Jimo Jung14e87bf2018-09-03 16:28:13 +090022 * Immutable representation of an openstack vtap identifier.
Jian Li38e4d942018-07-03 22:19:16 +090023 */
24public final class OpenstackVtapId {
25
26 /**
Jimo Jung14e87bf2018-09-03 16:28:13 +090027 * Represents either no vtap, or an unspecified vtap.
Jian Li38e4d942018-07-03 22:19:16 +090028 */
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 Jung14e87bf2018-09-03 16:28:13 +090053 * Creates a vtap identifier using the supplied UUID.
Jian Li38e4d942018-07-03 22:19:16 +090054 *
Jimo Jung14e87bf2018-09-03 16:28:13 +090055 * @param uuid vtap UUID
56 * @return vtap identifier
Jian Li38e4d942018-07-03 22:19:16 +090057 */
Jimo Jung14e87bf2018-09-03 16:28:13 +090058 public static OpenstackVtapId vtapId(UUID uuid) {
Jian Li38e4d942018-07-03 22:19:16 +090059 return new OpenstackVtapId(uuid);
60 }
61
62 /**
Jimo Jung14e87bf2018-09-03 16:28:13 +090063 * Creates a vtap identifier using the supplied string format of UUID.
Jian Li38e4d942018-07-03 22:19:16 +090064 *
Jimo Jung14e87bf2018-09-03 16:28:13 +090065 * @param uuidString vtap UUID
66 * @return vtap identifier
Jian Li38e4d942018-07-03 22:19:16 +090067 */
Jimo Jung14e87bf2018-09-03 16:28:13 +090068 public static OpenstackVtapId vtapId(String uuidString) {
Jian Li38e4d942018-07-03 22:19:16 +090069 try {
Jimo Jung14e87bf2018-09-03 16:28:13 +090070 return vtapId(UUID.fromString(uuidString));
Jian Li38e4d942018-07-03 22:19:16 +090071 } catch (Exception e) {
72 throw new IllegalArgumentException("Invalid UUID string: " + uuidString);
73 }
74 }
75
76 /**
Jimo Jung14e87bf2018-09-03 16:28:13 +090077 * Creates a openstack vtap id using random uuid.
Jian Li38e4d942018-07-03 22:19:16 +090078 *
Jimo Jung14e87bf2018-09-03 16:28:13 +090079 * @return openstack vtap identifier
Jian Li38e4d942018-07-03 22:19:16 +090080 */
Jimo Jung14e87bf2018-09-03 16:28:13 +090081 public static OpenstackVtapId vtapId() {
Jian Li38e4d942018-07-03 22:19:16 +090082 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}