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