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