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