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