Replace Preconditions.CheckNotNull per RequireNonNull
[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 static java.util.Objects.requireNonNull;
11
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;
25
26 final class CodecsRegistryImpl implements CodecsRegistry {
27
28     private static final Logger LOG = LoggerFactory.getLogger(CodecsRegistryImpl.class);
29
30     private final LoadingCache<RIBSupport, Codecs> contexts = CacheBuilder.newBuilder()
31         .build(new CacheLoader<RIBSupport, Codecs>(){
32
33             @Override
34             public Codecs load(final RIBSupport key) {
35                 return createContext(key);
36             }
37         });
38     private final BindingCodecTreeFactory codecFactory;
39     private final GeneratedClassLoadingStrategy classContext;
40     private volatile BindingCodecTree latestCodecTree;
41
42     private CodecsRegistryImpl(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy strategy) {
43         this.codecFactory = requireNonNull(codecFactory);
44         this.classContext = requireNonNull(strategy);
45     }
46
47     static CodecsRegistryImpl create(final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
48         return new CodecsRegistryImpl(codecFactory, classStrategy);
49     }
50
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);
57         }
58         return codecs;
59     }
60
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()) {
65             try {
66                 codecs.onCodecTreeUpdated(this.latestCodecTree);
67             } catch (final Exception e) {
68                 LOG.error("Codec creation threw {}", e.getMessage(), e);
69             }
70         }
71     }
72
73     @Override
74     public Codecs getCodecs(final RIBSupport ribSupport) {
75         return this.contexts.getUnchecked(ribSupport);
76     }
77 }