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