Bug-6468: Duplicate cleanup and connected-peer-counter decrement during BGP session...
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import static org.opendaylight.protocol.bgp.rib.impl.AdjRibInWriter.isAnnounceNone;
11 import static org.opendaylight.protocol.bgp.rib.impl.AdjRibInWriter.isLearnNone;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.Preconditions;
16 import com.google.common.net.InetAddresses;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26 import javax.annotation.concurrent.GuardedBy;
27 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeMXBean;
28 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistration;
29 import org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerRuntimeRegistrator;
30 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpPeerState;
31 import org.opendaylight.controller.config.yang.bgp.rib.impl.BgpSessionState;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
35 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
36 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
37 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
38 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
39 import org.opendaylight.protocol.bgp.parser.BGPError;
40 import org.opendaylight.protocol.bgp.parser.impl.message.update.LocalPreferenceAttributeParser;
41 import org.opendaylight.protocol.bgp.parser.spi.MessageUtil;
42 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
43 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
44 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStats;
45 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPPeerStatsImpl;
46 import org.opendaylight.protocol.bgp.rib.impl.stats.peer.BGPSessionStats;
47 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
48 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
49 import org.opendaylight.protocol.bgp.rib.spi.BGPTerminationReason;
50 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
51 import org.opendaylight.protocol.bgp.rib.spi.Peer;
52 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
57 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;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.RouteRefresh;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.mp.capabilities.add.path.capability.AddressFamilies;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.BgpPeerRpcService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.peer.rpc.rev160322.PeerContext;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.SimpleRoutingPolicy;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.PeerKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
80 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
81 import org.opendaylight.yangtools.yang.binding.Notification;
82 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85
86 /**
87  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
88  * RIB actions.
89  */
90 public class BGPPeer implements BGPSessionListener, Peer, AutoCloseable, BGPPeerRuntimeMXBean, TransactionChainListener {
91
92     private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
93
94     @GuardedBy("this")
95     private final Set<TablesKey> tables = new HashSet<>();
96     @GuardedBy("this")
97     private BGPSession session;
98     @GuardedBy("this")
99     private byte[] rawIdentifier;
100     @GuardedBy("this")
101     private DOMTransactionChain chain;
102     @GuardedBy("this")
103     private AdjRibInWriter ribWriter;
104     @GuardedBy("this")
105     private EffectiveRibInWriter effRibInWriter;
106
107     private final RIB rib;
108     private final String name;
109     private BGPPeerRuntimeRegistrator registrator;
110     private BGPPeerRuntimeRegistration runtimeReg;
111     private final Map<TablesKey, AdjRibOutListener> adjRibOutListenerSet = new HashMap();
112     private final RpcProviderRegistry rpcRegistry;
113     private RoutedRpcRegistration<BgpPeerRpcService> rpcRegistration;
114     private final PeerRole peerRole;
115     private final Optional<SimpleRoutingPolicy> simpleRoutingPolicy;
116     private final BGPPeerStats peerStats;
117
118     public BGPPeer(final String name, final RIB rib, final PeerRole role, final SimpleRoutingPolicy peerStatus, final RpcProviderRegistry rpcRegistry) {
119         this.peerRole = role;
120         this.simpleRoutingPolicy = Optional.ofNullable(peerStatus);
121         this.rib = Preconditions.checkNotNull(rib);
122         this.name = name;
123         this.rpcRegistry = rpcRegistry;
124         this.peerStats = new BGPPeerStatsImpl(this.name, this.tables);
125
126         // add current peer to "configured BGP peer" stats
127         this.rib.getRenderStats().getConfiguredPeerCounter().increaseCount();
128         this.chain = rib.createPeerChain(this);
129     }
130
131     public BGPPeer(final String name, final RIB rib, final PeerRole role, final RpcProviderRegistry rpcRegistry) {
132         this(name, rib, role, null, rpcRegistry);
133     }
134
135     public void instantiateServiceInstance() {
136         this.ribWriter = AdjRibInWriter.create(rib.getYangRibId(), this.peerRole, this.simpleRoutingPolicy, this.chain);
137     }
138
139     @Override
140     public synchronized void close() {
141         releaseConnection();
142         this.chain.close();
143     }
144
145     @Override
146     public void onMessage(final BGPSession session, final Notification msg) throws BGPDocumentedException {
147         if (!(msg instanceof Update) && !(msg instanceof RouteRefresh)) {
148             LOG.info("Ignoring unhandled message class {}", msg.getClass());
149             return;
150         }
151         if (msg instanceof Update) {
152             onUpdateMessage((Update) msg);
153         } else {
154             onRouteRefreshMessage((RouteRefresh) msg, session);
155         }
156     }
157
158     private void onRouteRefreshMessage(final RouteRefresh message, final BGPSession session) {
159         final Class<? extends AddressFamily> rrAfi = message.getAfi();
160         final Class<? extends SubsequentAddressFamily> rrSafi = message.getSafi();
161
162         final TablesKey key = new TablesKey(rrAfi, rrSafi);
163         final AdjRibOutListener listener = this.adjRibOutListenerSet.get(key);
164         if (listener != null) {
165             listener.close();
166             this.adjRibOutListenerSet.remove(key);
167             createAdjRibOutListener(RouterIds.createPeerId(session.getBgpId()), key, listener.isMpSupported());
168         } else {
169             LOG.info("Ignoring RouteRefresh message. Afi/Safi is not supported: {}, {}.", rrAfi, rrSafi);
170         }
171     }
172
173     /**
174      * Check for presence of well known mandatory attribute LOCAL_PREF in Update message
175      *
176      * @param message Update message
177      * @throws BGPDocumentedException
178      */
179     private void checkMandatoryAttributesPresence(final Update message) throws BGPDocumentedException {
180         if (MessageUtil.isAnyNlriPresent(message)) {
181             final Attributes attrs = message.getAttributes();
182             if (this.peerRole == PeerRole.Ibgp && (attrs == null || attrs.getLocalPref() == null)) {
183                 throw new BGPDocumentedException(BGPError.MANDATORY_ATTR_MISSING_MSG + "LOCAL_PREF",
184                         BGPError.WELL_KNOWN_ATTR_MISSING,
185                         new byte[] { LocalPreferenceAttributeParser.TYPE });
186             }
187         }
188     }
189
190     /**
191      * Process Update message received.
192      * Calls {@link #checkMandatoryAttributesPresence(Update)} to check for presence of mandatory attributes.
193      *
194      * @param message Update message
195      * @throws BGPDocumentedException
196      */
197     private void onUpdateMessage(final Update message) throws BGPDocumentedException {
198         checkMandatoryAttributesPresence(message);
199
200         // update AdjRibs
201         final Attributes attrs = message.getAttributes();
202         MpReachNlri mpReach = null;
203         final boolean isAnyNlriAnnounced = message.getNlri() != null;
204         if (isAnyNlriAnnounced) {
205             mpReach = prefixesToMpReach(message);
206         } else {
207             mpReach = MessageUtil.getMpReachNlri(attrs);
208         }
209         if (mpReach != null) {
210             this.ribWriter.updateRoutes(mpReach, nextHopToAttribute(attrs, mpReach));
211         }
212         MpUnreachNlri mpUnreach = null;
213         if (message.getWithdrawnRoutes() != null) {
214             mpUnreach = prefixesToMpUnreach(message, isAnyNlriAnnounced);
215         } else {
216             mpUnreach = MessageUtil.getMpUnreachNlri(attrs);
217         }
218         if (mpUnreach != null) {
219             this.ribWriter.removeRoutes(mpUnreach);
220         }
221     }
222
223     private static Attributes nextHopToAttribute(final Attributes attrs, final MpReachNlri mpReach) {
224         if (attrs.getCNextHop() == null && mpReach.getCNextHop() != null) {
225             final AttributesBuilder attributesBuilder = new AttributesBuilder(attrs);
226             attributesBuilder.setCNextHop(mpReach.getCNextHop());
227             return attributesBuilder.build();
228         }
229         return attrs;
230     }
231
232     /**
233      * Creates MPReach for the prefixes to be handled in the same way as linkstate routes
234      *
235      * @param message Update message containing prefixes in NLRI
236      * @return MpReachNlri with prefixes from the nlri field
237      */
238     private static MpReachNlri prefixesToMpReach(final Update message) {
239         final List<Ipv4Prefixes> prefixes = new ArrayList<>();
240         for (final Ipv4Prefix p : message.getNlri().getNlri()) {
241             prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
242         }
243         final MpReachNlriBuilder b = new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(
244                 UnicastSubsequentAddressFamily.class).setAdvertizedRoutes(
245                         new AdvertizedRoutesBuilder().setDestinationType(
246                                 new DestinationIpv4CaseBuilder().setDestinationIpv4(
247                                         new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build());
248         if (message.getAttributes() != null) {
249             b.setCNextHop(message.getAttributes().getCNextHop());
250         }
251         return b.build();
252     }
253
254     /**
255      * Create MPUnreach for the prefixes to be handled in the same way as linkstate routes
256      *
257      * @param message Update message containing withdrawn routes
258      * @param isAnyNlriAnnounced
259      * @return MpUnreachNlri with prefixes from the withdrawn routes field
260      */
261     private static MpUnreachNlri prefixesToMpUnreach(final Update message, final boolean isAnyNlriAnnounced) {
262         final List<Ipv4Prefixes> prefixes = new ArrayList<>();
263         for (final Ipv4Prefix p : message.getWithdrawnRoutes().getWithdrawnRoutes()) {
264             boolean nlriAnounced = false;
265             if(isAnyNlriAnnounced) {
266                 nlriAnounced = message.getNlri().getNlri().contains(p);
267             }
268
269             if(!nlriAnounced) {
270                 prefixes.add(new Ipv4PrefixesBuilder().setPrefix(p).build());
271             }
272         }
273         return new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
274                 new WithdrawnRoutesBuilder().setDestinationType(
275                         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(
276                                 new DestinationIpv4Builder().setIpv4Prefixes(prefixes).build()).build()).build()).build();
277     }
278
279     @Override
280     public synchronized void onSessionUp(final BGPSession session) {
281         final List<AddressFamilies> addPathTablesType = session.getAdvertisedAddPathTableTypes();
282         LOG.info("Session with peer {} went up with tables {} and Add Path tables {}", this.name, addPathTablesType,
283                 session.getAdvertisedAddPathTableTypes());
284         this.session = session;
285
286         this.rawIdentifier = InetAddresses.forString(session.getBgpId().getValue()).getAddress();
287         final PeerId peerId = RouterIds.createPeerId(session.getBgpId());
288
289         this.tables.addAll(this.session.getAdvertisedTableTypes().stream().map(t -> new TablesKey(t.getAfi(), t.getSafi())).collect(Collectors.toList()));
290         final boolean announceNone = isAnnounceNone(this.simpleRoutingPolicy);
291         if(!announceNone) {
292             createAdjRibOutListener(peerId);
293         }
294         addBgp4Support(peerId, announceNone);
295
296         if(!isLearnNone(this.simpleRoutingPolicy)) {
297             final YangInstanceIdentifier peerIId = this.rib.getYangRibId().node(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer.QNAME).node(IdentifierUtils.domPeerId(peerId));
298             this.effRibInWriter = EffectiveRibInWriter.create(this.rib.getService(), this.rib.createPeerChain(this), peerIId, this.rib.getImportPolicyPeerTracker(),
299                     this.rib.getRibSupportContext(), this.peerRole, this.peerStats.getEffectiveRibInRouteCounters(), this.peerStats.getAdjRibInRouteCounters());
300         }
301         this.ribWriter = this.ribWriter.transform(peerId, this.rib.getRibSupportContext(), this.tables, addPathTablesType);
302
303         // register BGP Peer stats
304         this.peerStats.getSessionEstablishedCounter().increaseCount();
305         if (this.registrator != null) {
306             this.runtimeReg = this.registrator.register(this);
307         }
308
309         if (this.rpcRegistry != null) {
310             this.rpcRegistration = this.rpcRegistry.addRoutedRpcImplementation(BgpPeerRpcService.class, new BgpPeerRpc(session, this.tables));
311             final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer, PeerKey> path =
312                     this.rib.getInstanceIdentifier().child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer.class, new PeerKey(peerId));
313             this.rpcRegistration.registerPath(PeerContext.class, path);
314         }
315
316         this.rib.getRenderStats().getConnectedPeerCounter().increaseCount();
317     }
318
319     private void createAdjRibOutListener(final PeerId peerId) {
320         this.tables.forEach(key->createAdjRibOutListener(peerId, key, true));
321     }
322
323     //try to add a support for old-school BGP-4, if peer did not advertise IPv4-Unicast MP capability
324     private void addBgp4Support(final PeerId peerId, final boolean announceNone) {
325         final TablesKey key = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
326         if (this.tables.add(key) && !announceNone) {
327             createAdjRibOutListener(peerId, key, false);
328         }
329     }
330
331     private void createAdjRibOutListener(final PeerId peerId, final TablesKey key, final boolean mpSupport) {
332         final RIBSupportContext context = this.rib.getRibSupportContext().getRIBSupportContext(key);
333
334         // not particularly nice
335         if (context != null && this.session instanceof BGPSessionImpl) {
336             this.adjRibOutListenerSet.put(key, AdjRibOutListener.create(peerId, key, this.rib.getYangRibId(), this.rib.getCodecsRegistry(),
337                     context.getRibSupport(), this.rib.getService(), ((BGPSessionImpl) this.session).getLimiter(), mpSupport, this.peerStats.getAdjRibOutRouteCounters().init(key)));
338         }
339     }
340
341     private void cleanup() {
342         // FIXME: BUG-196: support graceful
343         this.adjRibOutListenerSet.values().forEach(AdjRibOutListener::close);
344         this.adjRibOutListenerSet.clear();
345         if (this.effRibInWriter != null) {
346             this.effRibInWriter.close();
347         }
348         if(this.ribWriter != null) {
349             this.ribWriter.removePeer();
350         }
351         this.tables.clear();
352     }
353
354     @Override
355     public void onSessionDown(final BGPSession session, final Exception e) {
356         if(e.getMessage().equals(BGPSessionImpl.END_OF_INPUT)) {
357             LOG.info("Session with peer {} went down", this.name);
358         } else {
359             LOG.info("Session with peer {} went down", this.name, e);
360         }
361         releaseConnection();
362     }
363
364     @Override
365     public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
366         LOG.info("Session with peer {} terminated: {}", this.name, cause);
367         releaseConnection();
368     }
369
370     @Override
371     public String toString() {
372         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
373     }
374
375     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
376         toStringHelper.add("name", this.name);
377         toStringHelper.add("tables", this.tables);
378         return toStringHelper;
379     }
380
381     @Override
382     public String getName() {
383         return this.name;
384     }
385
386     @Override
387     @GuardedBy("this")
388     public synchronized void releaseConnection() {
389         if (this.rpcRegistration != null) {
390             this.rpcRegistration.close();
391         }
392         addPeerToDisconnectedSharedList();
393         cleanup();
394         dropConnection();
395     }
396
397     private void addPeerToDisconnectedSharedList() {
398         if(this.session != null) {
399             this.rib.getCacheDisconnectedPeers().insertDesconectedPeer(this.session.getBgpId());
400         }
401     }
402
403     private void dropConnection() {
404         if (this.runtimeReg != null) {
405             this.runtimeReg.close();
406             this.runtimeReg = null;
407         }
408         if (this.session != null) {
409             try {
410                 this.session.close();
411             } catch (final Exception e) {
412                 LOG.warn("Error closing session with peer", e);
413             }
414             this.session = null;
415
416             this.rib.getRenderStats().getConnectedPeerCounter().decreaseCount();
417         }
418     }
419
420     @Override
421     public boolean isSessionActive() {
422         return this.session != null;
423     }
424
425     @Override
426     public synchronized byte[] getRawIdentifier() {
427         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
428     }
429
430     @Override
431     public void resetSession() {
432         releaseConnection();
433     }
434
435     @Override
436     public void resetStats() {
437         if (this.session instanceof BGPSessionStats) {
438             ((BGPSessionStats) this.session).resetBgpSessionStats();
439         }
440     }
441
442     public synchronized void registerRootRuntimeBean(final BGPPeerRuntimeRegistrator registrator) {
443         this.registrator = registrator;
444     }
445
446     @Override
447     public BgpSessionState getBgpSessionState() {
448         if (this.session instanceof BGPSessionStats) {
449             return ((BGPSessionStats) this.session).getBgpSessionState();
450         }
451         return new BgpSessionState();
452     }
453
454     @Override
455     public synchronized BgpPeerState getBgpPeerState() {
456         return this.peerStats.getBgpPeerState();
457     }
458
459     @Override
460     public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
461         LOG.error("Transaction chain failed.", cause);
462         this.chain.close();
463         this.chain = this.rib.createPeerChain(this);
464         this.ribWriter = AdjRibInWriter.create(this.rib.getYangRibId(), this.peerRole, this.simpleRoutingPolicy, this.chain);
465         releaseConnection();
466     }
467
468     @Override
469     public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
470         LOG.debug("Transaction chain {} successfull.", chain);
471     }
472
473     @Override
474     public void markUptodate(final TablesKey tablesKey) {
475         this.ribWriter.markTableUptodate(tablesKey);
476     }
477 }