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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions;
11 import static java.util.Objects.requireNonNull;
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.policy.types.rev151009.MatchSetOptionsType;
32 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
33 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ClusterId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.ClusterIdentifier;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.BgpClusterIdSets;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.MatchClusterIdSetCondition;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.cluster.id.set.ClusterIdSet;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.cluster.id.set.ClusterIdSetKey;
41 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;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 * Match a set of Cluster Id(ALL, NAY, INVERT).
47 public final class MatchClusterIdSetHandler
48 implements BgpConditionsAugmentationPolicy<MatchClusterIdSetCondition, ClusterId> {
49 private static final InstanceIdentifier<ClusterIdSets> CLUSTERS_ID_SETS_IID
50 = InstanceIdentifier.create(RoutingPolicy.class).child(DefinedSets.class)
51 .augmentation(DefinedSets1.class).child(BgpDefinedSets.class)
52 .augmentation(BgpClusterIdSets.class).child(ClusterIdSets.class);
53 private final DataBroker dataBroker;
54 private final LoadingCache<String, ClusterIdSet> sets = CacheBuilder.newBuilder()
55 .build(new CacheLoader<String, ClusterIdSet>() {
57 public ClusterIdSet load(final String key) throws ExecutionException, InterruptedException {
62 public MatchClusterIdSetHandler(final DataBroker dataBroker) {
63 this.dataBroker = requireNonNull(dataBroker);
66 private ClusterIdSet loadSets(final String key) throws ExecutionException, InterruptedException {
67 final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
68 final Optional<ClusterIdSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
69 CLUSTERS_ID_SETS_IID.child(ClusterIdSet.class, new ClusterIdSetKey(key))).get();
70 return result.orNull();
74 public boolean matchImportCondition(
75 final RouteEntryBaseAttributes routeEntryInfo,
76 final BGPRouteEntryImportParameters importParameters,
77 final ClusterId clusterIdAtt,
78 final MatchClusterIdSetCondition conditions) {
79 final ClusterIdentifier clusterIdLocal = importParameters.getFromClusterId() == null
80 ? routeEntryInfo.getClusterId() : importParameters.getFromClusterId();
81 return matchClusterIdCondition(clusterIdLocal, clusterIdAtt,
82 conditions.getMatchClusterIdSetCondition());
86 public boolean matchExportCondition(
87 final RouteEntryBaseAttributes routeEntryInfo,
88 final BGPRouteEntryExportParameters exportParameters,
89 final ClusterId clusterIdAtt,
90 final MatchClusterIdSetCondition conditions) {
91 final ClusterIdentifier clusterIdLocal = exportParameters.getFromClusterId() == null
92 ? routeEntryInfo.getClusterId() : exportParameters.getFromClusterId();
93 return matchClusterIdCondition(clusterIdLocal, clusterIdAtt,
94 conditions.getMatchClusterIdSetCondition());
98 public ClusterId getConditionParameter(final Attributes attributes) {
99 return attributes.getClusterId();
102 private boolean matchClusterIdCondition(
103 final ClusterIdentifier localClusterId,
104 final ClusterId clusterId, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl
105 .bgp._default.policy.rev180329.match.cluster.id.set.condition.grouping
106 .MatchClusterIdSetCondition matchClusterIdSetCondition) {
107 final ClusterIdSet clusterIdSet = this.sets.getUnchecked(StringUtils
108 .substringBetween(matchClusterIdSetCondition.getClusterIdSet(), "=\"", "\""));
110 if (clusterIdSet == null) {
113 final MatchSetOptionsType matchOption = matchClusterIdSetCondition.getMatchSetOptions();
115 if (clusterId != null) {
116 List<ClusterIdentifier> newList = new ArrayList<>();
117 if (clusterIdSet.getClusterId() != null) {
118 newList.addAll(clusterIdSet.getClusterId());
120 if (clusterIdSet.getLocal() != null) {
121 newList.add(localClusterId);
124 final List<ClusterIdentifier> matchClusterList = clusterId.getCluster();
125 if (matchOption.equals(MatchSetOptionsType.ALL)) {
126 return matchClusterList.containsAll(newList) && newList.containsAll(matchClusterList);
128 final boolean noneInCommon = Collections.disjoint(matchClusterList, newList);
129 if (matchOption.equals(MatchSetOptionsType.ANY)) {
130 return !noneInCommon;
131 } else if (matchOption.equals(MatchSetOptionsType.INVERT)) {
134 } else if (matchOption.equals(MatchSetOptionsType.INVERT)) {