X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fsal%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fimplementation%2Finternal%2FReadService.java;h=12de35f53677492581412017e9c763de96a05c0b;hb=d11d61d31ea0c439bc4d9957f6af314f54ed0d58;hp=da725f010cffedc7646edb82f2d5087a947252ae;hpb=c8b79431119d6952b60a092e89727aa648a89bdd;p=controller.git diff --git a/opendaylight/sal/implementation/src/main/java/org/opendaylight/controller/sal/implementation/internal/ReadService.java b/opendaylight/sal/implementation/src/main/java/org/opendaylight/controller/sal/implementation/internal/ReadService.java index da725f010c..12de35f536 100644 --- a/opendaylight/sal/implementation/src/main/java/org/opendaylight/controller/sal/implementation/internal/ReadService.java +++ b/opendaylight/sal/implementation/src/main/java/org/opendaylight/controller/sal/implementation/internal/ReadService.java @@ -9,12 +9,15 @@ package org.opendaylight.controller.sal.implementation.internal; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArraySet; import org.eclipse.osgi.framework.console.CommandInterpreter; import org.eclipse.osgi.framework.console.CommandProvider; @@ -25,39 +28,42 @@ import org.opendaylight.controller.sal.action.Output; import org.opendaylight.controller.sal.action.PopVlan; import org.opendaylight.controller.sal.core.ConstructionException; import org.opendaylight.controller.sal.core.Node; -import org.opendaylight.controller.sal.core.NodeConnector; import org.opendaylight.controller.sal.core.Node.NodeIDType; +import org.opendaylight.controller.sal.core.NodeConnector; +import org.opendaylight.controller.sal.core.NodeTable; import org.opendaylight.controller.sal.flowprogrammer.Flow; import org.opendaylight.controller.sal.match.Match; import org.opendaylight.controller.sal.match.MatchType; import org.opendaylight.controller.sal.reader.FlowOnNode; import org.opendaylight.controller.sal.reader.IPluginInReadService; +import org.opendaylight.controller.sal.reader.IPluginOutReadService; import org.opendaylight.controller.sal.reader.IReadService; +import org.opendaylight.controller.sal.reader.IReadServiceListener; import org.opendaylight.controller.sal.reader.NodeConnectorStatistics; import org.opendaylight.controller.sal.reader.NodeDescription; +import org.opendaylight.controller.sal.reader.NodeTableStatistics; import org.opendaylight.controller.sal.utils.EtherTypes; +import org.opendaylight.controller.sal.utils.GlobalConstants; import org.opendaylight.controller.sal.utils.IPProtocols; import org.opendaylight.controller.sal.utils.NodeConnectorCreator; import org.opendaylight.controller.sal.utils.NodeCreator; +import org.opendaylight.controller.sal.utils.NodeTableCreator; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * The SAL Read Service. It dispatches the read request to - * the proper SDN protocol plugin - * - * - * + * The SAL Read Service. Dispatches read requests to the proper SDN protocol + * plugin, and notifies any listeners on updates from any plugin readers */ -public class ReadService implements IReadService, CommandProvider { +public class ReadService implements IReadService, CommandProvider, IPluginOutReadService { - protected static final Logger logger = LoggerFactory - .getLogger(ReadService.class); - private ConcurrentHashMap - pluginReader = + protected static final Logger logger = LoggerFactory.getLogger(ReadService.class); + private ConcurrentHashMap pluginReader = new ConcurrentHashMap(); + private Set readerListeners = + new CopyOnWriteArraySet(); /** * Function called by the dependency manager when all the required @@ -77,6 +83,7 @@ public class ReadService implements IReadService, CommandProvider { // In case of plugin disactivating make sure we clear the // dependencies this.pluginReader.clear(); + this.readerListeners.clear(); } /** @@ -99,7 +106,7 @@ public class ReadService implements IReadService, CommandProvider { } // Set the reference to the plugin flow Reader service - public void setService(Map props, IPluginInReadService s) { + public void setService(Map props, IPluginInReadService s) { if (this.pluginReader == null) { logger.error("pluginReader store null"); return; @@ -108,12 +115,12 @@ public class ReadService implements IReadService, CommandProvider { logger.trace("Got a service set request {}", s); String type = null; for (Object e : props.entrySet()) { - Map.Entry entry = (Map.Entry) e; + Map.Entry entry = (Map.Entry) e; logger.trace("Prop key:({}) value:({})", entry.getKey(), - entry.getValue()); + entry.getValue()); } - Object value = props.get("protocolPluginType"); + Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString()); if (value instanceof String) { type = (String) value; } @@ -126,7 +133,7 @@ public class ReadService implements IReadService, CommandProvider { } } - public void unsetService(Map props, IPluginInReadService s) { + public void unsetService(Map props, IPluginInReadService s) { if (this.pluginReader == null) { logger.error("pluginReader store null"); return; @@ -135,12 +142,12 @@ public class ReadService implements IReadService, CommandProvider { String type = null; logger.debug("Received unsetpluginReader request"); for (Object e : props.entrySet()) { - Map.Entry entry = (Map.Entry) e; + Map.Entry entry = (Map.Entry) e; logger.trace("Prop key:({}) value:({})", entry.getKey(), - entry.getValue()); + entry.getValue()); } - Object value = props.get("protocoloPluginType"); + Object value = props.get(GlobalConstants.PROTOCOLPLUGINTYPE.toString()); if (value instanceof String) { type = (String) value; } @@ -152,16 +159,25 @@ public class ReadService implements IReadService, CommandProvider { logger.debug("Removed the pluginReader for type: {}", type); } } + public void setReaderListener(IReadServiceListener service) { + logger.trace("Got a listener set request {}", service); + this.readerListeners.add(service); + } + + public void unsetReaderListener(IReadServiceListener service) { + logger.trace("Got a listener Unset request"); + this.readerListeners.remove(service); + } @Override public FlowOnNode readFlow(Node node, Flow flow) { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readFlow(node, flow, true); + .readFlow(node, flow, true); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -170,10 +186,10 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readFlow(node, flow, false); + .readFlow(node, flow, false); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -182,11 +198,11 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readAllFlow(node, true); + .readAllFlow(node, true); } } - logger.warn("Plugin unavailable"); - return null; + logger.warn("Plugin {} unavailable", node.getType()); + return Collections.emptyList(); } @Override @@ -194,11 +210,11 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readAllFlow(node, false); + .readAllFlow(node, false); } } - logger.warn("Plugin unavailable"); - return null; + logger.warn("Plugin {} unavailable", node.getType()); + return Collections.emptyList(); } @Override @@ -206,10 +222,10 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readDescription(node, true); + .readDescription(node, true); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -218,10 +234,10 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readDescription(node, false); + .readDescription(node, false); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -231,10 +247,10 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null && node != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readNodeConnector(connector, true); + .readNodeConnector(connector, true); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -245,10 +261,10 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null && node != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readNodeConnector(connector, false); + .readNodeConnector(connector, false); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -257,10 +273,49 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readAllNodeConnector(node, true); + .readAllNodeConnector(node, true); + } + } + logger.warn("Plugin {} unavailable", node.getType()); + return Collections.emptyList(); + } + + @Override + public List readNodeTable(Node node) { + if (pluginReader != null) { + if (this.pluginReader.get(node.getType()) != null) { + return this.pluginReader.get(node.getType()) + .readAllNodeTable(node, true); + } + } + logger.warn("Plugin {} unavailable", node.getType()); + return Collections.emptyList(); + } + + + @Override + public NodeTableStatistics nonCachedReadNodeTable(NodeTable table) { + Node node = table.getNode(); + if (pluginReader != null && node != null) { + if (this.pluginReader.get(node.getType()) != null) { + return this.pluginReader.get(node.getType()) + .readNodeTable(table, false); + } + } + logger.warn("Plugin {} unavailable", node.getType()); + return null; + } + + @Override + public NodeTableStatistics readNodeTable(NodeTable table) { + Node node = table.getNode(); + if (pluginReader != null && node != null) { + if (this.pluginReader.get(node.getType()) != null) { + return this.pluginReader.get(node.getType()) + .readNodeTable(table, true); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return null; } @@ -269,11 +324,11 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .readAllNodeConnector(node, false); + .readAllNodeConnector(node, false); } } - logger.warn("Plugin unavailable"); - return null; + logger.warn("Plugin {} unavailable", node.getType()); + return Collections.emptyList(); } @Override @@ -282,13 +337,41 @@ public class ReadService implements IReadService, CommandProvider { if (pluginReader != null && node != null) { if (this.pluginReader.get(node.getType()) != null) { return this.pluginReader.get(node.getType()) - .getTransmitRate(connector); + .getTransmitRate(connector); } } - logger.warn("Plugin unavailable"); + logger.warn("Plugin {} unavailable", node.getType()); return 0; } + @Override + public void nodeFlowStatisticsUpdated(Node node, List flowStatsList) { + for (IReadServiceListener l : readerListeners){ + l.nodeFlowStatisticsUpdated(node, flowStatsList); + } + } + + @Override + public void nodeConnectorStatisticsUpdated(Node node, List ncStatsList) { + for (IReadServiceListener l : readerListeners){ + l.nodeConnectorStatisticsUpdated(node, ncStatsList); + } + } + + @Override + public void nodeTableStatisticsUpdated(Node node, List tableStatsList) { + for (IReadServiceListener l : readerListeners){ + l.nodeTableStatisticsUpdated(node, tableStatsList); + } + } + + @Override + public void descriptionStatisticsUpdated(Node node, NodeDescription nodeDescription) { + for (IReadServiceListener l : readerListeners){ + l.descriptionStatisticsUpdated(node, nodeDescription); + } + } + // ---------------- OSGI TEST CODE ------------------------------// private void registerWithOSGIConsole() { @@ -302,16 +385,15 @@ public class ReadService implements IReadService, CommandProvider { public String getHelp() { StringBuffer help = new StringBuffer(); help.append("---SAL Reader testing commands---\n"); - help - .append("\t readflows - Read all the (cached) flows from the openflow switch \n"); - help - .append("\t readflow - Read the (cached) sample flow from the openflow switch \n"); - help - .append("\t readdesc - Read the (cached) description from openflow switch \n"); - help - .append("\t cached=true/false. If false or not specified, the protocol plugin cached info\n"); - help - .append("\t is returned. If true, the info is directly retrieved from the switch\n"); + help.append("\t readflows - Read all the (cached) flows from the openflow switch \n"); + help.append("\t readflow - Read the (cached) sample flow from the openflow switch \n"); + help.append("\t readdescr - Read the (cached) description from openflow switch \n"); + help.append("\t\t cached = (true|false). If false or not specified, the plugin cached info\n"); + help.append("\t\t is returned. If true, the info is directly retrieved from the switch\n"); + help.append("\t readport - Read port statistics for the specified port\n"); + help.append("\t readports - Read port statistics for all ports of specified switch\n"); + help.append("\t readtable - Read specified table statistics\n"); + return help.toString(); } @@ -328,9 +410,9 @@ public class ReadService implements IReadService, CommandProvider { try { node = new Node(NodeIDType.OPENFLOW, Long.valueOf(nodeId)); } catch (NumberFormatException e) { - e.printStackTrace(); + logger.error("",e); } catch (ConstructionException e) { - e.printStackTrace(); + logger.error("",e); } List list = (cached) ? this.readAllFlows(node) : this .nonCachedReadAllFlows(node); @@ -355,9 +437,9 @@ public class ReadService implements IReadService, CommandProvider { try { node = new Node(NodeIDType.OPENFLOW, Long.valueOf(nodeId)); } catch (NumberFormatException e) { - e.printStackTrace(); + logger.error("",e); } catch (ConstructionException e) { - e.printStackTrace(); + logger.error("",e); } Flow flow = getSampleFlow(node); FlowOnNode flowOnNode = (cached) ? this.readFlow(node, flow) : this @@ -382,18 +464,18 @@ public class ReadService implements IReadService, CommandProvider { try { node = new Node(NodeIDType.OPENFLOW, Long.valueOf(nodeId)); } catch (NumberFormatException e) { - e.printStackTrace(); + logger.error("",e); } catch (ConstructionException e) { - e.printStackTrace(); + logger.error("",e); } List list = (cached) ? this .readNodeConnectors(node) : this .nonCachedReadNodeConnectors(node); - if (list != null) { - ci.println(list.toString()); - } else { - ci.println("null"); - } + if (list != null) { + ci.println(list.toString()); + } else { + ci.println("null"); + } } public void _readport(CommandInterpreter ci) { @@ -417,11 +499,39 @@ public class ReadService implements IReadService, CommandProvider { NodeConnectorStatistics stats = (cached) ? this .readNodeConnector(nodeConnector) : this .nonCachedReadNodeConnector(nodeConnector); - if (stats != null) { - ci.println(stats.toString()); - } else { - ci.println("null"); + if (stats != null) { + ci.println(stats.toString()); + } else { + ci.println("null"); + } + } + + public void _readtable(CommandInterpreter ci) { + String nodeId = ci.nextArgument(); + String tableId = ci.nextArgument(); + String cacheReq = ci.nextArgument(); + boolean cached; + if (nodeId == null) { + ci.print("Node id not specified"); + return; } + if (tableId == null) { + ci.print("Table id not specified"); + return; + } + cached = (cacheReq == null) ? true : cacheReq.equals("true"); + NodeTable nodeTable = null; + Node node = NodeCreator.createOFNode(Long.parseLong(nodeId)); + nodeTable = NodeTableCreator.createNodeTable(Byte + .valueOf(tableId), node); + NodeTableStatistics stats = (cached) ? this + .readNodeTable(nodeTable) : this + .nonCachedReadNodeTable(nodeTable); + if (stats != null) { + ci.println(stats.toString()); + } else { + ci.println("null"); + } } public void _readdescr(CommandInterpreter ci) { @@ -438,9 +548,9 @@ public class ReadService implements IReadService, CommandProvider { try { node = new Node(NodeIDType.OPENFLOW, Long.valueOf(nodeId)); } catch (NumberFormatException e) { - e.printStackTrace(); + logger.error("",e); } catch (ConstructionException e) { - e.printStackTrace(); + logger.error("",e); } NodeDescription desc = (cached) ? this.readDescription(node) : this .nonCachedReadDescription(node); @@ -496,5 +606,4 @@ public class ReadService implements IReadService, CommandProvider { actions.add(new Controller()); return new Flow(match, actions); } - }