Dedicated provider for BgpNeighbours
[honeycomb.git] / infra / bgp-distribution / src / main / java / io / fd / honeycomb / infra / bgp / BgpModule.java
1 /*
2  * Copyright (c) 2017 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.bgp;
18
19 import static io.fd.honeycomb.infra.distro.data.InmemoryDOMDataBrokerProvider.CONFIG;
20 import static io.fd.honeycomb.infra.distro.data.InmemoryDOMDataBrokerProvider.OPERATIONAL;
21
22 import com.google.inject.PrivateModule;
23 import com.google.inject.Singleton;
24 import com.google.inject.name.Names;
25 import io.fd.honeycomb.infra.distro.data.BindingDataBrokerProvider;
26 import io.fd.honeycomb.infra.distro.data.DataStoreProvider;
27 import io.fd.honeycomb.infra.distro.data.InmemoryDOMDataBrokerProvider;
28 import io.netty.channel.EventLoopGroup;
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.protocol.bgp.openconfig.impl.BGPOpenConfigMappingServiceImpl;
34 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
35 import org.opendaylight.protocol.bgp.rib.impl.StrictBGPPeerRegistry;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
39 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.BgpNeighbors;
40
41 public final class BgpModule extends PrivateModule {
42
43     static final String HONEYCOMB_BGP = "honeycomb-bgp";
44
45     protected void configure() {
46         // Create BGPDispatcher BGPDispatcher for creating BGP clients
47         bind(EventLoopGroup.class).toProvider(BgpNettyThreadGroupProvider.class).in(Singleton.class);
48         bind(BGPDispatcher.class).toProvider(BGPDispatcherImplProvider.class).in(Singleton.class);
49
50         configureRIB();
51
52         // Configure peer registry
53         bind(BGPOpenConfigMappingService.class).toInstance(new BGPOpenConfigMappingServiceImpl());
54         bind(BGPPeerRegistry.class).toInstance(StrictBGPPeerRegistry.instance());
55
56
57         // Create BGP server instance
58         bind(BgpServerProvider.BgpServer.class).toProvider(BgpServerProvider.class).in(Singleton.class);
59         expose(BgpServerProvider.BgpServer.class);
60
61         // Initialize BgpNeighbours
62         bind(BgpNeighbors.class).toProvider(BgpNeighboursProvider.class).in(Singleton.class);
63         expose(BgpNeighbors.class);
64     }
65
66     private void configureRIB() {
67         // Create inmemory config data store for HONEYCOMB_BGP
68         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(CONFIG))
69             .toProvider(new DataStoreProvider(CONFIG, LogicalDatastoreType.CONFIGURATION))
70             .in(Singleton.class);
71
72         // Create inmemory operational data store for HONEYCOMB_BGP
73         bind(InMemoryDOMDataStore.class).annotatedWith(Names.named(OPERATIONAL))
74             .toProvider(new DataStoreProvider(OPERATIONAL, LogicalDatastoreType.OPERATIONAL))
75             .in(Singleton.class);
76
77         // Wrap datastores as DOMDataBroker
78         // TODO make executor service configurable
79         bind(DOMDataBroker.class).toProvider(InmemoryDOMDataBrokerProvider.class).in(Singleton.class);
80
81         // Wrap DOMDataBroker as BA data broker (required by BgpReaderFactoryProvider)
82         bind(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_BGP)).toProvider(BindingDataBrokerProvider.class)
83             .in(Singleton.class);
84         expose(DataBroker.class).annotatedWith(Names.named(HONEYCOMB_BGP));
85
86         // Create RIB instance
87         bind(RIB.class).toProvider(BgpRIBProvider.class).in(Singleton.class);
88     }
89 }