2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.infra.bgp;
19 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.toTableTypes;
21 import com.google.common.collect.ImmutableList;
22 import com.google.inject.Inject;
23 import io.fd.honeycomb.binding.init.ProviderTrait;
24 import io.fd.honeycomb.data.init.ShutdownHandler;
25 import java.util.List;
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.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4LABELLEDUNICAST;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2Builder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 final class BgpRIBProvider extends ProviderTrait<RIB> {
58 private static final Logger LOG = LoggerFactory.getLogger(BgpRIBProvider.class);
61 private BgpConfiguration cfg;
63 private RIBExtensionConsumerContext extensions;
65 private BGPDispatcher dispatcher;
67 private BindingToNormalizedNodeCodec codec;
69 private DOMDataBroker domBroker;
71 private BGPTableTypeRegistryConsumer tableTypeRegistry;
73 private SchemaService schemaService;
75 private ShutdownHandler shutdownHandler;
78 protected RIB create() {
79 final AsNumber asNumber = new AsNumber(cfg.bgpAsNumber.get().longValue());
80 final Ipv4Address routerId = new Ipv4Address(cfg.bgpBindingAddress.get());
81 final ClusterIdentifier clusterId = new ClusterIdentifier(routerId);
82 LOG.debug("Creating BGP RIB: routerId={}, asNumber={}", routerId, asNumber);
83 // TODO configure other BGP Multiprotocol extensions:
84 final List<AfiSafi> afiSafi = ImmutableList.of(
85 new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class)
86 .addAugmentation(AfiSafi2.class,
87 new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
88 .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
90 new AfiSafiBuilder().setAfiSafiName(IPV4LABELLEDUNICAST.class)
91 .addAugmentation(AfiSafi2.class,
92 new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
93 .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
96 final Map<TablesKey, PathSelectionMode> pathSelectionModes =
97 OpenConfigMappingUtil.toPathSelectionMode(afiSafi, tableTypeRegistry)
98 .entrySet().stream().collect(Collectors.toMap(entry ->
99 new TablesKey(entry.getKey().getAfi(), entry.getKey().getSafi()), Map.Entry::getValue));
100 // based on org.opendaylight.protocol.bgp.rib.impl.config.RibImpl.createRib
101 final PingPongDataBroker pingPongDataBroker = new PingPongDataBroker(domBroker);
103 new RIBImpl(new NoopClusterSingletonServiceProvider(), new RibId(cfg.bgpProtocolInstanceName.get()),
104 asNumber, new BgpId(routerId), clusterId, extensions, dispatcher, codec,
105 pingPongDataBroker, toTableTypes(afiSafi, tableTypeRegistry), pathSelectionModes,
106 extensions.getClassLoadingStrategy(), null);
108 // required for proper RIB's CodecRegistry initialization (based on RIBImpl.start)
109 schemaService.registerSchemaContextListener(rib);
110 shutdownHandler.register("ping-pong-data-broker", pingPongDataBroker);
111 LOG.debug("BGP RIB created successfully: {}", rib);
116 * HC does not support clustering, but BGP uses {@link ClusterSingletonServiceProvider} to initialize {@link
117 * RIBImpl}. Therefore we provide this dummy implementation.
119 private static final class NoopClusterSingletonServiceProvider implements ClusterSingletonServiceProvider {
120 private static final Logger LOG = LoggerFactory.getLogger(NoopClusterSingletonServiceProvider.class);
122 private static final ClusterSingletonServiceRegistration REGISTRATION =
123 () -> LOG.debug("Closing ClusterSingletonServiceRegistration");
126 public ClusterSingletonServiceRegistration registerClusterSingletonService(
127 final ClusterSingletonService clusterSingletonService) {
128 clusterSingletonService.instantiateServiceInstance();
133 public void close() throws Exception {
134 LOG.debug("Closing NoopClusterSingletonServiceProvider");