Neutron Model update
[neutron.git] / neutron-spi / src / main / java / org / opendaylight / neutron / spi / NeutronObject.java
1 /*
2  * Copyright (c) 2015 Intel Corporation  All rights reserved.
3  * Copyright (c) 2015 Isaku Yamahata  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.neutron.spi;
11
12 import com.fasterxml.jackson.annotation.JsonIgnore;
13 import java.io.Serializable;
14 import java.util.List;
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlElement;
18 import javax.xml.bind.annotation.XmlRootElement;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
20
21 @XmlRootElement
22 @XmlAccessorType(XmlAccessType.NONE)
23 public abstract class NeutronObject<T extends NeutronObject> extends NeutronID
24         implements Serializable, INeutronObject<T> {
25     private static final long serialVersionUID = 1L;
26
27     @XmlElement(name = "tenant_id")
28     String tenantID;
29
30     @XmlElement(name = "project_id")
31     String projectID;
32
33     @XmlElement(name = "revision_number")
34     Long revisionNumber;
35
36     public NeutronObject() {
37         super();
38     }
39
40     @Override
41     public String getTenantID() {
42         if (tenantID != null && tenantID.isEmpty()) {
43             // Bug 4775 - Treat empty string tenantId as null, so no attempt is made
44             //            to turn it into a uuid.
45             return null;
46         }
47         return tenantID;
48     }
49
50     @Override
51     public void setTenantID(String tenantID) {
52         this.tenantID = tenantID;
53     }
54
55     @Override
56     @JsonIgnore
57     public void setTenantID(Uuid tenantID) {
58         this.tenantID = tenantID.getValue().replace("-", "");
59     }
60
61     @Override
62     public String toString() {
63         return "NeutronObject [id=" + uuid + ", tenantID=" + tenantID + "]";
64     }
65
66     @Override
67     public void setProjectID(String projectID) {
68         this.projectID = projectID;
69     }
70
71     @Override
72     public String getProjectID() {
73         return this.projectID;
74     }
75
76     @Override
77     public Long getRevisionNumber() {
78         return revisionNumber;
79     }
80
81     @Override
82     public void setRevisionNumber(Long revisionNumber) {
83         this.revisionNumber = revisionNumber;
84     }
85
86     @Override
87     public void initDefaults() {
88         if (projectID != null && tenantID == null) {
89             tenantID = projectID;
90         }
91         if (projectID == null && tenantID != null) {
92             projectID = tenantID;
93         }
94     }
95
96     @Override
97     public abstract T extractFields(List<String> fields);
98
99     protected void extractField(String field, T ans) {
100         if (field.equals("id")) {
101             ans.setID(this.getID());
102         }
103         if (field.equals("tenant_id")) {
104             ans.setTenantID(this.getTenantID());
105         }
106         if (field.equals("project_id")) {
107             ans.setProjectID(this.getProjectID());
108         }
109         if (field.equals("revision_number")) {
110             ans.setRevisionNumber(this.getRevisionNumber());
111         }
112     }
113 }