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.mdsal.binding.dom.codec.api.BindingCodecTree;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
16 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
17 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.Codecs;
19 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
20 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 final class CodecsRegistryImpl implements CodecsRegistry {
27 private static final Logger LOG = LoggerFactory.getLogger(CodecsRegistryImpl.class);
29 private final LoadingCache<RIBSupport, Codecs> contexts = CacheBuilder.newBuilder()
30 .build(new CacheLoader<RIBSupport, Codecs>(){
33 public Codecs load(final RIBSupport key) {
34 return createContext(key);
37 private final BindingCodecTreeFactory codecFactory;
38 private final GeneratedClassLoadingStrategy classContext;
39 private volatile BindingCodecTree latestCodecTree;
41 private CodecsRegistryImpl(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy strategy) {
42 this.codecFactory = Preconditions.checkNotNull(codecFactory);
43 this.classContext = Preconditions.checkNotNull(strategy);
46 static CodecsRegistryImpl create(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
47 return new CodecsRegistryImpl(codecFactory, classStrategy);
50 private Codecs createContext(final RIBSupport ribSupport) {
51 final Codecs codecs = new CodecsImpl(ribSupport);
52 if (this.latestCodecTree != null) {
53 // FIXME: Do we need to recalculate latestCodecTree? E.g. new rib support was added
54 // after bgp was started.
55 codecs.onCodecTreeUpdated(this.latestCodecTree);
60 void onSchemaContextUpdated(final SchemaContext context) {
61 final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(this.classContext, context);
62 this.latestCodecTree = this.codecFactory.create(runtimeContext);
63 for (final Codecs codecs : this.contexts.asMap().values()) {
65 codecs.onCodecTreeUpdated(this.latestCodecTree);
66 } catch (final Exception e) {
67 LOG.error("Codec creation threw {}", e.getMessage(), e);
73 public Codecs getCodecs(final RIBSupport ribSupport) {
74 return this.contexts.getUnchecked(ribSupport);