BUG-4827: Introduce Advertise N Paths Selection Mode
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / IPv6RIBSupport.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.collect.ImmutableCollection;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.Ipv6Routes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6Route;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41
42 /**
43  * Class supporting IPv6 unicast RIBs.
44  */
45 final class IPv6RIBSupport extends AbstractIPRIBSupport {
46     private static final QName PREFIX_QNAME = QName.create(Ipv6Route.QNAME, "prefix").intern();
47     private static final IPv6RIBSupport SINGLETON = new IPv6RIBSupport();
48     private static final ImmutableCollection<Class<? extends DataObject>> CACHEABLE_NLRI_OBJECTS =
49             ImmutableSet.<Class<? extends DataObject>>of(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.Ipv6Prefix.class);
50     private final ChoiceNode emptyRoutes = Builders.choiceBuilder()
51             .withNodeIdentifier(new NodeIdentifier(Routes.QNAME))
52             .addChild(Builders.containerBuilder()
53                 .withNodeIdentifier(new NodeIdentifier(Ipv6Routes.QNAME))
54                 .withChild(ImmutableNodes.mapNodeBuilder(Ipv6Route.QNAME).build()).build()).build();
55     private final NodeIdentifier destination = new NodeIdentifier(DestinationIpv6.QNAME);
56     private final NodeIdentifier route = new NodeIdentifier(Ipv6Route.QNAME);
57     private final NodeIdentifier nlriRoutesList = new NodeIdentifier(Ipv6Prefixes.QNAME);
58     private final NodeIdentifier routeKeyLeaf = new NodeIdentifier(PREFIX_QNAME);
59
60     private IPv6RIBSupport() {
61         super(Ipv6RoutesCase.class, Ipv6Routes.class, Ipv6Route.class);
62     }
63
64     static IPv6RIBSupport getInstance() {
65         return SINGLETON;
66     }
67
68     @Override
69     public ChoiceNode emptyRoutes() {
70         return this.emptyRoutes;
71     }
72
73     @Override
74     protected NodeIdentifier destinationContainerIdentifier() {
75         return this.destination;
76     }
77
78     @Override
79     protected NodeIdentifier routeIdentifier() {
80         return this.route;
81     }
82
83     @Override
84     protected NodeIdentifier routeKeyLeafIdentifier() {
85         return this.routeKeyLeaf;
86     }
87
88     @Override
89     protected NodeIdentifier nlriRoutesListIdentifier() {
90         return this.nlriRoutesList;
91     }
92
93     @Override
94     protected QName keyLeafQName() {
95         return PREFIX_QNAME;
96     }
97
98     @Override
99     protected QName routeQName() {
100         return Ipv6Route.QNAME;
101     }
102
103     @Override
104     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
105         return CACHEABLE_NLRI_OBJECTS;
106     }
107
108     private List<Ipv6Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
109         final List<Ipv6Prefixes> prefs = new ArrayList<>(routes.size());
110         for (final MapEntryNode ipv6Route : routes) {
111             final String prefix = (String) ipv6Route.getChild(this.routeKeyLeaf).get().getValue();
112             prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix(prefix)).build());
113         }
114         return prefs;
115     }
116
117     @Override
118     protected MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
119         final MpReachNlriBuilder mb = new MpReachNlriBuilder();
120         mb.setAfi(Ipv6AddressFamily.class);
121         mb.setSafi(UnicastSubsequentAddressFamily.class);
122         mb.setCNextHop(hop);
123         mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
124             new DestinationIpv6CaseBuilder().setDestinationIpv6(
125                 new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes)).build()).build()).build());
126         return mb.build();
127     }
128
129     @Override
130     protected MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
131         final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
132         mb.setAfi(Ipv6AddressFamily.class);
133         mb.setSafi(UnicastSubsequentAddressFamily.class);
134         mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
135             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder().setDestinationIpv6(
136                 new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes)).build()).build()).build());
137         return mb.build();
138     }
139 }