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