BUG-1422 Introduce Call-Home functionality for the NETCONF Topology.
[netconf.git] / netconf / 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
9 package org.opendaylight.netconf.callhome.mount;
10
11 import java.io.IOException;
12 import java.io.File;
13 import java.net.InetSocketAddress;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.netconf.callhome.protocol.CallHomeAuthorizationProvider;
16 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServer;
17 import org.opendaylight.netconf.callhome.protocol.NetconfCallHomeServerBuilder;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class IetfZeroTouchCallHomeServerProvider implements AutoCloseable {
22     private static final Logger LOG = LoggerFactory.getLogger(IetfZeroTouchCallHomeServerProvider.class);
23     private static final String appName = "CallHomeServer";
24
25     private final DataBroker dataBroker;
26     private final CallHomeMountDispatcher mountDispacher;
27
28     protected NetconfCallHomeServer server;
29
30
31     private static final String CALL_HOME_PORT_KEY = "DefaultCallHomePort";
32
33     private final CallHomeAuthProviderImpl authProvider;
34
35
36     static String configurationPath = "etc"+File.pathSeparator+"ztp-callhome-config.cfg";
37
38     private int port = 0; // 0 = use default in NetconfCallHomeBuilder
39
40     public IetfZeroTouchCallHomeServerProvider(DataBroker dataBroker, CallHomeMountDispatcher mountDispacher) {
41         this.dataBroker = dataBroker;
42         this.mountDispacher = mountDispacher;
43         this.authProvider = new CallHomeAuthProviderImpl(dataBroker);
44     }
45
46     public void init() {
47         // Register itself as a listener to changes in Devices subtree
48         try {
49             LOG.info("Initializing provider for {}", appName);
50             loadConfigurableValues(configurationPath);
51             initializeServer();
52             LOG.info("Initialization complete for {}", appName);
53         } catch (IndexOutOfBoundsException | Configuration.ConfigurationException e) {
54             LOG.error("Unable to successfully initialize", e);
55         }
56     }
57
58     void loadConfigurableValues(String configurationPath) throws Configuration.ConfigurationException {
59         try {
60             Configuration configuration = new Configuration(configurationPath);
61             port = configuration.getAsPort(CALL_HOME_PORT_KEY);
62         } catch (Exception e) {
63             LOG.error("Problem trying to load configuration values from {}", configurationPath, e);
64         }
65     }
66
67     private CallHomeAuthorizationProvider getCallHomeAuthorization() {
68         return authProvider;
69     }
70
71     private void initializeServer() {
72         LOG.info("Initializing Call Home server instance");
73         CallHomeAuthorizationProvider auth =  getCallHomeAuthorization();
74         NetconfCallHomeServerBuilder builder = new NetconfCallHomeServerBuilder(auth, mountDispacher);
75
76         if (port > 0)
77             builder.setBindAddress(new InetSocketAddress(port));
78         server = builder.build();
79         try {
80             server.bind();
81             mountDispacher.createTopology();
82             LOG.info("Initialization complete for Call Home server instance");
83         } catch (IOException e) {
84             throw new IllegalStateException("Unable to create Call Home Server.",e);
85         }
86     }
87
88     @Override
89     public void close() throws Exception {
90         authProvider.close();
91         if (server != null) {
92             server.close();
93         }
94
95         LOG.info("Successfully closed provider for {}", appName);
96     }
97
98 }