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