Bump versions by x.y.(z+1)
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ImportPolicyPeerTrackerImpl.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.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import org.opendaylight.protocol.bgp.rib.impl.spi.AbstractImportPolicy;
14 import org.opendaylight.protocol.bgp.rib.impl.spi.ImportPolicyPeerTracker;
15 import org.opendaylight.protocol.bgp.rib.spi.IdentifierUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 final class ImportPolicyPeerTrackerImpl implements ImportPolicyPeerTracker {
24     private static final Logger LOG = LoggerFactory.getLogger(ImportPolicyPeerTrackerImpl.class);
25
26     private final Map<PeerId, AbstractImportPolicy> policies = new ConcurrentHashMap<>();
27     private final PolicyDatabase policyDatabase;
28
29     protected ImportPolicyPeerTrackerImpl(final PolicyDatabase policyDatabase) {
30         super();
31         this.policyDatabase = Preconditions.checkNotNull(policyDatabase);
32     }
33
34     @Override
35     public void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
36         final PeerId peer = IdentifierUtils.peerId((NodeIdentifierWithPredicates) peerPath.getLastPathArgument());
37
38         if (role != null) {
39             // Lookup policy based on role
40             final AbstractImportPolicy policy = this.policyDatabase.importPolicyForRole(role);
41
42             // Update lookup map
43             this.policies.put(peer, policy);
44             LOG.debug("Updating policy {} for peer {}", policy, peer);
45         } else {
46             this.policies.remove(peer);
47         }
48     }
49
50     @Override
51     public AbstractImportPolicy policyFor(final PeerId peerId) {
52         LOG.trace("Peer ID : {}", peerId);
53         return this.policies.get(peerId);
54     }
55 }