summaryrefslogtreecommitdiffstats
path: root/infra/northbound/bgp-extensions/extension-common/src/main/java/io/fd/honeycomb/northbound/bgp/extension/AbstractBgpExtensionModule.java
blob: caab1a83f6bc9dc78893e01311aeeb1a6b281733 (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
/*
 * 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.northbound.bgp.extension;

import com.google.inject.AbstractModule;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import io.fd.honeycomb.translate.write.WriterFactory;
import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
import org.opendaylight.protocol.bgp.parser.spi.BGPExtensionProviderActivator;
import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionProviderActivator;
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.types.rev151009.AfiSafiType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRib;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;

import javax.annotation.Nonnull;

/**
 * General blueprint for any module that wants to bind new BGP extension
 */
public abstract class AbstractBgpExtensionModule extends AbstractModule implements BgpExtensionModule {

    public static final InstanceIdentifier<Tables> TABLES_IID = InstanceIdentifier.create(ApplicationRib.class).child(Tables.class);

    @Override
    protected void configure() {
        bindRibActivators();
        bindExtensionActivators();
        bindTableTypes();
        bindAfiSafis();
        bindApplicationRibWriters();
        bind(BGPTableTypeRegistryConsumer.class).toProvider(BGPTableTypeRegistryConsumerProvider.class)
                .in(Singleton.class);

        configureAlso();

        // TODO(HONEYCOMB-395): should all afi-safis use the same send-max value?
    }

    protected abstract Logger getLogger();

    private void bindAfiSafis() {
        final Multibinder<AfiSafi> afiSafiBinder = Multibinder.newSetBinder(binder(), AfiSafi.class);
        getAfiSafiTypeProviders().stream()
                .peek(aClass -> getLogger().debug("Binding afi {}", aClass))
                .forEach(providerClass -> afiSafiBinder.addBinding().toProvider(providerClass));
    }

    private void bindRibActivators() {
        final Multibinder<RIBExtensionProviderActivator> ribBinder = Multibinder.newSetBinder(binder(),
                RIBExtensionProviderActivator.class);
        getRibActivators().stream()
                .peek(aClass -> getLogger().debug("Binding RIB activator {}", aClass))
                .forEach(activator -> ribBinder.addBinding().to(activator));
    }

    private void bindExtensionActivators() {
        final Multibinder<BGPExtensionProviderActivator> extensionsBinder = Multibinder.newSetBinder(binder(),
                BGPExtensionProviderActivator.class);
        getExtensionActivators().stream()
                .peek(aClass -> getLogger().debug("Binding Extension activator {}", aClass))
                .forEach(activator -> extensionsBinder.addBinding().to(activator));
    }

    private void bindTableTypes() {
        final Multibinder<BGPTableTypeRegistryConsumerProvider.BGPTableType> tableTypeBinder =
                Multibinder.newSetBinder(binder(), BGPTableTypeRegistryConsumerProvider.BGPTableType.class);
        getTableTypes().stream()
                .peek(tableTypeRegistration -> getLogger().debug("Binding table type {}", tableTypeRegistration))
                .forEach(registration -> tableTypeBinder.addBinding()
                        .toInstance(provider -> provider.registerBGPTableType(registration.addressFamily, registration.subsequentAddressFamily, registration.afiSafiType)));
    }

    private void bindApplicationRibWriters() {
        final Multibinder<WriterFactory> applicationRibWritersBinder = Multibinder.newSetBinder(binder(), WriterFactory.class);

        getApplicationRibWriters().stream()
                .peek(aClass -> getLogger().debug("Binding writer factory {}", aClass))
                .forEach(aClass -> applicationRibWritersBinder.addBinding().to(aClass));
    }

    static final class TableTypeRegistration {
        private final Class<? extends AddressFamily> addressFamily;
        private final Class<? extends SubsequentAddressFamily> subsequentAddressFamily;
        private final Class<? extends AfiSafiType> afiSafiType;

        private TableTypeRegistration(final Class<? extends AddressFamily> addressFamily,
                                      final Class<? extends SubsequentAddressFamily> subsequentAddressFamily,
                                      final Class<? extends AfiSafiType> afiSafiType) {
            this.addressFamily = addressFamily;
            this.subsequentAddressFamily = subsequentAddressFamily;
            this.afiSafiType = afiSafiType;
        }

        static TableTypeRegistration tableType(@Nonnull final Class<? extends AddressFamily> addressFamily,
                                                      @Nonnull final Class<? extends SubsequentAddressFamily> subsequentAddressFamily,
                                                      @Nonnull final Class<? extends AfiSafiType> afiSafiType) {
            return new TableTypeRegistration(addressFamily, subsequentAddressFamily, afiSafiType);
        }

        @Override
        public String toString() {
            return "TableTypeRegistration{" +
                    "addressFamily=" + addressFamily +
                    ", subsequentAddressFamily=" + subsequentAddressFamily +
                    ", afiSafiType=" + afiSafiType +
                    '}';
        }
    }

}