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