From ce8ad4e92f8128750f7ce7216f7e73ad238efaae Mon Sep 17 00:00:00 2001 From: Stephen Kitt Date: Thu, 26 Jul 2018 10:35:02 +0200 Subject: [PATCH] Simplify code using Java 8 features * method references * lambda expressions * Map::computeIfAbsent Change-Id: Icbfa5c0a76b5b6f2b4339dbbd49b8a44e7b14fb2 Signed-off-by: Stephen Kitt --- .../impl/NetconfServerDispatcherImpl.java | 17 ++------- .../NetconfCapabilityMonitoringService.java | 7 ++-- .../osgi/NetconfSessionMonitoringService.java | 7 +--- .../AbstractNetconfSessionNegotiator.java | 26 +++++--------- .../nettyutil/handler/NetconfEXICodec.java | 7 +--- .../handler/ssh/client/AsyncSshHandler.java | 2 +- .../netconf/ssh/RemoteNetconfCommand.java | 35 +++++++------------ .../impl/NetconfTopologyManager.java | 6 ++-- .../impl/tx/ProxyReadWriteTransaction.java | 3 +- .../test/tool/config/Configuration.java | 11 ++---- .../common/util/MultivaluedHashMap.java | 8 +---- .../sal/rest/doc/util/RestDocgenUtil.java | 14 +++----- 12 files changed, 41 insertions(+), 102 deletions(-) diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerDispatcherImpl.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerDispatcherImpl.java index 7db8db4f09..394771b122 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerDispatcherImpl.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/NetconfServerDispatcherImpl.java @@ -12,9 +12,7 @@ import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; -import io.netty.channel.local.LocalChannel; import io.netty.channel.local.LocalServerChannel; -import io.netty.channel.socket.SocketChannel; import io.netty.util.concurrent.Promise; import java.net.InetSocketAddress; import org.opendaylight.netconf.api.NetconfServerDispatcher; @@ -35,23 +33,12 @@ public class NetconfServerDispatcherImpl extends AbstractDispatcher() { - @Override - public void initializeChannel(final SocketChannel ch, final Promise promise) { - initializer.initialize(ch, promise); - } - }); + return super.createServer(address, initializer::initialize); } @Override public ChannelFuture createLocalServer(LocalAddress address) { - return super.createServer(address, LocalServerChannel.class, new ChannelPipelineInitializer() { - @Override - public void initializeChannel(final LocalChannel ch, final Promise promise) { - initializer.initialize(ch, promise); - } - }); + return super.createServer(address, LocalServerChannel.class, initializer::initialize); } public static class ServerChannelInitializer extends AbstractChannelInitializer { diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfCapabilityMonitoringService.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfCapabilityMonitoringService.java index fd520a363e..f627f15a7e 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfCapabilityMonitoringService.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfCapabilityMonitoringService.java @@ -106,11 +106,8 @@ class NetconfCapabilityMonitoringService implements CapabilityListener, AutoClos } final String currentModuleName = cap.getModuleName().get(); - Map revisionMap = mappedModulesToRevisionToSchema.get(currentModuleName); - if (revisionMap == null) { - revisionMap = Maps.newHashMap(); - mappedModulesToRevisionToSchema.put(currentModuleName, revisionMap); - } + Map revisionMap = + mappedModulesToRevisionToSchema.computeIfAbsent(currentModuleName, k -> Maps.newHashMap()); final String currentRevision = cap.getRevision().get(); revisionMap.put(currentRevision, cap.getCapabilitySchema().get()); diff --git a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfSessionMonitoringService.java b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfSessionMonitoringService.java index da9a96b6d1..639daa803d 100644 --- a/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfSessionMonitoringService.java +++ b/netconf/netconf-impl/src/main/java/org/opendaylight/netconf/impl/osgi/NetconfSessionMonitoringService.java @@ -99,12 +99,7 @@ class NetconfSessionMonitoringService implements SessionListener, AutoCloseable if (!running) { startUpdateSessionStats(); } - return new AutoCloseable() { - @Override - public void close() { - listeners.remove(listener); - } - }; + return () -> listeners.remove(listener); } @Override diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java index 065a5bc9c4..d9c15ae035 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/AbstractNetconfSessionNegotiator.java @@ -19,7 +19,6 @@ import io.netty.handler.ssl.SslHandler; import io.netty.util.Timeout; import io.netty.util.Timer; import io.netty.util.TimerTask; -import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.Promise; import java.util.concurrent.TimeUnit; @@ -84,14 +83,10 @@ public abstract class AbstractNetconfSessionNegotiator

