Replace Preconditions.CheckNotNull per RequireNonNull
[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 static java.util.Objects.requireNonNull;
11
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 = requireNonNull(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 boolean containsPeer(final PeerId routePeerId) {
39         synchronized (this.peers) {
40             return this.peers.containsKey(routePeerId);
41         }
42     }
43
44     @Override
45     public void forEach(final BiConsumer<PeerId, YangInstanceIdentifier> action) {
46         synchronized (this.peers) {
47             for (final Map.Entry<PeerId, PeerExporTuple> pid : this.peers.entrySet()) {
48                 action.accept(pid.getKey(), pid.getValue().getYii());
49             }
50         }
51     }
52
53     @Override
54     public AbstractRegistration registerPeer(final PeerId peerId, final PeerExporTuple peerExporTuple) {
55         synchronized (this.peers) {
56             this.peers.put(peerId, peerExporTuple);
57         }
58
59         return new AbstractRegistration() {
60             @Override
61             protected void removeRegistration() {
62                 synchronized (PeerExportGroupImpl.this.peers) {
63                     PeerExportGroupImpl.this.peers.remove(peerId);
64                 }
65             }
66         };
67     }
68
69     @Override
70     public boolean isEmpty() {
71         synchronized (this.peers) {
72             return this.peers.isEmpty();
73         }
74     }
75 }