Route Constrain policies
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / conditions / MatchClusterIdSetHandler.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.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.concurrent.ExecutionException;
21 import org.apache.commons.lang3.StringUtils;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
26 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.condition.BgpConditionsAugmentationPolicy;
27 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
28 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.DefinedSets1;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.defined.sets.BgpDefinedSets;
31 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.MatchSetOptionsType;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
34 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.BgpClusterIdSets;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.MatchClusterIdSetCondition;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.cluster.id.set.ClusterIdSet;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.cluster.id.set.ClusterIdSetKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.routing.policy.defined.sets.bgp.defined.sets.ClusterIdSets;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 /**
46  * Match a set of Cluster Id(ALL, NAY, INVERT).
47  */
48 public final class MatchClusterIdSetHandler
49         implements BgpConditionsAugmentationPolicy<MatchClusterIdSetCondition, ClusterId> {
50     private static final InstanceIdentifier<ClusterIdSets> CLUSTERS_ID_SETS_IID
51             = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
52             .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
53             .augmentation(BgpClusterIdSets.class).child(ClusterIdSets.class);
54     private final DataBroker dataBroker;
55     private final LoadingCache<String, ClusterIdSet> sets = CacheBuilder.newBuilder()
56             .build(new CacheLoader<String, ClusterIdSet>() {
57                 @Override
58                 public ClusterIdSet load(final String key) throws ExecutionException, InterruptedException {
59                     return loadSets(key);
60                 }
61             });
62
63     public MatchClusterIdSetHandler(final DataBroker dataBroker) {
64         this.dataBroker = requireNonNull(dataBroker);
65     }
66
67     private ClusterIdSet loadSets(final String key) throws ExecutionException, InterruptedException {
68         final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
69         final Optional<ClusterIdSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
70                 CLUSTERS_ID_SETS_IID.child(ClusterIdSet.class, new ClusterIdSetKey(key))).get();
71         return result.orNull();
72     }
73
74     @Override
75     public boolean matchImportCondition(
76             final Class<? extends AfiSafiType> afiSafi,
77             final RouteEntryBaseAttributes routeEntryInfo,
78             final BGPRouteEntryImportParameters importParameters,
79             final ClusterId clusterIdAtt,
80             final MatchClusterIdSetCondition conditions) {
81         final ClusterIdentifier clusterIdLocal = importParameters.getFromClusterId() == null
82                 ? routeEntryInfo.getClusterId() : importParameters.getFromClusterId();
83         return matchClusterIdCondition(clusterIdLocal, clusterIdAtt,
84                 conditions.getMatchClusterIdSetCondition());
85     }
86
87     @Override
88     public boolean matchExportCondition(
89             final Class<? extends AfiSafiType> afiSafi,
90             final RouteEntryBaseAttributes routeEntryInfo,
91             final BGPRouteEntryExportParameters exportParameters,
92             final ClusterId clusterIdAtt,
93             final MatchClusterIdSetCondition conditions) {
94         final ClusterIdentifier clusterIdLocal = exportParameters.getFromClusterId() == null
95                 ? routeEntryInfo.getClusterId() : exportParameters.getFromClusterId();
96         return matchClusterIdCondition(clusterIdLocal, clusterIdAtt,
97                 conditions.getMatchClusterIdSetCondition());
98     }
99
100     @Override
101     public ClusterId getConditionParameter(final Attributes attributes) {
102         return attributes.getClusterId();
103     }
104
105     private boolean matchClusterIdCondition(
106             final ClusterIdentifier localClusterId,
107             final ClusterId clusterId, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl
108             .bgp._default.policy.rev180329.match.cluster.id.set.condition.grouping
109             .MatchClusterIdSetCondition matchClusterIdSetCondition) {
110         final ClusterIdSet clusterIdSet = this.sets.getUnchecked(StringUtils
111                 .substringBetween(matchClusterIdSetCondition.getClusterIdSet(), "=\"", "\""));
112
113         if (clusterIdSet == null) {
114             return false;
115         }
116         final MatchSetOptionsType matchOption = matchClusterIdSetCondition.getMatchSetOptions();
117
118         if (clusterId != null) {
119             List<ClusterIdentifier> newList = new ArrayList<>();
120             if (clusterIdSet.getClusterId() != null) {
121                 newList.addAll(clusterIdSet.getClusterId());
122             }
123             if (clusterIdSet.getLocal() != null) {
124                 newList.add(localClusterId);
125             }
126
127             final List<ClusterIdentifier> matchClusterList = clusterId.getCluster();
128             if (matchOption.equals(MatchSetOptionsType.ALL)) {
129                 return matchClusterList.containsAll(newList) && newList.containsAll(matchClusterList);
130             }
131             final boolean noneInCommon = Collections.disjoint(matchClusterList, newList);
132             if (matchOption.equals(MatchSetOptionsType.ANY)) {
133                 return !noneInCommon;
134             } else if (matchOption.equals(MatchSetOptionsType.INVERT)) {
135                 return noneInCommon;
136             }
137         } else if (matchOption.equals(MatchSetOptionsType.INVERT)) {
138             return true;
139         }
140         return false;
141     }
142 }