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