summaryrefslogtreecommitdiffstats
path: root/infra/northbound/bgp/src/main/java/io/fd/honeycomb/infra/bgp/BgpRIBProvider.java
blob: 5663b7c4d528c139c80775fb9753d600f56f409f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.infra.bgp;

import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.toTableTypes;

import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import io.fd.honeycomb.binding.init.ProviderTrait;
import io.fd.honeycomb.data.init.ShutdownHandler;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
import org.opendaylight.controller.md.sal.dom.broker.impl.PingPongDataBroker;
import org.opendaylight.controller.sal.core.api.model.SchemaService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
import org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil;
import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafiBuilder;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4LABELLEDUNICAST;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.IPV4UNICAST;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.AfiSafi2Builder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.openconfig.extensions.rev160614.LINKSTATE;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class BgpRIBProvider extends ProviderTrait<RIB> {
    private static final Logger LOG = LoggerFactory.getLogger(BgpRIBProvider.class);

    @Inject
    private BgpConfiguration cfg;
    @Inject
    private RIBExtensionConsumerContext extensions;
    @Inject
    private BGPDispatcher dispatcher;
    @Inject
    private BindingToNormalizedNodeCodec codec;
    @Inject
    private DOMDataBroker domBroker;
    @Inject
    private BGPTableTypeRegistryConsumer tableTypeRegistry;
    @Inject
    private SchemaService schemaService;
    @Inject
    private ShutdownHandler shutdownHandler;

    @Override
    protected RIB create() {
        final AsNumber asNumber = new AsNumber(cfg.bgpAsNumber.get().longValue());
        final Ipv4Address routerId = new Ipv4Address(cfg.bgpBindingAddress.get());
        final ClusterIdentifier clusterId = new ClusterIdentifier(routerId);
        LOG.debug("Creating BGP RIB: routerId={}, asNumber={}", routerId, asNumber);
        // TODO(HONEYCOMB-395): should all afi-safis use the same send-max value?
        // TODO(HONEYCOMB-363): configure other BGP Multiprotocol extensions:
        final List<AfiSafi> afiSafi = ImmutableList.of(
                new AfiSafiBuilder().setAfiSafiName(IPV4UNICAST.class)
                        .addAugmentation(AfiSafi2.class,
                                new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
                                        .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
                        .build(),
                new AfiSafiBuilder().setAfiSafiName(IPV4LABELLEDUNICAST.class)
                        .addAugmentation(AfiSafi2.class,
                                new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
                                        .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
                        .build(),
                new AfiSafiBuilder().setAfiSafiName(LINKSTATE.class)
                        .addAugmentation(AfiSafi2.class,
                                new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
                                        .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
                        .build()
        );
        final Map<TablesKey, PathSelectionMode> pathSelectionModes =
                OpenConfigMappingUtil.toPathSelectionMode(afiSafi, tableTypeRegistry)
                        .entrySet().stream().collect(Collectors.toMap(entry ->
                        new TablesKey(entry.getKey().getAfi(), entry.getKey().getSafi()), Map.Entry::getValue));
        // based on org.opendaylight.protocol.bgp.rib.impl.config.RibImpl.createRib
        final PingPongDataBroker pingPongDataBroker = new PingPongDataBroker(domBroker);
        final RIBImpl rib =
                new RIBImpl(new NoopClusterSingletonServiceProvider(), new RibId(cfg.bgpProtocolInstanceName.get()),
                        asNumber, new BgpId(routerId), clusterId, extensions, dispatcher, codec,
                        pingPongDataBroker, toTableTypes(afiSafi, tableTypeRegistry), pathSelectionModes,
                        extensions.getClassLoadingStrategy(), null);

        // required for proper RIB's CodecRegistry initialization (based on RIBImpl.start)
        schemaService.registerSchemaContextListener(rib);
        shutdownHandler.register("ping-pong-data-broker", pingPongDataBroker);
        LOG.debug("BGP RIB created successfully: {}", rib);
        return rib;
    }

    /**
     * HC does not support clustering, but BGP uses {@link ClusterSingletonServiceProvider} to initialize {@link
     * RIBImpl}. Therefore we provide this dummy implementation.
     */
    private static final class NoopClusterSingletonServiceProvider implements ClusterSingletonServiceProvider {
        private static final Logger LOG = LoggerFactory.getLogger(NoopClusterSingletonServiceProvider.class);

        private static final ClusterSingletonServiceRegistration REGISTRATION =
                () -> LOG.debug("Closing ClusterSingletonServiceRegistration");

        @Override
        public ClusterSingletonServiceRegistration registerClusterSingletonService(
                final ClusterSingletonService clusterSingletonService) {
            clusterSingletonService.instantiateServiceInstance();
            return REGISTRATION;
        }

        @Override
        public void close() throws Exception {
            LOG.debug("Closing NoopClusterSingletonServiceProvider");
        }
    }
}