checkstyle: TypeName
[neutron.git] / neutron-spi / src / main / java / org / opendaylight / neutron / spi / NeutronID.java
1 /*
2  * Copyright (c) 2015 IBM Corporation and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import java.util.regex.Pattern;
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
18
19 @XmlRootElement
20 @XmlAccessorType(XmlAccessType.NONE)
21 public class NeutronID implements Serializable {
22     private static final long serialVersionUID = 1L;
23     private static final String UUID_PATTERN_REGEX =
24             "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$";
25     private static final Pattern UUID_PATTERN = Pattern.compile(UUID_PATTERN_REGEX);
26
27     // See OpenStack Network API v2.0 Reference for description of
28     // annotated attributes
29
30     @XmlElement(name = "id")
31     String uuid;
32
33     private void checkUuidPattern(String uuid) {
34         Preconditions.checkNotNull(uuid, "Supplied value may not be null");
35         Preconditions.checkArgument(UUID_PATTERN.matcher(uuid).matches(),
36                 "Supplied value \"%s\" does not match uuid pattern \"%s\"", uuid, UUID_PATTERN_REGEX);
37     }
38
39     public NeutronID() {
40     }
41
42     public NeutronID(String uuid) {
43         checkUuidPattern(uuid);
44         this.uuid = uuid;
45     }
46
47     public String getID() {
48         return uuid;
49     }
50
51     public void setID(String uuid) {
52         checkUuidPattern(uuid);
53         this.uuid = uuid;
54     }
55
56     @Override
57     public String toString() {
58         return "NeutronID{" + "id='" + uuid + '\'' + "}";
59     }
60 }