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