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