2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.collect.Maps;
11 import java.util.EnumMap;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
14 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
17 * Defines the internal hooks invoked when a new route appears.
19 abstract class AbstractExportPolicy {
20 // Invoked on routes which we send outside of our home AS
21 private static final class ToExternalExportPolicy extends AbstractExportPolicy {
23 ContainerNode effectiveAttributes(final PeerRole sourceRole, final ContainerNode attributes) {
24 // FIXME: filter all non-transitive attributes
25 // FIXME: but that may end up hurting our informedness
26 // FIXME: prepend local AS to add our AS into AS_PATH
30 // eBGP -> eBGP, propagate
33 // Non-Client iBGP -> eBGP, propagate
36 // Client iBGP -> eBGP, propagate
43 // Invoked on routes which we send to our normal home AS peers.
44 private static class ToInternalExportPolicy extends AbstractExportPolicy {
45 protected static ContainerNode prependClusterId(final ContainerNode attributes) {
46 // FIXME: prepend local CLUSTER_ID to CLUSTER_LIST
50 // Attributes when reflecting a route
51 protected static ContainerNode toClientAttributes(final ContainerNode attributes) {
52 // TODO: (defensiveness) verify ORIGINATOR_ID (should have been set)
54 return prependClusterId(attributes);
58 ContainerNode effectiveAttributes(final PeerRole sourceRole, final ContainerNode attributes) {
59 // Implement filtering according to <a ref="http://tools.ietf.org/html/rfc4456#section-8"/>.
63 // eBGP -> Non-Client iBGP, propagate
67 // Non-Client iBGP -> Non-Client iBGP, block
71 // Client iBGP -> Non-Client iBGP, reflect
72 return toClientAttributes(attributes);
79 * Invoked on routes which we send to our reflector peers. This is a special-case of
80 * FromInternalImportPolicy.
82 private static final class ToReflectorClientExportPolicy extends AbstractExportPolicy {
84 ContainerNode effectiveAttributes(final PeerRole sourceRole, final ContainerNode attributes) {
87 // eBGP -> Client iBGP, propagate
90 // Non-Client iBGP -> Client iBGP, reflect
91 // FIXME: add ORIGINATOR_ID
93 return ToInternalExportPolicy.prependClusterId(attributes);
95 // Client iBGP -> Client iBGP, reflect
96 return ToInternalExportPolicy.toClientAttributes(attributes);
99 throw new IllegalStateException("Unhandled source role " + sourceRole);
103 static final Map<PeerRole, AbstractExportPolicy> POLICIES;
106 final Map<PeerRole, AbstractExportPolicy> p = new EnumMap<PeerRole, AbstractExportPolicy>(PeerRole.class);
107 p.put(PeerRole.Ebgp, new ToExternalExportPolicy());
108 p.put(PeerRole.Ibgp, new ToInternalExportPolicy());
109 p.put(PeerRole.RrClient, new ToReflectorClientExportPolicy());
110 POLICIES = Maps.immutableEnumMap(p);
113 static AbstractExportPolicy forRole(final PeerRole peerRole) {
114 return POLICIES.get(peerRole);
118 * Transform outgoing attributes according to policy.
120 * @param sourceRole role of the peer which originated the routes
121 * @param attributes outgoing attributes
122 * @return Filtered attributes, or null if the advertisement should be ignored.
124 abstract ContainerNode effectiveAttributes(PeerRole sourceRole, ContainerNode attributes);