Convert yangtools binding APIs to mdsal bindings
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / CodecsRegistryImpl.java
1 /*
2  * Copyright (c) 2015 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 package org.opendaylight.protocol.bgp.rib.impl;
9
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.protocol.bgp.rib.impl.spi.Codecs;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
18 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
20 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 final class CodecsRegistryImpl implements CodecsRegistry {
26
27     private static final Logger LOG = LoggerFactory.getLogger(CodecsRegistryImpl.class);
28
29     private final LoadingCache<RIBSupport, Codecs> contexts = CacheBuilder.newBuilder()
30         .build(new CacheLoader<RIBSupport, Codecs>(){
31
32             @Override
33             public Codecs load(final RIBSupport key) {
34                 return createContext(key);
35             }
36         });
37     private final BindingCodecTreeFactory codecFactory;
38     private final GeneratedClassLoadingStrategy classContext;
39     private volatile BindingCodecTree latestCodecTree;
40
41     private CodecsRegistryImpl(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy strategy) {
42         this.codecFactory = Preconditions.checkNotNull(codecFactory);
43         this.classContext = Preconditions.checkNotNull(strategy);
44     }
45
46     static CodecsRegistryImpl create(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
47         return new CodecsRegistryImpl(codecFactory, classStrategy);
48     }
49
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);
56         }
57         return codecs;
58     }
59
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()) {
64             try {
65                 codecs.onCodecTreeUpdated(this.latestCodecTree);
66             } catch (final Exception e) {
67                 LOG.error("Codec creation threw {}", e.getMessage(), e);
68             }
69         }
70     }
71
72     @Override
73     public Codecs getCodecs(final RIBSupport ribSupport) {
74         return this.contexts.getUnchecked(ribSupport);
75     }
76 }