BUG-2383: split out policies into separate files
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / FromInternalImportPolicy.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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ClusterIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Invoked on routes which we get from our normal home AS peers.
21  */
22 class FromInternalImportPolicy extends AbstractImportPolicy {
23     private static final Logger LOG = LoggerFactory.getLogger(FromInternalImportPolicy.class);
24     private final ClusterIdentifier clusterIdentifier;
25     private final Ipv4Address bgpIdentifier;
26
27     FromInternalImportPolicy(final Ipv4Address bgpIdentifier, final ClusterIdentifier clusterIdentifier) {
28         this.bgpIdentifier = Preconditions.checkNotNull(bgpIdentifier);
29         this.clusterIdentifier = Preconditions.checkNotNull(clusterIdentifier);
30     }
31
32     @Override
33     ContainerNode effectiveAttributes(final ContainerNode attributes) {
34         final AttributeOperations oper = AttributeOperations.getInstance(attributes);
35
36         /*
37          * This is an implementation of https://tools.ietf.org/html/rfc4456#section-8
38          *
39          * We first check the ORIGINATOR_ID, if present. If it matches our BGP identifier,
40          * we filter the route.
41          */
42         final Object originatorId = oper.getOriginatorId(attributes);
43         if (bgpIdentifier.getValue().equals(originatorId)) {
44             LOG.debug("Filtering route with our ORIGINATOR_ID {}", bgpIdentifier);
45             return null;
46         }
47
48         /*
49          * Second we check CLUSTER_LIST, if present. If it contains our CLUSTER_ID, we issue
50          * a warning and ignore the route.
51          */
52         final LeafSetNode<?> clusterList = oper.getClusterList(attributes);
53         if (clusterList != null) {
54             for (LeafSetEntryNode<?> node : clusterList.getValue()) {
55                 if (clusterIdentifier.getValue().equals(node.getValue())) {
56                     LOG.info("Received a route with our CLUSTER_ID {} in CLUSTER_LIST {}, filtering it", clusterIdentifier.getValue(), clusterList);
57                     return null;
58                 }
59             }
60         }
61
62         return attributes;
63     }
64 }