Introduce BGPTableType Registry
[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.concurrent.GuardedBy;
16 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
17 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.SubsequentAddressFamily;
21 import org.opendaylight.yangtools.concepts.AbstractRegistration;
22
23 public final class SimpleBGPTableTypeRegistryProvider implements BGPTableTypeRegistryProvider {
24
25     @GuardedBy("this")
26     private final BiMap<BgpTableType, Class<? extends AfiSafiType>> tableTypes = HashBiMap.create();
27
28     @Override
29     public synchronized AbstractRegistration registerBGPTableType(final Class<? extends AddressFamily> afi,
30             final Class<? extends SubsequentAddressFamily> safi, final Class<? extends AfiSafiType> afiSafiType) {
31         final BgpTableType tableType = new BgpTableTypeImpl(afi, safi);
32         final Class<? extends AfiSafiType> prev = this.tableTypes.putIfAbsent(tableType, afiSafiType);
33         Preconditions.checkState(prev == null, "AFI %s SAFI %s is already registered with %s", afi, safi, prev);
34         return new AbstractRegistration() {
35             @Override
36             protected void removeRegistration() {
37                 synchronized (SimpleBGPTableTypeRegistryProvider.this) {
38                     SimpleBGPTableTypeRegistryProvider.this.tableTypes.remove(tableType);
39                 }
40             }
41         };
42     }
43
44     @Override
45     public synchronized Optional<BgpTableType> getTableType(final Class<? extends AfiSafiType> afiSafiType) {
46         final BgpTableType tableType = this.tableTypes.inverse().get(afiSafiType);
47         return Optional.ofNullable(tableType);
48     }
49
50     @Override
51     public synchronized Optional<Class<? extends AfiSafiType>> getAfiSafiType(final BgpTableType bgpTableType) {
52         final Class<? extends AfiSafiType> afiSafi = this.tableTypes.get(bgpTableType);
53         return Optional.ofNullable(afiSafi);
54     }
55
56 }