89d05b64a4b8015f0265316f46f6b4993836d73a
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / IetfZeroTouchCallHomeServerProvider.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.callhome.mount;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.Optional;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.netconf.callhome.server.CallHomeStatusRecorder;
16 import org.opendaylight.netconf.callhome.server.ssh.CallHomeSshAuthProvider;
17 import org.opendaylight.netconf.callhome.server.ssh.CallHomeSshServer;
18 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
19 import org.opendaylight.netconf.common.NetconfTimer;
20 import org.osgi.service.component.annotations.Activate;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Deactivate;
23 import org.osgi.service.component.annotations.Reference;
24 import org.osgi.service.metatype.annotations.AttributeDefinition;
25 import org.osgi.service.metatype.annotations.Designate;
26 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Component(service = { }, configurationPid = "org.opendaylight.netconf.callhome.mount.ssh.server")
31 @Designate(ocd = IetfZeroTouchCallHomeServerProvider.Configuration.class)
32 @Singleton
33 public final class IetfZeroTouchCallHomeServerProvider implements AutoCloseable {
34
35     @ObjectClassDefinition
36     public @interface Configuration {
37         @AttributeDefinition
38         String host() default "0.0.0.0";
39
40         @AttributeDefinition(min = "1", max = "65535")
41         int port() default 4334;
42     }
43
44     private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
45     private final CallHomeSshServer server;
46
47     @Activate
48     @Inject
49     public IetfZeroTouchCallHomeServerProvider(
50             final @Reference NetconfTimer timer,
51             final @Reference CallHomeMountService mountService,
52             final @Reference CallHomeSshAuthProvider authProvider,
53             final @Reference CallHomeStatusRecorder statusRecorder,
54             final Configuration configuration) {
55
56         LOG.info("Starting Call-Home SSH server at {}:{}", configuration.host(), configuration.port());
57
58         try {
59             server = CallHomeSshServer.builder()
60                 .withAddress(InetAddress.getByName(configuration.host()))
61                 .withPort(configuration.port())
62                 .withAuthProvider(authProvider)
63                 .withStatusRecorder(statusRecorder)
64                 .withSessionContextManager(mountService.createSshSessionContextManager())
65                 .withNegotiationFactory(new NetconfClientSessionNegotiatorFactory(timer, Optional.empty(), 10000L,
66                     NetconfClientSessionNegotiatorFactory.DEFAULT_CLIENT_CAPABILITIES))
67                 .build();
68         } catch (UnknownHostException e) {
69             throw new IllegalArgumentException("Invalid address", e);
70         }
71
72         LOG.info("Call-Home SSH server started successfully");
73     }
74
75     @Deactivate
76     @Override
77     public void close() throws Exception {
78         if (server != null) {
79             server.close();
80         }
81         LOG.info("Call-Home SSH server stopped.");
82     }
83 }