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