2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.rib.impl.config;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getHoldTimer;
13 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getPeerAs;
14 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getSimpleRoutingPolicy;
16 import com.google.common.base.Optional;
17 import com.google.common.base.Preconditions;
18 import com.google.common.collect.Iterables;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import io.netty.util.concurrent.Future;
22 import java.net.InetSocketAddress;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Objects;
28 import javax.annotation.concurrent.GuardedBy;
29 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
30 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
31 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
32 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
33 import org.opendaylight.protocol.bgp.openconfig.spi.BGPTableTypeRegistryConsumer;
34 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
35 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
36 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
39 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
40 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
41 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
42 import org.opendaylight.protocol.concepts.KeyMapping;
43 import org.opendaylight.protocol.util.Ipv4Util;
44 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafis;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapabilityBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
61 import org.osgi.framework.ServiceRegistration;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
66 public final class BgpPeer implements PeerBean, BGPPeerStateConsumer {
68 private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
70 private final RpcProviderRegistry rpcRegistry;
72 private ServiceRegistration<?> serviceRegistration;
74 private Neighbor currentConfiguration;
76 private BgpPeerSingletonService bgpPeerSingletonService;
78 public BgpPeer(final RpcProviderRegistry rpcRegistry) {
79 this.rpcRegistry = rpcRegistry;
82 private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
83 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
84 final List<BgpParameters> tlvs = new ArrayList<>();
85 final List<OptionalCapabilities> caps = new ArrayList<>();
86 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(
87 new As4BytesCapabilityBuilder().setAsNumber(rib.getLocalAs()).build()).build()).build());
89 caps.add(new OptionalCapabilitiesBuilder()
90 .setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
91 caps.add(new OptionalCapabilitiesBuilder()
92 .setCParameters(MultiprotocolCapabilitiesUtil.RR_CAPABILITY).build());
94 final List<AfiSafi> afiSafi = OpenConfigMappingUtil
95 .getAfiSafiWithDefault(neighbor.getAfiSafis(), false);
96 final List<AddressFamilies> addPathCapability = OpenConfigMappingUtil
97 .toAddPathCapability(afiSafi, tableTypeRegistry);
98 if (!addPathCapability.isEmpty()) {
99 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder()
100 .addAugmentation(CParameters1.class, new CParameters1Builder().setAddPathCapability(
101 new AddPathCapabilityBuilder().setAddressFamilies(addPathCapability)
102 .build()).build()).build()).build());
105 final List<BgpTableType> tableTypes = OpenConfigMappingUtil.toTableTypes(afiSafi, tableTypeRegistry);
106 for (final BgpTableType tableType : tableTypes) {
107 if (!rib.getLocalTables().contains(tableType)) {
108 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.",
112 caps.add(new OptionalCapabilitiesBuilder().setCParameters(
113 new CParametersBuilder().addAugmentation(CParameters1.class,
114 new CParameters1Builder().setMultiprotocolCapability(
115 new MultiprotocolCapabilityBuilder(tableType).build()).build()).build()).build());
117 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(caps).build());
121 private static Optional<byte[]> getPassword(final KeyMapping key) {
123 return Optional.of(Iterables.getOnlyElement(key.values()));
125 return Optional.absent();
129 public synchronized void start(final RIB rib, final Neighbor neighbor,
130 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
131 Preconditions.checkState(this.bgpPeerSingletonService == null,
132 "Previous peer instance was not closed.");
133 this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, tableTypeRegistry);
134 this.currentConfiguration = neighbor;
138 public synchronized void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
139 Preconditions.checkState(this.currentConfiguration != null);
140 start(rib, this.currentConfiguration, tableTypeRegistry);
144 public synchronized void close() {
145 closeSingletonService();
146 if (this.serviceRegistration != null) {
147 this.serviceRegistration.unregister();
148 this.serviceRegistration = null;
153 public synchronized ListenableFuture<Void> closeServiceInstance() {
154 if (this.bgpPeerSingletonService != null) {
155 return this.bgpPeerSingletonService.closeServiceInstance();
157 return Futures.immediateFuture(null);
160 private void closeSingletonService() {
161 if (this.bgpPeerSingletonService != null) {
163 this.bgpPeerSingletonService.close();
164 this.bgpPeerSingletonService = null;
165 } catch (final Exception e) {
166 LOG.warn("Failed to close peer instance", e);
172 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
173 final AfiSafis actAfiSafi = this.currentConfiguration.getAfiSafis();
174 final AfiSafis extAfiSafi = neighbor.getAfiSafis();
175 final List<AfiSafi> actualSafi = actAfiSafi != null ? actAfiSafi.getAfiSafi() : Collections.emptyList();
176 final List<AfiSafi> extSafi = extAfiSafi != null ? extAfiSafi.getAfiSafi() : Collections.emptyList();
177 return actualSafi.containsAll(extSafi) && extSafi.containsAll(actualSafi)
178 && Objects.equals(this.currentConfiguration.getConfig(), neighbor.getConfig())
179 && Objects.equals(this.currentConfiguration.getNeighborAddress(), neighbor.getNeighborAddress())
180 && Objects.equals(this.currentConfiguration.getAddPaths(), neighbor.getAddPaths())
181 && Objects.equals(this.currentConfiguration.getApplyPolicy(), neighbor.getApplyPolicy())
182 && Objects.equals(this.currentConfiguration.getAsPathOptions(), neighbor.getAsPathOptions())
183 && Objects.equals(this.currentConfiguration.getEbgpMultihop(), neighbor.getEbgpMultihop())
184 && Objects.equals(this.currentConfiguration.getGracefulRestart(), neighbor.getGracefulRestart())
185 && Objects.equals(this.currentConfiguration.getErrorHandling(), neighbor.getErrorHandling())
186 && Objects.equals(this.currentConfiguration.getLoggingOptions(), neighbor.getLoggingOptions())
187 && Objects.equals(this.currentConfiguration.getRouteReflector(), neighbor.getRouteReflector())
188 && Objects.equals(this.currentConfiguration.getState(), neighbor.getState())
189 && Objects.equals(this.currentConfiguration.getTimers(), neighbor.getTimers())
190 && Objects.equals(this.currentConfiguration.getTransport(), neighbor.getTransport());
194 public BGPPeerState getPeerState() {
195 if (this.bgpPeerSingletonService == null) {
198 return this.bgpPeerSingletonService.getPeerState();
201 synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
202 this.serviceRegistration = serviceRegistration;
205 private final class BgpPeerSingletonService implements BGPPeerStateConsumer, ClusterSingletonService,
207 private final ServiceGroupIdentifier serviceGroupIdentifier;
208 private final boolean activeConnection;
209 private final BGPDispatcher dispatcher;
210 private final InetSocketAddress inetAddress;
211 private final int retryTimer;
212 private final KeyMapping keys;
213 private final BGPPeer bgpPeer;
214 private final IpAddress neighborAddress;
215 private final BGPSessionPreferences prefs;
216 private ClusterSingletonServiceRegistration registration;
217 private Future<Void> connection;
219 private boolean isServiceInstantiated;
221 private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor,
222 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
223 this.neighborAddress = neighbor.getNeighborAddress();
224 final AfiSafis afisSAfis = requireNonNull(neighbor.getAfiSafis());
225 final Set<TablesKey> afiSafisAdvertized = OpenConfigMappingUtil
226 .toTableKey(afisSAfis.getAfiSafi(), tableTypeRegistry);
227 this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib,
228 OpenConfigMappingUtil.toPeerRole(neighbor), getSimpleRoutingPolicy(neighbor),
229 BgpPeer.this.rpcRegistry, afiSafisAdvertized, Collections.emptySet());
230 final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, tableTypeRegistry);
231 final KeyMapping keyMapping = OpenConfigMappingUtil.getNeighborKey(neighbor);
232 this.prefs = new BGPSessionPreferences(rib.getLocalAs(), getHoldTimer(neighbor), rib.getBgpIdentifier(),
233 getPeerAs(neighbor, rib), bgpParameters, getPassword(keyMapping));
234 this.activeConnection = OpenConfigMappingUtil.isActive(neighbor);
235 this.dispatcher = rib.getDispatcher();
236 this.inetAddress = Ipv4Util.toInetSocketAddress(this.neighborAddress, OpenConfigMappingUtil.getPort(neighbor));
237 this.retryTimer = OpenConfigMappingUtil.getRetryTimer(neighbor);
238 this.keys = keyMapping;
239 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
240 LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier.getValue());
241 //this need to be always the last step
242 this.registration = rib.registerClusterSingletonService(this);
246 public void close() throws Exception {
247 if (this.registration != null) {
248 this.registration.close();
249 this.registration = null;
254 public synchronized void instantiateServiceInstance() {
255 this.isServiceInstantiated = true;
256 LOG.info("Peer Singleton Service {} instantiated, Peer {}", getIdentifier().getValue(),
257 this.neighborAddress);
258 this.bgpPeer.instantiateServiceInstance();
259 this.dispatcher.getBGPPeerRegistry().addPeer(this.neighborAddress, this.bgpPeer, this.prefs);
260 if (this.activeConnection) {
261 this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, this.retryTimer,
267 public synchronized ListenableFuture<Void> closeServiceInstance() {
268 if (!this.isServiceInstantiated) {
269 LOG.info("Peer Singleton Service {} already closed, Peer {}", getIdentifier().getValue(),
270 this.neighborAddress);
271 return Futures.immediateFuture(null);
273 LOG.info("Close Peer Singleton Service {}, Peer {}", getIdentifier().getValue(), this.neighborAddress);
274 this.isServiceInstantiated = false;
275 if (this.connection != null) {
276 this.connection.cancel(true);
277 this.connection = null;
279 final ListenableFuture<Void> future = this.bgpPeer.close();
280 if (BgpPeer.this.currentConfiguration != null) {
281 this.dispatcher.getBGPPeerRegistry().removePeer(BgpPeer.this.currentConfiguration.getNeighborAddress());
287 public ServiceGroupIdentifier getIdentifier() {
288 return this.serviceGroupIdentifier;
292 public BGPPeerState getPeerState() {
293 return this.bgpPeer.getPeerState();