Handle end-of-RIB messages
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBImpl.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 javax.annotation.concurrent.ThreadSafe;
11
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
14 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
15 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
16 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
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.update.Nlri;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.WithdrawnRoutes;
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.PathAttributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.destination.type.DestinationIpv4Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.base.Objects;
39 import com.google.common.base.Objects.ToStringHelper;
40 import com.google.common.base.Preconditions;
41 import com.google.common.util.concurrent.FutureCallback;
42 import com.google.common.util.concurrent.Futures;
43 import com.google.common.util.concurrent.JdkFutureAdapters;
44
45 @ThreadSafe
46 public class RIBImpl {
47         private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
48         private static final Update EOR = new UpdateBuilder().build();
49         private final DataProviderService dps;
50         private final RIBTables tables;
51
52         public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
53                 this.dps = Preconditions.checkNotNull(dps);
54                 this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
55         }
56
57         synchronized void updateTables(final BGPPeer peer, final Update message) {
58                 final DataModificationTransaction trans = this.dps.beginTransaction();
59
60                 if (EOR.equals(message)) {
61                         final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
62                         if (ari != null) {
63                                 ari.markUptodate(trans, peer);
64                         } else {
65                                 LOG.debug("End-of-RIB for IPv4 Unicast ignored");
66                         }
67                         return;
68                 }
69
70                 final WithdrawnRoutes wr = message.getWithdrawnRoutes();
71                 if (wr != null) {
72                         final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
73                         if (ari != null) {
74                                 ari.removeRoutes(trans, peer, new MpUnreachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).setWithdrawnRoutes(
75                                                 new WithdrawnRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(wr.getWithdrawnRoutes()).build()).build()).build());
76                         } else {
77                                 LOG.debug("Not removing objects from unhandled IPv4 Unicast");
78                         }
79                 }
80
81                 final PathAttributes attrs = message.getPathAttributes();
82                 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
83                 if (mpu != null) {
84                         final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
85
86                         final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
87                         if (ari != null) {
88                                 ari.removeRoutes(trans, peer, nlri);
89                         } else {
90                                 LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
91                         }
92                 }
93
94                 final Nlri ar = message.getNlri();
95                 if (ar != null) {
96                         final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class));
97                         if (ari != null) {
98                                 ari.addRoutes(trans, peer, new MpReachNlriBuilder().setAfi(Ipv4AddressFamily.class).setSafi(UnicastSubsequentAddressFamily.class).
99                                                 setCNextHop(attrs.getCNextHop()).setAdvertizedRoutes(
100                                                                 new AdvertizedRoutesBuilder().setDestinationType(new DestinationIpv4Builder().setIpv4Prefixes(ar.getNlri()).build()).build()).build(), attrs);
101                         } else {
102                                 LOG.debug("Not adding objects from unhandled IPv4 Unicast");
103                         }
104                 }
105
106                 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
107                 if (mpr != null) {
108                         final MpReachNlri nlri = mpr.getMpReachNlri();
109
110                         final AdjRIBsIn ari = this.tables.getOrCreate(trans, new TablesKey(nlri.getAfi(), nlri.getSafi()));
111                         if (ari != null) {
112                                 ari.addRoutes(trans, peer, nlri, attrs);
113                                 if (message.equals(ari.endOfRib())) {
114                                         ari.markUptodate(trans, peer);
115                                 }
116                         } else {
117                                 LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
118                         }
119                 }
120
121                 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
122                         @Override
123                         public void onSuccess(final RpcResult<TransactionStatus> result) {
124                                 LOG.debug("RIB modification successfully committed.");
125                         }
126
127                         @Override
128                         public void onFailure(final Throwable t) {
129                                 LOG.error("Failed to commit RIB modification", t);
130                         }
131                 });
132         }
133
134         synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
135                 final AdjRIBsIn ari = this.tables.get(key);
136                 if (ari != null) {
137                         final DataModificationTransaction trans = this.dps.beginTransaction();
138                         ari.clear(trans, peer);
139
140                         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(trans.commit()), new FutureCallback<RpcResult<TransactionStatus>>() {
141                                 @Override
142                                 public void onSuccess(final RpcResult<TransactionStatus> result) {
143                                         // Nothing to do
144                                 }
145
146                                 @Override
147                                 public void onFailure(final Throwable t) {
148                                         LOG.error("Failed to commit RIB modification", t);
149                                 }
150                         });
151                 }
152         }
153
154         @Override
155         public final String toString() {
156                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
157         }
158
159         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
160                 return toStringHelper;
161         }
162 }