BUG 2799: SPI for EventSources
[controller.git] / opendaylight / netconf / netconf-cli / src / main / java / org / opendaylight / controller / netconf / cli / NetconfDeviceConnectionHandler.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.Optional;
11 import jline.console.completer.Completer;
12 import jline.console.completer.NullCompleter;
13 import org.opendaylight.controller.netconf.cli.commands.CommandDispatcher;
14 import org.opendaylight.controller.netconf.cli.io.ConsoleContext;
15 import org.opendaylight.controller.netconf.cli.io.ConsoleIO;
16 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
17 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionPreferences;
18 import org.opendaylight.controller.sal.core.api.RpcImplementation;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 /**
23  * Implementation of RemoteDeviceHandler. Integrates cli with
24  * sal-netconf-connector.
25  */
26 public class NetconfDeviceConnectionHandler implements RemoteDeviceHandler<NetconfSessionPreferences> {
27
28     private final CommandDispatcher commandDispatcher;
29     private final SchemaContextRegistry schemaContextRegistry;
30     private final ConsoleIO console;
31     private final String deviceId;
32
33     private boolean up = false;
34
35     public NetconfDeviceConnectionHandler(final CommandDispatcher commandDispatcher,
36             final SchemaContextRegistry schemaContextRegistry, final ConsoleIO console, final String deviceId) {
37         this.commandDispatcher = commandDispatcher;
38         this.schemaContextRegistry = schemaContextRegistry;
39         this.console = console;
40         this.deviceId = deviceId;
41     }
42
43     @Override
44     public synchronized void onDeviceConnected(final SchemaContext context,
45             final NetconfSessionPreferences preferences, final RpcImplementation rpcImplementation) {
46         console.enterRootContext(new ConsoleContext() {
47
48             @Override
49             public Optional<String> getPrompt() {
50                 return Optional.of(deviceId);
51             }
52
53             @Override
54             public Completer getCompleter() {
55                 return new NullCompleter();
56             }
57         });
58
59         // TODO Load schemas for base netconf + inet types from remote device if
60         // possible
61         // TODO detect netconf base version
62         // TODO detect inet types version
63         commandDispatcher.addRemoteCommands(rpcImplementation, context);
64         schemaContextRegistry.setRemoteSchemaContext(context);
65         up = true;
66         this.notify();
67     }
68
69     /**
70      * @return true if connection was fully established
71      */
72     public synchronized boolean isUp() {
73         return up;
74     }
75
76     @Override
77     public synchronized void onDeviceDisconnected() {
78         console.leaveRootContext();
79         commandDispatcher.removeRemoteCommands();
80         schemaContextRegistry.setRemoteSchemaContext(null);
81         up = false;
82     }
83
84     @Override
85     public void onDeviceFailed(Throwable throwable) {
86         // FIXME
87     }
88
89     @Override
90     public void onNotification(final CompositeNode compositeNode) {
91         // FIXME
92     }
93
94     @Override
95     public void close() {
96         // FIXME
97     }
98 }