Route Constrain policies
[bgpcep.git] / bgp / openconfig-rp-statement / src / main / java / org / opendaylight / protocol / bgp / openconfig / routing / policy / statement / conditions / VpnNonMemberHandler.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.statement.conditions;
10
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.RouteEntryBaseAttributes;
15 import org.opendaylight.protocol.bgp.openconfig.routing.policy.spi.policy.condition.BgpConditionsAugmentationPolicy;
16 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryExportParameters;
17 import org.opendaylight.protocol.bgp.rib.spi.policy.BGPRouteEntryImportParameters;
18 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.attributes.ExtendedCommunities;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteTarget;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.ExtendedCommunity;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.As4RouteTargetExtendedCommunityCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetExtendedCommunityCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.extended.community.extended.community.RouteTargetIpv4Case;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.constrain._default.route.grouping.RouteTargetConstrainDefaultRoute;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.route.target.constrain._default.route.grouping.RouteTargetConstrainDefaultRouteBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.odl.bgp._default.policy.rev180329.VpnNonMemberCondition;
29
30 /**
31  * Returns true if Route Target extended communities attributes are not part of the VPN membership of destiny peer.
32  *
33  * @author Claudio D. Gasparini
34  */
35 public final class VpnNonMemberHandler implements
36         BgpConditionsAugmentationPolicy<VpnNonMemberCondition, List<ExtendedCommunities>> {
37     private static final VpnNonMemberHandler INSTANCE = new VpnNonMemberHandler();
38     private static final RouteTargetConstrainDefaultRoute DEFAULT = new RouteTargetConstrainDefaultRouteBuilder()
39             .build();
40
41     private VpnNonMemberHandler() {
42
43     }
44
45     public static VpnNonMemberHandler getInstance() {
46         return INSTANCE;
47     }
48
49     @Override
50     public boolean matchImportCondition(
51             final Class<? extends AfiSafiType> afiSafiType,
52             final RouteEntryBaseAttributes routeEntryInfo,
53             final BGPRouteEntryImportParameters routeEntryImportParameters,
54             final List<ExtendedCommunities> attributes,
55             final VpnNonMemberCondition conditions) {
56         return false;
57     }
58
59     @Override
60     public boolean matchExportCondition(
61             final Class<? extends AfiSafiType> afiSafiType,
62             final RouteEntryBaseAttributes routeEntryInfo,
63             final BGPRouteEntryExportParameters routeEntryExportParameters,
64             final List<ExtendedCommunities> attributes,
65             final VpnNonMemberCondition conditions) {
66         final List<RouteTarget> allowedRouteTarget = routeEntryExportParameters.getMemberships();
67         if (allowedRouteTarget.contains(DEFAULT)) {
68             return false;
69         }
70         final List<RouteTarget> toRT = attributes.stream()
71                 .map(ext -> ext.getExtendedCommunity())
72                 .filter(this::filterRTExtComm)
73                 .map(this::extendedCommunityToRouteTarget)
74                 .collect(Collectors.toList());
75         return Collections.disjoint(allowedRouteTarget, toRT);
76     }
77
78     private RouteTarget extendedCommunityToRouteTarget(final ExtendedCommunity rt) {
79         if (rt instanceof RouteTargetExtendedCommunityCase) {
80             return ((RouteTargetExtendedCommunityCase) rt).getRouteTargetExtendedCommunity();
81         } else if (rt instanceof As4RouteTargetExtendedCommunityCase) {
82             return ((As4RouteTargetExtendedCommunityCase) rt).getAs4RouteTargetExtendedCommunity();
83         }
84         return ((RouteTargetIpv4Case) rt).getRouteTargetIpv4();
85     }
86
87     private boolean filterRTExtComm(final ExtendedCommunity rt) {
88         return rt instanceof RouteTargetExtendedCommunityCase || rt instanceof As4RouteTargetExtendedCommunityCase
89                 || rt instanceof RouteTargetIpv4Case;
90     }
91
92     @Override
93     public List<ExtendedCommunities> getConditionParameter(final Attributes attributes) {
94         return attributes.getExtendedCommunities();
95     }
96 }