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