}
}
+ grouping bgp-neighbor {
+ leaf neighbor-set {
+ type leafref {
+ path "/rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/rpol:neighbor-set/rpol:neighbor-set-name";
+ require-instance true;
+ }
+ description "References a defined neighbor set";
+ }
+ uses rpol:match-set-options-restricted-group;
+ }
+
+ grouping match-bgp-neighbor-grouping {
+ container match-bgp-neighbor-set {
+ presence
+ "The presence of this container indicates that the routes
+ should match the neighbour address of set referenced";
+
+ description
+ "Match a referenced neighbor set according to the logic
+ defined in the match-set-options-leaf";
+
+ container from-neighbor {
+ uses bgp-neighbor;
+ }
+
+ container to-neighbor {
+ uses bgp-neighbor;
+ }
+ }
+ }
+
grouping match-originator-id-set-condition-grouping {
description
"Match a list of referenced originator-id-set according to the logic
}
}
+ augment /rpol:routing-policy/rpol:policy-definitions/rpol:policy-definition/rpol:statements/rpol:statement/rpol:conditions/bgppol:bgp-conditions {
+ ext:augment-identifier match-bgp-neighbor-condition;
+ uses match-bgp-neighbor-grouping;
+ }
+
augment /rpol:routing-policy/rpol:policy-definitions/rpol:policy-definition/rpol:statements/rpol:statement/rpol:conditions/bgppol:bgp-conditions {
ext:augment-identifier match-role-set-condition;
uses match-role-set-condition-grouping;
final BGPRouteEntryExportParameters routeEntryExportParameters,
final Attributes attributes,
final Conditions conditions) {
- /* if (!this.genericConditionHandler
- .matchExportCondition(routeEntryExportParameters, conditions)) {
- return false;
- }*/
if (!this.bgpConditionsRegistry
.matchExportConditions(entryInfo, routeEntryExportParameters, attributes, conditions)) {
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions.SetExtCommunityHandler;
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.actions.SetOriginatorIdPrependHandler;
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions.MatchAsPathSetHandler;
+import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions.MatchBgpNeighborSetHandler;
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions.MatchClusterIdSetHandler;
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions.MatchCommunitySetHandler;
import org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions.MatchExtCommunitySetHandler;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetAsPathPrepend;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetCommunity;
import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.actions.bgp.actions.SetExtCommunity;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchBgpNeighborCondition;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchClusterIdSetCondition;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchOriginatorIdSetCondition;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchRoleSetCondition;
@Override
protected synchronized List<AutoCloseable> startImpl(final StatementRegistryProvider provider) {
- final List<AutoCloseable> registration = new ArrayList<>(12);
+ final List<AutoCloseable> registration = new ArrayList<>(13);
registerActions(provider, registration);
registerConditions(provider, registration);
return registration;
registration.add(provider.registerBgpConditionsPolicy(MatchCommunitySet.class,
new MatchCommunitySetHandler(this.dataBroker)));
+
+ registration.add(provider.registerBgpConditionsAugmentationPolicy(MatchBgpNeighborCondition.class,
+ new MatchBgpNeighborSetHandler(this.dataBroker)));
}
private void registerActions(final StatementRegistryProvider provider, final List<AutoCloseable> registration) {
--- /dev/null
+/*
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement.conditions;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Optional;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ExecutionException;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
+import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.condition.BgpConditionsAugmentationPolicy;
+import org.opendaylight.protocol.bgp.rib.spi.RouterIds;
+import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
+import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.MatchSetOptionsRestrictedType;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.generic.defined.sets.NeighborSets;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSet;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.neighbor.set.NeighborSetKey;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.RoutingPolicy;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.DefinedSets;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.BgpNeighbor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.MatchBgpNeighborCondition;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180109.match.bgp.neighbor.grouping.MatchBgpNeighborSet;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Match a set of Neighbors(ip address) (ANY, INVERT).
+ */
+public final class MatchBgpNeighborSetHandler
+ implements BgpConditionsAugmentationPolicy<MatchBgpNeighborCondition, Void> {
+ private static final InstanceIdentifier<NeighborSets> NEIGHBOR_SET_IID
+ = InstanceIdentifier.create(RoutingPolicy.class)
+ .child(DefinedSets.class)
+ .child(NeighborSets.class);
+ private final DataBroker dataBroker;
+ private final LoadingCache<String, List<PeerId>> peerSets = CacheBuilder.newBuilder()
+ .build(new CacheLoader<String, List<PeerId>>() {
+ @Override
+ public List<PeerId> load(final String key) throws ExecutionException, InterruptedException {
+ return loadRoleSets(key);
+ }
+ });
+
+ public MatchBgpNeighborSetHandler(final DataBroker dataBroker) {
+ this.dataBroker = requireNonNull(dataBroker);
+ }
+
+ private List<PeerId> loadRoleSets(final String key) throws ExecutionException, InterruptedException {
+ final ReadOnlyTransaction tr = this.dataBroker.newReadOnlyTransaction();
+ final Optional<NeighborSet> result = tr.read(LogicalDatastoreType.CONFIGURATION,
+ NEIGHBOR_SET_IID.child(NeighborSet.class, new NeighborSetKey(key))).get();
+ if (!result.isPresent()) {
+ return Collections.emptyList();
+ }
+ return result.get().getNeighbor().stream()
+ .map(nei -> RouterIds.createPeerId(nei.getAddress()))
+ .collect(Collectors.toList());
+ }
+
+ @Override
+ public boolean matchImportCondition(
+ final RouteEntryBaseAttributes routeEntryInfo,
+ final BGPRouteEntryImportParameters importParameters,
+ final Void nonAttributres,
+ final MatchBgpNeighborCondition conditions) {
+ return matchBgpNeighborSetCondition(importParameters.getFromPeerId(), null,
+ conditions.getMatchBgpNeighborSet());
+ }
+
+ @Override
+ public boolean matchExportCondition(
+ final RouteEntryBaseAttributes routeEntryInfo,
+ final BGPRouteEntryExportParameters exportParameters,
+ final Void nonAttributres,
+ final MatchBgpNeighborCondition conditions) {
+ return matchBgpNeighborSetCondition(exportParameters.getFromPeerId(), exportParameters.getToPeerId(),
+ conditions.getMatchBgpNeighborSet());
+ }
+
+ private boolean matchBgpNeighborSetCondition(
+ @Nonnull final PeerId fromPeerId,
+ @Nullable final PeerId toPeerId,
+ final MatchBgpNeighborSet matchBgpNeighborSet) {
+
+ final BgpNeighbor from = matchBgpNeighborSet.getFromNeighbor();
+ Boolean match = null;
+ if (from != null) {
+ match = checkMatch(from.getNeighborSet(), fromPeerId, from.getMatchSetOptions());
+ }
+
+ if (match != null && !match) {
+ return false;
+ }
+
+ final BgpNeighbor to = matchBgpNeighborSet.getToNeighbor();
+ if (to != null) {
+ match = checkMatch(to.getNeighborSet(), toPeerId, to.getMatchSetOptions());
+ }
+
+ return match;
+ }
+
+ private boolean checkMatch(
+ final String neighborSetName,
+ final PeerId peerId,
+ final MatchSetOptionsRestrictedType matchSetOptions) {
+ final List<PeerId> roles = this.peerSets.getUnchecked(StringUtils
+ .substringBetween(neighborSetName, "=\"", "\""));
+
+ final boolean found = roles.contains(peerId);
+ if (MatchSetOptionsRestrictedType.ANY.equals(matchSetOptions)) {
+ return found;
+ }
+ //INVERT
+ return !found;
+ }
+
+ @Override
+ public Void getConditionParameter(final Attributes attributes) {
+ return null;
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.protocol.bgp.openconfig.routing.policy.statement;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.doReturn;
+import static org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer.routeAttributeContainerFalse;
+
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.opendaylight.protocol.bgp.openconfig.routing.policy.impl.PolicyRIBBaseParametersImpl;
+import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry.RouteAttributeContainer;
+import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
+import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.routing.policy.rev151009.routing.policy.top.routing.policy.policy.definitions.policy.definition.statements.Statement;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.AttributesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev171207.PeerId;
+
+public class MatchBgpNeighborSetTest extends AbstractStatementRegistryConsumerTest {
+ @Mock
+ private BGPRouteEntryExportParameters exportParameters;
+ private List<Statement> basicStatements;
+ private PolicyRIBBaseParametersImpl baseAttributes;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ this.basicStatements = loadStatement("bgp-neighbor-statements-test");
+ this.baseAttributes = new PolicyRIBBaseParametersImpl(LOCAL_AS, IPV4, CLUSTER);
+ }
+
+
+ @Test
+ public void testMatchFromBgpNeighborAny() {
+ Statement statement = this.basicStatements.stream()
+ .filter(st -> st.getName().equals("reject-from-neighbor-test")).findFirst().get();
+ RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
+ new AttributesBuilder().build());
+
+ doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getFromPeerId();
+
+ RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNotNull(result.getAttributes());
+
+
+ doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getFromPeerId();
+ result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNull(result.getAttributes());
+ }
+
+ @Test
+ public void testMatchFromBgpNeighborInvert() {
+ Statement statement = this.basicStatements.stream()
+ .filter(st -> st.getName().equals("reject-from-neighbor-invert-test")).findFirst().get();
+ RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
+ new AttributesBuilder().build());
+
+ doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getFromPeerId();
+
+ RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNull(result.getAttributes());
+
+ doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getFromPeerId();
+ result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNotNull(result.getAttributes());
+ }
+
+ @Test
+ public void testMatchToBgpNeighborAny() {
+ Statement statement = this.basicStatements.stream()
+ .filter(st -> st.getName().equals("reject-to-neighbor-test")).findFirst().get();
+ RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
+ new AttributesBuilder().build());
+
+ doReturn(new PeerId("bgp://127.0.0.2")).when(this.exportParameters).getFromPeerId();
+ doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getToPeerId();
+
+ RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNotNull(result.getAttributes());
+
+
+ doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getToPeerId();
+ result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNull(result.getAttributes());
+ }
+
+ @Test
+ public void testMatchToBgpNeighborInvert() {
+ Statement statement = this.basicStatements.stream()
+ .filter(st -> st.getName().equals("reject-to-neighbor-invert-test")).findFirst().get();
+ RouteAttributeContainer attributeContainer = routeAttributeContainerFalse(
+ new AttributesBuilder().build());
+
+ doReturn(new PeerId("bgp://127.0.0.2")).when(this.exportParameters).getFromPeerId();
+ doReturn(new PeerId("bgp://42.42.42.42")).when(this.exportParameters).getToPeerId();
+
+ RouteAttributeContainer result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNull(result.getAttributes());
+
+ doReturn(new PeerId("bgp://127.0.0.1")).when(this.exportParameters).getToPeerId();
+ result = this.statementRegistry.applyExportStatement(
+ this.baseAttributes,
+ this.exportParameters,
+ attributeContainer,
+ statement);
+ assertNotNull(result.getAttributes());
+ }
+}
</statement>
</statements>
</policy-definition>
+ <policy-definition>
+ <name>bgp-neighbor-statements-test</name>
+ <statements>
+ <statement>
+ <name>reject-from-neighbor-test</name>
+ <conditions>
+ <bgp-conditions xmlns="http://openconfig.net/yang/bgp-policy">
+ <match-bgp-neighbor-set xmlns="urn:opendaylight:params:xml:ns:yang:odl:bgp:default:policy">
+ <from-neighbor>
+ <neighbor-set>
+ /rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/neighbor-set[neighbor-set-name="test-bgp-neighbor-set"
+ </neighbor-set>
+ </from-neighbor>
+ </match-bgp-neighbor-set>
+ </bgp-conditions>
+ </conditions>
+ <actions>
+ <reject-route/>
+ </actions>
+ </statement>
+ <statement>
+ <name>reject-from-neighbor-invert-test</name>
+ <conditions>
+ <bgp-conditions xmlns="http://openconfig.net/yang/bgp-policy">
+ <match-bgp-neighbor-set xmlns="urn:opendaylight:params:xml:ns:yang:odl:bgp:default:policy">
+ <from-neighbor>
+ <neighbor-set>
+ /rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/neighbor-set[neighbor-set-name="test-bgp-neighbor-set"
+ </neighbor-set>
+ <match-set-options>INVERT</match-set-options>
+ </from-neighbor>
+ </match-bgp-neighbor-set>
+ </bgp-conditions>
+ </conditions>
+ <actions>
+ <reject-route/>
+ </actions>
+ </statement>
+ <statement>
+ <name>reject-to-neighbor-test</name>
+ <conditions>
+ <bgp-conditions xmlns="http://openconfig.net/yang/bgp-policy">
+ <match-bgp-neighbor-set xmlns="urn:opendaylight:params:xml:ns:yang:odl:bgp:default:policy">
+ <to-neighbor>
+ <neighbor-set>
+ /rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/neighbor-set[neighbor-set-name="test-bgp-neighbor-set"
+ </neighbor-set>
+ </to-neighbor>
+ </match-bgp-neighbor-set>
+ </bgp-conditions>
+ </conditions>
+ <actions>
+ <reject-route/>
+ </actions>
+ </statement>
+ <statement>
+ <name>reject-to-neighbor-invert-test</name>
+ <conditions>
+ <bgp-conditions xmlns="http://openconfig.net/yang/bgp-policy">
+ <match-bgp-neighbor-set xmlns="urn:opendaylight:params:xml:ns:yang:odl:bgp:default:policy">
+ <to-neighbor>
+ <neighbor-set>
+ /rpol:routing-policy/rpol:defined-sets/rpol:neighbor-sets/neighbor-set[neighbor-set-name="test-bgp-neighbor-set"
+ </neighbor-set>
+ <match-set-options>INVERT</match-set-options>
+ </to-neighbor>
+ </match-bgp-neighbor-set>
+ </bgp-conditions>
+ </conditions>
+ <actions>
+ <reject-route/>
+ </actions>
+ </statement>
+ </statements>
+ </policy-definition>
</policy-definitions>
<defined-sets>
<bgp-defined-sets xmlns="http://openconfig.net/yang/bgp-policy">
</prefix>
</prefix-set>
</prefix-sets>
+ <neighbor-sets>
+ <neighbor-set>
+ <neighbor-set-name>test-bgp-neighbor-set</neighbor-set-name>
+ <neighbor>
+ <address>127.0.0.1</address>
+ </neighbor>
+ <neighbor>
+ <address>127.0.0.2</address>
+ </neighbor>
+ </neighbor-set>
+ </neighbor-sets>
</defined-sets>
</routing-policy>
\ No newline at end of file