Bump versions by x.y.(z+1)
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / PeerExportGroupImpl.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 java.util.Collection;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.function.BiConsumer;
15 import javax.annotation.concurrent.GuardedBy;
16 import org.opendaylight.protocol.bgp.rib.impl.spi.PeerExportGroupRegistry;
17 import org.opendaylight.protocol.concepts.AbstractRegistration;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22
23 final class PeerExportGroupImpl implements PeerExportGroupRegistry {
24     @GuardedBy("this")
25     private final Map<PeerId, PeerExporTuple> peers = new HashMap<>();
26     private final AbstractExportPolicy policy;
27
28     public PeerExportGroupImpl(final AbstractExportPolicy policy) {
29         this.policy = Preconditions.checkNotNull(policy);
30     }
31
32     @Override
33     public ContainerNode effectiveAttributes(final PeerRole role, final ContainerNode attributes) {
34         return attributes == null || role == null ? null : this.policy.effectiveAttributes(role, attributes);
35     }
36
37     @Override
38     public Collection<Map.Entry<PeerId, PeerExporTuple>> getPeers() {
39         synchronized (this.peers) {
40             return this.peers.entrySet();
41         }
42     }
43
44     @Override
45     public boolean containsPeer(final PeerId routePeerId) {
46         synchronized (this.peers) {
47             return this.peers.containsKey(routePeerId);
48         }
49     }
50
51     @Override
52     public void forEach(final BiConsumer<PeerId, YangInstanceIdentifier> action) {
53         synchronized (this.peers) {
54             for (final Map.Entry<PeerId, PeerExporTuple> pid : this.peers.entrySet()) {
55                 action.accept(pid.getKey(), pid.getValue().getYii());
56             }
57         }
58     }
59
60     @Override
61     public AbstractRegistration registerPeer(final PeerId peerId, final PeerExporTuple peerExporTuple) {
62         synchronized (this.peers) {
63             this.peers.put(peerId, peerExporTuple);
64         }
65
66         return new AbstractRegistration() {
67             @Override
68             protected void removeRegistration() {
69                 synchronized (PeerExportGroupImpl.this.peers) {
70                     PeerExportGroupImpl.this.peers.remove(peerId);
71                 }
72             }
73         };
74     }
75
76     @Override
77     public boolean isEmpty() {
78         synchronized (this.peers) {
79             return this.peers.isEmpty();
80         }
81     }
82 }