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.BgpDeployer.WriteConfiguration;
40 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
41 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerState;
42 import org.opendaylight.protocol.bgp.rib.spi.state.BGPPeerStateConsumer;
43 import org.opendaylight.protocol.concepts.KeyMapping;
44 import org.opendaylight.protocol.util.Ipv4Util;
45 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
46 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbor.group.AfiSafis;
47 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapabilityBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
62 import org.osgi.framework.ServiceRegistration;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
67 public final class BgpPeer implements PeerBean, BGPPeerStateConsumer {
69 private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
71 private final RpcProviderRegistry rpcRegistry;
73 private ServiceRegistration<?> serviceRegistration;
75 private Neighbor currentConfiguration;
77 private BgpPeerSingletonService bgpPeerSingletonService;
79 public BgpPeer(final RpcProviderRegistry rpcRegistry) {
80 this.rpcRegistry = rpcRegistry;
84 public synchronized void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
85 final WriteConfiguration configurationWriter) {
86 Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance was not closed.");
87 this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, tableTypeRegistry, configurationWriter);
88 this.currentConfiguration = neighbor;
92 public synchronized void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
93 Preconditions.checkState(this.currentConfiguration != null);
94 start(rib, this.currentConfiguration, tableTypeRegistry, null);
98 public synchronized void close() {
99 closeSingletonService();
100 if (this.serviceRegistration != null) {
101 this.serviceRegistration.unregister();
102 this.serviceRegistration = null;
107 public synchronized ListenableFuture<Void> closeServiceInstance() {
108 if (this.bgpPeerSingletonService != null) {
109 return this.bgpPeerSingletonService.closeServiceInstance();
111 return Futures.immediateFuture(null);
114 private void closeSingletonService() {
115 if (this.bgpPeerSingletonService != null) {
117 this.bgpPeerSingletonService.close();
118 this.bgpPeerSingletonService = null;
119 } catch (final Exception e) {
120 LOG.warn("Failed to close peer instance", e);
126 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
127 final AfiSafis actAfiSafi = this.currentConfiguration.getAfiSafis();
128 final AfiSafis extAfiSafi = neighbor.getAfiSafis();
129 final List<AfiSafi> actualSafi = actAfiSafi != null ? actAfiSafi.getAfiSafi() : Collections.emptyList();
130 final List<AfiSafi> extSafi = extAfiSafi != null ? extAfiSafi.getAfiSafi() : Collections.emptyList();
131 return actualSafi.containsAll(extSafi) && extSafi.containsAll(actualSafi)
132 && Objects.equals(this.currentConfiguration.getConfig(), neighbor.getConfig())
133 && Objects.equals(this.currentConfiguration.getNeighborAddress(), neighbor.getNeighborAddress())
134 && Objects.equals(this.currentConfiguration.getAddPaths(), neighbor.getAddPaths())
135 && Objects.equals(this.currentConfiguration.getApplyPolicy(), neighbor.getApplyPolicy())
136 && Objects.equals(this.currentConfiguration.getAsPathOptions(), neighbor.getAsPathOptions())
137 && Objects.equals(this.currentConfiguration.getEbgpMultihop(), neighbor.getEbgpMultihop())
138 && Objects.equals(this.currentConfiguration.getGracefulRestart(), neighbor.getGracefulRestart())
139 && Objects.equals(this.currentConfiguration.getErrorHandling(), neighbor.getErrorHandling())
140 && Objects.equals(this.currentConfiguration.getLoggingOptions(), neighbor.getLoggingOptions())
141 && Objects.equals(this.currentConfiguration.getRouteReflector(), neighbor.getRouteReflector())
142 && Objects.equals(this.currentConfiguration.getState(), neighbor.getState())
143 && Objects.equals(this.currentConfiguration.getTimers(), neighbor.getTimers())
144 && Objects.equals(this.currentConfiguration.getTransport(), neighbor.getTransport());
147 private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
148 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
149 final List<BgpParameters> tlvs = new ArrayList<>();
150 final List<OptionalCapabilities> caps = new ArrayList<>();
151 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(
152 new As4BytesCapabilityBuilder().setAsNumber(rib.getLocalAs()).build()).build()).build());
154 caps.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
155 caps.add(new OptionalCapabilitiesBuilder().setCParameters(MultiprotocolCapabilitiesUtil.RR_CAPABILITY).build());
157 final List<AfiSafi> afiSafi = OpenConfigMappingUtil.getAfiSafiWithDefault(neighbor.getAfiSafis(), false);
158 final List<AddressFamilies> addPathCapability = OpenConfigMappingUtil.toAddPathCapability(afiSafi, tableTypeRegistry);
159 if (!addPathCapability.isEmpty()) {
160 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
161 new CParameters1Builder().setAddPathCapability(
162 new AddPathCapabilityBuilder().setAddressFamilies(addPathCapability).build()).build()).build()).build());
165 final List<BgpTableType> tableTypes = OpenConfigMappingUtil.toTableTypes(afiSafi, tableTypeRegistry);
166 for (final BgpTableType tableType : tableTypes) {
167 if (!rib.getLocalTables().contains(tableType)) {
168 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", tableType);
171 caps.add(new OptionalCapabilitiesBuilder().setCParameters(
172 new CParametersBuilder().addAugmentation(CParameters1.class,
173 new CParameters1Builder().setMultiprotocolCapability(
174 new MultiprotocolCapabilityBuilder(tableType).build()).build()).build()).build());
176 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(caps).build());
180 private static Optional<byte[]> getPassword(final KeyMapping key) {
182 return Optional.of(Iterables.getOnlyElement(key.values()));
184 return Optional.absent();
188 public BGPPeerState getPeerState() {
189 if (this.bgpPeerSingletonService == null) {
192 return this.bgpPeerSingletonService.getPeerState();
195 synchronized void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
196 this.serviceRegistration = serviceRegistration;
199 private final class BgpPeerSingletonService implements BGPPeerStateConsumer, ClusterSingletonService,
201 private final ServiceGroupIdentifier serviceGroupIdentifier;
202 private final boolean activeConnection;
203 private final BGPDispatcher dispatcher;
204 private final InetSocketAddress inetAddress;
205 private final int retryTimer;
206 private final KeyMapping keys;
207 private final WriteConfiguration configurationWriter;
208 private ClusterSingletonServiceRegistration registration;
209 private final BGPPeer bgpPeer;
210 private final IpAddress neighborAddress;
211 private final BGPSessionPreferences prefs;
212 private Future<Void> connection;
214 private boolean isServiceInstantiated;
216 private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor,
217 final BGPTableTypeRegistryConsumer tableTypeRegistry, final WriteConfiguration configurationWriter) {
218 this.neighborAddress = neighbor.getNeighborAddress();
219 final AfiSafis afisSAfis = requireNonNull(neighbor.getAfiSafis());
220 final Set<TablesKey> afiSafisAdvertized = OpenConfigMappingUtil
221 .toTableKey(afisSAfis.getAfiSafi(), tableTypeRegistry);
222 this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib,
223 OpenConfigMappingUtil.toPeerRole(neighbor), getSimpleRoutingPolicy(neighbor), BgpPeer.this.rpcRegistry,
224 afiSafisAdvertized, Collections.emptySet());
225 final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, tableTypeRegistry);
226 final KeyMapping keyMapping = OpenConfigMappingUtil.getNeighborKey(neighbor);
227 this.prefs = new BGPSessionPreferences(rib.getLocalAs(), getHoldTimer(neighbor), rib.getBgpIdentifier(),
228 getPeerAs(neighbor, rib), bgpParameters, getPassword(keyMapping));
229 this.activeConnection = OpenConfigMappingUtil.isActive(neighbor);
230 this.dispatcher = rib.getDispatcher();
231 this.inetAddress = Ipv4Util.toInetSocketAddress(this.neighborAddress, OpenConfigMappingUtil.getPort(neighbor));
232 this.retryTimer = OpenConfigMappingUtil.getRetryTimer(neighbor);
233 this.keys = keyMapping;
234 this.configurationWriter = configurationWriter;
235 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
236 LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier.getValue());
237 //this need to be always the last step
238 this.registration = rib.registerClusterSingletonService(this);
242 public void close() throws Exception {
243 if (this.registration != null) {
244 this.registration.close();
245 this.registration = null;
250 public synchronized void instantiateServiceInstance() {
251 this.isServiceInstantiated = true;
252 if (this.configurationWriter != null) {
253 this.configurationWriter.apply();
255 LOG.info("Peer Singleton Service {} instantiated, Peer {}", getIdentifier().getValue(), this.neighborAddress);
256 this.bgpPeer.instantiateServiceInstance();
257 this.dispatcher.getBGPPeerRegistry().addPeer(this.neighborAddress, this.bgpPeer, this.prefs);
258 if (this.activeConnection) {
259 this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, this.retryTimer, this.keys);
264 public synchronized ListenableFuture<Void> closeServiceInstance() {
265 if (!this.isServiceInstantiated) {
266 LOG.info("Peer Singleton Service {} already closed, Peer {}", getIdentifier().getValue(),
267 this.neighborAddress);
268 return Futures.immediateFuture(null);
270 LOG.info("Close Peer Singleton Service {}, Peer {}", getIdentifier().getValue(), this.neighborAddress);
271 this.isServiceInstantiated = false;
272 if (this.connection != null) {
273 this.connection.cancel(true);
274 this.connection = null;
276 final ListenableFuture<Void> future = this.bgpPeer.close();
277 if (BgpPeer.this.currentConfiguration != null) {
278 this.dispatcher.getBGPPeerRegistry().removePeer(BgpPeer.this.currentConfiguration.getNeighborAddress());
284 public ServiceGroupIdentifier getIdentifier() {
285 return this.serviceGroupIdentifier;
289 public BGPPeerState getPeerState() {
290 return this.bgpPeer.getPeerState();