HONEYCOMB-18 Fixing comments from reviews
[honeycomb.git] / infra / minimal-distribution / src / main / java / io / fd / honeycomb / infra / distro / netconf / NetconfModule.groovy
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.infra.distro.netconf
18
19 import com.google.inject.PrivateModule
20 import com.google.inject.Singleton
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.notification.NotificationCollector
27 import io.netty.channel.nio.NioEventLoopGroup
28 import io.netty.util.Timer
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker
32 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore
33 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker
34 import org.opendaylight.netconf.api.NetconfServerDispatcher
35 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService
36 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory
37 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory
38 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactoryListener
39 import org.opendaylight.netconf.notifications.NetconfNotificationCollector
40 import org.opendaylight.netconf.notifications.NetconfNotificationListener
41 import org.opendaylight.netconf.notifications.NetconfNotificationRegistry
42 import org.opendaylight.netconf.notifications.impl.NetconfNotificationManager
43
44 import static InmemoryDOMDataBrokerProvider.CONFIG
45 import static InmemoryDOMDataBrokerProvider.OPERATIONAL
46
47 class NetconfModule extends PrivateModule {
48
49
50     public static final String HONEYCOMB_NETCONF = "honeycomb-netconf"
51     public static final String HONEYCOMB_NETCONF_MAPPER_AGGR = "netconf-mapper-aggregator"
52     public static final String HONEYCOMB_NETCONF_MAPPER_NOTIF = "netconf-mapper-notification"
53     public static final String HONEYCOMB_NETCONF_MAPPER_CORE = "netconf-mapper-honeycomb"
54     public static final String HONEYCOMB_NETCONF_MAPPER_OPER = "netconf-mapper-monitoring"
55
56     @Override
57     protected void configure() {
58         // Create inmemory data store for HONEYCOMB_NETCONF config metadata
59         bind(InMemoryDOMDataStore)
60                 .annotatedWith(Names.named(CONFIG))
61                 .toProvider(new DataStoreProvider(type: LogicalDatastoreType.CONFIGURATION, name: CONFIG))
62                 .in(Singleton)
63         // Create inmemory data store for HONEYCOMB_NETCONF operational metadata
64         bind(InMemoryDOMDataStore)
65                 .annotatedWith(Names.named(OPERATIONAL))
66                 .toProvider(new DataStoreProvider(type: LogicalDatastoreType.OPERATIONAL, name: OPERATIONAL))
67                 .in(Singleton)
68         // Wrap datastores as DOMDataBroker
69         bind(DOMDataBroker).toProvider(InmemoryDOMDataBrokerProvider).in(Singleton)
70
71         // Wrap DOMDataBroker as BA data broker
72         bind(DataBroker)
73                 .annotatedWith(Names.named(HONEYCOMB_NETCONF))
74                 .toProvider(BindingDataBrokerProvider)
75                 .in(Singleton)
76         expose(DataBroker).annotatedWith(Names.named(HONEYCOMB_NETCONF))
77
78         // Wrap BA data broker as BindingAwareBroker (requied by HONEYCOMB_NETCONF)
79         bind(BindingAwareBroker)
80                 .annotatedWith(Names.named(HONEYCOMB_NETCONF))
81                 .toProvider(NetconfBindingBrokerProvider)
82                 .in(Singleton)
83
84         // Create netconf operation service factory aggregator to aggregate different services
85         def factory = new AggregatedNetconfOperationServiceFactory()
86         bind(NetconfOperationServiceFactory)
87                 .annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_AGGR))
88                 .toInstance(factory)
89         bind(NetconfOperationServiceFactoryListener).toInstance(factory)
90
91         // Create netconf notification manager
92         def manager = new NetconfNotificationManager()
93         bind(NetconfNotificationCollector).toInstance(manager)
94         bind(NetconfNotificationRegistry).toInstance(manager)
95         bind(NetconfNotificationListener).toInstance(manager)
96
97         // Netconf notification service factory
98         bind(NetconfOperationServiceFactory)
99                 .annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF))
100                 .toProvider(NetconfNotificationMapperProvider)
101                 .in(Singleton)
102         expose(NetconfOperationServiceFactory).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_NOTIF))
103
104         // Netconf core part - mapping between Honeycomb and Netconf
105         bind(NetconfOperationServiceFactory)
106                 .annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE))
107                 .toProvider(NetconfMdsalMapperProvider)
108                 .in(Singleton)
109         expose(NetconfOperationServiceFactory).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_CORE))
110
111         // Netconf monitoring service factory
112         bind(NetconfMonitoringService).toProvider(NetconfMonitoringServiceProvider).in(Singleton)
113         bind(NetconfOperationServiceFactory)
114                 .annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER))
115                 .toProvider(NetconfMonitoringMapperProvider)
116                 .in(Singleton)
117         expose(NetconfOperationServiceFactory).annotatedWith(Names.named(HONEYCOMB_NETCONF_MAPPER_OPER))
118
119         // Create HC notification manager + HC2Netconf translator
120         bind(NotificationCollector).toProvider(HoneycombNotificationManagerProvider).in(Singleton)
121         bind(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf)
122                 .toProvider(HoneycombNotification2NetconfProvider)
123                 .in(Singleton)
124         expose(HoneycombNotification2NetconfProvider.HoneycombNotification2Netconf)
125
126         configureServer()
127     }
128
129     /**
130      * Provide HONEYCOMB_NETCONF TCP and SSH servers
131      */
132     def configureServer() {
133         bind(NioEventLoopGroup).toProvider(NettyThreadGroupProvider).in(Singleton)
134         bind(Timer).toProvider(NettyTimerProvider).in(Singleton)
135         bind(NetconfServerDispatcher).toProvider(NetconfServerDispatcherProvider).in(Singleton)
136         bind(NetconfTcpServerProvider.NetconfTcpServer).toProvider(NetconfTcpServerProvider).in(Singleton)
137         expose(NetconfTcpServerProvider.NetconfTcpServer)
138         bind(NetconfSshServerProvider.NetconfSshServer).toProvider(NetconfSshServerProvider).in(Singleton)
139         expose(NetconfSshServerProvider.NetconfSshServer)
140     }
141 }