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