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