Introduction of XSQL
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLBluePrintNode.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
3 import java.io.Serializable;
4 import java.sql.SQLException;
5 import java.util.Collection;
6 import java.util.HashSet;
7 import java.util.Set;
8
9 public class XSQLBluePrintNode implements Serializable {
10
11     private static final long serialVersionUID = 1L;
12     private Class<?> myInterface = null;
13     private String myInterfaceString = null;
14     private Set<XSQLBluePrintRelation> relations =
15         new HashSet<XSQLBluePrintRelation>();
16     private Set<XSQLBluePrintNode> inheritingNodes =
17         new HashSet<XSQLBluePrintNode>();
18     private Set<XSQLBluePrintNode> children = new HashSet<XSQLBluePrintNode>();
19     private XSQLBluePrintNode parent = null;
20
21     private int level = -1;
22     private transient Set<String> parentHierarchySet = null;
23     private String myInterfaceName = null;
24     private Set<XSQLColumn> columns = new HashSet<XSQLColumn>();
25
26     private transient Object odlNode = null;
27     private boolean module = false;
28     private String bluePrintTableName = null;
29     private String odlTableName = null;
30
31     public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
32         this.myInterface = _myInterface;
33         this.myInterfaceString = _myInterface.getName();
34         this.myInterfaceName = myInterface.getSimpleName();
35         this.level = _level;
36     }
37
38     public XSQLBluePrintNode(Object _odlNode, int _level,XSQLBluePrintNode _parent) {
39         this.odlNode = _odlNode;
40         this.level = _level;
41         this.module = XSQLODLUtils.isModule(_odlNode);
42         this.parent = _parent;
43         this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
44
45     }
46
47     public String getBluePrintNodeName(){
48         return this.bluePrintTableName;
49     }
50
51     public boolean isModule() {
52         return this.module;
53     }
54
55     public Set<XSQLBluePrintNode> getChildren() {
56         return this.children;
57     }
58
59     public String getODLTableName() {
60         if (this.odlTableName == null) {
61             this.odlTableName = XSQLODLUtils.getODLNodeName(this.odlNode);
62         }
63         return this.odlTableName;
64     }
65
66     public Object getODLNode() {
67         return this.odlNode;
68     }
69
70     public void AddChild(XSQLBluePrintNode ch) {
71         this.children.add(ch);
72     }
73
74     public boolean isModelChild(Class p) {
75         if (this.relations.size() == 0) {
76             return false;
77         }
78         for (XSQLBluePrintRelation parentRelation : this.relations) {
79             if (parentRelation.getParent().getInterface().equals(p)) {
80                 return true;
81             }
82         }
83         for (XSQLBluePrintRelation dtr : this.relations) {
84             XSQLBluePrintNode parent = dtr.getParent();
85             if (!parent.getInterface().equals(this.getInterface()) && !parent
86                 .getInterface().isAssignableFrom(this.getInterface()) &&
87                 this.getInterface().isAssignableFrom(parent.getInterface())
88                 && parent.isModelChild(p)) {
89                 return true;
90             }
91         }
92         return false;
93     }
94
95     public Set<XSQLBluePrintRelation> getRelations() {
96         return this.relations;
97     }
98
99     public String getClassName() {
100         return this.myInterfaceString;
101     }
102
103     public void addInheritingNode(XSQLBluePrintNode node) {
104         this.inheritingNodes.add(node);
105     }
106
107     public Set<XSQLBluePrintNode> getInheritingNodes() {
108         return this.inheritingNodes;
109     }
110
111     public void addColumn(Object node, String tableName) {
112         XSQLColumn c = new XSQLColumn(node,getBluePrintNodeName(), this);
113         this.columns.add(c);
114     }
115
116     public void addColumn(String methodName) {
117         if (methodName.startsWith("get")) {
118             methodName = methodName.substring(3);
119         } else if (methodName.startsWith("is")) {
120             methodName = methodName.substring(2);
121         }
122         XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
123         this.columns.add(c);
124     }
125
126     public Collection<XSQLColumn> getColumns() {
127         return this.columns;
128     }
129
130     public XSQLColumn findColumnByName(String name) throws SQLException {
131
132         XSQLColumn exactMatch = null;
133         XSQLColumn indexOfMatch = null;
134         XSQLColumn exactLowercaseMatch = null;
135         XSQLColumn indexOfLowerCaseMatch = null;
136
137         for (XSQLColumn col : columns) {
138             if (col.getName().equals(name)) {
139                 exactMatch = col;
140             }
141             if (col.getName().indexOf(name) != -1) {
142                 indexOfMatch = col;
143             }
144             if (col.getName().toLowerCase().equals(name.toLowerCase())) {
145                 exactLowercaseMatch = col;
146             }
147             if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
148                 indexOfLowerCaseMatch = col;
149             }
150         }
151
152         if (exactMatch != null) {
153             return exactMatch;
154         }
155         if (exactLowercaseMatch != null) {
156             return exactLowercaseMatch;
157         }
158         if (indexOfMatch != null) {
159             return indexOfMatch;
160         }
161         if (indexOfLowerCaseMatch != null) {
162             return indexOfLowerCaseMatch;
163         }
164
165         throw new SQLException("Unknown field name '" + name + "'");
166     }
167
168
169     public void addParent(XSQLBluePrintNode parent, String property) {
170         try {
171             if (property.equals("ContainingTPs")) {
172                 return;
173             }
174             //Method m = parent.getInterface().getMethod("get"+property, null);
175             //if(!m.getDeclaringClass().equals(parent.getInterface()))
176             //return;
177             XSQLBluePrintRelation rel =
178                 new XSQLBluePrintRelation(parent, property, myInterface);
179             relations.add(rel);
180         } catch (Exception err) {
181             err.printStackTrace();
182         }
183     }
184
185     public XSQLBluePrintNode getParent() {
186         return this.parent;
187     }
188
189     public Set<XSQLBluePrintRelation> getClonedParents() {
190         Set<XSQLBluePrintRelation> result =
191             new HashSet<XSQLBluePrintRelation>();
192         result.addAll(this.relations);
193         return result;
194     }
195
196     public String toString() {
197         if (myInterfaceName != null) {
198             return myInterfaceName;
199         }
200         if (odlNode != null) {
201             return getBluePrintNodeName();
202         }
203         return "Unknown";
204     }
205
206     public Class getInterface() {
207         return this.myInterface;
208     }
209
210     public int getLevel() {
211         return this.level;
212     }
213
214     @Override
215     public boolean equals(Object obj) {
216         XSQLBluePrintNode other = (XSQLBluePrintNode) obj;
217         if (odlNode != null) {
218             return getBluePrintNodeName().equals(other.getBluePrintNodeName());
219         } else if (this.odlTableName != null) {
220             return this.odlTableName.equals(other.odlTableName);
221         } else {
222             return other.myInterface.equals(myInterface);
223         }
224     }
225
226     @Override
227     public int hashCode() {
228         if (myInterfaceString != null) {
229             return myInterfaceString.hashCode();
230         } else if (odlNode != null) {
231             return bluePrintTableName.hashCode();
232         }
233         return 0;
234     }
235
236 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.