b5f2a00a5be35258914254ca468c1a2085402b23
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ImportPolicyPeerTracker.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 java.util.Map;
11 import java.util.concurrent.ConcurrentHashMap;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerId;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
17
18 /**
19  * Tracks import policy corresponding to a particular peer.
20  */
21 final class ImportPolicyPeerTracker extends AbstractPeerRoleTracker {
22     private final Map<PeerId, AbstractImportPolicy> policies = new ConcurrentHashMap<>();
23
24     protected ImportPolicyPeerTracker(final DOMDataTreeChangeService service, final YangInstanceIdentifier ribId) {
25         super(service, ribId);
26     }
27
28     @Override
29     protected void peerRoleChanged(final YangInstanceIdentifier peerPath, final PeerRole role) {
30         final PeerId peer = IdentifierUtils.peerId((NodeIdentifierWithPredicates) peerPath.getLastPathArgument());
31
32         if (role != null) {
33             // Lookup policy based on role
34             final AbstractImportPolicy policy = AbstractImportPolicy.forRole(role);
35
36             // Update lookup map
37             policies.put(peer, policy);
38         } else {
39             policies.remove(peer);
40         }
41     }
42
43     AbstractImportPolicy policyFor(final PeerId peerId) {
44         return policies.get(peerId);
45     }
46 }