BGPCEP-589: BGP Neighbor match
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / conditions / MatchBgpNeighborSetHandler.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions;
10
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.Optional;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.stream.Collectors;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.apache.commons.lang3.StringUtils;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
28 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.condition.BgpConditionsAugmentationPolicy;
29 import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
30 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
31 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.MatchSetOptionsRestrictedType;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.generic.defined.sets.NeighborSets;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSet;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSetKey;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.BgpNeighbor;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchBgpNeighborCondition;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.match.bgp.neighbor.grouping.MatchBgpNeighborSet;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 /**
46  * Match a set of Neighbors(ip address) (ANY, INVERT).
47  */
48 public final class MatchBgpNeighborSetHandler
49         implements BgpConditionsAugmentationPolicy<MatchBgpNeighborCondition, Void> {
50     private static final InstanceIdentifier<NeighborSets> NEIGHBOR_SET_IID
51             = InstanceIdentifier.create(RoutingPolicy.class)
52             .child(DefinedSets.class)
53             .child(NeighborSets.class);
54     private final DataBroker dataBroker;
55     private final LoadingCache<String, List<PeerId>> peerSets = CacheBuilder.newBuilder()
56             .build(new CacheLoader<String, List<PeerId>>() {
57                 @Override
58                 public List<PeerId> load(final String key) throws ExecutionException, InterruptedException {
59                     return loadRoleSets(key);
60                 }
61             });
62
63     public MatchBgpNeighborSetHandler(final DataBroker dataBroker) {
64         this.dataBroker = requireNonNull(dataBroker);
65     }
66
67     private List<PeerId> loadRoleSets(final String key) throws ExecutionException, InterruptedException {
68         final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
69         final Optional<NeighborSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
70                 NEIGHBOR_SET_IID.child(NeighborSet.class, new NeighborSetKey(key))).get();
71         if (!result.isPresent()) {
72             return Collections.emptyList();
73         }
74         return result.get().getNeighbor().stream()
75                 .map(nei -> RouterIds.createPeerId(nei.getAddress()))
76                 .collect(Collectors.toList());
77     }
78
79     @Override
80     public boolean matchImportCondition(
81             final RouteEntryBaseAttributes routeEntryInfo,
82             final BGPRouteEntryImportParameters importParameters,
83             final Void nonAttributres,
84             final MatchBgpNeighborCondition conditions) {
85         return matchBgpNeighborSetCondition(importParameters.getFromPeerId(), null,
86                 conditions.getMatchBgpNeighborSet());
87     }
88
89     @Override
90     public boolean matchExportCondition(
91             final RouteEntryBaseAttributes routeEntryInfo,
92             final BGPRouteEntryExportParameters exportParameters,
93             final Void nonAttributres,
94             final MatchBgpNeighborCondition conditions) {
95         return matchBgpNeighborSetCondition(exportParameters.getFromPeerId(), exportParameters.getToPeerId(),
96                 conditions.getMatchBgpNeighborSet());
97     }
98
99     private boolean matchBgpNeighborSetCondition(
100             @Nonnull final PeerId fromPeerId,
101             @Nullable final PeerId toPeerId,
102             final MatchBgpNeighborSet matchBgpNeighborSet) {
103
104         final BgpNeighbor from = matchBgpNeighborSet.getFromNeighbor();
105         Boolean match = null;
106         if (from != null) {
107             match = checkMatch(from.getNeighborSet(), fromPeerId, from.getMatchSetOptions());
108         }
109
110         if (match != null && !match) {
111             return false;
112         }
113
114         final BgpNeighbor to = matchBgpNeighborSet.getToNeighbor();
115         if (to != null) {
116             match = checkMatch(to.getNeighborSet(), toPeerId, to.getMatchSetOptions());
117         }
118
119         return match;
120     }
121
122     private boolean checkMatch(
123             final String neighborSetName,
124             final PeerId peerId,
125             final MatchSetOptionsRestrictedType matchSetOptions) {
126         final List<PeerId> roles = this.peerSets.getUnchecked(StringUtils
127                 .substringBetween(neighborSetName, "=\"", "\""));
128
129         final boolean found = roles.contains(peerId);
130         if (MatchSetOptionsRestrictedType.ANY.equals(matchSetOptions)) {
131             return found;
132         }
133         //INVERT
134         return !found;
135     }
136
137     @Override
138     public Void getConditionParameter(final Attributes attributes) {
139         return null;
140     }
141 }