HONEYCOMB-414: use NetconfNorthboundSshServer for NETCONF initialization
[honeycomb.git] / infra / northbound / netconf / src / main / java / io / fd / honeycomb / northbound / netconf / NetconfModule.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.northbound.netconf;
18
19 import com.google.inject.Singleton;
20 import com.google.inject.binder.AnnotatedElementBuilder;
21 import com.google.inject.name.Names;
22 import io.fd.honeycomb.infra.distro.data.BindingDataBrokerProvider;
23 import io.fd.honeycomb.infra.distro.data.DataStoreProvider;
24 import io.fd.honeycomb.infra.distro.data.HoneycombNotificationManagerProvider;
25 import io.fd.honeycomb.infra.distro.data.InmemoryDOMDataBrokerProvider;
26 import io.fd.honeycomb.northbound.NetconfConfiguration;
27 import io.fd.honeycomb.northbound.NorthboundPrivateModule;
28 import io.fd.honeycomb.notification.NotificationCollector;
29 import io.netty.channel.nio.NioEventLoopGroup;
30 import io.netty.util.HashedWheelTimer;
31 import io.netty.util.Timer;
32 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
35 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
36 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
37 import org.opendaylight.netconf.api.NetconfServerDispatcher;
38 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
39 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
40 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
41 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
42 import org.opendaylight.netconf.notifications.NetconfNotificationCollector;
43 import org.opendaylight.netconf.notifications.NetconfNotificationListener;
44 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry;
45 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager;
46 import org.opendaylight.netconf.ssh.NetconfNorthboundSshServer;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class NetconfModule extends NorthboundPrivateModule<NetconfConfiguration> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(NetconfModule.class);
53
54     public static final String HONEYCOMB_NETCONF = "honeycomb-netconf";
55     public static final String HONEYCOMB_NETCONF_MAPPER_AGGR = "netconf-mapper-aggregator";
56     public static final String HONEYCOMB_NETCONF_MAPPER_NOTIF = "netconf-mapper-notification";
57     public static final String HONEYCOMB_NETCONF_MAPPER_CORE = "netconf-mapper-honeycomb";
58     public static final String HONEYCOMB_NETCONF_MAPPER_OPER = "netconf-mapper-monitoring";
59
60     public NetconfModule() {
61         super(new NetconfConfigurationModule(), NetconfConfiguration.class);
62     }
63
64     @Override
65     protected void configure() {
66         if (!getConfiguration().isNetconfEnabled()) {
67             LOG.debug("Netconf disabled, skipping initialization");
68             return;
69         }
70         install(getConfigurationModule());
71         LOG.info("Starting NETCONF Northbound");
72         // Create inmemory data store for HONEYCOMB_NETCONF config metadata
73         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(InmemoryDOMDataBrokerProvider.CONFIG))
74                 .toProvider(
75                         new DataStoreProvider(InmemoryDOMDataBrokerProvider.CONFIG, LogicalDatastoreType.CONFIGURATION))
76                 .in(Singleton.class);
77
78         // Create inmemory data store for HONEYCOMB_NETCONF operational metadata
79         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(InmemoryDOMDataBrokerProvider.OPERATIONAL))
80                 .toProvider(new DataStoreProvider(InmemoryDOMDataBrokerProvider.OPERATIONAL,
81                         LogicalDatastoreType.OPERATIONAL))
82                 .in(Singleton.class);
83         // Wrap datastores as DOMDataBroker
84         bind(DOMDataBroker.class).toProvider(InmemoryDOMDataBrokerProvider.class).in(Singleton.class);
85
86         // Wrap DOMDataBroker as BA data broker
87         bind(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF)).toProvider(BindingDataBrokerProvider.class)
88                 .in(Singleton.class);
89         expose(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF));
90
91         // Wrap BA data broker as BindingAwareBroker (requied by HONEYCOMB_NETCONF)
92         bind(BindingAwareBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF))
93                 .toProvider(NetconfBindingBrokerProvider.class).in(Singleton.class);
94
95         // Create netconf operation service factory aggregator to aggregate different services
96         AggregatedNetconfOperationServiceFactory factory = new AggregatedNetconfOperationServiceFactory();
97         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_AGGR))
98                 .toInstance(factory);
99         bind(NetconfOperationServiceFactoryListener.class).toInstance(factory);
100
101         // Create netconf notification manager
102         NetconfNotificationManager manager = new NetconfNotificationManager();
103         bind(NetconfNotificationCollector.class).toInstance(manager);
104         bind(NetconfNotificationRegistry.class).toInstance(manager);
105         bind(NetconfNotificationListener.class).toInstance(manager);
106
107         // Netconf notification service factory
108         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF))
109                 .toProvider(NetconfNotificationMapperProvider.class).asEagerSingleton();
110         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF));
111
112         // Netconf core part - mapping between Honeycomb and Netconf
113         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE))
114                 .toProvider(NetconfMdsalMapperProvider.class).asEagerSingleton();
115         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE));
116
117         // Netconf monitoring service factory
118         bind(NetconfMonitoringService.class).toProvider(NetconfMonitoringServiceProvider.class).in(Singleton.class);
119         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER))
120                 .toProvider(NetconfMonitoringMapperProvider.class).asEagerSingleton();
121         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER));
122
123         // Create HC notification manager + HC2Netconf translator
124         bind(NotificationCollector.class).toProvider(HoneycombNotificationManagerProvider.class).in(Singleton.class);
125         bind(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf.class)
126                 .toProvider(HoneycombNotification2NetconfProvider.class).asEagerSingleton();
127         expose(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf.class);
128
129         configureServer();
130     }
131
132     /**
133      * Provide HONEYCOMB_NETCONF TCP and SSH servers.
134      */
135     private AnnotatedElementBuilder configureServer() {
136         bind(NioEventLoopGroup.class).toProvider(NettyThreadGroupProvider.class).in(Singleton.class);
137         bind(Timer.class).toInstance(new HashedWheelTimer());
138         bind(NetconfServerDispatcher.class).toProvider(NetconfServerDispatcherProvider.class).in(Singleton.class);
139         bind(NetconfTcpServerProvider.NetconfTcpServer.class).toProvider(NetconfTcpServerProvider.class)
140                 .asEagerSingleton();
141         expose(NetconfTcpServerProvider.NetconfTcpServer.class);
142         bind(NetconfNorthboundSshServer.class).toProvider(NetconfSshServerProvider.class).asEagerSingleton();
143         return expose(NetconfNorthboundSshServer.class);
144     }
145 }