Move YangLibrarySchemaYangSourceProvider
[netconf.git] / apps / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / RemoteDeviceConnectorImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.topology.singleton.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import java.math.BigDecimal;
18 import java.net.InetSocketAddress;
19 import java.net.URL;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
24 import org.opendaylight.netconf.client.NetconfClientSessionListener;
25 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
26 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
27 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder;
28 import org.opendaylight.netconf.client.mdsal.DatastoreBackedPublicKeyAuth;
29 import org.opendaylight.netconf.client.mdsal.LibraryModulesSchemas;
30 import org.opendaylight.netconf.client.mdsal.LibrarySchemaSourceProvider;
31 import org.opendaylight.netconf.client.mdsal.NetconfDevice;
32 import org.opendaylight.netconf.client.mdsal.NetconfDeviceBuilder;
33 import org.opendaylight.netconf.client.mdsal.SchemalessNetconfDevice;
34 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
35 import org.opendaylight.netconf.client.mdsal.api.RemoteDevice;
36 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
37 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
38 import org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory;
39 import org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory;
40 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
41 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
42 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
43 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
44 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfKeystoreAdapter;
45 import org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl;
46 import org.opendaylight.netconf.topology.singleton.api.RemoteDeviceConnector;
47 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
48 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
49 import org.opendaylight.netconf.topology.spi.NetconfConnectorDTO;
50 import org.opendaylight.netconf.topology.spi.NetconfNodeUtils;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.connection.parameters.OdlHelloMessageCapabilities;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.connection.parameters.Protocol.Name;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.credentials.Credentials;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.credentials.credentials.KeyAuth;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.credentials.credentials.LoginPw;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430.credentials.credentials.LoginPwUnencrypted;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
60 import org.opendaylight.yangtools.yang.common.Decimal64;
61 import org.opendaylight.yangtools.yang.common.Empty;
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;
68
69 public class RemoteDeviceConnectorImpl implements RemoteDeviceConnector {
70     private static final Logger LOG = LoggerFactory.getLogger(RemoteDeviceConnectorImpl.class);
71
72     // Initializes default constant instances for the case when the default schema repository
73     // directory cache/schema is used.
74
75     private final NetconfTopologySetup netconfTopologyDeviceSetup;
76     private final RemoteDeviceId remoteDeviceId;
77     private final AAAEncryptionService encryptionService;
78     private final NetconfKeystoreAdapter keystoreAdapter;
79     private final DeviceActionFactory deviceActionFactory;
80
81     // FIXME: this seems to be a builder-like transition between {start,stop}RemoteDeviceConnection. More documentation
82     //        is needed, as to what the lifecycle is here.
83     private NetconfConnectorDTO deviceCommunicatorDTO;
84
85     public RemoteDeviceConnectorImpl(final NetconfTopologySetup netconfTopologyDeviceSetup,
86             final RemoteDeviceId remoteDeviceId, final DeviceActionFactory deviceActionFactory) {
87         this.netconfTopologyDeviceSetup = requireNonNull(netconfTopologyDeviceSetup);
88         this.remoteDeviceId = remoteDeviceId;
89         this.deviceActionFactory = requireNonNull(deviceActionFactory);
90         encryptionService = netconfTopologyDeviceSetup.getEncryptionService();
91         keystoreAdapter = new NetconfKeystoreAdapter(netconfTopologyDeviceSetup.getDataBroker());
92     }
93
94     @Override
95     public void startRemoteDeviceConnection(final RemoteDeviceHandler deviceHandler) {
96
97         final NetconfNode netconfNode = netconfTopologyDeviceSetup.getNode().augmentation(NetconfNode.class);
98         final NodeId nodeId = netconfTopologyDeviceSetup.getNode().getNodeId();
99         requireNonNull(netconfNode.getHost());
100         requireNonNull(netconfNode.getPort());
101
102         deviceCommunicatorDTO = createDeviceCommunicator(nodeId, netconfNode, deviceHandler);
103         final NetconfDeviceCommunicator deviceCommunicator = deviceCommunicatorDTO.getCommunicator();
104         final NetconfClientSessionListener netconfClientSessionListener = deviceCommunicatorDTO.getSessionListener();
105         final NetconfReconnectingClientConfiguration clientConfig =
106                 getClientConfig(netconfClientSessionListener, netconfNode);
107         final ListenableFuture<Empty> future = deviceCommunicator
108                 .initializeRemoteConnection(netconfTopologyDeviceSetup.getNetconfClientDispatcher(), clientConfig);
109
110         Futures.addCallback(future, new FutureCallback<>() {
111             @Override
112             public void onSuccess(final Empty result) {
113                 LOG.debug("{}: Connector started successfully", remoteDeviceId);
114             }
115
116             @Override
117             public void onFailure(final Throwable throwable) {
118                 LOG.error("{}: Connector failed", remoteDeviceId, throwable);
119             }
120         }, MoreExecutors.directExecutor());
121     }
122
123     @SuppressWarnings("checkstyle:IllegalCatch")
124     @Override
125     public void stopRemoteDeviceConnection() {
126         if (deviceCommunicatorDTO != null) {
127             try {
128                 deviceCommunicatorDTO.close();
129             } catch (final Exception e) {
130                 LOG.error("{}: Error at closing device communicator.", remoteDeviceId, e);
131             }
132         }
133     }
134
135     @VisibleForTesting
136     NetconfConnectorDTO createDeviceCommunicator(final NodeId nodeId, final NetconfNode node,
137             final RemoteDeviceHandler deviceHandler) {
138         //setup default values since default value is not supported in mdsal
139         final long defaultRequestTimeoutMillis = node.getDefaultRequestTimeoutMillis() == null
140                 ? NetconfTopologyUtils.DEFAULT_REQUEST_TIMEOUT_MILLIS : node.getDefaultRequestTimeoutMillis().toJava();
141         final long keepaliveDelay = node.getKeepaliveDelay() == null
142                 ? NetconfTopologyUtils.DEFAULT_KEEPALIVE_DELAY : node.getKeepaliveDelay().toJava();
143         final boolean reconnectOnChangedSchema = node.getReconnectOnChangedSchema() == null
144                 ? NetconfTopologyUtils.DEFAULT_RECONNECT_ON_CHANGED_SCHEMA : node.getReconnectOnChangedSchema();
145
146         RemoteDeviceHandler salFacade = requireNonNull(deviceHandler);
147         if (keepaliveDelay > 0) {
148             LOG.info("{}: Adding keepalive facade.", remoteDeviceId);
149             salFacade = new KeepaliveSalFacade(remoteDeviceId, salFacade,
150                     netconfTopologyDeviceSetup.getKeepaliveExecutor(), keepaliveDelay,
151                     defaultRequestTimeoutMillis);
152         }
153
154         final NetconfDevice.SchemaResourcesDTO schemaResourcesDTO = netconfTopologyDeviceSetup.getSchemaResourcesDTO();
155
156         // pre register yang library sources as fallback schemas to schema registry
157         final List<SchemaSourceRegistration<?>> registeredYangLibSources = new ArrayList<>();
158         if (node.getYangLibrary() != null) {
159             final String yangLibURL = node.getYangLibrary().getYangLibraryUrl().getValue();
160             final String yangLibUsername = node.getYangLibrary().getUsername();
161             final String yangLigPassword = node.getYangLibrary().getPassword();
162
163             final LibraryModulesSchemas libraryModulesSchemas;
164             if (yangLibURL != null) {
165                 if (yangLibUsername != null && yangLigPassword != null) {
166                     libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL, yangLibUsername, yangLigPassword);
167                 } else {
168                     libraryModulesSchemas = LibraryModulesSchemas.create(yangLibURL);
169                 }
170
171                 for (final Map.Entry<SourceIdentifier, URL> sourceIdentifierURLEntry :
172                         libraryModulesSchemas.getAvailableModels().entrySet()) {
173                     registeredYangLibSources
174                             .add(schemaResourcesDTO.getSchemaRegistry().registerSchemaSource(
175                                     new LibrarySchemaSourceProvider(remoteDeviceId,
176                                             libraryModulesSchemas.getAvailableModels()),
177                                     PotentialSchemaSource
178                                             .create(sourceIdentifierURLEntry.getKey(), YangTextSchemaSource.class,
179                                                     PotentialSchemaSource.Costs.REMOTE_IO.getValue())));
180                 }
181             }
182         }
183
184         final RemoteDevice<NetconfDeviceCommunicator> device;
185         if (node.getSchemaless()) {
186             device = new SchemalessNetconfDevice(netconfTopologyDeviceSetup.getBaseSchemas(), remoteDeviceId,
187                 salFacade);
188         } else {
189             device = new NetconfDeviceBuilder()
190                     .setReconnectOnSchemasChange(reconnectOnChangedSchema)
191                     .setSchemaResourcesDTO(schemaResourcesDTO)
192                     .setGlobalProcessingExecutor(netconfTopologyDeviceSetup.getProcessingExecutor())
193                     .setBaseSchemas(netconfTopologyDeviceSetup.getBaseSchemas())
194                     .setId(remoteDeviceId)
195                     .setDeviceActionFactory(deviceActionFactory)
196                     .setSalFacade(salFacade)
197                     .build();
198         }
199
200         final int rpcMessageLimit = node.getConcurrentRpcLimit() == null
201             ? NetconfTopologyUtils.DEFAULT_CONCURRENT_RPC_LIMIT : node.getConcurrentRpcLimit().toJava();
202
203         if (rpcMessageLimit < 1) {
204             LOG.info("{}: Concurrent rpc limit is smaller than 1, no limit will be enforced.", remoteDeviceId);
205         }
206
207         final var netconfDeviceCommunicator = new NetconfDeviceCommunicator(remoteDeviceId, device, rpcMessageLimit,
208             NetconfNodeUtils.extractUserCapabilities(node));
209
210         if (salFacade instanceof KeepaliveSalFacade) {
211             ((KeepaliveSalFacade)salFacade).setListener(netconfDeviceCommunicator);
212         }
213         return new NetconfConnectorDTO(netconfDeviceCommunicator, salFacade, registeredYangLibSources);
214     }
215
216     @VisibleForTesting
217     NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener,
218                                                            final NetconfNode node) {
219
220         //setup default values since default value is not supported in mdsal
221         final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null
222                 ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis().toJava();
223         final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null
224                 ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts().toJava();
225         final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null
226                 ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS
227                 : node.getBetweenAttemptsTimeoutMillis().toJava();
228         final boolean isTcpOnly = node.getTcpOnly() == null
229                 ? NetconfTopologyUtils.DEFAULT_IS_TCP_ONLY : node.getTcpOnly();
230         final Decimal64 sleepFactor = node.getSleepFactor() == null
231                 ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
232
233         final InetSocketAddress socketAddress = NetconfNodeUtils.toInetSocketAddress(node);
234
235         final ReconnectStrategyFactory sf =
236             new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts,
237                 betweenAttemptsTimeoutMillis, BigDecimal.valueOf(sleepFactor.unscaledValue(), sleepFactor.scale()));
238
239
240         final NetconfReconnectingClientConfigurationBuilder reconnectingClientConfigurationBuilder;
241         final var protocol = node.getProtocol();
242         if (isTcpOnly) {
243             reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create()
244                     .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP)
245                     .withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
246         } else if (protocol == null || protocol.getName() == Name.SSH) {
247             reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create()
248                     .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH)
249                     .withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
250         } else if (protocol.getName() == Name.TLS) {
251             reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create()
252                     .withSslHandlerFactory(new SslHandlerFactoryImpl(keystoreAdapter, protocol.getSpecification()))
253                     .withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS);
254         } else {
255             throw new IllegalStateException("Unsupported protocol type: " + protocol.getName());
256         }
257
258         final List<Uri> odlHelloCapabilities = getOdlHelloCapabilities(node);
259         if (odlHelloCapabilities != null) {
260             reconnectingClientConfigurationBuilder.withOdlHelloCapabilities(odlHelloCapabilities);
261         }
262
263         return reconnectingClientConfigurationBuilder
264                 .withAddress(socketAddress)
265                 .withConnectionTimeoutMillis(clientConnectionTimeoutMillis)
266                 .withReconnectStrategy(sf.createReconnectStrategy())
267                 .withConnectStrategyFactory(sf)
268                 .withSessionListener(listener)
269                 .build();
270     }
271
272     private static List<Uri> getOdlHelloCapabilities(final NetconfNode node) {
273         final OdlHelloMessageCapabilities helloCapabilities = node.getOdlHelloMessageCapabilities();
274         return helloCapabilities != null ? List.copyOf(helloCapabilities.getCapability()) : null;
275     }
276
277     private AuthenticationHandler getHandlerFromCredentials(final Credentials credentials) {
278         if (credentials
279                 instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev230430
280                     .credentials.credentials.LoginPassword loginPassword) {
281             return new LoginPasswordHandler(loginPassword.getUsername(), loginPassword.getPassword());
282         }
283         if (credentials instanceof LoginPwUnencrypted unencrypted) {
284             final var loginPassword = unencrypted.getLoginPasswordUnencrypted();
285             return new LoginPasswordHandler(loginPassword.getUsername(), loginPassword.getPassword());
286         }
287         if (credentials instanceof LoginPw loginPw) {
288             final var loginPassword = loginPw.getLoginPassword();
289             return new LoginPasswordHandler(loginPassword.getUsername(),
290                     encryptionService.decrypt(loginPassword.getPassword()));
291         }
292         if (credentials instanceof KeyAuth keyAuth) {
293             final var keyPair = keyAuth.getKeyBased();
294             return new DatastoreBackedPublicKeyAuth(keyPair.getUsername(), keyPair.getKeyId(),
295                     keystoreAdapter, encryptionService);
296         }
297         throw new IllegalStateException("Unsupported credential type: " + credentials.getClass());
298     }
299 }