Fixup Augmentable and Identifiable methods changing
[bgpcep.git] / bgp / openconfig-rp-spi / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / spi / registry / BgpAttributeConditionsUtil.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
9 package org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.registry;
10
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;
33
34 /**
35  * Bgp Attribute Conditions Util per check conditions matchs.
36  *
37  * @author Claudio D. Gasparini
38  */
39 final class BgpAttributeConditionsUtil {
40     private BgpAttributeConditionsUtil() {
41         throw new UnsupportedOperationException();
42     }
43
44     static boolean matchConditions(
45             final Class<? extends AfiSafiType> afiSafi,
46             final Attributes attributes,
47             final BgpConditions conditions) {
48         if (!matchAfiSafi(afiSafi, conditions.getAfiSafiIn())) {
49             return false;
50         }
51
52         if (!matchAsPathLength(attributes.getAsPath(), conditions.getAsPathLength())) {
53             return false;
54         }
55
56         if (!matchMED(attributes.getMultiExitDisc(), conditions.getMedEq())) {
57             return false;
58         }
59
60         if (!matchOrigin(attributes.getOrigin(), conditions.getOriginEq())) {
61             return false;
62         }
63
64         if (!matchNextHopIn(attributes.getCNextHop(), conditions.getNextHopIn())) {
65             return false;
66         }
67
68         if (!matchLocalPref(attributes.getLocalPref(), conditions.getLocalPrefEq())) {
69             return false;
70         }
71         return true;
72     }
73
74     private static boolean matchAfiSafi(
75             final Class<? extends AfiSafiType> afiSafi,
76             final List<Class<? extends AfiSafiType>> afiSafiIn) {
77         if (afiSafiIn == null) {
78             return true;
79         }
80         return afiSafiIn.contains(afiSafi);
81     }
82
83     private static boolean matchMED(final MultiExitDisc multiExitDisc, final Long med) {
84         if (multiExitDisc == null || med == null) {
85             return true;
86         }
87
88         return multiExitDisc.getMed().equals(med);
89     }
90
91     private static boolean matchOrigin(final Origin origin, final BgpOriginAttrType originEq) {
92         if (origin == null || originEq == null) {
93             return true;
94         }
95         return origin.getValue().getIntValue() == originEq.getIntValue();
96     }
97
98     private static boolean matchAsPathLength(final AsPath asPath, final AsPathLength asPathLength) {
99         if (asPath == null || asPathLength == null) {
100             return true;
101         }
102
103         final List<Segments> segments = asPath.getSegments();
104         int total = segments.stream().map(AsPathSegment::getAsSequence)
105                 .filter(Objects::nonNull).mapToInt(List::size).sum();
106
107         if (total == 0) {
108             total = segments.stream().map(AsPathSegment::getAsSet)
109                     .filter(Objects::nonNull).mapToInt(List::size).sum();
110         }
111
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;
120         }
121         return false;
122     }
123
124
125     private static boolean matchNextHopIn(final CNextHop nextHop, final List<IpAddress> nextHopIn) {
126         if (nextHop == null || nextHopIn == null || nextHop instanceof EmptyNextHopCase) {
127             return true;
128         }
129
130         IpAddress global;
131         if (nextHop instanceof Ipv4NextHopCase) {
132             global = new IpAddress(((Ipv4NextHopCase) nextHop).getIpv4NextHop().getGlobal());
133         } else {
134             global = new IpAddress(((Ipv6NextHopCase) nextHop).getIpv6NextHop().getGlobal());
135         }
136         return nextHopIn.contains(global);
137     }
138
139     private static boolean matchLocalPref(final LocalPref localPref, final Long localPrefEq) {
140         if (localPref == null || localPrefEq == null) {
141             return true;
142         }
143         return localPref.getPref().equals(localPrefEq);
144     }
145 }