Merge "Support proper route redistribution"
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / SimpleRIBExtensionProviderContext.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.spi;
9
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import org.opendaylight.protocol.concepts.AbstractRegistration;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
17
18 public class SimpleRIBExtensionProviderContext implements RIBExtensionProviderContext {
19     private final ConcurrentMap<TablesKey, AdjRIBsFactory> factories = new ConcurrentHashMap<>();
20     private final ConcurrentMap<TablesKey, RIBSupport> supports = new ConcurrentHashMap<>();
21
22     @Override
23     public final synchronized AbstractRegistration registerAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
24             final Class<? extends SubsequentAddressFamily> safi, final AdjRIBsFactory factory) {
25         final TablesKey key = new TablesKey(afi, safi);
26
27         if (this.factories.containsKey(key)) {
28             throw new IllegalArgumentException("Specified AFI/SAFI combination is already registered");
29         }
30
31         this.factories.put(key, factory);
32
33         return new AbstractRegistration() {
34             @Override
35             protected void removeRegistration() {
36                 synchronized (SimpleRIBExtensionProviderContext.this) {
37                     SimpleRIBExtensionProviderContext.this.factories.remove(key);
38                 }
39             }
40         };
41     }
42
43     @Override
44     public final synchronized AdjRIBsFactory getAdjRIBsInFactory(final Class<? extends AddressFamily> afi,
45             final Class<? extends SubsequentAddressFamily> safi) {
46         return this.factories.get(new TablesKey(afi, safi));
47     }
48
49     @Override
50     public <T extends RIBSupport> RIBSupportRegistration<T> registerRIBSupport(final Class<? extends AddressFamily> afi,
51             final Class<? extends SubsequentAddressFamily> safi, final T support) {
52         final TablesKey key = new TablesKey(afi, safi);
53
54         final RIBSupport prev = this.supports.putIfAbsent(key, support);
55         Preconditions.checkArgument(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
56
57         return new AbstractRIBSupportRegistration<T>(support) {
58             @Override
59             protected void removeRegistration() {
60                 SimpleRIBExtensionProviderContext.this.supports.remove(key);
61             }
62         };
63     }
64
65     @Override
66     public RIBSupport getRIBSupport(final Class<? extends AddressFamily> afi, final Class<? extends SubsequentAddressFamily> safi) {
67         return getRIBSupport(new TablesKey(afi, safi));
68     }
69
70     @Override
71     public RIBSupport getRIBSupport(final TablesKey key) {
72         return this.supports.get(Preconditions.checkNotNull(key));
73     }
74 }