Bug 6675: add missing cluster-id configuration knob
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / config / RibImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bgp.rib.impl.config;
10
11 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getAfiSafiWithDefault;
12
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.stream.Collectors;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
28 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
29 import org.opendaylight.protocol.bgp.mode.api.PathSelectionMode;
30 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
31 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigProvider;
32 import org.opendaylight.protocol.bgp.rib.impl.RIBImpl;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
39 import org.opendaylight.protocol.bgp.rib.impl.stats.rib.impl.BGPRenderStats;
40 import org.opendaylight.protocol.bgp.rib.spi.CacheDisconnectedPeers;
41 import org.opendaylight.protocol.bgp.rib.spi.ExportPolicyPeerTracker;
42 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
43 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.global.base.Config;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.top.bgp.Global;
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.multiprotocol.rev130919.BgpTableType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.RibId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.Rib;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.RibKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpId;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
55 import org.opendaylight.yangtools.concepts.ListenerRegistration;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
59 import org.osgi.framework.ServiceRegistration;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 public final class RibImpl implements RIB, AutoCloseable {
64
65     private static final Logger LOG = LoggerFactory.getLogger(RibImpl.class);
66
67     private final RIBExtensionConsumerContext extensions;
68     private final BGPDispatcher dispatcher;
69     private final BindingCodecTreeFactory codecTreeFactory;
70     private final DOMDataBroker domBroker;
71     private final SchemaService schemaService;
72     private RIBImpl ribImpl;
73     private ServiceRegistration<?> serviceRegistration;
74     private ListenerRegistration<SchemaContextListener> schemaContextRegistration;
75     private final ClusterSingletonServiceProvider provider;
76     private List<AfiSafi> afiSafi;
77     private AsNumber asNumber;
78     private Ipv4Address routerId;
79
80     private ClusterIdentifier clusterId;
81
82     public RibImpl(final ClusterSingletonServiceProvider provider, final RIBExtensionConsumerContext contextProvider, final BGPDispatcher dispatcher,
83             final BindingCodecTreeFactory codecTreeFactory, final DOMDataBroker domBroker, final SchemaService schemaService) {
84         this.provider = Preconditions.checkNotNull(provider);
85         this.extensions = contextProvider;
86         this.dispatcher = dispatcher;
87         this.codecTreeFactory = codecTreeFactory;
88         this.domBroker = domBroker;
89         this.schemaService = schemaService;
90     }
91
92     void start(final Global global, final String instanceName, final BGPOpenConfigMappingService mappingService,
93         final BgpDeployer.WriteConfiguration configurationWriter) {
94         Preconditions.checkState(this.ribImpl == null, "Previous instance %s was not closed.", this);
95         this.ribImpl = createRib(this.provider, global, instanceName, mappingService, configurationWriter);
96         this.schemaContextRegistration = this.schemaService.registerSchemaContextListener(this.ribImpl);
97     }
98
99     Boolean isGlobalEqual(final Global global) {
100         final List<AfiSafi> globalAfiSafi = getAfiSafiWithDefault(global.getAfiSafis(), true);
101         final Config globalConfig = global.getConfig();
102         final AsNumber globalAs = globalConfig.getAs();
103         final Ipv4Address globalRouterId = global.getConfig().getRouterId();
104         final ClusterIdentifier globalClusterId = OpenConfigMappingUtil.getClusterIdentifier(globalConfig);
105         return this.afiSafi.containsAll(globalAfiSafi) && globalAfiSafi.containsAll(this.afiSafi)
106             && globalAs.equals(this.asNumber)
107             && globalRouterId.getValue().equals(this.routerId.getValue())
108             && globalClusterId.getValue().equals(this.clusterId.getValue());
109     }
110
111     @Override
112     public KeyedInstanceIdentifier<Rib, RibKey> getInstanceIdentifier() {
113         return this.ribImpl.getInstanceIdentifier();
114     }
115
116     @Override
117     public AsNumber getLocalAs() {
118         return this.ribImpl.getLocalAs();
119     }
120
121     @Override
122     public BgpId getBgpIdentifier() {
123         return this.ribImpl.getBgpIdentifier();
124     }
125
126     @Override
127     public Set<? extends BgpTableType> getLocalTables() {
128         return this.ribImpl.getLocalTables();
129     }
130
131     @Override
132     public BGPDispatcher getDispatcher() {
133         return this.ribImpl.getDispatcher();
134     }
135
136     @Override
137     public DOMTransactionChain createPeerChain(final TransactionChainListener listener) {
138         return this.ribImpl.createPeerChain(listener);
139     }
140
141     @Override
142     public RIBExtensionConsumerContext getRibExtensions() {
143         return this.ribImpl.getRibExtensions();
144     }
145
146     @Override
147     public RIBSupportContextRegistry getRibSupportContext() {
148         return this.ribImpl.getRibSupportContext();
149     }
150
151     @Override
152     public YangInstanceIdentifier getYangRibId() {
153         return this.ribImpl.getYangRibId();
154     }
155
156     @Override
157     public CodecsRegistry getCodecsRegistry() {
158         return this.ribImpl.getCodecsRegistry();
159     }
160
161     @Override
162     public Optional<BGPOpenConfigProvider> getOpenConfigProvider() {
163         return this.ribImpl.getOpenConfigProvider();
164     }
165
166     @Override
167     public DOMDataTreeChangeService getService() {
168         return this.ribImpl.getService();
169     }
170
171     @Override
172     public void close() {
173         if (this.ribImpl != null) {
174             try {
175                 this.ribImpl.close();
176             } catch (final Exception e) {
177                 LOG.warn("Failed to close {} rib instance", this, e);
178             }
179             this.ribImpl = null;
180         }
181         if (this.schemaContextRegistration != null) {
182             this.schemaContextRegistration.close();
183             this.schemaContextRegistration = null;
184         }
185         if (this.serviceRegistration != null) {
186             try {
187                 this.serviceRegistration.unregister();
188             } catch(final IllegalStateException e) {
189                 LOG.warn("Failed to unregister {} service instance", this, e);
190             }
191             this.serviceRegistration = null;
192         }
193     }
194
195     void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
196         this.serviceRegistration = serviceRegistration;
197     }
198
199     @Override
200     public BGPRenderStats getRenderStats() {
201         return this.ribImpl.getRenderStats();
202     }
203
204     @Override
205     public ImportPolicyPeerTracker getImportPolicyPeerTracker() {
206         return this.ribImpl.getImportPolicyPeerTracker();
207     }
208
209     @Override
210     public ExportPolicyPeerTracker getExportPolicyPeerTracker(final TablesKey tablesKey) {
211         return this.ribImpl.getExportPolicyPeerTracker(tablesKey);
212     }
213
214     @Override
215     public Set<TablesKey> getLocalTablesKeys() {
216         return this.ribImpl.getLocalTablesKeys();
217     }
218
219     @Override
220     public ServiceGroupIdentifier getRibIServiceGroupIdentifier() {
221         return this.ribImpl.getRibIServiceGroupIdentifier();
222     }
223
224     @Override
225     public String toString() {
226         return this.ribImpl != null ? this.ribImpl.toString() : null;
227     }
228
229     private RIBImpl createRib(final ClusterSingletonServiceProvider provider, final Global global, final String bgpInstanceName,
230         final BGPOpenConfigMappingService mappingService, final BgpDeployer.WriteConfiguration configurationWriter) {
231         this.afiSafi = getAfiSafiWithDefault(global.getAfiSafis(), true);
232         final Config globalConfig = global.getConfig();
233         this.asNumber = globalConfig.getAs();
234         this.routerId = globalConfig.getRouterId();
235         this.clusterId = OpenConfigMappingUtil.getClusterIdentifier(globalConfig);
236         final Map<TablesKey, PathSelectionMode> pathSelectionModes = mappingService.toPathSelectionMode(this.afiSafi).entrySet()
237                 .stream().collect(Collectors.toMap(entry -> new TablesKey(entry.getKey().getAfi(), entry.getKey().getSafi()), Map.Entry::getValue));
238         return new RIBImpl(provider, new RibId(bgpInstanceName), this.asNumber, new BgpId(this.routerId), this.clusterId,
239                 this.extensions, this.dispatcher, this.codecTreeFactory, this.domBroker, mappingService.toTableTypes(this.afiSafi), pathSelectionModes,
240                 this.extensions.getClassLoadingStrategy(), configurationWriter);
241     }
242
243     @Override
244     public ClusterSingletonServiceRegistration registerClusterSingletonService(final ClusterSingletonService clusterSingletonService) {
245         return this.provider.registerClusterSingletonService(clusterSingletonService);
246     }
247 }