2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.xsql;
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;
19 * @author Sharon Aicler(saichler@gmail.com)
22 * To be removed in Nitrogen
25 public class XSQLBluePrintNode implements Serializable {
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;
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<>();
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;
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) {
60 public XSQLBluePrintNode(String name, String _origName, int _level) {
62 this.odlTableName = name;
63 this.bluePrintTableName = name;
64 this.origName = _origName;
67 public XSQLBluePrintNode(Class<?> _myInterface, int _level) {
68 this.myInterface = _myInterface;
69 this.myInterfaceString = _myInterface.getName();
70 this.myInterfaceName = myInterface.getSimpleName();
74 public XSQLBluePrintNode(Object _odlNode, int _level,
75 XSQLBluePrintNode _parent) {
76 addToSchemaNodes(_odlNode);
78 this.module = XSQLODLUtils.isModule(_odlNode);
79 this.parent = _parent;
80 this.bluePrintTableName = XSQLODLUtils.getBluePrintName(_odlNode);
81 this.odlTableName = XSQLODLUtils
82 .getODLNodeName(getFirstFromSchemaNodes());
85 private void addToSchemaNodes(Object schemaObject) {
86 if (this.odlSchemaNodes == null)
87 this.odlSchemaNodes = new Object[1];
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;
94 this.odlSchemaNodes[this.odlSchemaNodes.length - 1] = schemaObject;
97 public Object getFirstFromSchemaNodes() {
98 if (this.odlSchemaNodes == null) {
101 return this.odlSchemaNodes[0];
104 public String getOrigName() {
105 return this.origName;
108 public String getBluePrintNodeName() {
109 return this.bluePrintTableName;
112 public boolean isModule() {
116 public Set<XSQLBluePrintNode> getChildren() {
117 return this.children;
120 public String getODLTableName() {
121 if (this.odlTableName == null) {
122 this.odlTableName = XSQLODLUtils
123 .getODLNodeName(getFirstFromSchemaNodes());
125 return this.odlTableName;
128 public void addChild(XSQLBluePrintNode ch) {
129 this.children.add(ch);
132 public boolean isModelChild(Class<?> p) {
133 if (this.relations.size() == 0) {
136 for (XSQLBluePrintRelation parentRelation : this.relations) {
137 if (parentRelation.getParent().getInterface().equals(p)) {
141 for (XSQLBluePrintRelation dtr : this.relations) {
142 XSQLBluePrintNode parent = dtr.getParent();
143 if (!parent.getInterface().equals(this.getInterface())
144 && !parent.getInterface().isAssignableFrom(
146 && this.getInterface().isAssignableFrom(
147 parent.getInterface()) && parent.isModelChild(p)) {
154 public Set<XSQLBluePrintRelation> getRelations() {
155 return this.relations;
158 public String getClassName() {
159 return this.myInterfaceString;
162 public void addInheritingNode(XSQLBluePrintNode node) {
163 this.inheritingNodes.add(node);
166 public Set<XSQLBluePrintNode> getInheritingNodes() {
167 return this.inheritingNodes;
170 public void addColumn(Object node, String tableName) {
171 XSQLColumn c = new XSQLColumn(node, getBluePrintNodeName(), this);
175 public XSQLColumn addColumn(String name, String tableName, String origName,
176 String origTableName) {
177 XSQLColumn c = new XSQLColumn(name, tableName, origName, origTableName);
179 this.origNameToColumn.put(origName, c);
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);
189 XSQLColumn c = new XSQLColumn(methodName, myInterfaceName, null);
193 public Collection<XSQLColumn> getColumns() {
197 public XSQLColumn findColumnByName(String name) throws SQLException {
199 XSQLColumn exactMatch = null;
200 XSQLColumn indexOfMatch = null;
201 XSQLColumn exactLowercaseMatch = null;
202 XSQLColumn indexOfLowerCaseMatch = null;
204 for (XSQLColumn col : columns) {
205 if (col.getName().equals(name)) {
208 if (col.getName().indexOf(name) != -1) {
211 if (col.getName().toLowerCase().equals(name.toLowerCase())) {
212 exactLowercaseMatch = col;
214 if (col.getName().toLowerCase().indexOf(name.toLowerCase()) != -1) {
215 indexOfLowerCaseMatch = col;
219 if (exactMatch != null) {
222 if (exactLowercaseMatch != null) {
223 return exactLowercaseMatch;
225 if (indexOfMatch != null) {
228 if (indexOfLowerCaseMatch != null) {
229 return indexOfLowerCaseMatch;
232 throw new SQLException("Unknown field name '" + name + "'");
235 public void addParent(XSQLBluePrintNode parent, String property) {
237 if (property.equals("ContainingTPs")) {
240 // Method m = parent.getInterface().getMethod("get"+property, null);
241 // if(!m.getDeclaringClass().equals(parent.getInterface()))
243 XSQLBluePrintRelation rel = new XSQLBluePrintRelation(parent,
244 property, myInterface);
246 } catch (Exception err) {
247 err.printStackTrace();
251 public XSQLBluePrintNode getParent() {
255 public Set<XSQLBluePrintRelation> getClonedParents() {
256 Set<XSQLBluePrintRelation> result = new HashSet<>();
257 result.addAll(this.relations);
261 public String toString() {
262 if (myInterfaceName != null) {
263 return myInterfaceName;
265 if (this.odlSchemaNodes != null) {
266 return getBluePrintNodeName();
268 if (odlTableName != null) {
274 public Class<?> getInterface() {
275 return this.myInterface;
278 public int getLevel() {
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) {
290 if (this.odlTableName != null && other.odlTableName == null) {
293 return this.odlTableName.equals(other.odlTableName);
298 public int hashCode() {
299 if (myInterfaceString != null) {
300 return myInterfaceString.hashCode();
301 } else if (this.odlSchemaNodes != null) {
302 return bluePrintTableName.hashCode();