HONEYCOMB-424: bump ODL dependencies to Oxygen
[honeycomb.git] / infra / northbound / bgp / src / main / java / io / fd / honeycomb / infra / bgp / BgpRIBProvider.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 org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.toTableTypes;
20
21 import com.google.inject.Inject;
22 import io.fd.honeycomb.binding.init.ProviderTrait;
23 import io.fd.honeycomb.data.init.ShutdownHandler;
24 import java.util.ArrayList;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
30 import org.opendaylight.controller.md.sal.dom.broker.impl.PingPongDataBroker;
31 import org.opendaylight.controller.sal.core.api.model.SchemaService;
32 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
34 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
35 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
36 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
37 import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
38 import org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
41 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
42 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.RibId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.rib.TablesKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 final class BgpRIBProvider extends ProviderTrait<RIB> {
53     private static final Logger LOG = LoggerFactory.getLogger(BgpRIBProvider.class);
54
55     @Inject
56     private BgpConfiguration cfg;
57     @Inject
58     private RIBExtensionConsumerContext extensions;
59     @Inject
60     private BGPDispatcher dispatcher;
61     @Inject
62     private BindingToNormalizedNodeCodec codec;
63     @Inject
64     private DOMDataBroker domBroker;
65     @Inject
66     private BGPTableTypeRegistryConsumer tableTypeRegistry;
67     @Inject
68     private SchemaService schemaService;
69     @Inject
70     private ShutdownHandler shutdownHandler;
71     @Inject
72     private Set<AfiSafi> configuredAfiSafis;
73
74     @Override
75     protected RIB create() {
76         final AsNumber asNumber = new AsNumber(cfg.bgpAsNumber.get().longValue());
77         final Ipv4Address routerId = new Ipv4Address(cfg.bgpBindingAddress.get());
78         final ClusterIdentifier clusterId = new ClusterIdentifier(routerId);
79         LOG.debug("Creating BGP RIB: routerId={}, asNumber={}", routerId, asNumber);
80         // TODO(HONEYCOMB-395): should all afi-safis use the same send-max value?
81         // TODO(HONEYCOMB-363): configure other BGP Multiprotocol extensions:
82
83         final ArrayList<AfiSafi> afiSafiList = new ArrayList<>(configuredAfiSafis);
84         final Map<TablesKey, PathSelectionMode> pathSelectionModes =
85                 OpenConfigMappingUtil.toPathSelectionMode(afiSafiList, tableTypeRegistry)
86                         .entrySet().stream().collect(Collectors.toMap(entry ->
87                         new TablesKey(entry.getKey().getAfi(), entry.getKey().getSafi()), Map.Entry::getValue));
88         // based on org.opendaylight.protocol.bgp.rib.impl.config.RibImpl.createRib
89         final PingPongDataBroker pingPongDataBroker = new PingPongDataBroker(domBroker);
90         final RIBImpl rib =
91                 new RIBImpl(new RibId(cfg.bgpProtocolInstanceName.get()),
92                         asNumber, new BgpId(routerId), clusterId, extensions, dispatcher, codec,
93                         pingPongDataBroker, toTableTypes(afiSafiList, tableTypeRegistry), pathSelectionModes,
94                         extensions.getClassLoadingStrategy());
95         rib.instantiateServiceInstance();
96
97         // required for proper RIB's CodecRegistry initialization (based on RIBImpl.start)
98         schemaService.registerSchemaContextListener(rib);
99         shutdownHandler.register("ping-pong-data-broker", pingPongDataBroker);
100         LOG.debug("BGP RIB created successfully: {}", rib);
101         return rib;
102     }
103
104     /**
105      * HC does not support clustering, but BGP uses {@link ClusterSingletonServiceProvider} to initialize {@link
106      * RIBImpl}. Therefore we provide this dummy implementation.
107      */
108     private static final class NoopClusterSingletonServiceProvider implements ClusterSingletonServiceProvider {
109         private static final Logger LOG = LoggerFactory.getLogger(NoopClusterSingletonServiceProvider.class);
110
111         private static final ClusterSingletonServiceRegistration REGISTRATION =
112                 () -> LOG.debug("Closing ClusterSingletonServiceRegistration");
113
114         @Override
115         public ClusterSingletonServiceRegistration registerClusterSingletonService(
116                 final ClusterSingletonService clusterSingletonService) {
117             clusterSingletonService.instantiateServiceInstance();
118             return REGISTRATION;
119         }
120
121         @Override
122         public void close() throws Exception {
123             LOG.debug("Closing NoopClusterSingletonServiceProvider");
124         }
125     }
126 }