2 * Copyright (c) 2015 Intel Corporation All rights reserved.
3 * Copyright (c) 2015 Isaku Yamahata All rights reserved.
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
10 package org.opendaylight.neutron.spi;
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;
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;
30 private static final Logger LOG = LoggerFactory.getLogger(NeutronFirewallRule.class);
32 private static final long serialVersionUID = 1L;
34 @XmlElement(name = "tenant_id")
37 @XmlElement(name = "project_id")
40 @XmlElement(name = "revision_number")
43 public NeutronObject() {
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.
57 public void setTenantID(String tenantID) {
58 this.tenantID = tenantID;
63 public void setTenantID(Uuid tenantID) {
64 this.tenantID = tenantID.getValue().replace("-", "");
68 public String toString() {
69 return "NeutronObject [id=" + uuid + ", tenantID=" + tenantID + "]";
73 public void setProjectID(String projectID) {
74 this.projectID = projectID;
78 public String getProjectID() {
79 return this.projectID;
83 public Long getRevisionNumber() {
84 return revisionNumber;
88 public void setRevisionNumber(Long revisionNumber) {
89 this.revisionNumber = revisionNumber;
93 public void initDefaults() {
94 if (projectID != null && tenantID == null) {
97 if (projectID == null && tenantID != null) {
103 * This method copies selected fields from the object and returns them
104 * as a new object, suitable for marshaling.
107 * List of attributes to be extracted
108 * @return an OpenStack Neutron object with only the selected fields
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];
120 ans = cls.newInstance();
121 } catch (IllegalAccessException | InstantiationException e) {
122 // should not happen.
123 throw new IllegalStateException(e);
125 for (String s : fields) {
126 if (!extractField(s, ans)) {
127 LOG.warn("Unknown {} {}.", cls.getSimpleName(), s);
133 protected boolean extractField(String field, T ans) {
136 ans.setID(this.getID());
139 ans.setTenantID(this.getTenantID());
142 ans.setProjectID(this.getProjectID());
144 case "revision_number":
145 ans.setRevisionNumber(this.getRevisionNumber());