Route Constrain policies
[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.bgp.types.rev151009.AfiSafiType;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.MatchSetOptionsRestrictedType;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.generic.defined.sets.NeighborSets;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSet;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSetKey;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
38 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.BgpNeighbor;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.MatchBgpNeighborCondition;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.match.bgp.neighbor.grouping.MatchBgpNeighborSet;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 /**
47  * Match a set of Neighbors(ip address) (ANY, INVERT).
48  */
49 public final class MatchBgpNeighborSetHandler
50         implements BgpConditionsAugmentationPolicy<MatchBgpNeighborCondition, Void> {
51     private static final InstanceIdentifier<NeighborSets> NEIGHBOR_SET_IID
52             = InstanceIdentifier.create(RoutingPolicy.class)
53             .child(DefinedSets.class)
54             .child(NeighborSets.class);
55     private final DataBroker dataBroker;
56     private final LoadingCache<String, List<PeerId>> peerSets = CacheBuilder.newBuilder()
57             .build(new CacheLoader<String, List<PeerId>>() {
58                 @Override
59                 public List<PeerId> load(final String key) throws ExecutionException, InterruptedException {
60                     return loadRoleSets(key);
61                 }
62             });
63
64     public MatchBgpNeighborSetHandler(final DataBroker dataBroker) {
65         this.dataBroker = requireNonNull(dataBroker);
66     }
67
68     private List<PeerId> loadRoleSets(final String key) throws ExecutionException, InterruptedException {
69         final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
70         final Optional<NeighborSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
71                 NEIGHBOR_SET_IID.child(NeighborSet.class, new NeighborSetKey(key))).get();
72         if (!result.isPresent()) {
73             return Collections.emptyList();
74         }
75         return result.get().getNeighbor().stream()
76                 .map(nei -> RouterIds.createPeerId(nei.getAddress()))
77                 .collect(Collectors.toList());
78     }
79
80     @Override
81     public boolean matchImportCondition(
82             final Class<? extends AfiSafiType> afiSafi,
83             final RouteEntryBaseAttributes routeEntryInfo,
84             final BGPRouteEntryImportParameters importParameters,
85             final Void nonAttributres,
86             final MatchBgpNeighborCondition conditions) {
87         return matchBgpNeighborSetCondition(importParameters.getFromPeerId(), null,
88                 conditions.getMatchBgpNeighborSet());
89     }
90
91     @Override
92     public boolean matchExportCondition(
93             final Class<? extends AfiSafiType> afiSafi,
94             final RouteEntryBaseAttributes routeEntryInfo,
95             final BGPRouteEntryExportParameters exportParameters,
96             final Void nonAttributres,
97             final MatchBgpNeighborCondition conditions) {
98         return matchBgpNeighborSetCondition(exportParameters.getFromPeerId(), exportParameters.getToPeerId(),
99                 conditions.getMatchBgpNeighborSet());
100     }
101
102     private boolean matchBgpNeighborSetCondition(
103             @Nonnull final PeerId fromPeerId,
104             @Nullable final PeerId toPeerId,
105             final MatchBgpNeighborSet matchBgpNeighborSet) {
106
107         final BgpNeighbor from = matchBgpNeighborSet.getFromNeighbor();
108         Boolean match = null;
109         if (from != null) {
110             match = checkMatch(from.getNeighborSet(), fromPeerId, from.getMatchSetOptions());
111         }
112
113         if (match != null && !match) {
114             return false;
115         }
116
117         final BgpNeighbor to = matchBgpNeighborSet.getToNeighbor();
118         if (to != null) {
119             match = checkMatch(to.getNeighborSet(), toPeerId, to.getMatchSetOptions());
120         }
121
122         return match;
123     }
124
125     private boolean checkMatch(
126             final String neighborSetName,
127             final PeerId peerId,
128             final MatchSetOptionsRestrictedType matchSetOptions) {
129         final List<PeerId> roles = this.peerSets.getUnchecked(StringUtils
130                 .substringBetween(neighborSetName, "=\"", "\""));
131
132         final boolean found = roles.contains(peerId);
133         if (MatchSetOptionsRestrictedType.ANY.equals(matchSetOptions)) {
134             return found;
135         }
136         //INVERT
137         return !found;
138     }
139
140     @Override
141     public Void getConditionParameter(final Attributes attributes) {
142         return null;
143     }
144 }