Update MRI projects for Aluminium
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ConstantCodecsRegistry.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 java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
17 import org.opendaylight.protocol.bgp.rib.impl.spi.Codecs;
18 import org.opendaylight.protocol.bgp.rib.impl.spi.CodecsRegistry;
19 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
20
21 @Singleton
22 public final class ConstantCodecsRegistry implements CodecsRegistry {
23     private final ConcurrentMap<RIBSupport<?, ?, ?, ?>, Codecs> contexts = new ConcurrentHashMap<>();
24     private final BindingCodecTree codecTree;
25
26     @Inject
27     public ConstantCodecsRegistry(final BindingCodecTree codecTree) {
28         this.codecTree = requireNonNull(codecTree);
29     }
30
31     @Override
32     public Codecs getCodecs(final RIBSupport<?, ?, ?, ?> ribSupport) {
33         return contexts.computeIfAbsent(ribSupport, this::createCodecs);
34     }
35
36     private Codecs createCodecs(final RIBSupport<?, ?, ?, ?> key) {
37         final Codecs codecs = new CodecsImpl(key);
38         codecs.onCodecTreeUpdated(codecTree);
39         return codecs;
40     }
41 }