86d90248801914177d66fb743dc1721993c5ba54
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPPeer.java
1 /*
2  * Copyright (c) 2013 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 io.netty.util.concurrent.Future;
11
12 import java.net.InetSocketAddress;
13 import java.util.Comparator;
14 import java.util.HashSet;
15 import java.util.Set;
16
17 import javax.annotation.concurrent.GuardedBy;
18
19 import org.opendaylight.protocol.bgp.parser.BGPSession;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.parser.BGPTerminationReason;
22 import org.opendaylight.protocol.bgp.rib.impl.spi.BGPSessionPreferences;
23 import org.opendaylight.protocol.bgp.rib.impl.spi.RIB;
24 import org.opendaylight.protocol.bgp.rib.spi.Peer;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.AsNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
30 import org.opendaylight.yangtools.yang.binding.Notification;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Objects;
35 import com.google.common.base.Objects.ToStringHelper;
36 import com.google.common.base.Preconditions;
37
38 /**
39  * Class representing a peer. We have a single instance for each peer, which provides translation from BGP events into
40  * RIB actions.
41  */
42 public final class BGPPeer implements BGPSessionListener, Peer, AutoCloseable {
43         private static final Logger LOG = LoggerFactory.getLogger(BGPPeer.class);
44
45         @GuardedBy("this")
46         private final Set<TablesKey> tables = new HashSet<>();
47         private final String name;
48         private final RIB rib;
49
50         private Comparator<PathAttributes> comparator;
51         private Future<Void> cf;
52         private BGPSession session;
53
54         public BGPPeer(final String name, final InetSocketAddress address, final BGPSessionPreferences prefs,
55                         final AsNumber remoteAs, final RIB rib) {
56                 this.rib = Preconditions.checkNotNull(rib);
57                 this.name = Preconditions.checkNotNull(name);
58                 this.cf = rib.getDispatcher().createReconnectingClient(address, prefs, remoteAs, this, rib.getTcpStrategyFactory(), rib.getSessionStrategyFactory());
59         }
60
61         @Override
62         public synchronized void close() {
63                 if (this.cf != null) {
64                         this.cf.cancel(true);
65                         if (this.session != null) {
66                                 this.session.close();
67                                 this.session = null;
68                         }
69                         this.cf = null;
70                 }
71         }
72
73         @Override
74         public void onMessage(final BGPSession session, final Notification message) {
75                 if (message instanceof Update) {
76                         this.rib.updateTables(this, (Update) message);
77                 } else {
78                         LOG.info("Ignoring unhandled message class " + message.getClass());
79                 }
80         }
81
82         @Override
83         public synchronized void onSessionUp(final BGPSession session) {
84                 LOG.info("Session with peer {} went up with tables: {}", this.name, session.getAdvertisedTableTypes());
85
86                 this.session = session;
87                 this.comparator = new BGPObjectComparator(this.rib.getLocalAs(), this.rib.getBgpIdentifier(), session.getBgpId());
88
89                 for (final BgpTableType t : session.getAdvertisedTableTypes()) {
90                         final TablesKey key = new TablesKey(t.getAfi(), t.getSafi());
91
92                         this.tables.add(key);
93                         this.rib.initTable(this, key);
94                 }
95         }
96
97         private synchronized void cleanup() {
98                 // FIXME: BUG-196: support graceful restart
99                 for (final TablesKey key : this.tables) {
100                         this.rib.clearTable(this, key);
101                 }
102
103                 this.tables.clear();
104                 this.session = null;
105                 this.comparator = null;
106         }
107
108         @Override
109         public void onSessionDown(final BGPSession session, final Exception e) {
110                 LOG.info("Session with peer {} went down", this.name, e);
111                 cleanup();
112         }
113
114         @Override
115         public void onSessionTerminated(final BGPSession session, final BGPTerminationReason cause) {
116                 LOG.info("Session with peer {} terminated: {}", this.name, cause);
117                 cleanup();
118         }
119
120         @Override
121         public String toString() {
122                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
123         }
124
125         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
126                 toStringHelper.add("name", this.name);
127                 toStringHelper.add("tables", this.tables);
128                 return toStringHelper;
129         }
130
131         @Override
132         public String getName() {
133                 return this.name;
134         }
135
136         @Override
137         public Comparator<PathAttributes> getComparator() {
138                 return this.comparator;
139         }
140 }