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