sslHandler = getSslHandler(channel); if (sslHandler.isPresent()) { - Future future = sslHandler.get().handshakeFuture(); - future.addListener(new GenericFutureListener>() { - @Override - public void operationComplete(final Future future) { - Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful"); - LOG.debug("Ssl handshake complete"); - start(); - } + sslHandler.get().handshakeFuture().addListener(future -> { + Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful"); + LOG.debug("Ssl handshake complete"); + start(); }); } else { start(); @@ -139,14 +134,11 @@ public abstract class AbstractNetconfSessionNegotiator

() { - @Override - public void operationComplete(final ChannelFuture future) { - if (future.isSuccess()) { - LOG.debug("Channel {} closed: success", future.channel()); - } else { - LOG.warn("Channel {} closed: fail", future.channel()); - } + channel.close().addListener((GenericFutureListener) future -> { + if (future.isSuccess()) { + LOG.debug("Channel {} closed: success", future.channel()); + } else { + LOG.warn("Channel {} closed: fail", future.channel()); } }); } diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java index 383e8b7d50..44d07209e7 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEXICodec.java @@ -26,12 +26,7 @@ public final class NetconfEXICodec { * OpenEXI does not allow us to directly prevent resolution of external entities. In order * to prevent XXE attacks, we reuse a single no-op entity resolver. */ - private static final EntityResolver ENTITY_RESOLVER = new EntityResolver() { - @Override - public InputSource resolveEntity(final String publicId, final String systemId) { - return new InputSource(); - } - }; + private static final EntityResolver ENTITY_RESOLVER = (publicId, systemId) -> new InputSource(); /** * Since we have a limited number of options we can have, instantiating a weak cache diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java index fa59b344b8..149ccf16f9 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/ssh/client/AsyncSshHandler.java @@ -164,7 +164,7 @@ public class AsyncSshHandler extends ChannelOutboundHandlerAdapter { ClientChannel localChannel = channel; sshReadAsyncListener = new AsyncSshHandlerReader(() -> AsyncSshHandler.this.disconnect(ctx, ctx.newPromise()), - msg -> ctx.fireChannelRead(msg), localChannel.toString(), localChannel.getAsyncOut()); + ctx::fireChannelRead, localChannel.toString(), localChannel.getAsyncOut()); // if readAsyncListener receives immediate close, // it will close this handler and closing this handler sets channel variable to null diff --git a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/RemoteNetconfCommand.java b/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/RemoteNetconfCommand.java index c016dc3959..733d81af1f 100644 --- a/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/RemoteNetconfCommand.java +++ b/netconf/netconf-ssh/src/main/java/org/opendaylight/netconf/ssh/RemoteNetconfCommand.java @@ -16,7 +16,6 @@ import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.local.LocalAddress; import io.netty.channel.local.LocalChannel; -import io.netty.util.concurrent.GenericFutureListener; import java.io.InputStream; import java.io.OutputStream; import java.net.InetSocketAddress; @@ -118,19 +117,15 @@ public class RemoteNetconfCommand implements AsyncCommand, SessionAware { } }); clientChannelFuture = clientBootstrap.connect(localAddress); - clientChannelFuture.addListener(new GenericFutureListener() { - - @Override - public void operationComplete(final ChannelFuture future) { - if (future.isSuccess()) { - clientChannel = clientChannelFuture.channel(); - } else { - LOG.warn("Unable to establish internal connection to netconf server for client: {}", - getClientAddress()); - Preconditions.checkNotNull(callback, "Exit callback must be set"); - callback.onExit(1, "Unable to establish internal connection to netconf server for client: " - + getClientAddress()); - } + clientChannelFuture.addListener(future -> { + if (future.isSuccess()) { + clientChannel = clientChannelFuture.channel(); + } else { + LOG.warn("Unable to establish internal connection to netconf server for client: {}", + getClientAddress()); + Preconditions.checkNotNull(callback, "Exit callback must be set"); + callback.onExit(1, "Unable to establish internal connection to netconf server for client: " + + getClientAddress()); } }); } @@ -142,14 +137,10 @@ public class RemoteNetconfCommand implements AsyncCommand, SessionAware { clientChannelFuture.cancel(true); if (clientChannel != null) { - clientChannel.close().addListener(new GenericFutureListener() { - - @Override - public void operationComplete(final ChannelFuture future) { - if (!future.isSuccess()) { - LOG.warn("Unable to release internal connection to netconf server on channel: {}", - clientChannel); - } + clientChannel.close().addListener(future -> { + if (!future.isSuccess()) { + LOG.warn("Unable to release internal connection to netconf server on channel: {}", + clientChannel); } }); } diff --git a/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManager.java b/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManager.java index a87f77c2e9..a71f7cb23f 100644 --- a/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManager.java +++ b/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/NetconfTopologyManager.java @@ -209,10 +209,8 @@ public class NetconfTopologyManager dataChangeListenerRegistration = null; } - contexts.values().forEach(netconfTopologyContext -> close(netconfTopologyContext)); - - clusterRegistrations.values().forEach( - clusterSingletonServiceRegistration -> close(clusterSingletonServiceRegistration)); + contexts.values().forEach(NetconfTopologyManager::close); + clusterRegistrations.values().forEach(NetconfTopologyManager::close); contexts.clear(); clusterRegistrations.clear(); diff --git a/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/tx/ProxyReadWriteTransaction.java b/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/tx/ProxyReadWriteTransaction.java index 82fbc41e05..bfe776e79e 100644 --- a/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/tx/ProxyReadWriteTransaction.java +++ b/netconf/netconf-topology-singleton/src/main/java/org/opendaylight/netconf/topology/singleton/impl/tx/ProxyReadWriteTransaction.java @@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import javax.annotation.concurrent.GuardedBy; import org.eclipse.jdt.annotation.NonNull; +import org.opendaylight.controller.md.sal.common.api.data.AsyncWriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction; @@ -79,7 +80,7 @@ public class ProxyReadWriteTransaction implements DOMDataReadWriteTransaction { return false; } - processTransactionOperation(facade -> facade.cancel()); + processTransactionOperation(AsyncWriteTransaction::cancel); return true; } diff --git a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/config/Configuration.java b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/config/Configuration.java index 665442535c..390be4b416 100644 --- a/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/config/Configuration.java +++ b/netconf/tools/netconf-testtool/src/main/java/org/opendaylight/netconf/test/tool/config/Configuration.java @@ -9,11 +9,9 @@ package org.opendaylight.netconf.test.tool.config; import com.google.common.collect.ImmutableSet; import java.io.File; -import java.security.PublicKey; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator; -import org.apache.sshd.server.session.ServerSession; import org.opendaylight.netconf.api.xml.XmlNetconfConstants; import org.opendaylight.netconf.auth.AuthProvider; import org.opendaylight.netconf.test.tool.operations.OperationsCreator; @@ -53,12 +51,9 @@ public class Configuration { return true; }; - public static final PublickeyAuthenticator DEFAULT_PUBLIC_KEY_AUTHENTICATOR = new PublickeyAuthenticator() { - @Override - public boolean authenticate(final String username, final PublicKey key, final ServerSession session) { - LOG.info("Auth with public key: {}", key); - return true; - } + public static final PublickeyAuthenticator DEFAULT_PUBLIC_KEY_AUTHENTICATOR = (username, key, session) -> { + LOG.info("Auth with public key: {}", key); + return true; }; private int generateConfigsTimeout = (int) TimeUnit.MINUTES.toMillis(30); diff --git a/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/util/MultivaluedHashMap.java b/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/util/MultivaluedHashMap.java index 1036757f49..4eedb93245 100644 --- a/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/util/MultivaluedHashMap.java +++ b/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/util/MultivaluedHashMap.java @@ -177,12 +177,6 @@ public class MultivaluedHashMap implements MultivaluedMap { } private List getValues(K key) { - List list = store.get(key); - if (list == null) { - list = new LinkedList<>(); - store.put(key, list); - } - - return list; + return store.computeIfAbsent(key, k -> new LinkedList<>()); } } diff --git a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/util/RestDocgenUtil.java b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/util/RestDocgenUtil.java index 1fa3a01dc3..ee96a376db 100644 --- a/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/util/RestDocgenUtil.java +++ b/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/util/RestDocgenUtil.java @@ -55,16 +55,10 @@ public final class RestDocgenUtil { final URI namespace = node.getQName().getNamespace(); final Optional revision = node.getQName().getRevision(); - Map, Module> revisionToModule = NAMESPACE_AND_REVISION_TO_MODULE.get(namespace); - if (revisionToModule == null) { - revisionToModule = new HashMap<>(); - NAMESPACE_AND_REVISION_TO_MODULE.put(namespace, revisionToModule); - } - Module module = revisionToModule.get(revision); - if (module == null) { - module = schemaContext.findModule(namespace, revision).orElse(null); - revisionToModule.put(revision, module); - } + Map, Module> revisionToModule = + NAMESPACE_AND_REVISION_TO_MODULE.computeIfAbsent(namespace, k -> new HashMap<>()); + Module module = + revisionToModule.computeIfAbsent(revision, k -> schemaContext.findModule(namespace, k).orElse(null)); if (module != null) { return module.getName() + ":" + node.getQName().getLocalName(); } -- 2.36.6