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