eb99e69160d3cdebb40420768440ca04c8b3e3a8
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / RIBSupportContextRegistryImpl.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.protocol.bgp.rib.impl.spi.RIBSupportContext;
15 import org.opendaylight.protocol.bgp.rib.impl.spi.RIBSupportContextRegistry;
16 import org.opendaylight.protocol.bgp.rib.spi.RIBExtensionConsumerContext;
17 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
19 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTree;
20 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeFactory;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
22 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
29
30     private static final Logger LOG = LoggerFactory.getLogger(RIBSupportContextRegistryImpl.class);
31     private final LoadingCache<RIBSupport, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
32             .build(new CacheLoader<RIBSupport, RIBSupportContextImpl>(){
33
34                 @Override
35                 public RIBSupportContextImpl load(final RIBSupport key) {
36                     return createContext(key);
37                 }
38             });
39
40     private final RIBExtensionConsumerContext extensionContext;
41     private final BindingCodecTreeFactory codecFactory;
42     private final GeneratedClassLoadingStrategy classContext;
43     private volatile BindingCodecTree latestCodecTree;
44
45     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final BindingCodecTreeFactory codecFactory,
46             final GeneratedClassLoadingStrategy strategy) {
47         this.extensionContext = Preconditions.checkNotNull(extensions);
48         this.codecFactory = Preconditions.checkNotNull(codecFactory);
49         this.classContext = Preconditions.checkNotNull(strategy);
50     }
51
52     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
53             final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
54         return new RIBSupportContextRegistryImpl(extensions, codecFactory, classStrategy);
55     }
56
57     @Override
58     public RIBSupportContext getRIBSupportContext(final TablesKey key) {
59         final RIBSupport ribSupport = this.extensionContext.getRIBSupport(key);
60         if(ribSupport != null) {
61             return this.contexts.getUnchecked(ribSupport);
62         }
63         return null;
64     }
65
66     @Override
67     public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
68         final RIBSupport ribSupport = this.extensionContext.getRIBSupport(key);
69         if(ribSupport != null) {
70             return this.contexts.getUnchecked(ribSupport);
71         }
72         return null;
73     }
74
75     private RIBSupportContextImpl createContext(final RIBSupport ribSupport) {
76         final RIBSupportContextImpl ribContext = new RIBSupportContextImpl(ribSupport);
77         if(this.latestCodecTree != null) {
78             // FIXME: Do we need to recalculate latestCodecTree? E.g. new rib support was added
79             // after bgp was started.
80             ribContext.onCodecTreeUpdated(this.latestCodecTree);
81         }
82         return ribContext;
83     }
84
85     void onSchemaContextUpdated(final SchemaContext context) {
86         final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(this.classContext, context);
87         this.latestCodecTree  = this.codecFactory.create(runtimeContext);
88         for(final RIBSupportContextImpl rib : this.contexts.asMap().values()) {
89             try {
90                 rib.onCodecTreeUpdated(this.latestCodecTree);
91             } catch (final Exception e) {
92                 LOG.error("Codec creation threw {}", e.getMessage(), e);
93             }
94         }
95     }
96 }