2 * Copyright (c) 2014 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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import com.google.common.net.InetAddresses;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashSet;
18 import java.util.List;
20 import javax.annotation.concurrent.GuardedBy;
21 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
22 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistration;
23 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistrator;
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.config.yang.bgp.rib.impl.RouteTable;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
30 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
31 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionStatistics;
32 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
33 import org.opendaylight.protocol.bgp.rib.impl.spi.ReusableBGPPeer;
34 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
35 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
36 import org.opendaylight.protocol.bgp.rib.spi.Peer;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
65 public class BGPPeer implements ReusableBGPPeer, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener {
67 private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
70 private final Set<TablesKey> tables = new HashSet<>();
72 private BGPSession session;
74 private byte[] rawIdentifier;
76 private DOMTransactionChain chain;
78 private AdjRibInWriter ribWriter;
80 private final RIB rib;
81 private final String name;
82 private BGPPeerRuntimeRegistrator registrator;
83 private BGPPeerRuntimeRegistration runtimeReg;
84 private long sessionEstablishedCounter = 0L;
86 public BGPPeer(final String name, final RIB rib) {
87 this(name, rib, PeerRole.Ibgp);
90 public BGPPeer(final String name, final RIB rib, final PeerRole role) {
91 this.rib = Preconditions.checkNotNull(rib);
93 this.chain = rib.createPeerChain(this);
94 this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), role, this.chain);
98 public synchronized void close() {
101 // TODO should this perform cleanup ?
105 public void onMessage(final BGPSession session, final Notification msg) {
106 if (!(msg instanceof Update)) {
107 LOG.info("Ignoring unhandled message class {}", msg.getClass());
110 final Update message = (Update) msg;
113 final Attributes attrs = message.getAttributes();
114 MpReachNlri mpReach = null;
115 if (message.getNlri() != null) {
116 mpReach = prefixesToMpReach(message);
117 } else if (attrs != null && attrs.getAugmentation(Attributes1.class) != null) {
118 mpReach = attrs.getAugmentation(Attributes1.class).getMpReachNlri();
120 if (mpReach != null) {
121 this.ribWriter.updateRoutes(mpReach, attrs);
124 MpUnreachNlri mpUnreach = null;
125 if (message.getWithdrawnRoutes() != null) {
126 mpUnreach = prefixesToMpUnreach(message);
127 } else if (attrs != null && attrs.getAugmentation(Attributes2.class) != null) {
128 mpUnreach = attrs.getAugmentation(Attributes2.class).getMpUnreachNlri();
130 if (mpUnreach != null) {
131 this.ribWriter.removeRoutes(mpUnreach);
136 * Creates MPReach for the prefixes to be handled in the same way as linkstate routes
138 * @param message Update message containing prefixes in NLRI
139 * @return MpReachNlri with prefixes from the nlri field
141 private MpReachNlri prefixesToMpReach(final Update message) {
142 final List<Ipv4Prefixes> prefixes = new ArrayList<>();
143 for (final Ipv4Prefix p : message.getNlri().getNlri()) {
144 prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
146 final MpReachNlriBuilder b = new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
147 UnicastSubsequentAddressFamily.class).setAdvertizedRoutes(
148 new AdvertizedRoutesBuilder().setDestinationType(
149 new DestinationIpv4CaseBuilder().setDestinationIpv4(
150 new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build());
151 if (message.getAttributes() != null) {
152 b.setCNextHop(message.getAttributes().getCNextHop());
158 * Create MPUnreach for the prefixes to be handled in the same way as linkstate routes
160 * @param message Update message containing withdrawn routes
161 * @return MpUnreachNlri with prefixes from the withdrawn routes field
163 private MpUnreachNlri prefixesToMpUnreach(final Update message) {
164 final List<Ipv4Prefixes> prefixes = new ArrayList<>();
165 for (final Ipv4Prefix p : message.getWithdrawnRoutes().getWithdrawnRoutes()) {
166 prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
168 return new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
169 new WithdrawnRoutesBuilder().setDestinationType(
170 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
171 new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build()).build();
175 public synchronized void onSessionUp(final BGPSession session) {
176 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
177 this.session = session;
178 this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
180 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
181 final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
182 this.tables.add(key);
184 // not particularly nice
185 if (session instanceof BGPSessionImpl) {
186 AdjRibOutListener.create(key, this.rib.getYangRibId(), ((RIBImpl)this.rib).getService(), this.rib.getRibSupportContext(), ((BGPSessionImpl) session).getLimiter());
189 this.ribWriter = this.ribWriter.transform(RouterIds.createPeerId(session.getBgpId()), this.rib.getRibSupportContext(), this.tables, false);
190 this.sessionEstablishedCounter++;
191 if (this.registrator != null) {
192 this.runtimeReg = this.registrator.register(this);
196 private synchronized void cleanup() {
197 // FIXME: BUG-196: support graceful restart
198 this.ribWriter.cleanTables(this.tables);
203 public void onSessionDown(final BGPSession session, final Exception e) {
204 LOG.info("Session with peer {} went down", this.name, e);
209 public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
210 LOG.info("Session with peer {} terminated: {}", this.name, cause);
215 public String toString() {
216 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
219 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
220 toStringHelper.add("name", this.name);
221 toStringHelper.add("tables", this.tables);
222 return toStringHelper;
226 public String getName() {
231 public void releaseConnection() {
237 private void dropConnection() {
238 if (this.runtimeReg != null) {
239 this.runtimeReg.close();
240 this.runtimeReg = null;
242 if (this.session != null) {
243 this.session.close();
249 public boolean isSessionActive() {
250 return this.session != null;
254 public synchronized byte[] getRawIdentifier() {
255 return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
259 public void resetSession() {
264 public void resetStats() {
265 if (this.session instanceof BGPSessionStatistics) {
266 ((BGPSessionStatistics) this.session).resetSessionStats();
270 public synchronized void registerRootRuntimeBean(final BGPPeerRuntimeRegistrator registrator) {
271 this.registrator = registrator;
275 public BgpSessionState getBgpSessionState() {
276 if (this.session instanceof BGPSessionStatistics) {
277 return ((BGPSessionStatistics) this.session).getBgpSesionState();
279 return new BgpSessionState();
283 public synchronized BgpPeerState getBgpPeerState() {
284 final BgpPeerState peerState = new BgpPeerState();
285 final List<RouteTable> routes = Lists.newArrayList();
286 for (final TablesKey tablesKey : this.tables) {
287 final RouteTable routeTable = new RouteTable();
288 routeTable.setTableType("afi=" + tablesKey.getAfi().getSimpleName() + ",safi=" + tablesKey.getSafi().getSimpleName());
289 routeTable.setRoutesCount(this.rib.getRoutesCount(tablesKey));
290 routes.add(routeTable);
292 peerState.setRouteTable(routes);
293 peerState.setSessionEstablishedCount(this.sessionEstablishedCounter);
298 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
299 LOG.error("Transaction chain failed.", cause);
300 this.dropConnection();
302 this.chain = this.rib.createPeerChain(this);
306 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
307 LOG.debug("Transaction chain {} successfull.", chain);
311 public void markUptodate(final TablesKey tablesKey) {
312 this.ribWriter.markTableUptodate(tablesKey);