Fix FindBugs violations
[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 java.lang.reflect.ParameterizedType;
13 import java.lang.reflect.Type;
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 javax.xml.bind.annotation.XmlTransient;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @XmlRootElement
25 @XmlAccessorType(XmlAccessType.NONE)
26 public abstract class NeutronObject<T extends NeutronObject<T>> extends NeutronID implements INeutronObject<T> {
27     // T extends NeutronObject as 0th type argument. Used by extractFields()
28     private static final int NEUTRON_OBJECT_CLASS_TYPE_INDEX = 0;
29
30     private static final Logger LOG = LoggerFactory.getLogger(NeutronFirewallRule.class);
31
32     private static final long serialVersionUID = 1L;
33
34     @XmlElement(name = "tenant_id")
35     String tenantID;
36
37     @XmlElement(name = "project_id")
38     String projectID;
39
40     @XmlElement(name = "revision_number")
41     Long revisionNumber;
42
43     public NeutronObject() {
44     }
45
46     @Override
47     public String getTenantID() {
48         if (tenantID != null && tenantID.isEmpty()) {
49             // Bug 4775 - Treat empty string tenantId as null, so no attempt is made
50             //            to turn it into a uuid.
51             return null;
52         }
53         return tenantID;
54     }
55
56     @Override
57     public void setTenantID(String tenantID) {
58         this.tenantID = tenantID;
59     }
60
61     @Override
62     @XmlTransient
63     public void setTenantID(Uuid tenantID) {
64         this.tenantID = tenantID.getValue().replace("-", "");
65     }
66
67     @Override
68     public String toString() {
69         return "NeutronObject [id=" + uuid + ", tenantID=" + tenantID + "]";
70     }
71
72     @Override
73     public void setProjectID(String projectID) {
74         this.projectID = projectID;
75     }
76
77     @Override
78     public String getProjectID() {
79         return this.projectID;
80     }
81
82     @Override
83     public Long getRevisionNumber() {
84         return revisionNumber;
85     }
86
87     @Override
88     public void setRevisionNumber(Long revisionNumber) {
89         this.revisionNumber = revisionNumber;
90     }
91
92     @Override
93     public void initDefaults() {
94         if (projectID != null && tenantID == null) {
95             tenantID = projectID;
96         }
97         if (projectID == null && tenantID != null) {
98             projectID = tenantID;
99         }
100     }
101
102     /**
103      * This method copies selected fields from the object and returns them
104      * as a new object, suitable for marshaling.
105      *
106      * @param fields
107      *            List of attributes to be extracted
108      * @return an OpenStack Neutron object with only the selected fields
109      *             populated
110      */
111
112     @Override
113     public T extractFields(List<String> fields) {
114         ParameterizedType parameterizedType = (ParameterizedType) getClass().getGenericSuperclass();
115         Type[] types = parameterizedType.getActualTypeArguments();
116         @SuppressWarnings("unchecked")
117         Class<T> cls = (Class<T>) types[NEUTRON_OBJECT_CLASS_TYPE_INDEX];
118         T ans;
119         try {
120             ans = cls.newInstance();
121         } catch (IllegalAccessException | InstantiationException e) {
122             // should not happen.
123             throw new IllegalStateException(e);
124         }
125         for (String s : fields) {
126             if (!extractField(s, ans)) {
127                 LOG.warn("Unknown {} {}.", cls.getSimpleName(), s);
128             }
129         }
130         return ans;
131     }
132
133     protected boolean extractField(String field, T ans) {
134         switch (field) {
135             case "id":
136                 ans.setID(this.getID());
137                 return true;
138             case "tenant_id":
139                 ans.setTenantID(this.getTenantID());
140                 return true;
141             case "project_id":
142                 ans.setProjectID(this.getProjectID());
143                 return true;
144             case "revision_number":
145                 ans.setRevisionNumber(this.getRevisionNumber());
146                 return true;
147             default:
148                 return false;
149         }
150     }
151 }