Merge "Bug 1007 - Statistics manager loses threads to uncaught exceptions"
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / NetconfDeviceConnectionManager.java
1 /*
2  * Copyright (c) 2014 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.controller.netconf.cli;
9
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;
14 import java.io.File;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Set;
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.listener.NetconfDeviceCommunicator;
26 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.yang.model.util.repo.AbstractCachingSchemaSourceProvider;
28 import org.opendaylight.yangtools.yang.model.util.repo.FilesystemSchemaCachingProvider;
29 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
30 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProviders;
31
32 /**
33  * Manages connect/disconnect to 1 remote device
34  */
35 public class NetconfDeviceConnectionManager implements Closeable {
36
37     private final CommandDispatcher commandDispatcher;
38     private final SchemaContextRegistry schemaContextRegistry;
39     private final ConsoleIO console;
40
41     private final ExecutorService executor;
42     private final NioEventLoopGroup nettyThreadGroup;
43     private final NetconfClientDispatcherImpl netconfClientDispatcher;
44
45     // Connection
46     private NetconfDeviceConnectionHandler handler;
47     private NetconfDevice device;
48     private NetconfDeviceCommunicator listener;
49
50     public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
51             final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
52             final ConsoleIO consoleIO) {
53         this.commandDispatcher = commandDispatcher;
54         this.schemaContextRegistry = schemaContextRegistry;
55         this.console = consoleIO;
56
57         executor = Executors.newSingleThreadExecutor();
58         nettyThreadGroup = new NioEventLoopGroup();
59         netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
60                 new HashedWheelTimer());
61     }
62
63     // TODO we receive configBuilder in order to add SessionListener, Session
64     // Listener should not be part of config
65     public synchronized void connect(final String name, final NetconfClientConfigurationBuilder configBuilder) {
66         // TODO change IllegalState exceptions to custom ConnectionException
67         Preconditions.checkState(listener == null, "Already connected");
68
69         final RemoteDeviceId deviceId = new RemoteDeviceId(name);
70
71         handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
72                 console, name);
73         device = NetconfDevice.createNetconfDevice(deviceId, getGlobalNetconfSchemaProvider(), executor, handler);
74         listener = new NetconfDeviceCommunicator(deviceId, device);
75         configBuilder.withSessionListener(listener);
76         listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
77     }
78
79     /**
80      * Blocks thread until connection is fully established
81      */
82     public synchronized Set<String> connectBlocking(final String name, final NetconfClientConfigurationBuilder configBuilder) {
83         this.connect(name, configBuilder);
84         synchronized (handler) {
85             while (handler.isUp() == false) {
86                 try {
87                     // TODO implement Timeout for unsuccessful connection
88                     handler.wait();
89                 } catch (final InterruptedException e) {
90                     Thread.currentThread().interrupt();
91                     throw new IllegalArgumentException(e);
92                 }
93             }
94         }
95
96         return commandDispatcher.getRemoteCommandIds();
97     }
98
99     public synchronized void disconnect() {
100         Preconditions.checkState(listener != null, "Not connected yet");
101         Preconditions.checkState(handler.isUp(), "Not connected yet");
102         listener.close();
103         listener = null;
104         device = null;
105         handler.close();
106         handler = null;
107     }
108
109     private static AbstractCachingSchemaSourceProvider<String, InputStream> getGlobalNetconfSchemaProvider() {
110         // FIXME move to args
111         final String storageFile = "cache/schema";
112         final File directory = new File(storageFile);
113         final SchemaSourceProvider<String> defaultProvider = SchemaSourceProviders.noopProvider();
114         return FilesystemSchemaCachingProvider.createFromStringSourceProvider(defaultProvider, directory);
115     }
116
117     @Override
118     public void close() throws IOException {
119         executor.shutdownNow();
120         nettyThreadGroup.shutdownGracefully();
121     }
122 }