BUG-5222: do not use reflection on Optional
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLBluePrintNode.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.md.sal.dom.xsql;
9
10 import java.io.Serializable;
11 import java.sql.SQLException;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17
18 /**
19  * @author Sharon Aicler(saichler@gmail.com)
20  **/
21 /**
22  * To be removed in Nitrogen
23  */
24 @Deprecated
25 public class XSQLBluePrintNode implements Serializable {
26
27     private static final long serialVersionUID = 1L;
28     private Class<?> myInterface = null;
29     private String myInterfaceString = null;
30     private Set<XSQLBluePrintRelation> relations = new HashSet<>();
31     private Set<XSQLBluePrintNode> inheritingNodes = new HashSet<>();
32     private Set<XSQLBluePrintNode> children = new HashSet<>();
33     private XSQLBluePrintNode parent = null;
34
35     private int level = -1;
36     private transient Set<String> parentHierarchySet = null;
37     private String myInterfaceName = null;
38     private Set<XSQLColumn> columns = new HashSet<>();
39     private Map<String, XSQLColumn> origNameToColumn = new HashMap<>();
40
41     private transient Object[] odlSchemaNodes = null;
42     private boolean module = false;
43     private String bluePrintTableName = null;
44     private String odlTableName = null;
45     private String origName = null;
46
47     public void mergeAugmentation(XSQLBluePrintNode aug) {
48         this.relations.addAll(aug.relations);
49         this.inheritingNodes.addAll(aug.inheritingNodes);
50         this.children.addAll(aug.children);
51         this.columns.addAll(aug.columns);
52         this.origNameToColumn.putAll(aug.origNameToColumn);
53         if (aug.odlSchemaNodes != null) {
54             for (Object sn : aug.odlSchemaNodes) {
55                 addToSchemaNodes(sn);
56             }
57         }
58     }
59
60     public XSQLBluePrintNode(String name, String _origName, int _level) {
61         this.level = _level;
62         this.odlTableName = name;
63         this.bluePrintTableName = name;
64         this.origName = _origName;
65     }
66
67     public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
68         this.myInterface = _myInterface;
69         this.myInterfaceString = _myInterface.getName();
70         this.myInterfaceName = myInterface.getSimpleName();
71         this.level = _level;
72     }
73
74     public XSQLBluePrintNode(Object _odlNode, int _level,
75             XSQLBluePrintNode _parent) {
76         addToSchemaNodes(_odlNode);
77         this.level = _level;
78         this.module = XSQLODLUtils.isModule(_odlNode);
79         this.parent = _parent;
80         this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
81         this.odlTableName = XSQLODLUtils
82                 .getODLNodeName(getFirstFromSchemaNodes());
83     }
84
85     private void addToSchemaNodes(Object schemaObject) {
86         if (this.odlSchemaNodes == null)
87             this.odlSchemaNodes = new Object[1];
88         else {
89             Object[] temp = new Object[this.odlSchemaNodes.length + 1];
90             System.arraycopy(this.odlSchemaNodes, 0, temp, 0,
91                     this.odlSchemaNodes.length);
92             this.odlSchemaNodes = temp;
93         }
94         this.odlSchemaNodes[this.odlSchemaNodes.length - 1] = schemaObject;
95     }
96
97     public Object getFirstFromSchemaNodes() {
98         if (this.odlSchemaNodes == null) {
99             return null;
100         }
101         return this.odlSchemaNodes[0];
102     }
103
104     public String getOrigName() {
105         return this.origName;
106     }
107
108     public String getBluePrintNodeName() {
109         return this.bluePrintTableName;
110     }
111
112     public boolean isModule() {
113         return this.module;
114     }
115
116     public Set<XSQLBluePrintNode> getChildren() {
117         return this.children;
118     }
119
120     public String getODLTableName() {
121         if (this.odlTableName == null) {
122             this.odlTableName = XSQLODLUtils
123                     .getODLNodeName(getFirstFromSchemaNodes());
124         }
125         return this.odlTableName;
126     }
127
128     public void addChild(XSQLBluePrintNode ch) {
129         this.children.add(ch);
130     }
131
132     public boolean isModelChild(Class<?> p) {
133         if (this.relations.size() == 0) {
134             return false;
135         }
136         for (XSQLBluePrintRelation parentRelation : this.relations) {
137             if (parentRelation.getParent().getInterface().equals(p)) {
138                 return true;
139             }
140         }
141         for (XSQLBluePrintRelation dtr : this.relations) {
142             XSQLBluePrintNode parent = dtr.getParent();
143             if (!parent.getInterface().equals(this.getInterface())
144                     && !parent.getInterface().isAssignableFrom(
145                             this.getInterface())
146                     && this.getInterface().isAssignableFrom(
147                             parent.getInterface()) && parent.isModelChild(p)) {
148                 return true;
149             }
150         }
151         return false;
152     }
153
154     public Set<XSQLBluePrintRelation> getRelations() {
155         return this.relations;
156     }
157
158     public String getClassName() {
159         return this.myInterfaceString;
160     }
161
162     public void addInheritingNode(XSQLBluePrintNode node) {
163         this.inheritingNodes.add(node);
164     }
165
166     public Set<XSQLBluePrintNode> getInheritingNodes() {
167         return this.inheritingNodes;
168     }
169
170     public void addColumn(Object node, String tableName) {
171         XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this);
172         this.columns.add(c);
173     }
174
175     public XSQLColumn addColumn(String name, String tableName, String origName,
176             String origTableName) {
177         XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName);
178         this.columns.add(c);
179         this.origNameToColumn.put(origName, c);
180         return c;
181     }
182
183     public void addColumn(String methodName) {
184         if (methodName.startsWith("get")) {
185             methodName = methodName.substring(3);
186         } else if (methodName.startsWith("is")) {
187             methodName = methodName.substring(2);
188         }
189         XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
190         this.columns.add(c);
191     }
192
193     public Collection<XSQLColumn> getColumns() {
194         return this.columns;
195     }
196
197     public XSQLColumn findColumnByName(String name) throws SQLException {
198
199         XSQLColumn exactMatch = null;
200         XSQLColumn indexOfMatch = null;
201         XSQLColumn exactLowercaseMatch = null;
202         XSQLColumn indexOfLowerCaseMatch = null;
203
204         for (XSQLColumn col : columns) {
205             if (col.getName().equals(name)) {
206                 exactMatch = col;
207             }
208             if (col.getName().indexOf(name) != -1) {
209                 indexOfMatch = col;
210             }
211             if (col.getName().toLowerCase().equals(name.toLowerCase())) {
212                 exactLowercaseMatch = col;
213             }
214             if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
215                 indexOfLowerCaseMatch = col;
216             }
217         }
218
219         if (exactMatch != null) {
220             return exactMatch;
221         }
222         if (exactLowercaseMatch != null) {
223             return exactLowercaseMatch;
224         }
225         if (indexOfMatch != null) {
226             return indexOfMatch;
227         }
228         if (indexOfLowerCaseMatch != null) {
229             return indexOfLowerCaseMatch;
230         }
231
232         throw new SQLException("Unknown field name '" + name + "'");
233     }
234
235     public void addParent(XSQLBluePrintNode parent, String property) {
236         try {
237             if (property.equals("ContainingTPs")) {
238                 return;
239             }
240             // Method m = parent.getInterface().getMethod("get"+property, null);
241             // if(!m.getDeclaringClass().equals(parent.getInterface()))
242             // return;
243             XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent,
244                     property, myInterface);
245             relations.add(rel);
246         } catch (Exception err) {
247             err.printStackTrace();
248         }
249     }
250
251     public XSQLBluePrintNode getParent() {
252         return this.parent;
253     }
254
255     public Set<XSQLBluePrintRelation> getClonedParents() {
256         Set<XSQLBluePrintRelation> result = new HashSet<>();
257         result.addAll(this.relations);
258         return result;
259     }
260
261     public String toString() {
262         if (myInterfaceName != null) {
263             return myInterfaceName;
264         }
265         if (this.odlSchemaNodes != null) {
266             return getBluePrintNodeName();
267         }
268         if (odlTableName != null) {
269             return odlTableName;
270         }
271         return "Unknown";
272     }
273
274     public Class<?> getInterface() {
275         return this.myInterface;
276     }
277
278     public int getLevel() {
279         return this.level;
280     }
281
282     @Override
283     public boolean equals(Object obj) {
284         XSQLBluePrintNode other = (XSQLBluePrintNode) obj;
285         if (this.odlSchemaNodes != null) {
286             return getBluePrintNodeName().equals(other.getBluePrintNodeName());
287         } else if (this.odlTableName == null && other.odlTableName != null) {
288             return false;
289         }
290         if (this.odlTableName != null && other.odlTableName == null) {
291             return false;
292         } else {
293             return this.odlTableName.equals(other.odlTableName);
294         }
295     }
296
297     @Override
298     public int hashCode() {
299         if (myInterfaceString != null) {
300             return myInterfaceString.hashCode();
301         } else if (this.odlSchemaNodes != null) {
302             return bluePrintTableName.hashCode();
303         }
304         return 0;
305     }
306
307 }