moved BGP Table type from concepts to parser-api.
[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 java.util.Map;
11 import java.util.Map.Entry;
12 import java.util.Set;
13
14 import org.opendaylight.protocol.bgp.concepts.BGPObject;
15 import org.opendaylight.protocol.bgp.parser.BGPLink;
16 import org.opendaylight.protocol.bgp.parser.BGPNode;
17 import org.opendaylight.protocol.bgp.parser.BGPPrefix;
18 import org.opendaylight.protocol.bgp.parser.BGPRoute;
19 import org.opendaylight.protocol.bgp.parser.BGPSession;
20 import org.opendaylight.protocol.bgp.parser.BGPSessionListener;
21 import org.opendaylight.protocol.bgp.parser.BGPTableType;
22 import org.opendaylight.protocol.bgp.parser.BGPUpdateMessage;
23 import org.opendaylight.protocol.bgp.parser.BGPUpdateSynchronized;
24 import org.opendaylight.protocol.bgp.util.BGPIPv4RouteImpl;
25 import org.opendaylight.protocol.bgp.util.BGPIPv6RouteImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev130918.LinkstateSubsequentAddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Preconditions;
35 import com.google.common.collect.Maps;
36
37 /**
38  * BGP speaker (without Graceful restart capability) sends KeepAlive message after sending all initial Update messages
39  * with certain AFI/SAFI. For each AFI/SAFI, it sends one KA message. As it is undetermined which KA message belongs to
40  * which AFI/SAFI, an algorithm needed to be implemented.
41  */
42 public class BGPSynchronization {
43
44         private static final Logger logger = LoggerFactory.getLogger(BGPSynchronization.class);
45
46         private static class SyncVariables {
47
48                 private boolean upd = false;
49                 private boolean eor = false;
50
51                 public void setUpd(final boolean upd) {
52                         this.upd = upd;
53                 }
54
55                 public void setEorTrue() {
56                         this.eor = true;
57                 }
58
59                 public boolean getEor() {
60                         return this.eor;
61                 }
62
63                 public boolean getUpd() {
64                         return this.upd;
65                 }
66         }
67
68         private final Map<BGPTableType, SyncVariables> syncStorage = Maps.newHashMap();
69
70         private final BGPSessionListener listener;
71
72         private final BGPSession session;
73
74         public BGPSynchronization(final BGPSession session, final BGPSessionListener listener, final Set<BGPTableType> types) {
75                 this.listener = Preconditions.checkNotNull(listener);
76                 this.session = Preconditions.checkNotNull(session);
77
78                 for (final BGPTableType type : types) {
79                         this.syncStorage.put(type, new SyncVariables());
80                 }
81         }
82
83         /**
84          * For each received Update message, the upd sync variable needs to be updated to true, for particular AFI/SAFI
85          * combination. Currently we only assume Unicast SAFI. From the Update message we have to extract the AFI. Each
86          * Update message can contain BGP Object with one type of AFI. If the object is BGP Link, BGP Node or BGPPrefix<?>
87          * the AFI is Linkstate. In case of BGPRoute, the AFI depends on the IP Address of the prefix.
88          * 
89          * @param msg received Update message
90          */
91         public void updReceived(final BGPUpdateMessage msg) {
92                 BGPTableType type = null;
93                 if (!msg.getAddedObjects().isEmpty()) {
94                         final BGPObject obj = msg.getAddedObjects().iterator().next();
95                         if (obj instanceof BGPRoute) {
96                                 if ((BGPRoute) obj instanceof BGPIPv4RouteImpl) {
97                                         type = new BGPTableType(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class);
98                                 } else if ((BGPRoute) obj instanceof BGPIPv6RouteImpl) {
99                                         type = new BGPTableType(Ipv6AddressFamily.class, UnicastSubsequentAddressFamily.class);
100                                 }
101                         } else if (obj instanceof BGPLink || obj instanceof BGPNode || obj instanceof BGPPrefix<?>) {
102                                 type = new BGPTableType(LinkstateAddressFamily.class, LinkstateSubsequentAddressFamily.class);
103                         }
104                 }
105                 final SyncVariables s = this.syncStorage.get(type);
106                 if (s == null) {
107                         logger.warn("BGPTableType was not present in open message : {}", type);
108                         return;
109                 }
110                 s.setUpd(true);
111         }
112
113         /**
114          * This method is called, when the second KA message is received. It checks each AFI/SAFI sync variables. If they
115          * are all false, which means, that there was at least one update message followed by one KA, the EOR is sent to
116          * session.
117          */
118         public void kaReceived() {
119                 for (final Entry<BGPTableType, SyncVariables> entry : this.syncStorage.entrySet()) {
120                         final SyncVariables s = entry.getValue();
121                         if (!s.getEor()) {
122                                 if (!s.getUpd()) {
123                                         s.setEorTrue();
124                                         final BGPUpdateSynchronized up = generateEOR(entry.getKey());
125                                         logger.debug("Sending synchronization message: {}", up);
126                                         this.listener.onMessage(this.session, up);
127                                 }
128                                 s.setUpd(false);
129                         }
130                 }
131         }
132
133         private BGPUpdateSynchronized generateEOR(final BGPTableType type) {
134                 return new BGPUpdateSynchronizedImpl(type);
135         }
136 }