2 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Objects;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
24 import org.apache.commons.lang3.StringUtils;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.binding.api.ReadTransaction;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
29 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.condition.BgpConditionsPolicy;
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.policy.rev151009.DefinedSets1;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.bgp.match.conditions.MatchAsPathSet;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.BgpDefinedSets;
35 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.AsPathSets;
36 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.as.path.sets.AsPathSet;
37 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.bgp.defined.sets.as.path.sets.AsPathSetKey;
38 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
39 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.MatchSetOptionsType;
40 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
41 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPath;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AsPathSegment;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 * Match a set of AS (All, ANY, INVERT).
52 public final class MatchAsPathSetHandler implements BgpConditionsPolicy<MatchAsPathSet, AsPath> {
53 private static final InstanceIdentifier<AsPathSets> AS_PATHS_SETS_IID
54 = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
55 .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
56 .child(AsPathSets.class);
57 private final DataBroker dataBroker;
58 private final LoadingCache<String, AsPathSet> sets = CacheBuilder.newBuilder()
59 .build(new CacheLoader<String, AsPathSet>() {
61 public AsPathSet load(final String key) throws ExecutionException, InterruptedException {
66 public MatchAsPathSetHandler(final DataBroker dataBroker) {
67 this.dataBroker = requireNonNull(dataBroker);
70 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
71 justification = "https://github.com/spotbugs/spotbugs/issues/811")
72 private AsPathSet loadSets(final String key) throws ExecutionException, InterruptedException {
73 final ReadTransaction tr = this.dataBroker.newReadOnlyTransaction();
74 final Optional<AsPathSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
75 AS_PATHS_SETS_IID.child(AsPathSet.class, new AsPathSetKey(key))).get();
76 return result.orElse(null);
80 public boolean matchImportCondition(
81 final Class<? extends AfiSafiType> afiSafi,
82 final RouteEntryBaseAttributes routeEntryInfo,
83 final BGPRouteEntryImportParameters routeEntryImportParameters,
85 final MatchAsPathSet conditions) {
86 return matchAsPathSetCondition(asPath, conditions.getAsPathSet(),
87 conditions.getMatchSetOptions());
92 public boolean matchExportCondition(
93 final Class<? extends AfiSafiType> afiSafi,
94 final RouteEntryBaseAttributes routeEntryInfo,
95 final BGPRouteEntryExportParameters routeEntryExportParameters,
97 final MatchAsPathSet conditions) {
98 return matchAsPathSetCondition(asPath, conditions.getAsPathSet(),
99 conditions.getMatchSetOptions());
104 public AsPath getConditionParameter(final Attributes attributes) {
105 return attributes.getAsPath();
109 private boolean matchAsPathSetCondition(final AsPath asPath, final String asPathSetName,
110 final MatchSetOptionsType matchSetOptions) {
111 if (asPath == null) {
114 final AsPathSet asPathSetFilter = this.sets.getUnchecked(StringUtils
115 .substringBetween(asPathSetName, "=\"", "\""));
117 final List<Segments> segments = asPath.getSegments();
118 if (asPathSetFilter == null || segments == null) {
122 final List<AsNumber> l1 = segments.stream()
123 .map(AsPathSegment::getAsSequence)
124 .filter(Objects::nonNull)
125 .flatMap(Collection::stream)
126 .filter(Objects::nonNull)
127 .collect(Collectors.toList());
129 final List<AsNumber> l2 = segments.stream()
130 .map(AsPathSegment::getAsSet)
131 .filter(Objects::nonNull)
132 .flatMap(Collection::stream)
133 .filter(Objects::nonNull)
134 .collect(Collectors.toList());
136 List<AsNumber> allAs = Stream.of(l1, l2).flatMap(Collection::stream).collect(Collectors.toList());
138 final List<AsNumber> asPathSetFilterList = asPathSetFilter.getAsPathSetMember();
139 if (matchSetOptions.equals(MatchSetOptionsType.ALL)) {
140 return allAs.containsAll(asPathSetFilterList)
141 && asPathSetFilterList.containsAll(allAs);
143 final boolean noneInCommon = Collections.disjoint(allAs, asPathSetFilterList);
144 if (matchSetOptions.equals(MatchSetOptionsType.ANY)) {
145 return !noneInCommon;
147 //(matchSetOptions.equals(MatchSetOptionsType.INVERT))