X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-xsql%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fxsql%2FXSQLAdapter.java;h=5f7cef5f053d9dc00adf5c42aeda3bf25e029974;hp=496f27ecaa1defe4f49d1cd724b58e8a3f673794;hb=d0dc66335889ecec5dbc962a8604c3df96eca758;hpb=351a78c9840c5b98a478b91ffd50befad998eb0e diff --git a/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/XSQLAdapter.java b/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/XSQLAdapter.java index 496f27ecaa..5f7cef5f05 100644 --- a/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/XSQLAdapter.java +++ b/opendaylight/md-sal/sal-dom-xsql/src/main/java/org/opendaylight/controller/md/sal/dom/xsql/XSQLAdapter.java @@ -1,3 +1,10 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.md.sal.dom.xsql; import java.io.File; @@ -8,13 +15,10 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Arrays; -import java.util.Calendar; +import java.util.Date; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - +import javax.annotation.concurrent.GuardedBy; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker; import org.opendaylight.controller.md.sal.dom.api.DOMDataReadTransaction; @@ -24,31 +28,45 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +/** + * @author Sharon Aicler(saichler@gmail.com) + **/ +/** + * To be removed in Nitrogen + */ +@Deprecated public class XSQLAdapter extends Thread implements SchemaContextListener { + private static final Logger LOG = LoggerFactory.getLogger(XSQLAdapter.class); private static final int SLEEP = 10000; private static XSQLAdapter a = new XSQLAdapter(); private static PrintStream l = null; + private static String tmpDir = null; + private static File xqlLog = null; public boolean stopped = false; - private List elementHosts = new ArrayList(); private String username; private String password; - private String transport = "tcp"; + private final String transport = "tcp"; private int reconnectTimeout; private int nThreads; private int qsize; - private String applicationName = "NQL Adapter"; - private Map elements = new ConcurrentHashMap(); + private final String applicationName = "NQL Adapter"; private StringBuffer lastInputString = new StringBuffer(); - private XSQLBluePrint bluePrint = new XSQLBluePrint(); private boolean toCsv = false; private String exportToFileName = null; - private XSQLThreadPool threadPool = new XSQLThreadPool(1, "Tasks", 2000); - private JDBCServer jdbcServer = new JDBCServer(this); + private final XSQLThreadPool threadPool = new XSQLThreadPool(1, "Tasks", 2000); + private final JDBCServer jdbcServer = new JDBCServer(this); private String pinningFile; private ServerSocket serverSocket = null; private DOMDataBroker domDataBroker = null; + private static final String REFERENCE_FIELD_NAME = "reference"; + + @GuardedBy("this") + private SchemaContext context; + @GuardedBy("this") + private XSQLBluePrint bluePrint = new XSQLBluePrint(); private XSQLAdapter() { XSQLAdapter.log("Starting Adapter"); @@ -63,26 +81,44 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } + public synchronized void loadBluePrint(){ + try{ + InputStream in = this.getClass().getClassLoader().getResourceAsStream("BluePrintCache.dat"); + if(in!=null){ + this.bluePrint = XSQLBluePrint.load(in); + in.close(); + } + }catch(Exception err){ + err.printStackTrace(); + } + } + public static XSQLAdapter getInstance() { return a; } - public static void main(String args[]) { + public static File getXQLLogfile() { + tmpDir = System.getProperty("java.io.tmpdir"); + xqlLog = new File(tmpDir + "/xql.log"); + return xqlLog; + } + + public static void main(final String args[]) { XSQLAdapter adapter = new XSQLAdapter(); adapter.start(); } - public static void log(String str) { + public static void log(final String str) { try { if (l == null) { synchronized (XSQLAdapter.class) { if (l == null) { l = new PrintStream( - new FileOutputStream("/tmp/xql.log")); + new FileOutputStream(getXQLLogfile())); } } } - l.print(Calendar.getInstance().getTime()); + l.print(new Date()); l.print(" - "); l.println(str); } catch (Exception err) { @@ -90,17 +126,17 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } } - public static void log(Exception e) { + public static void log(final Exception e) { try { if (l == null) { synchronized (XSQLAdapter.class) { if (l == null) { l = new PrintStream( - new FileOutputStream("/tmp/xql.log")); + new FileOutputStream(getXQLLogfile())); } } } - l.print(Calendar.getInstance().getTime()); + l.print(new Date()); l.print(" - "); e.printStackTrace(l); } catch (Exception err) { @@ -109,67 +145,68 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } @Override - public void onGlobalContextUpdated(SchemaContext context) { - Set modules = context.getModules(); - for (Module m : modules) { - if (XSQLODLUtils.createOpenDaylightCache(this.bluePrint, m)) { - this.addRootElement(m); - } - } + public synchronized void onGlobalContextUpdated(final SchemaContext context) { + this.bluePrint = null; + this.context = context; } - public void setDataBroker(DOMDataBroker ddb) { + public void setDataBroker(final DOMDataBroker ddb) { this.domDataBroker = ddb; } - public XSQLBluePrint getBluePrint() { - return this.bluePrint; + public synchronized XSQLBluePrint getBluePrint() { + if (bluePrint == null) { + LOG.warn("XSQL is not supported in production environments and will be removed in a future release"); + bluePrint = XSQLBluePrint.create(context); + } + + return bluePrint; } - public List collectModuleRoots(XSQLBluePrintNode table) { + public List collectModuleRoots(final XSQLBluePrintNode table,final LogicalDatastoreType type) { if (table.getParent().isModule()) { try { - List result = new LinkedList(); + List result = new LinkedList<>(); YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier .builder() - .node(XSQLODLUtils.getPath(table.getODLNode()).get(0)) - .toInstance(); + .node(XSQLODLUtils.getPath(table.getFirstFromSchemaNodes()).get(0)) + .build(); DOMDataReadTransaction t = this.domDataBroker .newReadOnlyTransaction(); - Object node = t.read(LogicalDatastoreType.OPERATIONAL, + Object node = t.read(type, instanceIdentifier).get(); - node = XSQLODLUtils.get(node, "reference"); + + node = XSQLODLUtils.get(node, REFERENCE_FIELD_NAME); if (node == null) { return result; } - - // XSQLAdapter.log(""+node); - Map children = XSQLODLUtils.getChildren(node); - for (Object c : children.values()) { - Map sons = XSQLODLUtils.getChildren(c); - for (Object child : sons.values()) { - result.add(child); - } - } - + result.add(node); return result; } catch (Exception err) { XSQLAdapter.log(err); } } else { - return collectModuleRoots(table.getParent()); + return collectModuleRoots(table.getParent(),type); } return null; } - public void execute(JDBCResultSet rs) { + public void execute(final JDBCResultSet rs) { + if(this.domDataBroker==null){ + rs.setFinished(true); + return; + } List tables = rs.getTables(); - List roots = collectModuleRoots(tables.get(0)); + List roots = collectModuleRoots(tables.get(0),LogicalDatastoreType.OPERATIONAL); + roots.addAll(collectModuleRoots(tables.get(0),LogicalDatastoreType.CONFIGURATION)); + if(roots.isEmpty()){ + rs.setFinished(true); + } XSQLBluePrintNode main = rs.getMainTable(); - List tasks = new LinkedList(); + List tasks = new LinkedList<>(); for (Object entry : roots) { - NETask task = new NETask(rs, entry, main, bluePrint); + NETask task = new NETask(rs, entry, main, getBluePrint()); rs.numberOfTasks++; tasks.add(task); } @@ -178,6 +215,7 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } } + @Override public void run() { while (!stopped) { try { @@ -194,13 +232,7 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } } - public void addRootElement(Object o) { - NEEntry entry = new NEEntry(o); - elements.put(o.toString(), entry); - - } - - public void processCommand(StringBuffer inputString, PrintStream sout) { + public void processCommand(StringBuffer inputString, final PrintStream sout) { if (inputString.toString().trim().equals("r")) { sout.println(lastInputString); inputString = lastInputString; @@ -215,37 +247,35 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { // sout.println("Excel Path="+excelPath01); } else if (input.startsWith("list vrel")) { String substr = input.substring("list vrel".length()).trim(); - XSQLBluePrintNode node = bluePrint - .getBluePrintNodeByTableName(substr); + XSQLBluePrintNode node = getBluePrint().getBluePrintNodeByTableName(substr); if (node == null) { sout.println("Unknown Interface " + substr); return; } - List fld = new ArrayList(); + List fld = new ArrayList<>(); for (XSQLBluePrintRelation r : node.getRelations()) { fld.add(r.toString()); } - String p[] = (String[]) fld.toArray(new String[fld.size()]); + String p[] = fld.toArray(new String[fld.size()]); Arrays.sort(p); - for (int i = 0; i < p.length; i++) { - sout.println(p[i]); + for (String element : p) { + sout.println(element); } } else if (input.startsWith("list vfields")) { String substr = input.substring("list vfields".length()).trim(); - XSQLBluePrintNode node = bluePrint - .getBluePrintNodeByTableName(substr); + XSQLBluePrintNode node = getBluePrint().getBluePrintNodeByTableName(substr); if (node == null) { sout.println("Unknown Interface " + substr); return; } - List fld = new ArrayList(); + List fld = new ArrayList<>(); for (XSQLColumn c : node.getColumns()) { fld.add(c.getName()); } - String p[] = (String[]) fld.toArray(new String[fld.size()]); + String p[] = fld.toArray(new String[fld.size()]); Arrays.sort(p); - for (int i = 0; i < p.length; i++) { - sout.println(p[i]); + for (String element : p) { + sout.println(element); } } else if (input.startsWith("jdbc")) { String addr = input.substring(5).trim(); @@ -255,12 +285,12 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { // fetchSize = Integer.parseInt(input.substring(6).trim()); } else if (input.startsWith("list vtables")) { - String iNames[] = bluePrint.getAllTableNames().toArray( + String iNames[] = getBluePrint().getAllTableNames().toArray( new String[0]); Arrays.sort(iNames); sout.println(); - for (int i = 0; i < iNames.length; i++) { - sout.println(iNames[i]); + for (String iName : iNames) { + sout.println(iName); } } else if (input.equals("help") || input.equals("?")) { // sout.println(getLongDescription()); @@ -277,6 +307,8 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { sout.close(); } catch (Exception err) { } + } else if (input.equals("save")) { + getBluePrint().save(); } else if (input.equals("tocsv")) { toCsv = !toCsv; sout.println("to csv file is " + toCsv); @@ -314,11 +346,11 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { sout.println(); } - public void executeSql(String sql, PrintStream out) { + public void executeSql(final String sql, final PrintStream out) { JDBCResultSet rs = new JDBCResultSet(sql); try { int count = 0; - jdbcServer.execute(rs, this); + JDBCServer.execute(rs, this); boolean isFirst = true; int loc = rs.getFields().size() - 1; int totalWidth = 0; @@ -424,14 +456,15 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { private XSQLBluePrintNode main = null; private XSQLBluePrint bluePrint = null; - public NETask(JDBCResultSet _rs, Object _modelRoot, - XSQLBluePrintNode _main, XSQLBluePrint _bluePrint) { + public NETask(final JDBCResultSet _rs, final Object _modelRoot, + final XSQLBluePrintNode _main, final XSQLBluePrint _bluePrint) { this.rs = _rs; this.modelRoot = _modelRoot; this.main = _main; this.bluePrint = _bluePrint; } + @Override public void run() { rs.addRecords(modelRoot, main, true, main.getBluePrintNodeName(), bluePrint); @@ -445,27 +478,14 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } } - private static class NEEntry { - private Object ne = null; - - public NEEntry(Object _ne) { - this.ne = _ne; - } - - public String toString() { - Module m = (Module) ne; - return m.getName() + " [" + m.getNamespace().toString() + "]"; - } - } - private class TelnetConnection extends Thread { private Socket socket = null; private InputStream in = null; private PrintStream out = null; - private Module currentModule = null; + private final Module currentModule = null; - public TelnetConnection(Socket s) { + public TelnetConnection(final Socket s) { this.socket = s; try { this.in = s.getInputStream(); @@ -476,6 +496,7 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { } } + @Override public void run() { StringBuffer inputString = new StringBuffer(); String prompt = "XSQL>"; @@ -487,13 +508,15 @@ public class XSQLAdapter extends Thread implements SchemaContextListener { out.print(prompt); char c = 0; byte data[] = new byte[1]; - while (c != '\n') { + while (!socket.isClosed() && socket.isConnected() && !socket.isInputShutdown() && c != '\n') { try { in.read(data); c = (char) data[0]; inputString.append(c); } catch (Exception err) { err.printStackTrace(out); + stopped = true; + break; } }