From: Robert Varga Date: Fri, 5 Dec 2014 09:38:26 +0000 (+0100) Subject: BUG-2459: optimize NetconfOperationRouterImpl operations X-Git-Tag: release/lithium~789^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=2638c48f448f8d5156c60ee8fcc0bf3970fad018 BUG-2459: optimize NetconfOperationRouterImpl operations Ensure operations are created immutable at initialization, thus removing the need for synchronization and speeding up lookups. Change-Id: I26ca5aba435d989689994364c53b4d27c7eccd84 Signed-off-by: Robert Varga --- diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionListenerFactory.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionListenerFactory.java index 4be8b91a94..0537942487 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionListenerFactory.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionListenerFactory.java @@ -22,10 +22,10 @@ public class NetconfServerSessionListenerFactory implements SessionListenerFacto private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; private final CapabilityProvider capabilityProvider; - public NetconfServerSessionListenerFactory(DefaultCommitNotificationProducer commitNotifier, - SessionMonitoringService monitor, - NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, - CapabilityProvider capabilityProvider) { + public NetconfServerSessionListenerFactory(final DefaultCommitNotificationProducer commitNotifier, + final SessionMonitoringService monitor, + final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, + final CapabilityProvider capabilityProvider) { this.commitNotifier = commitNotifier; this.monitor = monitor; @@ -35,8 +35,7 @@ public class NetconfServerSessionListenerFactory implements SessionListenerFacto @Override public NetconfServerSessionListener getSessionListener() { - NetconfOperationRouter operationRouter = NetconfOperationRouterImpl.createOperationRouter( - netconfOperationServiceSnapshot, capabilityProvider, commitNotifier); + NetconfOperationRouter operationRouter = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot, capabilityProvider, commitNotifier); return new NetconfServerSessionListener(operationRouter, monitor, netconfOperationServiceSnapshot); } } diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java index e16c0c9d9d..aeab13f7e2 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfOperationRouterImpl.java @@ -8,11 +8,11 @@ package org.opendaylight.controller.netconf.impl.osgi; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; -import com.google.common.collect.Sets; +import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.Map; import java.util.NavigableMap; import java.util.Set; import java.util.TreeMap; @@ -39,61 +39,35 @@ import org.w3c.dom.Document; public class NetconfOperationRouterImpl implements NetconfOperationRouter { private static final Logger LOG = LoggerFactory.getLogger(NetconfOperationRouterImpl.class); - private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot; - private Set allNetconfOperations; - - private NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot; - } + private final Collection allNetconfOperations; - private synchronized void initNetconfOperations(final Set allOperations) { - allNetconfOperations = allOperations; - } - - /** - * Factory method to produce instance of NetconfOperationRouter - */ - public static NetconfOperationRouter createOperationRouter(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, - final CapabilityProvider capabilityProvider, final DefaultCommitNotificationProducer commitNotifier) { - NetconfOperationRouterImpl router = new NetconfOperationRouterImpl(netconfOperationServiceSnapshot); - - Preconditions.checkNotNull(netconfOperationServiceSnapshot); - Preconditions.checkNotNull(capabilityProvider); + public NetconfOperationRouterImpl(final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot, final CapabilityProvider capabilityProvider, + final DefaultCommitNotificationProducer commitNotifier) { + this.netconfOperationServiceSnapshot = Preconditions.checkNotNull(netconfOperationServiceSnapshot); final String sessionId = netconfOperationServiceSnapshot.getNetconfSessionIdForReporting(); - final Set defaultNetconfOperations = Sets.newHashSet(); - defaultNetconfOperations.add(new DefaultGetSchema(capabilityProvider, sessionId)); - defaultNetconfOperations.add(new DefaultCloseSession(sessionId, router)); - defaultNetconfOperations.add(new DefaultStartExi(sessionId)); - defaultNetconfOperations.add(new DefaultStopExi(sessionId)); - defaultNetconfOperations.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, router)); - - router.initNetconfOperations(getAllNetconfOperations(defaultNetconfOperations, netconfOperationServiceSnapshot)); - - return router; - } - - private static Set getAllNetconfOperations(final Set defaultNetconfOperations, - final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) { - Set result = new HashSet<>(); - result.addAll(defaultNetconfOperations); + final Set ops = new HashSet<>(); + ops.add(new DefaultGetSchema(capabilityProvider, sessionId)); + ops.add(new DefaultCloseSession(sessionId, this)); + ops.add(new DefaultStartExi(sessionId)); + ops.add(new DefaultStopExi(sessionId)); + ops.add(new DefaultCommit(commitNotifier, capabilityProvider, sessionId, this)); for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) { - final Set netOpsFromService = netconfOperationService.getNetconfOperations(); - for (NetconfOperation netconfOperation : netOpsFromService) { - Preconditions.checkState(!result.contains(netconfOperation), + for (NetconfOperation netconfOperation : netconfOperationService.getNetconfOperations()) { + Preconditions.checkState(!ops.contains(netconfOperation), "Netconf operation %s already present", netconfOperation); - result.add(netconfOperation); + ops.add(netconfOperation); } } - return Collections.unmodifiableSet(result); + + allNetconfOperations = ImmutableSet.copyOf(ops); } @Override - public synchronized Document onNetconfMessage(final Document message, - final NetconfServerSession session) throws NetconfDocumentedException { + public Document onNetconfMessage(final Document message, final NetconfServerSession session) throws NetconfDocumentedException { Preconditions.checkNotNull(allNetconfOperations, "Operation router was not initialized properly"); final NetconfOperationExecution netconfOperationExecution; @@ -131,15 +105,13 @@ public class NetconfOperationRouterImpl implements NetconfOperationRouter { netconfOperationServiceSnapshot.close(); } - private NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException { - LOG.error(s, e); - - Map info = Maps.newHashMap(); - info.put(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString()); + private static NetconfDocumentedException handleUnexpectedEx(final String s, final Exception e) throws NetconfDocumentedException { + LOG.error("{}", s, e); return new NetconfDocumentedException("Unexpected error", NetconfDocumentedException.ErrorType.application, NetconfDocumentedException.ErrorTag.operation_failed, - NetconfDocumentedException.ErrorSeverity.error, info); + NetconfDocumentedException.ErrorSeverity.error, + Collections.singletonMap(NetconfDocumentedException.ErrorSeverity.error.toString(), e.toString())); } private Document executeOperationWithHighestPriority(final Document message,