BGPCEP-754: Rework EffectiveRibInWriter
[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.Collections;
13 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.DestinationIpv4;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.DestinationIpv4Builder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.Ipv4Routes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4RouteKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.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.types.rev130919.Ipv4AddressFamily;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34
35 /**
36  * Class supporting IPv4 unicast RIBs.
37  */
38 final class IPv4RIBSupport extends AbstractIPRibSupport<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
39         .yang.bgp.inet.rev171207.bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv4RoutesCase,
40         Ipv4Route, Ipv4RouteKey> {
41
42     private static final IPv4RIBSupport SINGLETON = new IPv4RIBSupport();
43
44     private IPv4RIBSupport() {
45         super(Ipv4PrefixAndPathId.class, Ipv4AddressFamily.class,
46                 Ipv4RoutesCase.class, Ipv4Routes.class, Ipv4Route.class, DestinationIpv4.QNAME, Ipv4Prefixes.QNAME);
47     }
48
49     static IPv4RIBSupport getInstance() {
50         return SINGLETON;
51     }
52
53     private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
54         final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
55         for (final MapEntryNode route : routes) {
56             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
57             final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
58             prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
59             prefs.add(prefixBuilder.build());
60         }
61         return prefs;
62     }
63
64     @Nonnull
65     @Override
66     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
67         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
68                 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
69     }
70
71     @Nonnull
72     @Override
73     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
74         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update
75                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
76                 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes))
77                         .build()).build();
78     }
79
80     @Override
81     public Ipv4RouteKey extractRouteKey(final Ipv4Route route) {
82         return route.getKey();
83     }
84
85     @Override
86     public Ipv4Route createRoute(final Ipv4Route route, final Ipv4RouteKey routeKey, final PathId pathId,
87             final Attributes attributes) {
88         final Ipv4RouteBuilder builder;
89         if (route != null) {
90             builder = new Ipv4RouteBuilder(route);
91         } else {
92             builder = new Ipv4RouteBuilder();
93         }
94         return builder.setPrefix(routeKey.getPrefix()).setPathId(pathId).setAttributes(attributes).build();
95     }
96
97     @Override
98     public Collection<Ipv4Route> changedRoutes(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
99             .bgp.inet.rev171207.bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv4RoutesCase routes) {
100         final Ipv4Routes routeCont = routes.getIpv4Routes();
101         if (routeCont == null) {
102             return Collections.emptyList();
103         }
104         return routeCont.getIpv4Route();
105     }
106
107     @Override
108     public Ipv4RouteKey createNewRouteKey(final PathId pathId, final Ipv4RouteKey routeKey) {
109         return new Ipv4RouteKey(pathId, routeKey.getPrefix());
110     }
111 }