Search in sources :

Example 1 with EdgeProvider

use of org.opennms.features.topology.api.topo.EdgeProvider in project opennms by OpenNMS.

the class GraphMLEdgeStatusProvider method getStatusForEdges.

@Override
public Map<EdgeRef, Status> getStatusForEdges(EdgeProvider edgeProvider, Collection<EdgeRef> edges, Criteria[] criteria) {
    final List<StatusScript> scripts = Lists.newArrayList();
    try (final DirectoryStream<Path> stream = Files.newDirectoryStream(getScriptPath())) {
        for (final Path path : stream) {
            // ignore readme
            if (".readme".equals(path.getFileName().toString())) {
                LOG.debug("Skipping .readme");
                continue;
            }
            final String extension = FilenameUtils.getExtension(path.toString());
            final ScriptEngine scriptEngine = this.scriptEngineManager.getEngineByExtension(extension);
            if (scriptEngine == null) {
                LOG.warn("No script engine found for extension '{}'", extension);
                continue;
            }
            LOG.debug("Found script: path={}, extension={}, engine={}", path, extension, scriptEngine);
            try (final Stream<String> lines = Files.lines(path, Charset.defaultCharset())) {
                final String source = lines.collect(Collectors.joining("\n"));
                scripts.add(new StatusScript(scriptEngine, source));
            }
        }
    } catch (final IOException e) {
        LOG.error("Failed to walk template directory: {}", getScriptPath());
        return Collections.emptyMap();
    }
    return serviceAccessor.getTransactionOperations().execute(transactionStatus -> edges.stream().filter(eachEdge -> eachEdge instanceof GraphMLEdge).map(edge -> (GraphMLEdge) edge).map(edge -> new HashMap.SimpleEntry<>(edge, computeEdgeStatus(scripts, edge))).filter(e -> e.getValue() != null).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}
Also used : Path(java.nio.file.Path) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) EdgeStatusProvider(org.opennms.features.topology.api.topo.EdgeStatusProvider) SimpleScriptContext(javax.script.SimpleScriptContext) AbstractVertex(org.opennms.features.topology.api.topo.AbstractVertex) EdgeProvider(org.opennms.features.topology.api.topo.EdgeProvider) DirectoryStream(java.nio.file.DirectoryStream) Lists(com.google.common.collect.Lists) Charset(java.nio.charset.Charset) Map(java.util.Map) Path(java.nio.file.Path) GraphMLServiceAccessor(org.opennms.features.topology.plugins.topo.graphml.internal.GraphMLServiceAccessor) OnmsNode(org.opennms.netmgt.model.OnmsNode) ScriptException(javax.script.ScriptException) Compilable(javax.script.Compilable) MeasurementsWrapper(org.opennms.features.topology.api.info.MeasurementsWrapper) Logger(org.slf4j.Logger) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) Collection(java.util.Collection) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) SimpleConnector(org.opennms.features.topology.api.topo.SimpleConnector) Collectors(java.util.stream.Collectors) ScriptContext(javax.script.ScriptContext) Objects(java.util.Objects) SimpleBindings(javax.script.SimpleBindings) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Criteria(org.opennms.features.topology.api.topo.Criteria) CompiledScript(javax.script.CompiledScript) ScriptEngine(javax.script.ScriptEngine) Optional(java.util.Optional) Status(org.opennms.features.topology.api.topo.Status) Collections(java.util.Collections) FilenameUtils(org.apache.commons.io.FilenameUtils) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Aggregations

Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 Charset (java.nio.charset.Charset)1 DirectoryStream (java.nio.file.DirectoryStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Compilable (javax.script.Compilable)1 CompiledScript (javax.script.CompiledScript)1 ScriptContext (javax.script.ScriptContext)1