2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.netconf.cli;
10 import com.google.common.base.Preconditions;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.util.HashedWheelTimer;
13 import java.io.Closeable;
15 import java.io.IOException;
16 import java.io.InputStream;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
21 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
22 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
23 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
24 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
25 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice.SchemaResourcesDTO;
26 import org.opendaylight.controller.sal.connect.netconf.NetconfStateSchemas.NetconfStateSchemasResolverImpl;
27 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfDeviceCommunicator;
28 import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
29 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
34 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
35 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
36 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
37 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
38 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
39 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
42 * Manages connect/disconnect to 1 remote device
44 public class NetconfDeviceConnectionManager implements Closeable {
46 private final CommandDispatcher commandDispatcher;
47 private final SchemaContextRegistry schemaContextRegistry;
48 private final ConsoleIO console;
50 private final ExecutorService executor;
51 private final NioEventLoopGroup nettyThreadGroup;
52 private final NetconfClientDispatcherImpl netconfClientDispatcher;
54 private static final String CACHE = "cache/schema";
57 private NetconfDeviceConnectionHandler handler;
58 private NetconfDevice device;
59 private NetconfDeviceCommunicator listener;
61 public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
62 final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
63 final ConsoleIO consoleIO) {
64 this.commandDispatcher = commandDispatcher;
65 this.schemaContextRegistry = schemaContextRegistry;
66 this.console = consoleIO;
68 executor = Executors.newSingleThreadExecutor();
69 nettyThreadGroup = new NioEventLoopGroup();
70 netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
71 new HashedWheelTimer());
74 // TODO we receive configBuilder in order to add SessionListener, Session
75 // Listener should not be part of config
76 public synchronized void connect(final String name, final NetconfClientConfigurationBuilder configBuilder) {
77 // TODO change IllegalState exceptions to custom ConnectionException
78 Preconditions.checkState(listener == null, "Already connected");
80 final RemoteDeviceId deviceId = new RemoteDeviceId(name);
82 handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
85 final SharedSchemaRepository repository = new SharedSchemaRepository("repo");
86 final SchemaContextFactory schemaContextFactory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
87 final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE));
88 repository.registerSchemaSourceListener(cache);
89 repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
91 device = new NetconfDevice(new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()),
92 deviceId, handler, executor, new NetconfMessageTransformer());
93 listener = new NetconfDeviceCommunicator(deviceId, device);
94 configBuilder.withSessionListener(listener);
95 listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
99 * Blocks thread until connection is fully established
101 public synchronized Set<String> connectBlocking(final String name, final NetconfClientConfigurationBuilder configBuilder) {
102 this.connect(name, configBuilder);
103 synchronized (handler) {
104 while (handler.isUp() == false) {
106 // TODO implement Timeout for unsuccessful connection
108 } catch (final InterruptedException e) {
109 Thread.currentThread().interrupt();
110 throw new IllegalArgumentException(e);
115 return commandDispatcher.getRemoteCommandIds();
118 public synchronized void disconnect() {
119 Preconditions.checkState(listener != null, "Not connected yet");
120 Preconditions.checkState(handler.isUp(), "Not connected yet");
128 private static AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider() {
129 // FIXME move to args
130 final String storageFile = "cache/schema";
131 final File directory = new File(storageFile);
132 final SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
133 return FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
137 public void close() throws IOException {
138 executor.shutdownNow();
139 nettyThreadGroup.shutdownGracefully();