Merge "BUG-2982 : moved path-attributes container to grouping"
[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
26 class RIBSupportContextRegistryImpl implements RIBSupportContextRegistry {
27
28     private final LoadingCache<RIBSupport, RIBSupportContextImpl> contexts = CacheBuilder.newBuilder()
29             .build(new CacheLoader<RIBSupport, RIBSupportContextImpl>(){
30
31                 @Override
32                 public RIBSupportContextImpl load(final RIBSupport key) {
33                     return createContext(key);
34                 };
35             });
36
37     private final RIBExtensionConsumerContext extensionContext;
38     private final BindingCodecTreeFactory codecFactory;
39     private final GeneratedClassLoadingStrategy classContext;
40     private volatile BindingCodecTree latestCodecTree;
41
42     private RIBSupportContextRegistryImpl(final RIBExtensionConsumerContext extensions, final BindingCodecTreeFactory codecFactory,
43             final GeneratedClassLoadingStrategy strategy) {
44         this.extensionContext = Preconditions.checkNotNull(extensions);
45         this.codecFactory = Preconditions.checkNotNull(codecFactory);
46         this.classContext = Preconditions.checkNotNull(strategy);
47     }
48
49     static RIBSupportContextRegistryImpl create(final RIBExtensionConsumerContext extensions,
50             final BindingCodecTreeFactory codecFactory, final GeneratedClassLoadingStrategy classStrategy) {
51         return new RIBSupportContextRegistryImpl(extensions, codecFactory, classStrategy);
52     }
53
54     @Override
55     public RIBSupportContext getRIBSupportContext(final TablesKey key) {
56         final RIBSupport ribSupport = extensionContext.getRIBSupport(key);
57         if(ribSupport != null) {
58             return contexts.getUnchecked(ribSupport);
59         }
60         return null;
61     }
62
63     @Override
64     public RIBSupportContext getRIBSupportContext(final NodeIdentifierWithPredicates key) {
65         final RIBSupport ribSupport = extensionContext.getRIBSupport(key);
66         if(ribSupport != null) {
67             return contexts.getUnchecked(ribSupport);
68         }
69         return null;
70     }
71
72     private RIBSupportContextImpl createContext(final RIBSupport ribSupport) {
73         final RIBSupportContextImpl ribContext = new RIBSupportContextImpl(ribSupport);
74         if(latestCodecTree != null) {
75             // FIXME: Do we need to recalculate latestCodecTree? E.g. new rib support was added
76             // after bgp was started.
77             ribContext.onCodecTreeUpdated(latestCodecTree);
78         }
79         return ribContext;
80     }
81
82     void onSchemaContextUpdated(final SchemaContext context) {
83         final BindingRuntimeContext runtimeContext = BindingRuntimeContext.create(classContext, context);
84         latestCodecTree  = codecFactory.create(runtimeContext);
85         for(final RIBSupportContextImpl rib : contexts.asMap().values()) {
86             rib.onCodecTreeUpdated(latestCodecTree);
87         }
88     }
89
90 }