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