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;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.collect.Iterables;
17 import com.google.common.util.concurrent.Futures;
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.List;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
24 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
25 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
28 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
29 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
30 import org.opendaylight.protocol.bgp.openconfig.spi.BGPOpenConfigMappingService;
31 import org.opendaylight.protocol.bgp.parser.BgpExtendedMessageUtil;
32 import org.opendaylight.protocol.bgp.parser.spi.MultiprotocolCapabilitiesUtil;
33 import org.opendaylight.protocol.bgp.rib.impl.BGPPeer;
34 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPDispatcher;
35 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPPeerRegistry;
36 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
37 import org.opendaylight.protocol.bgp.rib.impl.spi.BgpDeployer.WriteConfiguration;
38 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
39 import org.opendaylight.protocol.concepts.KeyMapping;
40 import org.opendaylight.protocol.util.Ipv4Util;
41 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.bgp.neighbors.Neighbor;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParameters;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.BgpParametersBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilities;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.OptionalCapabilitiesBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.CParametersBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.open.message.bgp.parameters.optional.capabilities.c.parameters.As4BytesCapabilityBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.CParameters1Builder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.AddPathCapabilityBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.MultiprotocolCapabilityBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
55 import org.osgi.framework.ServiceRegistration;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 public class BgpPeer implements PeerBean, BGPPeerRuntimeMXBean {
62 private static final Logger LOG = LoggerFactory.getLogger(BgpPeer.class);
64 private final RpcProviderRegistry rpcRegistry;
65 private final BGPPeerRegistry peerRegistry;
66 private ServiceRegistration<?> serviceRegistration;
67 private Neighbor currentConfiguration;
68 private BgpPeerSingletonService bgpPeerSingletonService;
70 public BgpPeer(final RpcProviderRegistry rpcRegistry, final BGPPeerRegistry peerRegistry) {
71 this.rpcRegistry = rpcRegistry;
72 this.peerRegistry = peerRegistry;
76 public void start(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
77 final WriteConfiguration configurationWriter) {
78 Preconditions.checkState(this.bgpPeerSingletonService == null, "Previous peer instance {} was not closed.");
79 this.bgpPeerSingletonService = new BgpPeerSingletonService(rib, neighbor, mappingService, configurationWriter);
80 this.currentConfiguration = neighbor;
84 public void restart(final RIB rib, final BGPOpenConfigMappingService mappingService) {
85 Preconditions.checkState(this.currentConfiguration != null);
86 start(rib, this.currentConfiguration, mappingService, null);
92 this.bgpPeerSingletonService.close();
93 this.bgpPeerSingletonService = null;
94 } catch (final Exception e) {
95 LOG.warn("Failed to close peer instance", e);
97 this.currentConfiguration = null;
98 if (this.serviceRegistration != null) {
99 this.serviceRegistration.unregister();
100 this.serviceRegistration = null;
105 public Boolean containsEqualConfiguration(final Neighbor neighbor) {
106 return this.currentConfiguration.equals(neighbor);
109 private static List<BgpParameters> getBgpParameters(final Neighbor neighbor, final RIB rib,
110 final BGPOpenConfigMappingService mappingService) {
111 final List<BgpParameters> tlvs = new ArrayList<>();
112 final List<OptionalCapabilities> caps = new ArrayList<>();
113 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().setAs4BytesCapability(
114 new As4BytesCapabilityBuilder().setAsNumber(rib.getLocalAs()).build()).build()).build());
116 caps.add(new OptionalCapabilitiesBuilder().setCParameters(BgpExtendedMessageUtil.EXTENDED_MESSAGE_CAPABILITY).build());
117 caps.add(new OptionalCapabilitiesBuilder().setCParameters(MultiprotocolCapabilitiesUtil.RR_CAPABILITY).build());
119 final List<AddressFamilies> addPathCapability = mappingService.toAddPathCapability(neighbor.getAfiSafis().getAfiSafi());
120 if (!addPathCapability.isEmpty()) {
121 caps.add(new OptionalCapabilitiesBuilder().setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class,
122 new CParameters1Builder().setAddPathCapability(
123 new AddPathCapabilityBuilder().setAddressFamilies(addPathCapability).build()).build()).build()).build());
126 final List<BgpTableType> tableTypes = mappingService.toTableTypes(neighbor.getAfiSafis().getAfiSafi());
127 for (final BgpTableType tableType : tableTypes) {
128 if (!rib.getLocalTables().contains(tableType)) {
129 LOG.info("RIB instance does not list {} in its local tables. Incoming data will be dropped.", tableType);
132 caps.add(new OptionalCapabilitiesBuilder().setCParameters(
133 new CParametersBuilder().addAugmentation(CParameters1.class,
134 new CParameters1Builder().setMultiprotocolCapability(
135 new MultiprotocolCapabilityBuilder(tableType).build()).build()).build()).build());
137 tlvs.add(new BgpParametersBuilder().setOptionalCapabilities(caps).build());
141 private static Optional<byte[]> getPassword(final KeyMapping key) {
143 return Optional.of(Iterables.getOnlyElement(key.values()));
145 return Optional.absent();
149 public BgpPeerState getBgpPeerState() {
150 return this.bgpPeerSingletonService.getPeer().getBgpPeerState();
154 public BgpSessionState getBgpSessionState() {
155 return this.bgpPeerSingletonService.getPeer().getBgpSessionState();
159 public void resetSession() {
160 this.bgpPeerSingletonService.getPeer().resetSession();
164 public void resetStats() {
165 this.bgpPeerSingletonService.getPeer().resetStats();
168 void setServiceRegistration(final ServiceRegistration<?> serviceRegistration) {
169 this.serviceRegistration = serviceRegistration;
172 private final class BgpPeerSingletonService implements ClusterSingletonService, AutoCloseable {
173 private final ServiceGroupIdentifier serviceGroupIdentifier;
174 private final boolean activeConnection;
175 private final BGPDispatcher dispatcher;
176 private final InetSocketAddress inetAddress;
177 private final int retryTimer;
178 private final Optional<KeyMapping> key;
179 private final WriteConfiguration configurationWriter;
180 private ClusterSingletonServiceRegistration registration;
181 private final BGPPeer bgpPeer;
182 private final IpAddress neighborAddress;
183 private final BGPSessionPreferences prefs;
184 private Future<Void> connection;
186 private BgpPeerSingletonService(final RIB rib, final Neighbor neighbor, final BGPOpenConfigMappingService mappingService,
187 final WriteConfiguration configurationWriter) {
188 this.neighborAddress = neighbor.getNeighborAddress();
189 this.bgpPeer = new BGPPeer(Ipv4Util.toStringIP(this.neighborAddress), rib, mappingService.toPeerRole(neighbor), rpcRegistry);
190 final List<BgpParameters> bgpParameters = getBgpParameters(neighbor, rib, mappingService);
191 final KeyMapping key = OpenConfigMappingUtil.getNeighborKey(neighbor);
192 this.prefs = new BGPSessionPreferences(rib.getLocalAs(), getHoldTimer(neighbor), rib.getBgpIdentifier(), getPeerAs(neighbor, rib),
193 bgpParameters, getPassword(key));
194 this.activeConnection = OpenConfigMappingUtil.isActive(neighbor);
195 this.dispatcher = rib.getDispatcher();
196 this.inetAddress = Ipv4Util.toInetSocketAddress(this.neighborAddress, OpenConfigMappingUtil.getPort(neighbor));
197 this.retryTimer = OpenConfigMappingUtil.getRetryTimer(neighbor);
198 this.key = Optional.fromNullable(key);
199 this.serviceGroupIdentifier = rib.getRibIServiceGroupIdentifier();
200 LOG.info("Peer Singleton Service {} registered", this.serviceGroupIdentifier);
201 this.registration = rib.registerClusterSingletonService(this);
202 this.configurationWriter = configurationWriter;
206 public void close() throws Exception {
207 if (this.registration != null) {
208 this.registration.close();
209 this.registration = null;
214 public void instantiateServiceInstance() {
215 if(this.configurationWriter != null) {
216 this.configurationWriter.apply();
218 LOG.info("Peer Singleton Service {} instantiated", getIdentifier());
219 this.bgpPeer.instantiateServiceInstance();
220 peerRegistry.addPeer(this.neighborAddress, this.bgpPeer, prefs);
221 if (this.activeConnection) {
222 this.connection = this.dispatcher.createReconnectingClient(this.inetAddress, peerRegistry, this.retryTimer, this.key);
227 public ListenableFuture<Void> closeServiceInstance() {
228 LOG.info("Close RIB Singleton Service {}", this.getIdentifier());
229 if (this.connection != null) {
230 this.connection.cancel(true);
231 this.connection = null;
233 this.bgpPeer.close();
234 if(currentConfiguration != null) {
235 peerRegistry.removePeer(currentConfiguration.getNeighborAddress());
237 return Futures.immediateFuture(null);
241 public ServiceGroupIdentifier getIdentifier() {
242 return this.serviceGroupIdentifier;
245 BGPPeerRuntimeMXBean getPeer() {