Fix checkstyle
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeFactory;
18 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
19 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
20 import org.opendaylight.protocol.bgp.rib.impl.spi.Codecs;
21 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
22 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public final class CodecsRegistryImpl implements CodecsRegistry {
28
29     private static final Logger LOG = LoggerFactory.getLogger(CodecsRegistryImpl.class);
30
31     private final LoadingCache<RIBSupport<?, ?, ?, ?>, Codecs> contexts = CacheBuilder.newBuilder()
32         .build(new CacheLoader<RIBSupport<?, ?, ?, ?>, Codecs>() {
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,
43             final GeneratedClassLoadingStrategy strategy) {
44         this.codecFactory = requireNonNull(codecFactory);
45         this.classContext = requireNonNull(strategy);
46     }
47
48     public static CodecsRegistryImpl create(final BindingCodecTreeFactory codecFactory,
49             final GeneratedClassLoadingStrategy classStrategy) {
50         return new CodecsRegistryImpl(codecFactory, classStrategy);
51     }
52
53     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
54             justification = "https://github.com/spotbugs/spotbugs/issues/811")
55     private Codecs createContext(final RIBSupport<?, ?, ?, ?> ribSupport) {
56         final Codecs codecs = new CodecsImpl(ribSupport);
57         if (this.latestCodecTree != null) {
58             // FIXME: Do we need to recalculate latestCodecTree? E.g. new rib support was added
59             // after bgp was started.
60             codecs.onCodecTreeUpdated(this.latestCodecTree);
61         }
62         return codecs;
63     }
64
65     @SuppressWarnings("checkstyle:illegalCatch")
66     void onSchemaContextUpdated(final SchemaContext context) {
67         final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(this.classContext, context);
68         this.latestCodecTree  = this.codecFactory.create(runtimeContext);
69         for (final Codecs codecs : this.contexts.asMap().values()) {
70             try {
71                 codecs.onCodecTreeUpdated(this.latestCodecTree);
72             } catch (final Exception e) {
73                 LOG.error("Failed to propagate SchemaContext to codec {}", codecs, e);
74             }
75         }
76     }
77
78     @Override
79     public Codecs getCodecs(final RIBSupport<?, ?, ?, ?> ribSupport) {
80         return this.contexts.getUnchecked(ribSupport);
81     }
82 }