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