2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.topology.singleton.impl;
11 import akka.actor.ActorRef;
12 import akka.util.Timeout;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.Lists;
16 import com.google.common.util.concurrent.FutureCallback;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.concurrent.EventExecutor;
20 import java.math.BigDecimal;
21 import java.net.InetSocketAddress;
23 import java.util.ArrayList;
24 import java.util.List;
26 import java.util.Optional;
27 import javax.annotation.Nullable;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.netconf.api.NetconfMessage;
30 import org.opendaylight.netconf.client.NetconfClientSessionListener;
31 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
32 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
33 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
34 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
35 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
36 import org.opendaylight.netconf.sal.connect.api.RemoteDevice;
37 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
38 import org.opendaylight.netconf.sal.connect.netconf.LibraryModulesSchemas;
39 import org.opendaylight.netconf.sal.connect.netconf.NetconfDevice;
40 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceBuilder;
41 import org.opendaylight.netconf.sal.connect.netconf.SchemalessNetconfDevice;
42 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
43 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
44 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
45 import org.opendaylight.netconf.sal.connect.netconf.listener.UserPreferences;
46 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
47 import org.opendaylight.netconf.sal.connect.netconf.schema.YangLibrarySchemaYangSourceProvider;
48 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
49 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
50 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfConnectorDTO;
51 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
52 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
53 import org.opendaylight.protocol.framework.ReconnectStrategy;
54 import org.opendaylight.protocol.framework.ReconnectStrategyFactory;
55 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability.CapabilityOrigin;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.Credentials;
61 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
62 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
63 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
64 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
65 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
69 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
71 private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
73 // Initializes default constant instances for the case when the default schema repository
74 // directory cache/schema is used.
76 private final NetconfTopologySetup netconfTopologyDeviceSetup;
77 private final RemoteDeviceId remoteDeviceId;
78 private final DOMMountPointService mountService;
79 private final Timeout actorResponseWaitTime;
81 private NetconfConnectorDTO deviceCommunicatorDTO;
83 public RemoteDeviceConnectorImpl(final NetconfTopologySetup netconfTopologyDeviceSetup,
84 final RemoteDeviceId remoteDeviceId, final Timeout actorResponseWaitTime,
85 final DOMMountPointService mountService) {
87 this.netconfTopologyDeviceSetup = Preconditions.checkNotNull(netconfTopologyDeviceSetup);
88 this.remoteDeviceId = remoteDeviceId;
89 this.actorResponseWaitTime = actorResponseWaitTime;
90 this.mountService = mountService;
94 public void startRemoteDeviceConnection(final ActorRef deviceContextActorRef) {
96 final NetconfNode netconfNode = netconfTopologyDeviceSetup.getNode().getAugmentation(NetconfNode.class);
97 final NodeId nodeId = netconfTopologyDeviceSetup.getNode().getNodeId();
98 Preconditions.checkNotNull(netconfNode.getHost());
99 Preconditions.checkNotNull(netconfNode.getPort());
100 Preconditions.checkNotNull(netconfNode.isTcpOnly());
102 this.deviceCommunicatorDTO = createDeviceCommunicator(nodeId, netconfNode, deviceContextActorRef);
103 final NetconfDeviceCommunicator deviceCommunicator = deviceCommunicatorDTO.getCommunicator();
104 final NetconfClientSessionListener netconfClientSessionListener = deviceCommunicatorDTO.getSessionListener();
105 final NetconfReconnectingClientConfiguration clientConfig =
106 getClientConfig(netconfClientSessionListener, netconfNode);
107 final ListenableFuture<NetconfDeviceCapabilities> future = deviceCommunicator
108 .initializeRemoteConnection(netconfTopologyDeviceSetup.getNetconfClientDispatcher(), clientConfig);
110 Futures.addCallback(future, new FutureCallback<NetconfDeviceCapabilities>() {
112 public void onSuccess(final NetconfDeviceCapabilities result) {
113 LOG.debug("{}: Connector started successfully", remoteDeviceId);
117 public void onFailure(@Nullable final Throwable throwable) {
118 LOG.error("{}: Connector failed, {}", remoteDeviceId, throwable);
124 public void stopRemoteDeviceConnection() {
125 Preconditions.checkNotNull(deviceCommunicatorDTO, remoteDeviceId + ": Device communicator was not created.");
127 deviceCommunicatorDTO.close();
128 } catch (final Exception e) {
129 LOG.error("{}: Error at closing device communicator.", remoteDeviceId, e);
134 NetconfConnectorDTO createDeviceCommunicator(final NodeId nodeId, final NetconfNode node,
135 final ActorRef deviceContextActorRef) {
136 //setup default values since default value is not supported in mdsal
137 final Long defaultRequestTimeoutMillis = node.getDefaultRequestTimeoutMillis() == null
138 ? NetconfTopologyUtils.DEFAULT_REQUEST_TIMEOUT_MILLIS : node.getDefaultRequestTimeoutMillis();
139 final Long keepaliveDelay = node.getKeepaliveDelay() == null
140 ? NetconfTopologyUtils.DEFAULT_KEEPALIVE_DELAY : node.getKeepaliveDelay();
141 final Boolean reconnectOnChangedSchema = node.isReconnectOnChangedSchema() == null
142 ? NetconfTopologyUtils.DEFAULT_RECONNECT_ON_CHANGED_SCHEMA : node.isReconnectOnChangedSchema();
144 RemoteDeviceHandler<NetconfSessionPreferences> salFacade = new MasterSalFacade(remoteDeviceId,
145 netconfTopologyDeviceSetup.getActorSystem(), deviceContextActorRef, actorResponseWaitTime,
146 mountService, netconfTopologyDeviceSetup.getDataBroker());
147 if (keepaliveDelay > 0) {
148 LOG.info("{}: Adding keepalive facade.", remoteDeviceId);
149 salFacade = new KeepaliveSalFacade(remoteDeviceId, salFacade,
150 netconfTopologyDeviceSetup.getKeepaliveExecutor().getExecutor(), keepaliveDelay,
151 defaultRequestTimeoutMillis);
154 final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = netconfTopologyDeviceSetup.getSchemaResourcesDTO();
157 // pre register yang library sources as fallback schemas to schema registry
158 final List<SchemaSourceRegistration<YangTextSchemaSource>> registeredYangLibSources = Lists.newArrayList();
159 if (node.getYangLibrary() != null) {
160 final String yangLibURL = node.getYangLibrary().getYangLibraryUrl().getValue();
161 final String yangLibUsername = node.getYangLibrary().getUsername();
162 final String yangLigPassword = node.getYangLibrary().getPassword();
164 final LibraryModulesSchemas libraryModulesSchemas;
165 if (yangLibURL != null) {
166 if (yangLibUsername != null && yangLigPassword != null) {
167 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL, yangLibUsername, yangLigPassword);
169 libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL);
172 for (final Map.Entry<SourceIdentifier, URL> sourceIdentifierURLEntry :
173 libraryModulesSchemas.getAvailableModels().entrySet()) {
174 registeredYangLibSources
175 .add(schemaResourcesDTO.getSchemaRegistry().registerSchemaSource(
176 new YangLibrarySchemaYangSourceProvider(remoteDeviceId,
177 libraryModulesSchemas.getAvailableModels()),
178 PotentialSchemaSource
179 .create(sourceIdentifierURLEntry.getKey(), YangTextSchemaSource.class,
180 PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
185 final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> device;
186 if (node.isSchemaless()) {
187 device = new SchemalessNetconfDevice(remoteDeviceId, salFacade);
189 device = new NetconfDeviceBuilder()
190 .setReconnectOnSchemasChange(reconnectOnChangedSchema)
191 .setSchemaResourcesDTO(schemaResourcesDTO)
192 .setGlobalProcessingExecutor(netconfTopologyDeviceSetup.getProcessingExecutor().getExecutor())
193 .setId(remoteDeviceId)
194 .setSalFacade(salFacade)
198 final Optional<NetconfSessionPreferences> userCapabilities = getUserCapabilities(node);
199 final int rpcMessageLimit =
200 node.getConcurrentRpcLimit() == null
201 ? NetconfTopologyUtils.DEFAULT_CONCURRENT_RPC_LIMIT : node.getConcurrentRpcLimit();
203 if (rpcMessageLimit < 1) {
204 LOG.info("{}: Concurrent rpc limit is smaller than 1, no limit will be enforced.", remoteDeviceId);
207 return new NetconfConnectorDTO(
208 userCapabilities.isPresent()
209 ? new NetconfDeviceCommunicator(
210 remoteDeviceId, device, new UserPreferences(userCapabilities.get(),
211 node.getYangModuleCapabilities().isOverride(), node.getNonModuleCapabilities().isOverride()),
213 : new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit), salFacade);
216 private Optional<NetconfSessionPreferences> getUserCapabilities(final NetconfNode node) {
217 if (node.getYangModuleCapabilities() == null && node.getNonModuleCapabilities() == null) {
218 return Optional.empty();
220 final List<String> capabilities = new ArrayList<>();
222 if (node.getYangModuleCapabilities() != null) {
223 capabilities.addAll(node.getYangModuleCapabilities().getCapability());
226 //non-module capabilities should not exist in yang module capabilities
227 final NetconfSessionPreferences netconfSessionPreferences = NetconfSessionPreferences.fromStrings(capabilities);
228 Preconditions.checkState(netconfSessionPreferences.getNonModuleCaps().isEmpty(), "List yang-module-capabilities/capability " +
229 "should contain only module based capabilities. Non-module capabilities used: " +
230 netconfSessionPreferences.getNonModuleCaps());
232 if (node.getNonModuleCapabilities() != null) {
233 capabilities.addAll(node.getNonModuleCapabilities().getCapability());
236 return Optional.of(NetconfSessionPreferences.fromStrings(capabilities, CapabilityOrigin.UserDefined));
239 //TODO: duplicate code
240 private InetSocketAddress getSocketAddress(final Host host, final int port) {
241 if (host.getDomainName() != null) {
242 return new InetSocketAddress(host.getDomainName().getValue(), port);
244 final IpAddress ipAddress = host.getIpAddress();
245 final String ip = ipAddress.getIpv4Address() != null ? ipAddress.getIpv4Address().getValue() :
246 ipAddress.getIpv6Address().getValue();
247 return new InetSocketAddress(ip, port);
252 NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
253 final NetconfNode node) {
255 //setup default values since default value is not supported in mdsal
256 final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null
257 ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis();
258 final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null
259 ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts();
260 final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null
261 ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS : node.getBetweenAttemptsTimeoutMillis();
262 final BigDecimal sleepFactor = node.getSleepFactor() == null
263 ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
265 final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue());
267 final ReconnectStrategyFactory sf =
268 new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
269 betweenAttemptsTimeoutMillis, sleepFactor);
270 final ReconnectStrategy strategy = sf.createReconnectStrategy();
272 final AuthenticationHandler authHandler;
273 final Credentials credentials = node.getCredentials();
274 if (credentials instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) {
275 authHandler = new LoginPassword(
276 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getUsername(),
277 ((org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPassword) credentials).getPassword());
279 throw new IllegalStateException(remoteDeviceId + ": Only login/password authentication is supported");
282 return NetconfReconnectingClientConfigurationBuilder.create()
283 .withAddress(socketAddress)
284 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
285 .withReconnectStrategy(strategy)
286 .withAuthHandler(authHandler)
287 .withProtocol(node.isTcpOnly()
288 ? NetconfClientConfiguration.NetconfClientProtocol.TCP
289 : NetconfClientConfiguration.NetconfClientProtocol.SSH)
290 .withConnectStrategyFactory(sf)
291 .withSessionListener(listener)
295 private static final class TimedReconnectStrategyFactory implements ReconnectStrategyFactory {
296 private final Long connectionAttempts;
297 private final EventExecutor executor;
298 private final double sleepFactor;
299 private final int minSleep;
301 TimedReconnectStrategyFactory(final EventExecutor executor, final Long maxConnectionAttempts,
302 final int minSleep, final BigDecimal sleepFactor) {
303 if (maxConnectionAttempts != null && maxConnectionAttempts > 0) {
304 connectionAttempts = maxConnectionAttempts;
306 connectionAttempts = null;
309 this.sleepFactor = sleepFactor.doubleValue();
310 this.executor = executor;
311 this.minSleep = minSleep;
315 public ReconnectStrategy createReconnectStrategy() {
316 final Long maxSleep = null;
317 final Long deadline = null;
319 return new TimedReconnectStrategy(executor, minSleep,
320 minSleep, sleepFactor, maxSleep, connectionAttempts, deadline);