BUG-1116: Move BGPSession and friends to a more private place
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / BGPSynchronization.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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12
13 import java.util.Map;
14 import java.util.Map.Entry;
15 import java.util.Set;
16
17 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
18 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
34  * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
35  * which AFI/SAFI, an algorithm needed to be implemented.
36  */
37 public class BGPSynchronization {
38
39     private static final Logger LOG = LoggerFactory.getLogger(BGPSynchronization.class);
40
41     private static class SyncVariables {
42
43         private boolean upd = false;
44         private boolean eor = false;
45
46         public void setUpd(final boolean upd) {
47             this.upd = upd;
48         }
49
50         public void setEorTrue() {
51             this.eor = true;
52         }
53
54         public boolean getEor() {
55             return this.eor;
56         }
57
58         public boolean getUpd() {
59             return this.upd;
60         }
61     }
62
63     private final Map<TablesKey, SyncVariables> syncStorage = Maps.newHashMap();
64
65     private final BGPSessionListener listener;
66
67     private final BGPSession session;
68
69     public BGPSynchronization(final BGPSession bgpSession, final BGPSessionListener listener, final Set<TablesKey> types) {
70         this.listener = Preconditions.checkNotNull(listener);
71         this.session = Preconditions.checkNotNull(bgpSession);
72
73         for (final TablesKey type : types) {
74             this.syncStorage.put(type, new SyncVariables());
75         }
76     }
77
78     /**
79      * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
80      * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
81      * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or BGPPrefix<?>
82      * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
83      *
84      * @param msg received Update message
85      */
86     public void updReceived(final Update msg) {
87         TablesKey type = new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
88         boolean isEOR = false;
89         if (msg.getNlri() == null && msg.getWithdrawnRoutes() == null) {
90             if (msg.getPathAttributes() != null) {
91                 if (msg.getPathAttributes().getAugmentation(PathAttributes1.class) != null) {
92                     final PathAttributes1 pa = msg.getPathAttributes().getAugmentation(PathAttributes1.class);
93                     if (pa.getMpReachNlri() != null) {
94                         type = new TablesKey(pa.getMpReachNlri().getAfi(), pa.getMpReachNlri().getSafi());
95                     }
96                 } else if (msg.getPathAttributes().getAugmentation(PathAttributes2.class) != null) {
97                     final PathAttributes2 pa = msg.getPathAttributes().getAugmentation(PathAttributes2.class);
98                     if (pa.getMpUnreachNlri() != null) {
99                         type = new TablesKey(pa.getMpUnreachNlri().getAfi(), pa.getMpUnreachNlri().getSafi());
100                     }
101                 }
102             } else {
103                 isEOR = true;
104             }
105         }
106         final SyncVariables s = this.syncStorage.get(type);
107         if (s == null) {
108             LOG.warn("BGPTableType was not present in open message : {}", type);
109             return;
110         }
111         s.setUpd(true);
112         if (isEOR) {
113             s.setEorTrue();
114         }
115     }
116
117     /**
118      * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
119      * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
120      * session.
121      */
122     public void kaReceived() {
123         for (final Entry<TablesKey, SyncVariables> entry : this.syncStorage.entrySet()) {
124             final SyncVariables s = entry.getValue();
125             if (!s.getEor()) {
126                 if (!s.getUpd()) {
127                     s.setEorTrue();
128                     LOG.info("BGP Synchronization finished for table {} ", entry.getKey());
129                     final Update up = generateEOR(entry.getKey());
130                     LOG.debug("Sending synchronization message: {}", up);
131                     this.listener.onMessage(this.session, up);
132                 }
133                 s.setUpd(false);
134             }
135         }
136     }
137
138     private Update generateEOR(final TablesKey type) {
139         if (type.getAfi().equals(Ipv4AddressFamily.class) && type.getSafi().equals(UnicastSubsequentAddressFamily.class)) {
140             return new UpdateBuilder().build();
141         }
142         return new UpdateBuilder().setPathAttributes(
143                 new PathAttributesBuilder().addAugmentation(
144                         PathAttributes1.class,
145                         new PathAttributes1Builder().setMpReachNlri(
146                                 new MpReachNlriBuilder().setAfi(type.getAfi()).setSafi(type.getSafi()).build()).build()).build()).build();
147     }
148 }