Serialization/Deserialization and a host of other fixes
[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.InstanceIdentifier;
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                 InstanceIdentifier instanceIdentifier =
134                     InstanceIdentifier.builder()
135                         .node(XSQLODLUtils.getPath(table.getODLNode()).get(0))
136                         .toInstance();
137                 DOMDataReadTransaction t = this.domDataBroker.newReadOnlyTransaction();
138                 Object node =
139                     t.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier)
140                         .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     protected void processCommand(StringBuffer inputString, PrintStream sout,
204         TelnetConnection tc) {
205         if (inputString.toString().trim().equals("r")) {
206             sout.println(lastInputString);
207             inputString = lastInputString;
208         }
209         lastInputString = inputString;
210         String input = inputString.toString().trim();
211         if (input.startsWith("setExcel")) {
212             String substr = input.substring("setExcel".length()).trim();
213             if (!substr.equals("")) {
214                 //excelPath01 = substr;
215             }
216             //sout.println("Excel Path="+excelPath01);
217         } else if (input.startsWith("list vrel")) {
218             String substr = input.substring("list vrel".length()).trim();
219             XSQLBluePrintNode node = bluePrint.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.getBluePrintNodeByTableName(substr);
236             if (node == null) {
237                 sout.println("Unknown Interface " + substr);
238                 return;
239             }
240             List<String> fld = new ArrayList<String>();
241             for (XSQLColumn c : node.getColumns()) {
242                 fld.add(c.getName());
243             }
244             String p[] = (String[]) fld.toArray(new String[fld.size()]);
245             Arrays.sort(p);
246             for (int i = 0; i < p.length; i++) {
247                 sout.println(p[i]);
248             }
249         } else if (input.startsWith("jdbc")) {
250             String addr = input.substring(5).trim();
251             jdbcServer.connectToClient(addr);
252             sout.println("Connected To " + addr);
253         } else if (input.startsWith("fetch")) {
254             //fetchSize = Integer.parseInt(input.substring(6).trim());
255         } else if (input.startsWith("list vtables")) {
256
257             String iNames[] =
258                 bluePrint.getAllTableNames().toArray(new String[0]);
259             Arrays.sort(iNames);
260             sout.println();
261             for (int i = 0; i < iNames.length; i++) {
262                 sout.println(iNames[i]);
263             }
264         } else if (input.startsWith("cd sid")) {
265             String substr = input.substring("cd sid".length()).trim();
266             for (NEEntry e : elements.values()) {
267                 if (((Module) e.ne).getName().equals(substr)) {
268                     tc.currentModule = (Module) e.ne;
269                 }
270             }
271         } else if (input.equals("list sid")) {
272             String arr[] = new String[elements.size()];
273
274             int i = 0;
275             for (NEEntry entry : elements.values()) {
276                 arr[i] = entry.toString();
277                 i++;
278             }
279             Arrays.sort(arr);
280             for (String s : arr) {
281                 sout.println(s);
282             }
283         } else if (input.equals("help") || input.equals("?")) {
284             //sout.println(getLongDescription());
285         } else if (input.equals("avmdata")) {
286             try {
287                 //myConnection.getManagedData();
288             } catch (Exception err) {
289             }
290         } else if (input.equals("innerjoin")) {
291             //innerJoin = !innerJoin;
292             //sout.println("Inner Join set to "+innerJoin);
293         } else if (input.equals("exit")) {
294             try {
295                 sout.close();
296             } catch (Exception err) {
297             }
298         } else if (input.equals("tocsv")) {
299             toCsv = !toCsv;
300             sout.println("to csv file is " + toCsv);
301         } else if (input.indexOf("filename") != -1) {
302             exportToFileName = input.substring(input.indexOf(" ")).trim();
303             sout.println("Exporting to file:" + exportToFileName);
304         } else if (!input.equals("")) {
305             if (toCsv) {
306                 if (exportToFileName != null) {
307                     try {
308                         PrintStream o =
309                             new PrintStream(new File(exportToFileName));
310                         executeSql(inputString.toString(), o);
311                         o.close();
312                     } catch (Exception err) {
313                         err.printStackTrace();
314                     }
315                 } else {
316                     try {
317                         String fName =
318                             "export-" + System.currentTimeMillis() + ".csv";
319                         PrintStream o = new PrintStream(new File(fName));
320                         executeSql(inputString.toString(), o);
321                         o.close();
322                         sout.println("Exported to file " + fName);
323                     } catch (Exception err) {
324                         err.printStackTrace();
325                     }
326
327                 }
328             } else {
329                 executeSql(inputString.toString(), sout);
330             }
331         }
332         sout.println();
333     }
334
335     public void executeSql(String sql, PrintStream out) {
336         JDBCResultSet rs = new JDBCResultSet(sql);
337         try {
338             int count = 0;
339             jdbcServer.execute(rs, this);
340             boolean isFirst = true;
341             int loc = rs.getFields().size() - 1;
342             int totalWidth = 0;
343             for (XSQLColumn c : rs.getFields()) {
344                 if (isFirst) {
345                     isFirst = false;
346                     if (toCsv) {
347                         out.print("\"");
348                     }
349                 }
350
351                 if (!toCsv) {
352                     out.print("|");
353                 }
354
355                 out.print(c.getName());
356
357                 if (!toCsv) {
358                     int cw = c.getCharWidth();
359                     int cnw = c.getName().length();
360                     if (cnw > cw) {
361                         c.setCharWidth(cnw);
362                     }
363                     int gap = cw - cnw;
364                     for (int i = 0; i < gap; i++) {
365                         out.print(" ");
366                     }
367                 }
368
369                 totalWidth += c.getCharWidth() + 1;
370
371                 if (loc > 0) {
372                     if (toCsv) {
373                         out.print("\",\"");
374                     }
375                 }
376                 loc--;
377             }
378
379             if (toCsv) {
380                 out.println("\"");
381             } else {
382                 totalWidth++;
383                 out.println("|");
384                 for (int i = 0; i < totalWidth; i++) {
385                     out.print("-");
386                 }
387                 out.println();
388             }
389
390             while (rs.next()) {
391                 isFirst = true;
392                 loc = rs.getFields().size() - 1;
393                 for (XSQLColumn c : rs.getFields()) {
394                     if (isFirst) {
395                         isFirst = false;
396                         if (toCsv) {
397                             out.print("\"");
398                         }
399                     }
400
401
402                     if (!toCsv) {
403                         out.print("|");
404                     }
405
406                     Object sValue = rs.getObject(c.toString());
407                     if (sValue == null) {
408                         sValue = "";
409                     }
410                     out.print(sValue);
411
412                     int cw = c.getCharWidth();
413                     int vw = sValue.toString().length();
414                     int gap = cw - vw;
415                     for (int i = 0; i < gap; i++) {
416                         out.print(" ");
417                     }
418
419
420                     if (loc > 0) {
421                         if (toCsv) {
422                             out.print("\",\"");
423                         }
424                     }
425                     loc--;
426                 }
427                 if (toCsv) {
428                     out.println("\"");
429                 } else {
430                     out.println("|");
431                 }
432                 count++;
433             }
434             out.println("Total Number Of Records=" + count);
435         } catch (Exception err) {
436             err.printStackTrace(out);
437         }
438     }
439
440
441     public static class NETask implements Runnable {
442
443         private JDBCResultSet rs = null;
444         private Object modelRoot = null;
445         private XSQLBluePrintNode main = null;
446         private XSQLBluePrint bluePrint = null;
447
448         public NETask(JDBCResultSet _rs, Object _modelRoot,XSQLBluePrintNode _main,XSQLBluePrint _bluePrint) {
449             this.rs = _rs;
450             this.modelRoot = _modelRoot;
451             this.main = _main;
452             this.bluePrint = _bluePrint;
453         }
454
455         public void run() {
456             rs.addRecords(modelRoot, main, true, main.getBluePrintNodeName(),bluePrint);
457             synchronized (rs) {
458                 rs.numberOfTasks--;
459                 if (rs.numberOfTasks == 0) {
460                     rs.setFinished(true);
461                     rs.notifyAll();
462                 }
463             }
464         }
465     }
466
467
468     private static class NEEntry {
469         private Object ne = null;
470
471         public NEEntry(Object _ne) {
472             this.ne = _ne;
473         }
474
475         public String toString() {
476             Module m = (Module) ne;
477             return m.getName() + "  [" + m.getNamespace().toString() + "]";
478         }
479     }
480
481
482     private class TelnetConnection extends Thread {
483
484         private Socket socket = null;
485         private InputStream in = null;
486         private PrintStream out = null;
487         private Module currentModule = null;
488
489         public TelnetConnection(Socket s) {
490             this.socket = s;
491             try {
492                 this.in = s.getInputStream();
493                 this.out = new PrintStream(s.getOutputStream());
494                 this.start();
495             } catch (Exception err) {
496                 XSQLAdapter.log(err);
497             }
498         }
499
500         public void run() {
501             StringBuffer inputString = new StringBuffer();
502             String prompt = "XSQL>";
503             try {
504                 while (!stopped) {
505                     if (currentModule != null) {
506                         prompt = "XQL/" + currentModule.getName() + ">";
507                     }
508                     out.print(prompt);
509                     char c = 0;
510                     byte data[] = new byte[1];
511                     while (c != '\n') {
512                         try {
513                             in.read(data);
514                             c = (char) data[0];
515                             inputString.append(c);
516                         } catch (Exception err) {
517                             err.printStackTrace(out);
518                         }
519                     }
520
521                     processCommand(inputString, out, this);
522                     inputString = new StringBuffer();
523                 }
524             } catch (Exception err) {
525                 try {
526                     socket.close();
527                 } catch (Exception err2) {
528                 }
529             }
530         }
531     }
532 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.