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.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;
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;
32 private static final Logger LOG = LoggerFactory.getLogger(NeutronFirewallRule.class);
34 private static final long serialVersionUID = 1L;
36 @XmlElement(name = "tenant_id")
39 @XmlElement(name = "project_id")
42 @XmlElement(name = "revision_number")
45 public NeutronObject() {
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.
59 public void setTenantID(String tenantID) {
60 this.tenantID = tenantID;
65 public void setTenantID(Uuid tenantID) {
66 this.tenantID = tenantID.getValue().replace("-", "");
70 public String toString() {
71 return "NeutronObject [id=" + uuid + ", tenantID=" + tenantID + "]";
75 public void setProjectID(String projectID) {
76 this.projectID = projectID;
80 public String getProjectID() {
81 return this.projectID;
85 public Long getRevisionNumber() {
86 return revisionNumber;
90 public void setRevisionNumber(Long revisionNumber) {
91 this.revisionNumber = revisionNumber;
95 public void initDefaults() {
96 if (projectID != null && tenantID == null) {
99 if (projectID == null && tenantID != null) {
100 projectID = tenantID;
105 * This method copies selected fields from the object and returns them
106 * as a new object, suitable for marshaling.
109 * List of attributes to be extracted
110 * @return an OpenStack Neutron object with only the selected fields
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];
122 ans = cls.newInstance();
123 } catch (IllegalAccessException | InstantiationException e) {
124 // should not happen.
125 throw new IllegalStateException(e);
127 for (String s : fields) {
128 if (!extractField(s, ans)) {
129 LOG.warn("Unknown {} {}.", cls.getSimpleName(), s);
135 protected boolean extractField(String field, T ans) {
138 ans.setID(this.getID());
141 ans.setTenantID(this.getTenantID());
144 ans.setProjectID(this.getProjectID());
146 case "revision_number":
147 ans.setRevisionNumber(this.getRevisionNumber());