fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 1.2.1 / 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.sal.binding.api.BindingAwareBroker;
33 import org.opendaylight.mdsal.binding.api.DataBroker;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
35 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
36 import org.opendaylight.netconf.api.NetconfServerDispatcher;
37 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
38 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
39 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
40 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener;
41 import org.opendaylight.netconf.mdsal.notification.impl.NetconfNotificationManager;
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.ssh.NetconfNorthboundSshServer;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class NetconfModule extends NorthboundPrivateModule<NetconfConfiguration> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(NetconfModule.class);
52
53     public static final String HONEYCOMB_NETCONF = "honeycomb-netconf";
54     public static final String HONEYCOMB_NETCONF_MAPPER_AGGR = "netconf-mapper-aggregator";
55     public static final String HONEYCOMB_NETCONF_MAPPER_NOTIF = "netconf-mapper-notification";
56     public static final String HONEYCOMB_NETCONF_MAPPER_CORE = "netconf-mapper-honeycomb";
57     public static final String HONEYCOMB_NETCONF_MAPPER_OPER = "netconf-mapper-monitoring";
58
59     public NetconfModule() {
60         super(new NetconfConfigurationModule(), NetconfConfiguration.class);
61     }
62
63     @Override
64     protected void configure() {
65         if (!getConfiguration().isNetconfEnabled()) {
66             LOG.debug("Netconf disabled, skipping initialization");
67             return;
68         }
69         install(getConfigurationModule());
70         LOG.info("Starting NETCONF Northbound");
71         // Create inmemory data store for HONEYCOMB_NETCONF config metadata
72         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(InmemoryDOMDataBrokerProvider.CONFIG))
73                 .toProvider(
74                         new DataStoreProvider(InmemoryDOMDataBrokerProvider.CONFIG, org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION))
75                 .in(Singleton.class);
76
77         // Create inmemory data store for HONEYCOMB_NETCONF operational metadata
78         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(InmemoryDOMDataBrokerProvider.OPERATIONAL))
79                 .toProvider(new DataStoreProvider(InmemoryDOMDataBrokerProvider.OPERATIONAL, org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL
80                 ))
81                 .in(Singleton.class);
82         // Wrap datastores as DOMDataBroker
83         bind(DOMDataBroker.class).toProvider(InmemoryDOMDataBrokerProvider.class).in(Singleton.class);
84
85         // Wrap DOMDataBroker as BA data broker
86         bind(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF)).toProvider(BindingDataBrokerProvider.class)
87                 .in(Singleton.class);
88         expose(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF));
89
90         // Wrap BA data broker as BindingAwareBroker (requied by HONEYCOMB_NETCONF)
91         bind(BindingAwareBroker.class).annotatedWith(Names.named(HONEYCOMB_NETCONF))
92                 .toProvider(NetconfBindingBrokerProvider.class).in(Singleton.class);
93
94         // Create netconf operation service factory aggregator to aggregate different services
95         AggregatedNetconfOperationServiceFactory factory = new AggregatedNetconfOperationServiceFactory();
96         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_AGGR))
97                 .toInstance(factory);
98         bind(NetconfOperationServiceFactoryListener.class).toInstance(factory);
99
100         // Create netconf notification manager
101         NetconfNotificationManager manager = new NetconfNotificationManager();
102         bind(NetconfNotificationCollector.class).toInstance(manager);
103         bind(NetconfNotificationRegistry.class).toInstance(manager);
104         bind(NetconfNotificationListener.class).toInstance(manager);
105
106         // Netconf notification service factory
107         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF))
108                 .toProvider(NetconfNotificationMapperProvider.class).asEagerSingleton();
109         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF));
110
111         // Netconf core part - mapping between Honeycomb and Netconf
112         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE))
113                 .toProvider(NetconfMdsalMapperProvider.class).asEagerSingleton();
114         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE));
115
116         // Netconf monitoring service factory
117         bind(NetconfMonitoringService.class).toProvider(NetconfMonitoringServiceProvider.class).in(Singleton.class);
118         bind(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER))
119                 .toProvider(NetconfMonitoringMapperProvider.class).asEagerSingleton();
120         expose(NetconfOperationServiceFactory.class).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER));
121
122         // Create HC notification manager + HC2Netconf translator
123         bind(NotificationCollector.class).toProvider(HoneycombNotificationManagerProvider.class).in(Singleton.class);
124         bind(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf.class)
125                 .toProvider(HoneycombNotification2NetconfProvider.class).asEagerSingleton();
126         expose(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf.class);
127
128         configureServer();
129     }
130
131     /**
132      * Provide HONEYCOMB_NETCONF TCP and SSH servers.
133      */
134     private AnnotatedElementBuilder configureServer() {
135         bind(NioEventLoopGroup.class).toProvider(NettyThreadGroupProvider.class).in(Singleton.class);
136         bind(Timer.class).toInstance(new HashedWheelTimer());
137         bind(NetconfServerDispatcher.class).toProvider(NetconfServerDispatcherProvider.class).in(Singleton.class);
138         bind(NetconfTcpServerProvider.NetconfTcpServer.class).toProvider(NetconfTcpServerProvider.class)
139                 .asEagerSingleton();
140         expose(NetconfTcpServerProvider.NetconfTcpServer.class);
141         bind(NetconfNorthboundSshServer.class).toProvider(NetconfSshServerProvider.class).asEagerSingleton();
142         return expose(NetconfNorthboundSshServer.class);
143     }
144 }