2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bgp.openconfig.spi;
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.rev180329.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.rev180329.AddressFamily;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.SubsequentAddressFamily;
23 import org.opendaylight.yangtools.concepts.AbstractRegistration;
25 public final class SimpleBGPTableTypeRegistryProvider implements BGPTableTypeRegistryProvider {
28 private final BiMap<BgpTableType, Class<? extends AfiSafiType>> tableTypes = HashBiMap.create();
30 private final BiMap<TablesKey, Class<? extends AfiSafiType>> tableKeys = HashBiMap.create();
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",
39 final TablesKey tableKey = new TablesKey(tableType.getAfi(), tableType.getSafi());
40 this.tableKeys.put(tableKey, afiSafiType);
42 return new AbstractRegistration() {
44 protected void removeRegistration() {
45 synchronized (SimpleBGPTableTypeRegistryProvider.this) {
46 SimpleBGPTableTypeRegistryProvider.this.tableTypes.remove(tableType);
47 SimpleBGPTableTypeRegistryProvider.this.tableKeys.remove(tableKey);
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);
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);
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);
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);