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.spi.registry;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.bgp.attribute.conditions.AsPathLength;
14 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.routing.policy.policy.definitions.policy.definition.statements.statement.conditions.BgpConditions;
15 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
16 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.BgpOriginAttrType;
17 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.AttributeComparison;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.AttributeEq;
19 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.AttributeGe;
20 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.policy.types.rev151009.AttributeLe;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.AsPath;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.LocalPref;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.MultiExitDisc;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.Origin;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.as.path.Segments;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AsPathSegment;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.CNextHop;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.EmptyNextHopCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv4NextHopCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.next.hop.c.next.hop.Ipv6NextHopCase;
35 * Bgp Attribute Conditions Util per check conditions matchs.
37 * @author Claudio D. Gasparini
39 final class BgpAttributeConditionsUtil {
40 private BgpAttributeConditionsUtil() {
41 throw new UnsupportedOperationException();
44 static boolean matchConditions(
45 final Class<? extends AfiSafiType> afiSafi,
46 final Attributes attributes,
47 final BgpConditions conditions) {
48 return matchAfiSafi(afiSafi, conditions.getAfiSafiIn())
49 && matchAsPathLength(attributes.getAsPath(), conditions.getAsPathLength())
50 && matchMED(attributes.getMultiExitDisc(), conditions.getMedEq())
51 && matchOrigin(attributes.getOrigin(), conditions.getOriginEq())
52 && matchNextHopIn(attributes.getCNextHop(), conditions.getNextHopIn())
53 && matchLocalPref(attributes.getLocalPref(), conditions.getLocalPrefEq());
56 private static boolean matchAfiSafi(
57 final Class<? extends AfiSafiType> afiSafi,
58 final List<Class<? extends AfiSafiType>> afiSafiIn) {
59 return afiSafiIn == null ? true : afiSafiIn.contains(afiSafi);
62 private static boolean matchMED(final MultiExitDisc multiExitDisc, final Long med) {
63 if (multiExitDisc == null || med == null) {
67 return multiExitDisc.getMed().equals(med);
70 private static boolean matchOrigin(final Origin origin, final BgpOriginAttrType originEq) {
71 if (origin == null || originEq == null) {
74 return origin.getValue().getIntValue() == originEq.getIntValue();
77 private static boolean matchAsPathLength(final AsPath asPath, final AsPathLength asPathLength) {
78 if (asPath == null || asPathLength == null) {
82 final List<Segments> segments = asPath.getSegments();
83 int total = segments.stream().map(AsPathSegment::getAsSequence)
84 .filter(Objects::nonNull).mapToInt(List::size).sum();
87 total = segments.stream().map(AsPathSegment::getAsSet)
88 .filter(Objects::nonNull).mapToInt(List::size).sum();
91 final Class<? extends AttributeComparison> comp = asPathLength.getOperator();
92 final long asPathLenght = asPathLength.getValue();
93 if (comp == AttributeEq.class) {
94 return total == asPathLenght;
95 } else if (comp == AttributeGe.class) {
96 return total >= asPathLenght;
97 } else if (comp == AttributeLe.class) {
98 return total <= asPathLenght;
104 private static boolean matchNextHopIn(final CNextHop nextHop, final List<IpAddress> nextHopIn) {
105 if (nextHop == null || nextHopIn == null || nextHop instanceof EmptyNextHopCase) {
110 if (nextHop instanceof Ipv4NextHopCase) {
111 global = new IpAddress(((Ipv4NextHopCase) nextHop).getIpv4NextHop().getGlobal());
113 global = new IpAddress(((Ipv6NextHopCase) nextHop).getIpv6NextHop().getGlobal());
115 return nextHopIn.contains(global);
118 private static boolean matchLocalPref(final LocalPref localPref, final Long localPrefEq) {
119 if (localPref == null || localPrefEq == null) {
122 return localPref.getPref().equals(localPrefEq);