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