2 * Copyright (c) 2015 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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContext;
15 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
16 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
17 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTree;
20 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeFactory;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
22 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
28 private final LoadingCache<RIBSupport, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
29 .build(new CacheLoader<RIBSupport, RIBSupportContextImpl>(){
32 public RIBSupportContextImpl load(final RIBSupport key) {
33 return createContext(key);
37 private final RIBExtensionConsumerContext extensionContext;
38 private final BindingCodecTreeFactory codecFactory;
39 private final GeneratedClassLoadingStrategy classContext;
40 private volatile BindingCodecTree latestCodecTree;
42 private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final BindingCodecTreeFactory codecFactory,
43 final GeneratedClassLoadingStrategy strategy) {
44 this.extensionContext = Preconditions.checkNotNull(extensions);
45 this.codecFactory = Preconditions.checkNotNull(codecFactory);
46 this.classContext = Preconditions.checkNotNull(strategy);
49 static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
50 final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
51 return new RIBSupportContextRegistryImpl(extensions, codecFactory, classStrategy);
55 public RIBSupportContext getRIBSupportContext(final TablesKey key) {
56 final RIBSupport ribSupport = extensionContext.getRIBSupport(key);
57 if(ribSupport != null) {
58 return contexts.getUnchecked(ribSupport);
64 public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
65 final RIBSupport ribSupport = extensionContext.getRIBSupport(key);
66 if(ribSupport != null) {
67 return contexts.getUnchecked(ribSupport);
72 private RIBSupportContextImpl createContext(final RIBSupport ribSupport) {
73 final RIBSupportContextImpl ribContext = new RIBSupportContextImpl(ribSupport);
74 if(latestCodecTree != null) {
75 // FIXME: Do we need to recalculate latestCodecTree? E.g. new rib support was added
76 // after bgp was started.
77 ribContext.onCodecTreeUpdated(latestCodecTree);
82 void onSchemaContextUpdated(final SchemaContext context) {
83 final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(classContext, context);
84 latestCodecTree = codecFactory.create(runtimeContext);
85 for(final RIBSupportContextImpl rib : contexts.asMap().values()) {
86 rib.onCodecTreeUpdated(latestCodecTree);