Remove unused code in NetconfDeviceConnectionManager
[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.net.InetSocketAddress;
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.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.util.RemoteDeviceId;
29 import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
30 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.model.repo.util.FilesystemSchemaSourceCache;
33 import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
34 import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
35
36 /**
37  * Manages connect/disconnect to 1 remote device
38  */
39 public class NetconfDeviceConnectionManager implements Closeable {
40
41     private final CommandDispatcher commandDispatcher;
42     private final SchemaContextRegistry schemaContextRegistry;
43     private final ConsoleIO console;
44
45     private final ExecutorService executor;
46     private final NioEventLoopGroup nettyThreadGroup;
47     private final NetconfClientDispatcherImpl netconfClientDispatcher;
48
49     private static final String CACHE = "cache/schema";
50
51     // Connection
52     private NetconfDeviceConnectionHandler handler;
53     private NetconfDevice device;
54     private NetconfDeviceCommunicator listener;
55
56     public NetconfDeviceConnectionManager(final CommandDispatcher commandDispatcher,
57             final CommandArgHandlerRegistry argumentHandlerRegistry, final SchemaContextRegistry schemaContextRegistry,
58             final ConsoleIO consoleIO) {
59         this.commandDispatcher = commandDispatcher;
60         this.schemaContextRegistry = schemaContextRegistry;
61         this.console = consoleIO;
62
63         executor = Executors.newSingleThreadExecutor();
64         nettyThreadGroup = new NioEventLoopGroup();
65         netconfClientDispatcher = new NetconfClientDispatcherImpl(nettyThreadGroup, nettyThreadGroup,
66                 new HashedWheelTimer());
67     }
68
69     // TODO we receive configBuilder in order to add SessionListener, Session
70     // Listener should not be part of config
71     public synchronized void connect(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
72         // TODO change IllegalState exceptions to custom ConnectionException
73         Preconditions.checkState(listener == null, "Already connected");
74
75         final RemoteDeviceId deviceId = new RemoteDeviceId(name, address);
76
77         handler = new NetconfDeviceConnectionHandler(commandDispatcher, schemaContextRegistry,
78                 console, name);
79
80         final SharedSchemaRepository repository = new SharedSchemaRepository("repo");
81         final SchemaContextFactory schemaContextFactory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
82         final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(repository, YangTextSchemaSource.class, new File(CACHE));
83         repository.registerSchemaSourceListener(cache);
84         repository.registerSchemaSourceListener(TextToASTTransformer.create(repository, repository));
85
86         device = new NetconfDevice(new SchemaResourcesDTO(repository, schemaContextFactory, new NetconfStateSchemasResolverImpl()),
87                 deviceId, handler, executor, true);
88         listener = new NetconfDeviceCommunicator(deviceId, device);
89         configBuilder.withSessionListener(listener);
90         listener.initializeRemoteConnection(netconfClientDispatcher, configBuilder.build());
91     }
92
93     /**
94      * Blocks thread until connection is fully established
95      */
96     public synchronized Set<String> connectBlocking(final String name, final InetSocketAddress address, final NetconfClientConfigurationBuilder configBuilder) {
97         this.connect(name, address, configBuilder);
98         synchronized (handler) {
99             while (handler.isUp() == false) {
100                 try {
101                     // TODO implement Timeout for unsuccessful connection
102                     handler.wait();
103                 } catch (final InterruptedException e) {
104                     Thread.currentThread().interrupt();
105                     throw new IllegalArgumentException(e);
106                 }
107             }
108         }
109
110         return commandDispatcher.getRemoteCommandIds();
111     }
112
113     public synchronized void disconnect() {
114         Preconditions.checkState(listener != null, "Not connected yet");
115         Preconditions.checkState(handler.isUp(), "Not connected yet");
116         listener.close();
117         listener = null;
118         device = null;
119         handler.close();
120         handler = null;
121     }
122
123     @Override
124     public void close() throws IOException {
125         executor.shutdownNow();
126         nettyThreadGroup.shutdownGracefully();
127     }
128 }