Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / openconfig-spi / src / main / java / org / opendaylight / protocol / bgp / openconfig / spi / SimpleBGPTableTypeRegistryProvider.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.protocol.bgp.openconfig.spi;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.BiMap;
13 import com.google.common.collect.HashBiMap;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
16 import javax.annotation.concurrent.GuardedBy;
17 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.BgpTableType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
23 import org.opendaylight.yangtools.concepts.AbstractRegistration;
24
25 public final class SimpleBGPTableTypeRegistryProvider implements BGPTableTypeRegistryProvider {
26
27     @GuardedBy("this")
28     private final BiMap<BgpTableType, Class<? extends AfiSafiType>> tableTypes = HashBiMap.create();
29     @GuardedBy("this")
30     private final BiMap<TablesKey, Class<? extends AfiSafiType>> tableKeys = HashBiMap.create();
31
32     @Override
33     public synchronized AbstractRegistration registerBGPTableType(final Class<? extends AddressFamily> afi,
34             final Class<? extends SubsequentAddressFamily> safi, final Class<? extends AfiSafiType> afiSafiType) {
35         final BgpTableType tableType = new BgpTableTypeImpl(afi, safi);
36         final Class<? extends AfiSafiType> prev = this.tableTypes.putIfAbsent(tableType, afiSafiType);
37         Preconditions.checkState(prev == null, "AFI %s SAFI %s is already registered with %s",
38                 afi, safi, prev);
39         final TablesKey tableKey = new TablesKey(tableType.getAfi(), tableType.getSafi());
40         this.tableKeys.put(tableKey, afiSafiType);
41
42         return new AbstractRegistration() {
43             @Override
44             protected void removeRegistration() {
45                 synchronized (SimpleBGPTableTypeRegistryProvider.this) {
46                     SimpleBGPTableTypeRegistryProvider.this.tableTypes.remove(tableType);
47                     SimpleBGPTableTypeRegistryProvider.this.tableKeys.remove(tableKey);
48                 }
49             }
50         };
51     }
52
53     @Override
54     public synchronized Optional<BgpTableType> getTableType(final Class<? extends AfiSafiType> afiSafiType) {
55         final BgpTableType tableType = this.tableTypes.inverse().get(afiSafiType);
56         return Optional.ofNullable(tableType);
57     }
58
59     @Nonnull
60     @Override
61     public Optional<TablesKey> getTableKey(@Nonnull final Class<? extends AfiSafiType> afiSafiType) {
62         final TablesKey tableKey = this.tableKeys.inverse().get(afiSafiType);
63         return Optional.ofNullable(tableKey);
64     }
65
66     @Override
67     public synchronized Optional<Class<? extends AfiSafiType>> getAfiSafiType(final BgpTableType bgpTableType) {
68         final Class<? extends AfiSafiType> afiSafi = this.tableTypes.get(bgpTableType);
69         return Optional.ofNullable(afiSafi);
70     }
71
72     @Nonnull
73     @Override
74     public Optional<Class<? extends AfiSafiType>> getAfiSafiType(@Nonnull final TablesKey tablesKey) {
75         final Class<? extends AfiSafiType> afiSafi = this.tableKeys.get(tablesKey);
76         return Optional.ofNullable(afiSafi);
77     }
78
79 }