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