Merge "Removed deprecated constructor in AbstractDispatcher"
[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 java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12
13 import javax.annotation.concurrent.ThreadSafe;
14
15 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
18 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.Update;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130918.update.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes1;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.PathAttributes2;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpUnreachNlri;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Objects;
32 import com.google.common.base.Objects.ToStringHelper;
33 import com.google.common.base.Preconditions;
34
35 @ThreadSafe
36 public class RIBImpl {
37         private static final Logger LOG = LoggerFactory.getLogger(RIBImpl.class);
38         private final DataProviderService dps;
39         private final RIBTables tables;
40
41         public RIBImpl(final RIBExtensionConsumerContext extensions, final DataProviderService dps) {
42                 this.dps = Preconditions.checkNotNull(dps);
43                 this.tables = new RIBTables(BGPObjectComparator.INSTANCE, extensions);
44         }
45
46         synchronized void updateTables(final BGPPeer peer, final Update message) {
47                 final DataModificationTransaction trans = this.dps.beginTransaction();
48
49                 // FIXME: detect and handle end-of-RIB markers
50
51                 // remove(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
52                 // trans, peer, message.getWithdrawnRoutes().getWithdrawnRoutes().iterator());
53
54                 final PathAttributes attrs = message.getPathAttributes();
55                 final PathAttributes2 mpu = attrs.getAugmentation(PathAttributes2.class);
56                 if (mpu != null) {
57                         final MpUnreachNlri nlri = mpu.getMpUnreachNlri();
58
59                         final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
60                         if (ari != null) {
61                                 ari.removeRoutes(trans, peer, nlri);
62                         } else {
63                                 LOG.debug("Not removing objects from unhandled NLRI {}", nlri);
64                         }
65                 }
66
67                 // add(Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class,
68                 // trans, peer, message.getNlri().getNlri().iterator(), attrs);
69
70                 final PathAttributes1 mpr = message.getPathAttributes().getAugmentation(PathAttributes1.class);
71                 if (mpr != null) {
72                         final MpReachNlri nlri = mpr.getMpReachNlri();
73
74                         final AdjRIBsIn ari = this.tables.getOrCreate(new TablesKey(nlri.getAfi(), nlri.getSafi()));
75                         if (ari != null) {
76                                 ari.addRoutes(trans, peer, nlri, attrs);
77                         } else {
78                                 LOG.debug("Not adding objects from unhandled NLRI {}", nlri);
79                         }
80                 }
81
82                 // FIXME: we need to attach to this future for failures
83                 final Future<RpcResult<TransactionStatus>> f = trans.commit();
84                 try {
85                         f.get();
86                 } catch (InterruptedException | ExecutionException e) {
87                         LOG.error("Failed to commit RIB modification", e);
88                 }
89         }
90
91         synchronized void clearTable(final BGPPeer peer, final TablesKey key) {
92                 final AdjRIBsIn ari = this.tables.get(key);
93                 if (ari != null) {
94                         final DataModificationTransaction trans = this.dps.beginTransaction();
95                         ari.clear(trans, peer);
96
97                         // FIXME: we need to attach to this future for failures
98                         final Future<RpcResult<TransactionStatus>> f = trans.commit();
99                         try {
100                                 f.get();
101                         } catch (InterruptedException | ExecutionException e) {
102                                 LOG.error("Failed to commit RIB modification", e);
103                         }
104                 }
105         }
106
107         @Override
108         public String toString() {
109                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
110         }
111
112         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
113                 return toStringHelper;
114         }
115 }