Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bgp / inet / src / main / java / org / opendaylight / protocol / bgp / inet / IPv4RIBSupport.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.inet;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.peer.effective.rib.in.tables.routes.Ipv4RoutesCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv4CaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Ipv4PrefixAndPathId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35
36 /**
37  * Class supporting IPv4 unicast RIBs.
38  */
39 final class IPv4RIBSupport extends AbstractIPRibSupport<Ipv4Route, Ipv4RouteKey> {
40
41     private static final IPv4RIBSupport SINGLETON = new IPv4RIBSupport();
42
43     private IPv4RIBSupport() {
44         super(Ipv4PrefixAndPathId.class, Ipv4AddressFamily.class,
45                 Ipv4RoutesCase.class, Ipv4Routes.class, Ipv4Route.class, DestinationIpv4.QNAME, Ipv4Prefixes.QNAME);
46     }
47
48     static IPv4RIBSupport getInstance() {
49         return SINGLETON;
50     }
51
52     private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
53         final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
54         for (final MapEntryNode route : routes) {
55             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
56             final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
57             prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
58             prefs.add(prefixBuilder.build());
59         }
60         return prefs;
61     }
62
63     @Override
64     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
65         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
66                 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
67     }
68
69     @Override
70     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
71         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
72                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
73                 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes))
74                         .build()).build();
75     }
76
77     @Override
78     public Ipv4Route createRoute(final Ipv4Route route, final String routeKey, final long pathId,
79             final Attributes attributes) {
80         final Ipv4RouteBuilder builder;
81         if (route != null) {
82             builder = new Ipv4RouteBuilder(route);
83         } else {
84             builder = new Ipv4RouteBuilder();
85         }
86         builder.setKey(new Ipv4RouteKey(new PathId(pathId), routeKey)).setAttributes(attributes);
87         return builder.build();
88     }
89
90     @Override
91     public Routes emptyRoutesContainer() {
92         return new Ipv4RoutesCaseBuilder().setIpv4Routes(new Ipv4RoutesBuilder().build()).build();
93     }
94
95     @Override
96     public Ipv4RouteKey createRouteListKey(final long pathId, final String routeKey) {
97         return new Ipv4RouteKey(new PathId(pathId), routeKey);
98     }
99 }