Merge "XSQL Karaf Command"
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLAdapter.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
3 import java.io.File;
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;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17
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;
27
28 public class XSQLAdapter extends Thread implements SchemaContextListener {
29
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;
39     private int nThreads;
40     private int qsize;
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;
52
53     private XSQLAdapter() {
54         XSQLAdapter.log("Starting Adapter");
55         this.setDaemon(true);
56         try {
57             serverSocket = new ServerSocket(34343);
58         } catch (Exception err) {
59             XSQLAdapter.log(err);
60         }
61         this.start();
62         XSQLAdapter.log("Adapter Started!");
63
64     }
65
66     public static XSQLAdapter getInstance() {
67         return a;
68     }
69
70     public static void main(String args[]) {
71         XSQLAdapter adapter = new XSQLAdapter();
72         adapter.start();
73     }
74
75     public static void log(String str) {
76         try {
77             if (l == null) {
78                 synchronized (XSQLAdapter.class) {
79                     if (l == null) {
80                         l = new PrintStream(
81                                 new FileOutputStream("/tmp/xql.log"));
82                     }
83                 }
84             }
85             l.print(Calendar.getInstance().getTime());
86             l.print(" - ");
87             l.println(str);
88         } catch (Exception err) {
89             err.printStackTrace();
90         }
91     }
92
93     public static void log(Exception e) {
94         try {
95             if (l == null) {
96                 synchronized (XSQLAdapter.class) {
97                     if (l == null) {
98                         l = new PrintStream(
99                                 new FileOutputStream("/tmp/xql.log"));
100                     }
101                 }
102             }
103             l.print(Calendar.getInstance().getTime());
104             l.print(" - ");
105             e.printStackTrace(l);
106         } catch (Exception err) {
107             err.printStackTrace();
108         }
109     }
110
111     @Override
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);
117             }
118         }
119     }
120
121     public void setDataBroker(DOMDataBroker ddb) {
122         this.domDataBroker = ddb;
123     }
124
125     public XSQLBluePrint getBluePrint() {
126         return this.bluePrint;
127     }
128
129     public List<Object> collectModuleRoots(XSQLBluePrintNode table) {
130         if (table.getParent().isModule()) {
131             try {
132                 List<Object> result = new LinkedList<Object>();
133                 YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier
134                         .builder()
135                         .node(XSQLODLUtils.getPath(table.getODLNode()).get(0))
136                         .toInstance();
137                 DOMDataReadTransaction t = this.domDataBroker
138                         .newReadOnlyTransaction();
139                 Object node = t.read(LogicalDatastoreType.OPERATIONAL,
140                         instanceIdentifier).get();
141                 node = XSQLODLUtils.get(node, "reference");
142                 if (node == null) {
143                     return result;
144                 }
145
146                 // XSQLAdapter.log(""+node);
147                 Map<?, ?> children = XSQLODLUtils.getChildren(node);
148                 for (Object c : children.values()) {
149                     Map<?, ?> sons = XSQLODLUtils.getChildren(c);
150                     for (Object child : sons.values()) {
151                         result.add(child);
152                     }
153                 }
154
155                 return result;
156             } catch (Exception err) {
157                 XSQLAdapter.log(err);
158             }
159         } else {
160             return collectModuleRoots(table.getParent());
161         }
162         return null;
163     }
164
165     public void execute(JDBCResultSet rs) {
166         List<XSQLBluePrintNode> tables = rs.getTables();
167         List<Object> roots = collectModuleRoots(tables.get(0));
168         XSQLBluePrintNode main = rs.getMainTable();
169         List<NETask> tasks = new LinkedList<XSQLAdapter.NETask>();
170
171         for (Object entry : roots) {
172             NETask task = new NETask(rs, entry, main, bluePrint);
173             rs.numberOfTasks++;
174             tasks.add(task);
175         }
176         for (NETask task : tasks) {
177             threadPool.addTask(task);
178         }
179     }
180
181     public void run() {
182         while (!stopped) {
183             try {
184                 Socket s = serverSocket.accept();
185                 new TelnetConnection(s);
186             } catch (Exception err) {
187                 err.printStackTrace();
188                 try {
189                     Thread.sleep(20000);
190                 } catch (Exception err2) {
191                 }
192                 stopped = true;
193             }
194         }
195     }
196
197     public void addRootElement(Object o) {
198         NEEntry entry = new NEEntry(o);
199         elements.put(o.toString(), entry);
200
201     }
202
203     public void processCommand(StringBuffer inputString, PrintStream sout) {
204         if (inputString.toString().trim().equals("r")) {
205             sout.println(lastInputString);
206             inputString = lastInputString;
207         }
208         lastInputString = inputString;
209         String input = inputString.toString().trim();
210         if (input.startsWith("setExcel")) {
211             String substr = input.substring("setExcel".length()).trim();
212             if (!substr.equals("")) {
213                 // excelPath01 = substr;
214             }
215             // sout.println("Excel Path="+excelPath01);
216         } else if (input.startsWith("list vrel")) {
217             String substr = input.substring("list vrel".length()).trim();
218             XSQLBluePrintNode node = bluePrint
219                     .getBluePrintNodeByTableName(substr);
220             if (node == null) {
221                 sout.println("Unknown Interface " + substr);
222                 return;
223             }
224             List<String> fld = new ArrayList<String>();
225             for (XSQLBluePrintRelation r : node.getRelations()) {
226                 fld.add(r.toString());
227             }
228             String p[] = (String[]) fld.toArray(new String[fld.size()]);
229             Arrays.sort(p);
230             for (int i = 0; i < p.length; i++) {
231                 sout.println(p[i]);
232             }
233         } else if (input.startsWith("list vfields")) {
234             String substr = input.substring("list vfields".length()).trim();
235             XSQLBluePrintNode node = bluePrint
236                     .getBluePrintNodeByTableName(substr);
237             if (node == null) {
238                 sout.println("Unknown Interface " + substr);
239                 return;
240             }
241             List<String> fld = new ArrayList<String>();
242             for (XSQLColumn c : node.getColumns()) {
243                 fld.add(c.getName());
244             }
245             String p[] = (String[]) fld.toArray(new String[fld.size()]);
246             Arrays.sort(p);
247             for (int i = 0; i < p.length; i++) {
248                 sout.println(p[i]);
249             }
250         } else if (input.startsWith("jdbc")) {
251             String addr = input.substring(5).trim();
252             jdbcServer.connectToClient(addr);
253             sout.println("Connected To " + addr);
254         } else if (input.startsWith("fetch")) {
255             // fetchSize = Integer.parseInt(input.substring(6).trim());
256         } else if (input.startsWith("list vtables")) {
257
258             String iNames[] = bluePrint.getAllTableNames().toArray(
259                     new String[0]);
260             Arrays.sort(iNames);
261             sout.println();
262             for (int i = 0; i < iNames.length; i++) {
263                 sout.println(iNames[i]);
264             }
265         } else if (input.equals("help") || input.equals("?")) {
266             // sout.println(getLongDescription());
267         } else if (input.equals("avmdata")) {
268             try {
269                 // myConnection.getManagedData();
270             } catch (Exception err) {
271             }
272         } else if (input.equals("innerjoin")) {
273             // innerJoin = !innerJoin;
274             // sout.println("Inner Join set to "+innerJoin);
275         } else if (input.equals("exit")) {
276             try {
277                 sout.close();
278             } catch (Exception err) {
279             }
280         } else if (input.equals("tocsv")) {
281             toCsv = !toCsv;
282             sout.println("to csv file is " + toCsv);
283         } else if (input.indexOf("filename") != -1) {
284             exportToFileName = input.substring(input.indexOf(" ")).trim();
285             sout.println("Exporting to file:" + exportToFileName);
286         } else if (!input.equals("")) {
287             if (toCsv) {
288                 if (exportToFileName != null) {
289                     try {
290                         PrintStream o = new PrintStream(new File(
291                                 exportToFileName));
292                         executeSql(inputString.toString(), o);
293                         o.close();
294                     } catch (Exception err) {
295                         err.printStackTrace();
296                     }
297                 } else {
298                     try {
299                         String fName = "export-" + System.currentTimeMillis()
300                                 + ".csv";
301                         PrintStream o = new PrintStream(new File(fName));
302                         executeSql(inputString.toString(), o);
303                         o.close();
304                         sout.println("Exported to file " + fName);
305                     } catch (Exception err) {
306                         err.printStackTrace();
307                     }
308
309                 }
310             } else {
311                 executeSql(inputString.toString(), sout);
312             }
313         }
314         sout.println();
315     }
316
317     public void executeSql(String sql, PrintStream out) {
318         JDBCResultSet rs = new JDBCResultSet(sql);
319         try {
320             int count = 0;
321             jdbcServer.execute(rs, this);
322             boolean isFirst = true;
323             int loc = rs.getFields().size() - 1;
324             int totalWidth = 0;
325             for (XSQLColumn c : rs.getFields()) {
326                 if (isFirst) {
327                     isFirst = false;
328                     if (toCsv) {
329                         out.print("\"");
330                     }
331                 }
332
333                 if (!toCsv) {
334                     out.print("|");
335                 }
336
337                 out.print(c.getName());
338
339                 if (!toCsv) {
340                     int cw = c.getCharWidth();
341                     int cnw = c.getName().length();
342                     if (cnw > cw) {
343                         c.setCharWidth(cnw);
344                     }
345                     int gap = cw - cnw;
346                     for (int i = 0; i < gap; i++) {
347                         out.print(" ");
348                     }
349                 }
350
351                 totalWidth += c.getCharWidth() + 1;
352
353                 if (loc > 0) {
354                     if (toCsv) {
355                         out.print("\",\"");
356                     }
357                 }
358                 loc--;
359             }
360
361             if (toCsv) {
362                 out.println("\"");
363             } else {
364                 totalWidth++;
365                 out.println("|");
366                 for (int i = 0; i < totalWidth; i++) {
367                     out.print("-");
368                 }
369                 out.println();
370             }
371
372             while (rs.next()) {
373                 isFirst = true;
374                 loc = rs.getFields().size() - 1;
375                 for (XSQLColumn c : rs.getFields()) {
376                     if (isFirst) {
377                         isFirst = false;
378                         if (toCsv) {
379                             out.print("\"");
380                         }
381                     }
382
383                     if (!toCsv) {
384                         out.print("|");
385                     }
386
387                     Object sValue = rs.getObject(c.toString());
388                     if (sValue == null) {
389                         sValue = "";
390                     }
391                     out.print(sValue);
392
393                     int cw = c.getCharWidth();
394                     int vw = sValue.toString().length();
395                     int gap = cw - vw;
396                     for (int i = 0; i < gap; i++) {
397                         out.print(" ");
398                     }
399
400                     if (loc > 0) {
401                         if (toCsv) {
402                             out.print("\",\"");
403                         }
404                     }
405                     loc--;
406                 }
407                 if (toCsv) {
408                     out.println("\"");
409                 } else {
410                     out.println("|");
411                 }
412                 count++;
413             }
414             out.println("Total Number Of Records=" + count);
415         } catch (Exception err) {
416             err.printStackTrace(out);
417         }
418     }
419
420     public static class NETask implements Runnable {
421
422         private JDBCResultSet rs = null;
423         private Object modelRoot = null;
424         private XSQLBluePrintNode main = null;
425         private XSQLBluePrint bluePrint = null;
426
427         public NETask(JDBCResultSet _rs, Object _modelRoot,
428                 XSQLBluePrintNode _main, XSQLBluePrint _bluePrint) {
429             this.rs = _rs;
430             this.modelRoot = _modelRoot;
431             this.main = _main;
432             this.bluePrint = _bluePrint;
433         }
434
435         public void run() {
436             rs.addRecords(modelRoot, main, true, main.getBluePrintNodeName(),
437                     bluePrint);
438             synchronized (rs) {
439                 rs.numberOfTasks--;
440                 if (rs.numberOfTasks == 0) {
441                     rs.setFinished(true);
442                     rs.notifyAll();
443                 }
444             }
445         }
446     }
447
448     private static class NEEntry {
449         private Object ne = null;
450
451         public NEEntry(Object _ne) {
452             this.ne = _ne;
453         }
454
455         public String toString() {
456             Module m = (Module) ne;
457             return m.getName() + "  [" + m.getNamespace().toString() + "]";
458         }
459     }
460
461     private class TelnetConnection extends Thread {
462
463         private Socket socket = null;
464         private InputStream in = null;
465         private PrintStream out = null;
466         private Module currentModule = null;
467
468         public TelnetConnection(Socket s) {
469             this.socket = s;
470             try {
471                 this.in = s.getInputStream();
472                 this.out = new PrintStream(s.getOutputStream());
473                 this.start();
474             } catch (Exception err) {
475                 XSQLAdapter.log(err);
476             }
477         }
478
479         public void run() {
480             StringBuffer inputString = new StringBuffer();
481             String prompt = "XSQL>";
482             try {
483                 while (!stopped) {
484                     if (currentModule != null) {
485                         prompt = "XQL/" + currentModule.getName() + ">";
486                     }
487                     out.print(prompt);
488                     char c = 0;
489                     byte data[] = new byte[1];
490                     while (c != '\n') {
491                         try {
492                             in.read(data);
493                             c = (char) data[0];
494                             inputString.append(c);
495                         } catch (Exception err) {
496                             err.printStackTrace(out);
497                         }
498                     }
499
500                     processCommand(inputString, out);
501                     inputString = new StringBuffer();
502                 }
503             } catch (Exception err) {
504                 try {
505                     socket.close();
506                 } catch (Exception err2) {
507                 }
508             }
509         }
510     }
511 }