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