Route Constrain policies
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / conditions / MatchAsPathSetHandler.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.Collection;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Objects;
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.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.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;
48
49 /**
50  * Match a set of AS (All, ANY, INVERT).
51  */
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>() {
60                 @Override
61                 public AsPathSet load(final String key) throws ExecutionException, InterruptedException {
62                     return loadSets(key);
63                 }
64             });
65
66     public MatchAsPathSetHandler(final DataBroker dataBroker) {
67         this.dataBroker = requireNonNull(dataBroker);
68     }
69
70     private AsPathSet loadSets(final String key) throws ExecutionException, InterruptedException {
71         final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
72         final Optional<AsPathSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
73                 AS_PATHS_SETS_IID.child(AsPathSet.class, new AsPathSetKey(key))).get();
74         return result.orNull();
75     }
76
77     @Override
78     public boolean matchImportCondition(
79             final Class<? extends AfiSafiType> afiSafi,
80             final RouteEntryBaseAttributes routeEntryInfo,
81             final BGPRouteEntryImportParameters routeEntryImportParameters,
82             final AsPath asPath,
83             final MatchAsPathSet conditions) {
84         return matchAsPathSetCondition(asPath, conditions.getAsPathSet(),
85                 conditions.getMatchSetOptions());
86     }
87
88
89     @Override
90     public boolean matchExportCondition(
91             final Class<? extends AfiSafiType> afiSafi,
92             final RouteEntryBaseAttributes routeEntryInfo,
93             final BGPRouteEntryExportParameters routeEntryExportParameters,
94             final AsPath asPath,
95             final MatchAsPathSet conditions) {
96         return matchAsPathSetCondition(asPath, conditions.getAsPathSet(),
97                 conditions.getMatchSetOptions());
98
99     }
100
101     @Override
102     public AsPath getConditionParameter(final Attributes attributes) {
103         return attributes.getAsPath();
104     }
105
106
107     private boolean matchAsPathSetCondition(final AsPath asPath, final String asPathSetName,
108             final MatchSetOptionsType matchSetOptions) {
109         if (asPath == null) {
110             return false;
111         }
112         final AsPathSet asPathSetFilter = this.sets.getUnchecked(StringUtils
113                 .substringBetween(asPathSetName, "=\"", "\""));
114
115         final List<Segments> segments = asPath.getSegments();
116         if (asPathSetFilter == null || segments == null) {
117             return false;
118         }
119
120         final List<AsNumber> l1 = segments.stream()
121                 .map(AsPathSegment::getAsSequence)
122                 .filter(Objects::nonNull)
123                 .flatMap(Collection::stream)
124                 .filter(Objects::nonNull)
125                 .collect(Collectors.toList());
126
127         final List<AsNumber> l2 = segments.stream()
128                 .map(AsPathSegment::getAsSet)
129                 .filter(Objects::nonNull)
130                 .flatMap(Collection::stream)
131                 .filter(Objects::nonNull)
132                 .collect(Collectors.toList());
133
134         List<AsNumber> allAs = Stream.of(l1, l2).flatMap(Collection::stream).collect(Collectors.toList());
135
136         final List<AsNumber> asPathSetFilterList = asPathSetFilter.getAsPathSetMember();
137         if (matchSetOptions.equals(MatchSetOptionsType.ALL)) {
138             return allAs.containsAll(asPathSetFilterList)
139                     && asPathSetFilterList.containsAll(allAs);
140         }
141         final boolean noneInCommon = Collections.disjoint(allAs, asPathSetFilterList);
142         if (matchSetOptions.equals(MatchSetOptionsType.ANY)) {
143             return !noneInCommon;
144         }
145         //(matchSetOptions.equals(MatchSetOptionsType.INVERT))
146         return noneInCommon;
147     }
148 }