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 org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getHoldTimer;
12 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getPeerAs;
13 import static org.opendaylight.protocol.bgp.rib.impl.config.OpenConfigMappingUtil.getSimpleRoutingPolicy;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.collect.Iterables;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import io.netty.util.concurrent.Future;
20 import java.net.InetSocketAddress;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Objects;
26 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
28 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
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, BGPPeerRuntimeMXBean {
69 private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
71 private final RpcProviderRegistry rpcRegistry;
72 private ServiceRegistration<?> serviceRegistration;
73 private Neighbor currentConfiguration;
74 private BgpPeerSingletonService bgpPeerSingletonService;
76 public BgpPeer(final RpcProviderRegistry rpcRegistry) {
77 this.rpcRegistry = rpcRegistry;
81 public void start(final RIB rib, final Neighbor neighbor, final BGPTableTypeRegistryConsumer tableTypeRegistry,
82 final WriteConfiguration configurationWriter) {
83 Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance was not closed.");
84 this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, tableTypeRegistry, configurationWriter);
85 this.currentConfiguration = neighbor;
89 public void restart(final RIB rib, final BGPTableTypeRegistryConsumer tableTypeRegistry) {
90 Preconditions.checkState(this.currentConfiguration != null);
91 start(rib, this.currentConfiguration, tableTypeRegistry, null);
96 closeSingletonService();
97 if (this.serviceRegistration != null) {
98 this.serviceRegistration.unregister();
99 this.serviceRegistration = null;
103 private void closeSingletonService() {
105 this.bgpPeerSingletonService.close();
106 this.bgpPeerSingletonService = null;
107 } catch (final Exception e) {
108 LOG.warn("Failed to close peer instance", e);
113 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
114 final AfiSafis actAfiSafi = this.currentConfiguration.getAfiSafis();
115 final AfiSafis extAfiSafi = neighbor.getAfiSafis();
116 final List<AfiSafi> actualSafi = actAfiSafi != null ? actAfiSafi.getAfiSafi() : Collections.emptyList();
117 final List<AfiSafi> extSafi = extAfiSafi != null ? extAfiSafi.getAfiSafi() : Collections.emptyList();
118 return actualSafi.containsAll(extSafi) && extSafi.containsAll(actualSafi)
119 && Objects.equals(this.currentConfiguration.getConfig(), neighbor.getConfig())
120 && Objects.equals(this.currentConfiguration.getNeighborAddress(), neighbor.getNeighborAddress())
121 && Objects.equals(this.currentConfiguration.getAddPaths(),neighbor.getAddPaths())
122 && Objects.equals(this.currentConfiguration.getApplyPolicy(), neighbor.getApplyPolicy())
123 && Objects.equals(this.currentConfiguration.getAsPathOptions(), neighbor.getAsPathOptions())
124 && Objects.equals(this.currentConfiguration.getEbgpMultihop(), neighbor.getEbgpMultihop())
125 && Objects.equals(this.currentConfiguration.getGracefulRestart(), neighbor.getGracefulRestart())
126 && Objects.equals(this.currentConfiguration.getErrorHandling(), neighbor.getErrorHandling())
127 && Objects.equals(this.currentConfiguration.getLoggingOptions(), neighbor.getLoggingOptions())
128 && Objects.equals(this.currentConfiguration.getRouteReflector(), neighbor.getRouteReflector())
129 && Objects.equals(this.currentConfiguration.getState(), neighbor.getState())
130 && Objects.equals(this.currentConfiguration.getTimers(), neighbor.getTimers())
131 && Objects.equals(this.currentConfiguration.getTransport(), neighbor.getTransport());
134 private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
135 final BGPTableTypeRegistryConsumer tableTypeRegistry) {
136 final List<BgpParameters> tlvs = new ArrayList<>();
137 final List<OptionalCapabilities> caps = new ArrayList<>();
138 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(
139 new As4BytesCapabilityBuilder().setAsNumber(rib.getLocalAs()).build()).build()).build());
141 caps.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
142 caps.add(new OptionalCapabilitiesBuilder().setCParameters(MultiprotocolCapabilitiesUtil.RR_CAPABILITY).build());
144 final List<AfiSafi> afiSafi = OpenConfigMappingUtil.getAfiSafiWithDefault(neighbor.getAfiSafis(), false);
145 final List<AddressFamilies> addPathCapability = OpenConfigMappingUtil.toAddPathCapability(afiSafi, tableTypeRegistry);
146 if (!addPathCapability.isEmpty()) {
147 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
148 new CParameters1Builder().setAddPathCapability(
149 new AddPathCapabilityBuilder().setAddressFamilies(addPathCapability).build()).build()).build()).build());
152 final List<BgpTableType> tableTypes = OpenConfigMappingUtil.toTableTypes(afiSafi, tableTypeRegistry);
153 for (final BgpTableType tableType : tableTypes) {
154 if (!rib.getLocalTables().contains(tableType)) {
155 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", tableType);
158 caps.add(new OptionalCapabilitiesBuilder().setCParameters(
159 new CParametersBuilder().addAugmentation(CParameters1.class,
160 new CParameters1Builder().setMultiprotocolCapability(
161 new MultiprotocolCapabilityBuilder(tableType).build()).build()).build()).build());
163 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(caps).build());
167 private static Optional<byte[]> getPassword(final KeyMapping key) {
169 return Optional.of(Iterables.getOnlyElement(key.values()));
171 return Optional.absent();
175 public BgpPeerState getBgpPeerState() {
176 return this.bgpPeerSingletonService.getPeer().getBgpPeerState();
180 public BgpSessionState getBgpSessionState() {
181 return this.bgpPeerSingletonService.getPeer().getBgpSessionState();
185 public void resetSession() {
186 this.bgpPeerSingletonService.getPeer().resetSession();
190 public void resetStats() {
191 this.bgpPeerSingletonService.getPeer().resetStats();
195 public BGPPeerState getPeerState() {
196 if (this.bgpPeerSingletonService == null) {
199 return this.bgpPeerSingletonService.getPeerState();
202 void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
203 this.serviceRegistration = serviceRegistration;
206 private final class BgpPeerSingletonService implements BGPPeerStateConsumer, ClusterSingletonService,
208 private final ServiceGroupIdentifier serviceGroupIdentifier;
209 private final boolean activeConnection;
210 private final BGPDispatcher dispatcher;
211 private final InetSocketAddress inetAddress;
212 private final int retryTimer;
213 private final Optional<KeyMapping> key;
214 private final WriteConfiguration configurationWriter;
215 private ClusterSingletonServiceRegistration registration;
216 private final BGPPeer bgpPeer;
217 private final IpAddress neighborAddress;
218 private final BGPSessionPreferences prefs;
219 private Future<Void> connection;
221 private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor,
222 final BGPTableTypeRegistryConsumer tableTypeRegistry, final WriteConfiguration configurationWriter) {
223 this.neighborAddress = neighbor.getNeighborAddress();
224 final AfiSafis afisSAfis = Preconditions.checkNotNull(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), BgpPeer.this.rpcRegistry,
229 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.key = Optional.fromNullable(keyMapping);
239 this.configurationWriter = configurationWriter;
240 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
241 LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier.getValue());
242 //this need to be always the last step
243 this.registration = rib.registerClusterSingletonService(this);
247 public void close() throws Exception {
248 if (this.registration != null) {
249 this.registration.close();
250 this.registration = null;
255 public void instantiateServiceInstance() {
256 if(this.configurationWriter != null) {
257 this.configurationWriter.apply();
259 LOG.info("Peer Singleton Service {} instantiated", getIdentifier().getValue());
260 this.bgpPeer.instantiateServiceInstance();
261 this.dispatcher.getBGPPeerRegistry().addPeer(this.neighborAddress, this.bgpPeer, this.prefs);
262 if (this.activeConnection) {
263 this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, this.retryTimer, this.key);
268 public ListenableFuture<Void> closeServiceInstance() {
269 LOG.info("Close Peer Singleton Service {}", getIdentifier().getValue());
270 if (this.connection != null) {
271 this.connection.cancel(true);
272 this.connection = null;
274 final ListenableFuture<Void> future = this.bgpPeer.close();
275 if(BgpPeer.this.currentConfiguration != null) {
276 this.dispatcher.getBGPPeerRegistry().removePeer(BgpPeer.this.currentConfiguration.getNeighborAddress());
282 public ServiceGroupIdentifier getIdentifier() {
283 return this.serviceGroupIdentifier;
286 BGPPeerRuntimeMXBean getPeer() {
291 public BGPPeerState getPeerState() {
292 return this.bgpPeer.getPeerState();