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