68d64e51df2d31dadae4a8657b024a14a49b5937
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / PolicyDatabase.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.EnumMap;
11 import java.util.Map;
12 import org.opendaylight.protocol.bgp.rib.impl.spi.AbstractImportPolicy;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerRole;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
16
17 /**
18  * Policy database attached to a particular RIB instance. Acts as the unified
19  * lookup point.
20  */
21 final class PolicyDatabase {
22     private final Map<PeerRole, AbstractExportPolicy> exportPolicies = new EnumMap<>(PeerRole.class);
23     private final Map<PeerRole, AbstractImportPolicy> importPolicies = new EnumMap<>(PeerRole.class);
24
25     PolicyDatabase(final Long localAs, final Ipv4Address bgpId, final ClusterIdentifier clusterId) {
26         this.exportPolicies.put(PeerRole.Ebgp, new ToExternalExportPolicy(localAs));
27         this.exportPolicies.put(PeerRole.Ibgp, new ToInternalExportPolicy(bgpId, clusterId));
28         this.exportPolicies.put(PeerRole.RrClient, new ToReflectorClientExportPolicy(bgpId, clusterId));
29         this.exportPolicies.put(PeerRole.Internal, new ToInternalReflectorClientExportPolicy(bgpId, clusterId));
30
31         this.importPolicies.put(PeerRole.Ebgp, new FromExternalImportPolicy());
32         this.importPolicies.put(PeerRole.Ibgp, new FromInternalImportPolicy(bgpId, clusterId));
33         this.importPolicies.put(PeerRole.RrClient, new FromReflectorClientImportPolicy(bgpId, clusterId));
34         this.importPolicies.put(PeerRole.Internal, new FromInternalReflectorClientImportPolicy(bgpId, clusterId));
35     }
36
37     AbstractExportPolicy exportPolicyForRole(final PeerRole peerRole) {
38         return this.exportPolicies.get(peerRole);
39     }
40
41     AbstractImportPolicy importPolicyForRole(final PeerRole peerRole) {
42         /*
43          * TODO: this solution does not share equivalent attributes across
44          *       multiple peers. If we need to do that, consider carefully:
45          *       - whether the interner should be shared with RIBSupportContextImpl.writeRoutes()
46          *       - lookup/update contention of both the cache and the interner
47          *       - ability to share resulting attributes across import policies
48          *       - ability to share resulting attributes with export policies
49          */
50         return new CachingImportPolicy(this.importPolicies.get(peerRole));
51     }
52 }