1 package org.opendaylight.controller.md.sal.dom.xsql;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.PrintStream;
7 import java.net.ServerSocket;
8 import java.net.Socket;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Calendar;
12 import java.util.LinkedList;
13 import java.util.List;
16 import java.util.concurrent.ConcurrentHashMap;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction;
21 import org.opendaylight.controller.md.sal.dom.xsql.jdbc.JDBCResultSet;
22 import org.opendaylight.controller.md.sal.dom.xsql.jdbc.JDBCServer;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
28 public class XSQLAdapter extends Thread implements SchemaContextListener {
30 private static final int SLEEP = 10000;
31 private static XSQLAdapter a = new XSQLAdapter();
32 private static PrintStream l = null;
33 public boolean stopped = false;
34 private List<String> elementHosts = new ArrayList<String>();
35 private String username;
36 private String password;
37 private String transport = "tcp";
38 private int reconnectTimeout;
41 private String applicationName = "NQL Adapter";
42 private Map<String, NEEntry> elements = new ConcurrentHashMap<String, XSQLAdapter.NEEntry>();
43 private StringBuffer lastInputString = new StringBuffer();
44 private XSQLBluePrint bluePrint = new XSQLBluePrint();
45 private boolean toCsv = false;
46 private String exportToFileName = null;
47 private XSQLThreadPool threadPool = new XSQLThreadPool(1, "Tasks", 2000);
48 private JDBCServer jdbcServer = new JDBCServer(this);
49 private String pinningFile;
50 private ServerSocket serverSocket = null;
51 private DOMDataBroker domDataBroker = null;
53 private XSQLAdapter() {
54 XSQLAdapter.log("Starting Adapter");
57 serverSocket = new ServerSocket(34343);
58 } catch (Exception err) {
62 XSQLAdapter.log("Adapter Started!");
66 public static XSQLAdapter getInstance() {
70 public static void main(String args[]) {
71 XSQLAdapter adapter = new XSQLAdapter();
75 public static void log(String str) {
78 synchronized (XSQLAdapter.class) {
81 new FileOutputStream("/tmp/xql.log"));
85 l.print(Calendar.getInstance().getTime());
88 } catch (Exception err) {
89 err.printStackTrace();
93 public static void log(Exception e) {
96 synchronized (XSQLAdapter.class) {
99 new FileOutputStream("/tmp/xql.log"));
103 l.print(Calendar.getInstance().getTime());
105 e.printStackTrace(l);
106 } catch (Exception err) {
107 err.printStackTrace();
112 public void onGlobalContextUpdated(SchemaContext context) {
113 Set<Module> modules = context.getModules();
114 for (Module m : modules) {
115 if (XSQLODLUtils.createOpenDaylightCache(this.bluePrint, m)) {
116 this.addRootElement(m);
121 public void setDataBroker(DOMDataBroker ddb) {
122 this.domDataBroker = ddb;
125 public XSQLBluePrint getBluePrint() {
126 return this.bluePrint;
129 public List<Object> collectModuleRoots(XSQLBluePrintNode table,LogicalDatastoreType type) {
130 if (table.getParent().isModule()) {
132 List<Object> result = new LinkedList<Object>();
133 YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier
135 .node(XSQLODLUtils.getPath(table.getODLNode()).get(0))
137 DOMDataReadTransaction t = this.domDataBroker
138 .newReadOnlyTransaction();
139 Object node = t.read(type,
140 instanceIdentifier).get();
142 node = XSQLODLUtils.get(node, "reference");
147 // XSQLAdapter.log(""+node);
148 Map<?, ?> children = XSQLODLUtils.getChildren(node);
149 for (Object c : children.values()) {
150 Map<?, ?> sons = XSQLODLUtils.getChildren(c);
151 for (Object child : sons.values()) {
157 } catch (Exception err) {
158 XSQLAdapter.log(err);
161 return collectModuleRoots(table.getParent(),type);
166 public void execute(JDBCResultSet rs) {
167 List<XSQLBluePrintNode> tables = rs.getTables();
168 List<Object> roots = collectModuleRoots(tables.get(0),LogicalDatastoreType.OPERATIONAL);
169 roots.addAll(collectModuleRoots(tables.get(0),LogicalDatastoreType.CONFIGURATION));
171 rs.setFinished(true);
173 XSQLBluePrintNode main = rs.getMainTable();
174 List<NETask> tasks = new LinkedList<XSQLAdapter.NETask>();
176 for (Object entry : roots) {
177 NETask task = new NETask(rs, entry, main, bluePrint);
181 for (NETask task : tasks) {
182 threadPool.addTask(task);
189 Socket s = serverSocket.accept();
190 new TelnetConnection(s);
191 } catch (Exception err) {
192 err.printStackTrace();
195 } catch (Exception err2) {
202 public void addRootElement(Object o) {
203 NEEntry entry = new NEEntry(o);
204 elements.put(o.toString(), entry);
208 public void processCommand(StringBuffer inputString, PrintStream sout) {
209 if (inputString.toString().trim().equals("r")) {
210 sout.println(lastInputString);
211 inputString = lastInputString;
213 lastInputString = inputString;
214 String input = inputString.toString().trim();
215 if (input.startsWith("setExcel")) {
216 String substr = input.substring("setExcel".length()).trim();
217 if (!substr.equals("")) {
218 // excelPath01 = substr;
220 // sout.println("Excel Path="+excelPath01);
221 } else if (input.startsWith("list vrel")) {
222 String substr = input.substring("list vrel".length()).trim();
223 XSQLBluePrintNode node = bluePrint
224 .getBluePrintNodeByTableName(substr);
226 sout.println("Unknown Interface " + substr);
229 List<String> fld = new ArrayList<String>();
230 for (XSQLBluePrintRelation r : node.getRelations()) {
231 fld.add(r.toString());
233 String p[] = (String[]) fld.toArray(new String[fld.size()]);
235 for (int i = 0; i < p.length; i++) {
238 } else if (input.startsWith("list vfields")) {
239 String substr = input.substring("list vfields".length()).trim();
240 XSQLBluePrintNode node = bluePrint
241 .getBluePrintNodeByTableName(substr);
243 sout.println("Unknown Interface " + substr);
246 List<String> fld = new ArrayList<String>();
247 for (XSQLColumn c : node.getColumns()) {
248 fld.add(c.getName());
250 String p[] = (String[]) fld.toArray(new String[fld.size()]);
252 for (int i = 0; i < p.length; i++) {
255 } else if (input.startsWith("jdbc")) {
256 String addr = input.substring(5).trim();
257 jdbcServer.connectToClient(addr);
258 sout.println("Connected To " + addr);
259 } else if (input.startsWith("fetch")) {
260 // fetchSize = Integer.parseInt(input.substring(6).trim());
261 } else if (input.startsWith("list vtables")) {
263 String iNames[] = bluePrint.getAllTableNames().toArray(
267 for (int i = 0; i < iNames.length; i++) {
268 sout.println(iNames[i]);
270 } else if (input.equals("help") || input.equals("?")) {
271 // sout.println(getLongDescription());
272 } else if (input.equals("avmdata")) {
274 // myConnection.getManagedData();
275 } catch (Exception err) {
277 } else if (input.equals("innerjoin")) {
278 // innerJoin = !innerJoin;
279 // sout.println("Inner Join set to "+innerJoin);
280 } else if (input.equals("exit")) {
283 } catch (Exception err) {
285 } else if (input.equals("tocsv")) {
287 sout.println("to csv file is " + toCsv);
288 } else if (input.indexOf("filename") != -1) {
289 exportToFileName = input.substring(input.indexOf(" ")).trim();
290 sout.println("Exporting to file:" + exportToFileName);
291 } else if (!input.equals("")) {
293 if (exportToFileName != null) {
295 PrintStream o = new PrintStream(new File(
297 executeSql(inputString.toString(), o);
299 } catch (Exception err) {
300 err.printStackTrace();
304 String fName = "export-" + System.currentTimeMillis()
306 PrintStream o = new PrintStream(new File(fName));
307 executeSql(inputString.toString(), o);
309 sout.println("Exported to file " + fName);
310 } catch (Exception err) {
311 err.printStackTrace();
316 executeSql(inputString.toString(), sout);
322 public void executeSql(String sql, PrintStream out) {
323 JDBCResultSet rs = new JDBCResultSet(sql);
326 jdbcServer.execute(rs, this);
327 boolean isFirst = true;
328 int loc = rs.getFields().size() - 1;
330 for (XSQLColumn c : rs.getFields()) {
342 out.print(c.getName());
345 int cw = c.getCharWidth();
346 int cnw = c.getName().length();
351 for (int i = 0; i < gap; i++) {
356 totalWidth += c.getCharWidth() + 1;
371 for (int i = 0; i < totalWidth; i++) {
379 loc = rs.getFields().size() - 1;
380 for (XSQLColumn c : rs.getFields()) {
392 Object sValue = rs.getObject(c.toString());
393 if (sValue == null) {
398 int cw = c.getCharWidth();
399 int vw = sValue.toString().length();
401 for (int i = 0; i < gap; i++) {
419 out.println("Total Number Of Records=" + count);
420 } catch (Exception err) {
421 err.printStackTrace(out);
425 public static class NETask implements Runnable {
427 private JDBCResultSet rs = null;
428 private Object modelRoot = null;
429 private XSQLBluePrintNode main = null;
430 private XSQLBluePrint bluePrint = null;
432 public NETask(JDBCResultSet _rs, Object _modelRoot,
433 XSQLBluePrintNode _main, XSQLBluePrint _bluePrint) {
435 this.modelRoot = _modelRoot;
437 this.bluePrint = _bluePrint;
441 rs.addRecords(modelRoot, main, true, main.getBluePrintNodeName(),
445 if (rs.numberOfTasks == 0) {
446 rs.setFinished(true);
453 private static class NEEntry {
454 private Object ne = null;
456 public NEEntry(Object _ne) {
460 public String toString() {
461 Module m = (Module) ne;
462 return m.getName() + " [" + m.getNamespace().toString() + "]";
466 private class TelnetConnection extends Thread {
468 private Socket socket = null;
469 private InputStream in = null;
470 private PrintStream out = null;
471 private Module currentModule = null;
473 public TelnetConnection(Socket s) {
476 this.in = s.getInputStream();
477 this.out = new PrintStream(s.getOutputStream());
479 } catch (Exception err) {
480 XSQLAdapter.log(err);
485 StringBuffer inputString = new StringBuffer();
486 String prompt = "XSQL>";
489 if (currentModule != null) {
490 prompt = "XQL/" + currentModule.getName() + ">";
494 byte data[] = new byte[1];
495 while (!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && c != '\n') {
499 inputString.append(c);
500 } catch (Exception err) {
501 err.printStackTrace(out);
507 processCommand(inputString, out);
508 inputString = new StringBuffer();
510 } catch (Exception err) {
513 } catch (Exception err2) {