Update MRI projects for Aluminium
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / OSGiCodecsRegistry.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.annotations.Beta;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
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.osgi.service.component.annotations.Activate;
20 import org.osgi.service.component.annotations.Component;
21 import org.osgi.service.component.annotations.Deactivate;
22 import org.osgi.service.component.annotations.Reference;
23 import org.osgi.service.component.annotations.ReferencePolicy;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Beta
28 @Component(immediate = true)
29 public final class OSGiCodecsRegistry implements CodecsRegistry {
30     private static final Logger LOG = LoggerFactory.getLogger(OSGiCodecsRegistry.class);
31
32     private final ConcurrentMap<RIBSupport<?, ?, ?, ?>, Codecs> contexts = new ConcurrentHashMap<>();
33     private volatile BindingCodecTree codecTree;
34
35     @Override
36     public Codecs getCodecs(final RIBSupport<?, ?, ?, ?> ribSupport) {
37         return contexts.computeIfAbsent(ribSupport, this::createCodecs);
38     }
39
40     @Reference(policy = ReferencePolicy.DYNAMIC)
41     void bindCodecTree(final BindingCodecTree newCodecTree) {
42         this.codecTree = requireNonNull(newCodecTree);
43     }
44
45     void unbindCodecTree(final BindingCodecTree unused) {
46         this.codecTree = null;
47     }
48
49     void updatedCodecTree(final BindingCodecTree newCodecTree) {
50         this.codecTree = requireNonNull(newCodecTree);
51         contexts.values().forEach(codecs -> codecs.onCodecTreeUpdated(newCodecTree));
52     }
53
54     @Activate
55     @SuppressWarnings("static-method")
56     void activate() {
57         LOG.info("BGP codec registry started");
58     }
59
60     @Deactivate
61     void deactivate() {
62         contexts.clear();
63         LOG.info("BGP codec registry stopped");
64     }
65
66     private Codecs createCodecs(final RIBSupport<?, ?, ?, ?> key) {
67         final Codecs codecs = new CodecsImpl(key);
68         codecs.onCodecTreeUpdated(codecTree);
69         return codecs;
70     }
71 }