Shutdown closing of resources
[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.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;
26 import java.util.Map;
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;
56
57 final class BgpRIBProvider extends ProviderTrait<RIB> {
58     private static final Logger LOG = LoggerFactory.getLogger(BgpRIBProvider.class);
59
60     @Inject
61     private BgpConfiguration cfg;
62     @Inject
63     private RIBExtensionConsumerContext extensions;
64     @Inject
65     private BGPDispatcher dispatcher;
66     @Inject
67     private BindingToNormalizedNodeCodec codec;
68     @Inject
69     private DOMDataBroker domBroker;
70     @Inject
71     private BGPTableTypeRegistryConsumer tableTypeRegistry;
72     @Inject
73     private SchemaService schemaService;
74     @Inject
75     private ShutdownHandler shutdownHandler;
76
77     @Override
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())
89                         .build(),
90                 new AfiSafiBuilder().setAfiSafiName(IPV4LABELLEDUNICAST.class)
91                         .addAugmentation(AfiSafi2.class,
92                                 new AfiSafi2Builder().setReceive(cfg.isBgpMultiplePathsEnabled())
93                                         .setSendMax(cfg.bgpSendMaxMaths.get().shortValue()).build())
94                         .build()
95         );
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);
102         final RIBImpl rib =
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);
107
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);
112         return rib;
113     }
114
115     /**
116      * HC does not support clustering, but BGP uses {@link ClusterSingletonServiceProvider} to initialize {@link
117      * RIBImpl}. Therefore we provide this dummy implementation.
118      */
119     private static final class NoopClusterSingletonServiceProvider implements ClusterSingletonServiceProvider {
120         private static final Logger LOG = LoggerFactory.getLogger(NoopClusterSingletonServiceProvider.class);
121
122         private static final ClusterSingletonServiceRegistration REGISTRATION =
123                 () -> LOG.debug("Closing ClusterSingletonServiceRegistration");
124
125         @Override
126         public ClusterSingletonServiceRegistration registerClusterSingletonService(
127                 final ClusterSingletonService clusterSingletonService) {
128             clusterSingletonService.instantiateServiceInstance();
129             return REGISTRATION;
130         }
131
132         @Override
133         public void close() throws Exception {
134             LOG.debug("Closing NoopClusterSingletonServiceProvider");
135         }
136     }
137 }