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 if (!matchAfiSafi(afiSafi, conditions.getAfiSafiIn())) {
52 if (!matchAsPathLength(attributes.getAsPath(), conditions.getAsPathLength())) {
56 if (!matchMED(attributes.getMultiExitDisc(), conditions.getMedEq())) {
60 if (!matchOrigin(attributes.getOrigin(), conditions.getOriginEq())) {
64 if (!matchNextHopIn(attributes.getCNextHop(), conditions.getNextHopIn())) {
68 if (!matchLocalPref(attributes.getLocalPref(), conditions.getLocalPrefEq())) {
74 private static boolean matchAfiSafi(
75 final Class<? extends AfiSafiType> afiSafi,
76 final List<Class<? extends AfiSafiType>> afiSafiIn) {
77 if (afiSafiIn == null) {
80 return afiSafiIn.contains(afiSafi);
83 private static boolean matchMED(final MultiExitDisc multiExitDisc, final Long med) {
84 if (multiExitDisc == null || med == null) {
88 return multiExitDisc.getMed().equals(med);
91 private static boolean matchOrigin(final Origin origin, final BgpOriginAttrType originEq) {
92 if (origin == null || originEq == null) {
95 return origin.getValue().getIntValue() == originEq.getIntValue();
98 private static boolean matchAsPathLength(final AsPath asPath, final AsPathLength asPathLength) {
99 if (asPath == null || asPathLength == null) {
103 final List<Segments> segments = asPath.getSegments();
104 int total = segments.stream().map(AsPathSegment::getAsSequence)
105 .filter(Objects::nonNull).mapToInt(List::size).sum();
108 total = segments.stream().map(AsPathSegment::getAsSet)
109 .filter(Objects::nonNull).mapToInt(List::size).sum();
112 final Class<? extends AttributeComparison> comp = asPathLength.getOperator();
113 final long asPathLenght = asPathLength.getValue();
114 if (comp == AttributeEq.class) {
115 return total == asPathLenght;
116 } else if (comp == AttributeGe.class) {
117 return total >= asPathLenght;
118 } else if (comp == AttributeLe.class) {
119 return total <= asPathLenght;
125 private static boolean matchNextHopIn(final CNextHop nextHop, final List<IpAddress> nextHopIn) {
126 if (nextHop == null || nextHopIn == null || nextHop instanceof EmptyNextHopCase) {
131 if (nextHop instanceof Ipv4NextHopCase) {
132 global = new IpAddress(((Ipv4NextHopCase) nextHop).getIpv4NextHop().getGlobal());
134 global = new IpAddress(((Ipv6NextHopCase) nextHop).getIpv6NextHop().getGlobal());
136 return nextHopIn.contains(global);
139 private static boolean matchLocalPref(final LocalPref localPref, final Long localPrefEq) {
140 if (localPref == null || localPrefEq == null) {
143 return localPref.getPref().equals(localPrefEq);