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 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;
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;
31 private static final Logger LOG = LoggerFactory.getLogger(NeutronObject.class);
33 private static final long serialVersionUID = 1L;
35 private String tenantID;
37 @XmlElement(name = "project_id")
40 @XmlElement(name = "revision_number")
43 public NeutronObject() {
47 @XmlElement(name = "tenant_id")
48 @XmlJavaTypeAdapter(EmptyStringAsNullAdapter.class)
49 public String getTenantID() {
54 @XmlElement(name = "tenant_id")
55 @XmlJavaTypeAdapter(EmptyStringAsNullAdapter.class)
56 public void setTenantID(String tenantID) {
57 this.tenantID = tenantID;
62 public void setTenantID(Uuid tenantID) {
63 this.tenantID = tenantID.getValue().replace("-", "");
67 public String toString() {
68 return "NeutronObject [id=" + uuid + ", tenantID=" + getTenantID() + "]";
72 public void setProjectID(String projectID) {
73 this.projectID = projectID;
77 public String getProjectID() {
78 return this.projectID;
82 public Long getRevisionNumber() {
83 return revisionNumber;
87 public void setRevisionNumber(Long revisionNumber) {
88 this.revisionNumber = revisionNumber;
92 public void initDefaults() {
93 if (projectID != null && getTenantID() == null) {
94 setTenantID(projectID);
96 if (projectID == null && getTenantID() != null) {
97 projectID = getTenantID();
102 * This method copies selected fields from the object and returns them
103 * as a new object, suitable for marshaling.
106 * List of attributes to be extracted
107 * @return an OpenStack Neutron object with only the selected fields
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];
119 ans = cls.getDeclaredConstructor().newInstance();
120 } catch (ReflectiveOperationException e) {
121 // should not happen.
122 throw new IllegalStateException(e);
124 for (String s : fields) {
125 if (!extractField(s, ans)) {
126 LOG.warn("Unknown {} {}.", cls.getSimpleName(), s);
132 protected boolean extractField(String field, T ans) {
135 ans.setID(this.getID());
138 ans.setTenantID(this.getTenantID());
141 ans.setProjectID(this.getProjectID());
143 case "revision_number":
144 ans.setRevisionNumber(this.getRevisionNumber());