2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import static java.util.Objects.requireNonNull;
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;
28 @Component(immediate = true)
29 public final class OSGiCodecsRegistry implements CodecsRegistry {
30 private static final Logger LOG = LoggerFactory.getLogger(OSGiCodecsRegistry.class);
32 private final ConcurrentMap<RIBSupport<?, ?, ?, ?>, Codecs> contexts = new ConcurrentHashMap<>();
33 private volatile BindingCodecTree codecTree;
36 public Codecs getCodecs(final RIBSupport<?, ?, ?, ?> ribSupport) {
37 return contexts.computeIfAbsent(ribSupport, this::createCodecs);
40 @Reference(policy = ReferencePolicy.DYNAMIC)
41 void bindCodecTree(final BindingCodecTree newCodecTree) {
42 this.codecTree = requireNonNull(newCodecTree);
45 void unbindCodecTree(final BindingCodecTree unused) {
46 this.codecTree = null;
49 void updatedCodecTree(final BindingCodecTree newCodecTree) {
50 this.codecTree = requireNonNull(newCodecTree);
51 contexts.values().forEach(codecs -> codecs.onCodecTreeUpdated(newCodecTree));
55 @SuppressWarnings("static-method")
57 LOG.info("BGP codec registry started");
63 LOG.info("BGP codec registry stopped");
66 private Codecs createCodecs(final RIBSupport<?, ?, ?, ?> key) {
67 final Codecs codecs = new CodecsImpl(key);
68 codecs.onCodecTreeUpdated(codecTree);