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
45 // Invoked on routes which we send to our normal home AS peers.
46 private static class ToInternalExportPolicy extends AbstractExportPolicy {
47 protected static ContainerNode prependClusterId(final ContainerNode attributes) {
48 // FIXME: prepend local CLUSTER_ID to CLUSTER_LIST
52 // Attributes when reflecting a route
53 protected static ContainerNode toClientAttributes(final ContainerNode attributes) {
54 // TODO: (defensiveness) verify ORIGINATOR_ID (should have been set)
56 return prependClusterId(attributes);
60 ContainerNode effectiveAttributes(final PeerRole sourceRole, final ContainerNode attributes) {
61 // Implement filtering according to <a ref="http://tools.ietf.org/html/rfc4456#section-8"/>.
65 // eBGP -> Non-Client iBGP, propagate
69 // Non-Client iBGP -> Non-Client iBGP, block
73 // Client iBGP -> Non-Client iBGP, reflect
74 return toClientAttributes(attributes);
83 * Invoked on routes which we send to our reflector peers. This is a special-case of
84 * FromInternalImportPolicy.
86 private static final class ToReflectorClientExportPolicy extends AbstractExportPolicy {
88 ContainerNode effectiveAttributes(final PeerRole sourceRole, final ContainerNode attributes) {
91 // eBGP -> Client iBGP, propagate
94 // Non-Client iBGP -> Client iBGP, reflect
95 // FIXME: add ORIGINATOR_ID
97 return ToInternalExportPolicy.prependClusterId(attributes);
99 // Client iBGP -> Client iBGP, reflect
100 return ToInternalExportPolicy.toClientAttributes(attributes);
102 throw new IllegalStateException("Unhandled source role " + sourceRole);
107 static final Map<PeerRole, AbstractExportPolicy> POLICIES;
110 final Map<PeerRole, AbstractExportPolicy> p = new EnumMap<PeerRole, AbstractExportPolicy>(PeerRole.class);
111 p.put(PeerRole.Ebgp, new ToExternalExportPolicy());
112 p.put(PeerRole.Ibgp, new ToInternalExportPolicy());
113 p.put(PeerRole.RrClient, new ToReflectorClientExportPolicy());
114 POLICIES = Maps.immutableEnumMap(p);
117 static AbstractExportPolicy forRole(final PeerRole peerRole) {
118 return POLICIES.get(peerRole);
122 * Transform outgoing attributes according to policy.
124 * @param sourceRole role of the peer which originated the routes
125 * @param attributes outgoing attributes
126 * @return Filtered attributes, or null if the advertisement should be ignored.
128 abstract ContainerNode effectiveAttributes(PeerRole sourceRole, ContainerNode attributes);