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 void loadBluePrint(){
68 InputStream in = this.getClass().getClassLoader().getResourceAsStream("BluePrintCache.dat");
70 this.bluePrint = XSQLBluePrint.load(in);
73 }catch(Exception err){
74 err.printStackTrace();
78 public static XSQLAdapter getInstance() {
82 public static void main(String args[]) {
83 XSQLAdapter adapter = new XSQLAdapter();
87 public static void log(String str) {
90 synchronized (XSQLAdapter.class) {
93 new FileOutputStream("/tmp/xql.log"));
97 l.print(Calendar.getInstance().getTime());
100 } catch (Exception err) {
101 err.printStackTrace();
105 public static void log(Exception e) {
108 synchronized (XSQLAdapter.class) {
111 new FileOutputStream("/tmp/xql.log"));
115 l.print(Calendar.getInstance().getTime());
117 e.printStackTrace(l);
118 } catch (Exception err) {
119 err.printStackTrace();
124 public void onGlobalContextUpdated(SchemaContext context) {
125 Set<Module> modules = context.getModules();
126 for (Module m : modules) {
127 if (XSQLODLUtils.createOpenDaylightCache(this.bluePrint, m)) {
128 this.addRootElement(m);
133 public void setDataBroker(DOMDataBroker ddb) {
134 this.domDataBroker = ddb;
137 public XSQLBluePrint getBluePrint() {
138 return this.bluePrint;
141 public List<Object> collectModuleRoots(XSQLBluePrintNode table,LogicalDatastoreType type) {
142 if (table.getParent().isModule()) {
144 List<Object> result = new LinkedList<Object>();
145 YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier
147 .node(XSQLODLUtils.getPath(table.getODLNode()).get(0))
149 DOMDataReadTransaction t = this.domDataBroker
150 .newReadOnlyTransaction();
151 Object node = t.read(type,
152 instanceIdentifier).get();
154 node = XSQLODLUtils.get(node, "reference");
159 // XSQLAdapter.log(""+node);
160 Map<?, ?> children = XSQLODLUtils.getChildren(node);
161 for (Object c : children.values()) {
162 Map<?, ?> sons = XSQLODLUtils.getChildren(c);
163 for (Object child : sons.values()) {
169 } catch (Exception err) {
170 XSQLAdapter.log(err);
173 return collectModuleRoots(table.getParent(),type);
178 public void execute(JDBCResultSet rs) {
179 if(this.domDataBroker==null){
180 rs.setFinished(true);
183 List<XSQLBluePrintNode> tables = rs.getTables();
184 List<Object> roots = collectModuleRoots(tables.get(0),LogicalDatastoreType.OPERATIONAL);
185 roots.addAll(collectModuleRoots(tables.get(0),LogicalDatastoreType.CONFIGURATION));
187 rs.setFinished(true);
189 XSQLBluePrintNode main = rs.getMainTable();
190 List<NETask> tasks = new LinkedList<XSQLAdapter.NETask>();
192 for (Object entry : roots) {
193 NETask task = new NETask(rs, entry, main, bluePrint);
197 for (NETask task : tasks) {
198 threadPool.addTask(task);
205 Socket s = serverSocket.accept();
206 new TelnetConnection(s);
207 } catch (Exception err) {
208 err.printStackTrace();
211 } catch (Exception err2) {
218 public void addRootElement(Object o) {
219 NEEntry entry = new NEEntry(o);
220 elements.put(o.toString(), entry);
224 public void processCommand(StringBuffer inputString, PrintStream sout) {
225 if (inputString.toString().trim().equals("r")) {
226 sout.println(lastInputString);
227 inputString = lastInputString;
229 lastInputString = inputString;
230 String input = inputString.toString().trim();
231 if (input.startsWith("setExcel")) {
232 String substr = input.substring("setExcel".length()).trim();
233 if (!substr.equals("")) {
234 // excelPath01 = substr;
236 // sout.println("Excel Path="+excelPath01);
237 } else if (input.startsWith("list vrel")) {
238 String substr = input.substring("list vrel".length()).trim();
239 XSQLBluePrintNode node = bluePrint
240 .getBluePrintNodeByTableName(substr);
242 sout.println("Unknown Interface " + substr);
245 List<String> fld = new ArrayList<String>();
246 for (XSQLBluePrintRelation r : node.getRelations()) {
247 fld.add(r.toString());
249 String p[] = (String[]) fld.toArray(new String[fld.size()]);
251 for (int i = 0; i < p.length; i++) {
254 } else if (input.startsWith("list vfields")) {
255 String substr = input.substring("list vfields".length()).trim();
256 XSQLBluePrintNode node = bluePrint
257 .getBluePrintNodeByTableName(substr);
259 sout.println("Unknown Interface " + substr);
262 List<String> fld = new ArrayList<String>();
263 for (XSQLColumn c : node.getColumns()) {
264 fld.add(c.getName());
266 String p[] = (String[]) fld.toArray(new String[fld.size()]);
268 for (int i = 0; i < p.length; i++) {
271 } else if (input.startsWith("jdbc")) {
272 String addr = input.substring(5).trim();
273 jdbcServer.connectToClient(addr);
274 sout.println("Connected To " + addr);
275 } else if (input.startsWith("fetch")) {
276 // fetchSize = Integer.parseInt(input.substring(6).trim());
277 } else if (input.startsWith("list vtables")) {
279 String iNames[] = bluePrint.getAllTableNames().toArray(
283 for (int i = 0; i < iNames.length; i++) {
284 sout.println(iNames[i]);
286 } else if (input.equals("help") || input.equals("?")) {
287 // sout.println(getLongDescription());
288 } else if (input.equals("avmdata")) {
290 // myConnection.getManagedData();
291 } catch (Exception err) {
293 } else if (input.equals("innerjoin")) {
294 // innerJoin = !innerJoin;
295 // sout.println("Inner Join set to "+innerJoin);
296 } else if (input.equals("exit")) {
299 } catch (Exception err) {
301 } else if (input.equals("save")) {
302 XSQLBluePrint.save(this.bluePrint);
303 } else if (input.equals("tocsv")) {
305 sout.println("to csv file is " + toCsv);
306 } else if (input.indexOf("filename") != -1) {
307 exportToFileName = input.substring(input.indexOf(" ")).trim();
308 sout.println("Exporting to file:" + exportToFileName);
309 } else if (!input.equals("")) {
311 if (exportToFileName != null) {
313 PrintStream o = new PrintStream(new File(
315 executeSql(inputString.toString(), o);
317 } catch (Exception err) {
318 err.printStackTrace();
322 String fName = "export-" + System.currentTimeMillis()
324 PrintStream o = new PrintStream(new File(fName));
325 executeSql(inputString.toString(), o);
327 sout.println("Exported to file " + fName);
328 } catch (Exception err) {
329 err.printStackTrace();
334 executeSql(inputString.toString(), sout);
340 public void executeSql(String sql, PrintStream out) {
341 JDBCResultSet rs = new JDBCResultSet(sql);
344 jdbcServer.execute(rs, this);
345 boolean isFirst = true;
346 int loc = rs.getFields().size() - 1;
348 for (XSQLColumn c : rs.getFields()) {
360 out.print(c.getName());
363 int cw = c.getCharWidth();
364 int cnw = c.getName().length();
369 for (int i = 0; i < gap; i++) {
374 totalWidth += c.getCharWidth() + 1;
389 for (int i = 0; i < totalWidth; i++) {
397 loc = rs.getFields().size() - 1;
398 for (XSQLColumn c : rs.getFields()) {
410 Object sValue = rs.getObject(c.toString());
411 if (sValue == null) {
416 int cw = c.getCharWidth();
417 int vw = sValue.toString().length();
419 for (int i = 0; i < gap; i++) {
437 out.println("Total Number Of Records=" + count);
438 } catch (Exception err) {
439 err.printStackTrace(out);
443 public static class NETask implements Runnable {
445 private JDBCResultSet rs = null;
446 private Object modelRoot = null;
447 private XSQLBluePrintNode main = null;
448 private XSQLBluePrint bluePrint = null;
450 public NETask(JDBCResultSet _rs, Object _modelRoot,
451 XSQLBluePrintNode _main, XSQLBluePrint _bluePrint) {
453 this.modelRoot = _modelRoot;
455 this.bluePrint = _bluePrint;
459 rs.addRecords(modelRoot, main, true, main.getBluePrintNodeName(),
463 if (rs.numberOfTasks == 0) {
464 rs.setFinished(true);
471 private static class NEEntry {
472 private Object ne = null;
474 public NEEntry(Object _ne) {
478 public String toString() {
479 Module m = (Module) ne;
480 return m.getName() + " [" + m.getNamespace().toString() + "]";
484 private class TelnetConnection extends Thread {
486 private Socket socket = null;
487 private InputStream in = null;
488 private PrintStream out = null;
489 private Module currentModule = null;
491 public TelnetConnection(Socket s) {
494 this.in = s.getInputStream();
495 this.out = new PrintStream(s.getOutputStream());
497 } catch (Exception err) {
498 XSQLAdapter.log(err);
503 StringBuffer inputString = new StringBuffer();
504 String prompt = "XSQL>";
507 if (currentModule != null) {
508 prompt = "XQL/" + currentModule.getName() + ">";
512 byte data[] = new byte[1];
513 while (!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && c != '\n') {
517 inputString.append(c);
518 } catch (Exception err) {
519 err.printStackTrace(out);
525 processCommand(inputString, out);
526 inputString = new StringBuffer();
528 } catch (Exception err) {
531 } catch (Exception err2) {