BGPCEP-574: Remove unnesary methods
[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 javax.annotation.Nonnull;
14 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.DestinationIpv4;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.DestinationIpv4Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.Ipv4Routes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4Route;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv4.routes.ipv4.routes.Ipv4RouteKey;
25 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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Ipv4PrefixAndPathId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
31 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
33
34 /**
35  * Class supporting IPv4 unicast RIBs.
36  */
37 final class IPv4RIBSupport extends AbstractIPRibSupport<Ipv4Route, Ipv4RouteKey> {
38
39     private static final IPv4RIBSupport SINGLETON = new IPv4RIBSupport();
40
41     private IPv4RIBSupport() {
42         super(Ipv4PrefixAndPathId.class, Ipv4AddressFamily.class,
43                 Ipv4RoutesCase.class, Ipv4Routes.class, Ipv4Route.class, DestinationIpv4.QNAME, Ipv4Prefixes.QNAME);
44     }
45
46     static IPv4RIBSupport getInstance() {
47         return SINGLETON;
48     }
49
50     private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
51         final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
52         for (final MapEntryNode route : routes) {
53             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
54             final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
55             prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
56             prefs.add(prefixBuilder.build());
57         }
58         return prefs;
59     }
60
61     @Nonnull
62     @Override
63     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
64         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
65                 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
66     }
67
68     @Nonnull
69     @Override
70     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
71         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.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 Ipv4RouteKey routeKey, final PathId 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         return builder.setPrefix(routeKey.getPrefix()).setPathId(pathId).setAttributes(attributes).build();
87     }
88
89     @Override
90     public Ipv4RouteKey createNewRouteKey(final PathId pathId, final Ipv4RouteKey routeKey) {
91         return new Ipv4RouteKey(pathId, routeKey.getPrefix());
92     }
93 }