X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-topology-singleton%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Ftopology%2Fsingleton%2Fimpl%2FNetconfTopologyManager.java;h=a71f7cb23f832a284c72b14867a6672691855a37;hb=1e174be6002940e17aee31d15f8914273d30f25c;hp=23a46aaa5a7a22e1bd710656ae28c2655b03d65a;hpb=f273cff9baab6d3bd6a93214b639ecab7061e242;p=netconf.git 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 23a46aaa5a..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 @@ -10,14 +10,17 @@ package org.opendaylight.netconf.topology.singleton.impl; import akka.actor.ActorSystem; import akka.util.Timeout; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.MoreExecutors; import io.netty.util.concurrent.EventExecutor; import java.util.Collection; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import javax.annotation.Nonnull; +import org.opendaylight.aaa.encrypt.AAAEncryptionService; import org.opendaylight.controller.cluster.ActorSystemProvider; import org.opendaylight.controller.config.threadpool.ScheduledThreadPool; import org.opendaylight.controller.config.threadpool.ThreadPool; @@ -28,9 +31,9 @@ import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier; import org.opendaylight.controller.md.sal.binding.api.DataTreeModification; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService; import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; -import org.opendaylight.controller.sal.core.api.Broker; +import org.opendaylight.mdsal.common.api.CommitInfo; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration; import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier; @@ -40,6 +43,7 @@ import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySet import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup.NetconfTopologySetupBuilder; import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.topology.singleton.config.rev170419.Config; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopologyBuilder; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; @@ -59,46 +63,53 @@ public class NetconfTopologyManager private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyManager.class); - private final Map, NetconfTopologyContext> contexts = new HashMap<>(); + private final Map, NetconfTopologyContext> contexts = new ConcurrentHashMap<>(); private final Map, ClusterSingletonServiceRegistration> - clusterRegistrations = new HashMap<>(); - - private ListenerRegistration dataChangeListenerRegistration; + clusterRegistrations = new ConcurrentHashMap<>(); private final DataBroker dataBroker; private final RpcProviderRegistry rpcProviderRegistry; private final ClusterSingletonServiceProvider clusterSingletonServiceProvider; - private final BindingAwareBroker bindingAwareBroker; private final ScheduledThreadPool keepaliveExecutor; private final ThreadPool processingExecutor; - private final Broker domBroker; private final ActorSystem actorSystem; private final EventExecutor eventExecutor; private final NetconfClientDispatcher clientDispatcher; private final String topologyId; + private final Duration writeTxIdleTimeout; + private final DOMMountPointService mountPointService; + private final AAAEncryptionService encryptionService; + private ListenerRegistration dataChangeListenerRegistration; + private String privateKeyPath; + private String privateKeyPassphrase; public NetconfTopologyManager(final DataBroker dataBroker, final RpcProviderRegistry rpcProviderRegistry, final ClusterSingletonServiceProvider clusterSingletonServiceProvider, - final BindingAwareBroker bindingAwareBroker, final ScheduledThreadPool keepaliveExecutor, final ThreadPool processingExecutor, - final Broker domBroker, final ActorSystemProvider actorSystemProvider, final EventExecutor eventExecutor, - final NetconfClientDispatcher clientDispatcher, final String topologyId) { + final ActorSystemProvider actorSystemProvider, + final EventExecutor eventExecutor, final NetconfClientDispatcher clientDispatcher, + final String topologyId, final Config config, + final DOMMountPointService mountPointService, + final AAAEncryptionService encryptionService) { + this.dataBroker = Preconditions.checkNotNull(dataBroker); this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry); this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonServiceProvider); - this.bindingAwareBroker = Preconditions.checkNotNull(bindingAwareBroker); this.keepaliveExecutor = Preconditions.checkNotNull(keepaliveExecutor); this.processingExecutor = Preconditions.checkNotNull(processingExecutor); - this.domBroker = Preconditions.checkNotNull(domBroker); this.actorSystem = Preconditions.checkNotNull(actorSystemProvider).getActorSystem(); this.eventExecutor = Preconditions.checkNotNull(eventExecutor); this.clientDispatcher = Preconditions.checkNotNull(clientDispatcher); this.topologyId = Preconditions.checkNotNull(topologyId); + this.writeTxIdleTimeout = Duration.apply(config.getWriteTransactionIdleTimeout(), TimeUnit.SECONDS); + this.mountPointService = mountPointService; + this.encryptionService = Preconditions.checkNotNull(encryptionService); + } // Blueprint init method public void init() { - dataChangeListenerRegistration = registerDataTreeChangeListener(topologyId); + dataChangeListenerRegistration = registerDataTreeChangeListener(); } @Override @@ -136,8 +147,13 @@ public class NetconfTopologyManager context.refresh(createSetup(instanceIdentifier, node)); } + // ClusterSingletonServiceRegistration registerClusterSingletonService method throws a Runtime exception if there + // are problems with registration and client has to deal with it. Only thing we can do if this error occurs is to + // retry registration several times and log the error. + // TODO change to a specific documented Exception when changed in ClusterSingletonServiceProvider + @SuppressWarnings("checkstyle:IllegalCatch") private void startNetconfDeviceContext(final InstanceIdentifier instanceIdentifier, final Node node) { - final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class); + final NetconfNode netconfNode = node.augmentation(NetconfNode.class); Preconditions.checkNotNull(netconfNode); Preconditions.checkNotNull(netconfNode.getHost()); Preconditions.checkNotNull(netconfNode.getHost().getIpAddress()); @@ -148,61 +164,88 @@ public class NetconfTopologyManager final ServiceGroupIdentifier serviceGroupIdent = ServiceGroupIdentifier.create(instanceIdentifier.toString()); - final NetconfTopologyContext newNetconfTopologyContext = - new NetconfTopologyContext(createSetup(instanceIdentifier, node), serviceGroupIdent, - actorResponseWaitTime); + final NetconfTopologyContext newNetconfTopologyContext = newNetconfTopologyContext( + createSetup(instanceIdentifier, node), serviceGroupIdent, actorResponseWaitTime); - final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration = - clusterSingletonServiceProvider.registerClusterSingletonService(newNetconfTopologyContext); + int tries = 3; + while (true) { + try { + final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration = + clusterSingletonServiceProvider.registerClusterSingletonService(newNetconfTopologyContext); + clusterRegistrations.put(instanceIdentifier, clusterSingletonServiceRegistration); + contexts.put(instanceIdentifier, newNetconfTopologyContext); + break; + } catch (final RuntimeException e) { + LOG.warn("Unable to register cluster singleton service {}, trying again", newNetconfTopologyContext, e); - clusterRegistrations.put(instanceIdentifier, clusterSingletonServiceRegistration); - contexts.put(instanceIdentifier, newNetconfTopologyContext); + if (--tries <= 0) { + LOG.error("Unable to register cluster singleton service {} - done trying, closing topology context", + newNetconfTopologyContext, e); + close(newNetconfTopologyContext); + break; + } + } + } } private void stopNetconfDeviceContext(final InstanceIdentifier instanceIdentifier) { - if (contexts.containsKey(instanceIdentifier)) { - try { - clusterRegistrations.get(instanceIdentifier).close(); - contexts.get(instanceIdentifier).closeFinal(); - } catch (final Exception e) { - LOG.warn("Error at closing topology context. InstanceIdentifier: " + instanceIdentifier); - } - contexts.remove(instanceIdentifier); - clusterRegistrations.remove(instanceIdentifier); + final NetconfTopologyContext netconfTopologyContext = contexts.remove(instanceIdentifier); + if (netconfTopologyContext != null) { + close(clusterRegistrations.remove(instanceIdentifier)); + close(netconfTopologyContext); } } + @VisibleForTesting + protected NetconfTopologyContext newNetconfTopologyContext(NetconfTopologySetup setup, + ServiceGroupIdentifier serviceGroupIdent, Timeout actorResponseWaitTime) { + return new NetconfTopologyContext(setup, serviceGroupIdent, actorResponseWaitTime, mountPointService); + } + @Override public void close() { if (dataChangeListenerRegistration != null) { dataChangeListenerRegistration.close(); dataChangeListenerRegistration = null; } - contexts.forEach((instanceIdentifier, netconfTopologyContext) -> { - try { - netconfTopologyContext.closeFinal(); - } catch (final Exception e) { - LOG.error("Error at closing topology context. InstanceIdentifier: " + instanceIdentifier, e); - } - }); - clusterRegistrations.forEach((instanceIdentifier, clusterSingletonServiceRegistration) -> { - try { - clusterSingletonServiceRegistration.close(); - } catch (final Exception e) { - LOG.error("Error at unregistering from cluster. InstanceIdentifier: " + instanceIdentifier, e); - } - }); + + contexts.values().forEach(NetconfTopologyManager::close); + clusterRegistrations.values().forEach(NetconfTopologyManager::close); + contexts.clear(); clusterRegistrations.clear(); } - private ListenerRegistration registerDataTreeChangeListener(final String topologyId) { + @SuppressWarnings("checkstyle:IllegalCatch") + private static void close(AutoCloseable closeable) { + try { + closeable.close(); + } catch (Exception e) { + LOG.warn("Error closing {}", closeable, e); + } + } + + /** + * Sets the private key path from location specified in configuration file using blueprint. + */ + public void setPrivateKeyPath(final String privateKeyPath) { + this.privateKeyPath = privateKeyPath; + } + + /** + * Sets the private key passphrase from location specified in configuration file using blueprint. + */ + public void setPrivateKeyPassphrase(final String privateKeyPassphrase) { + this.privateKeyPassphrase = privateKeyPassphrase; + } + + private ListenerRegistration registerDataTreeChangeListener() { final WriteTransaction wtx = dataBroker.newWriteOnlyTransaction(); - initTopology(wtx, LogicalDatastoreType.CONFIGURATION, topologyId); - initTopology(wtx, LogicalDatastoreType.OPERATIONAL, topologyId); - Futures.addCallback(wtx.submit(), new FutureCallback() { + initTopology(wtx, LogicalDatastoreType.CONFIGURATION); + initTopology(wtx, LogicalDatastoreType.OPERATIONAL); + wtx.commit().addCallback(new FutureCallback() { @Override - public void onSuccess(final Void result) { + public void onSuccess(final CommitInfo result) { LOG.debug("topology initialization successful"); } @@ -210,15 +253,15 @@ public class NetconfTopologyManager public void onFailure(@Nonnull final Throwable throwable) { LOG.error("Unable to initialize netconf-topology, {}", throwable); } - }); + }, MoreExecutors.directExecutor()); LOG.debug("Registering datastore listener"); return dataBroker.registerDataTreeChangeListener( - new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, - NetconfTopologyUtils.createTopologyListPath(topologyId).child(Node.class)), this); + new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, + NetconfTopologyUtils.createTopologyListPath(topologyId).child(Node.class)), this); } - private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType, final String topologyId) { + private void initTopology(final WriteTransaction wtx, final LogicalDatastoreType datastoreType) { final NetworkTopology networkTopology = new NetworkTopologyBuilder().build(); final InstanceIdentifier networkTopologyId = InstanceIdentifier.builder(NetworkTopology.class).build(); @@ -235,14 +278,17 @@ public class NetconfTopologyManager .setInstanceIdentifier(instanceIdentifier) .setRpcProviderRegistry(rpcProviderRegistry) .setNode(node) - .setBindingAwareBroker(bindingAwareBroker) .setActorSystem(actorSystem) .setEventExecutor(eventExecutor) - .setDomBroker(domBroker) .setKeepaliveExecutor(keepaliveExecutor) .setProcessingExecutor(processingExecutor) .setTopologyId(topologyId) - .setNetconfClientDispatcher(clientDispatcher); + .setNetconfClientDispatcher(clientDispatcher) + .setSchemaResourceDTO(NetconfTopologyUtils.setupSchemaCacheDTO(node)) + .setIdleTimeout(writeTxIdleTimeout) + .setPrivateKeyPath(privateKeyPath) + .setPrivateKeyPassphrase(privateKeyPassphrase) + .setEncryptionService(encryptionService); return builder.build(); }