BGP statistics CLI
[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.rev150305.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.DestinationIpv4Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.Ipv4Routes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv4.routes.ipv4.routes.Ipv4Route;
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.DestinationIpv4CaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.destination.DestinationType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28
29 /**
30  * Class supporting IPv4 unicast RIBs.
31  */
32 final class IPv4RIBSupport extends AbstractIPRIBSupport {
33
34     private static final IPv4RIBSupport SINGLETON = new IPv4RIBSupport();
35
36     private IPv4RIBSupport() {
37         super(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.Ipv4Prefix.class, Ipv4AddressFamily.class,
38                 Ipv4RoutesCase.class, Ipv4Routes.class, Ipv4Route.class, DestinationIpv4.QNAME, Ipv4Prefixes.QNAME);
39     }
40
41     static IPv4RIBSupport getInstance() {
42         return SINGLETON;
43     }
44
45     private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
46         final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
47         for (final MapEntryNode route : routes) {
48             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
49             final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
50             prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
51             prefs.add(prefixBuilder.build());
52         }
53         return prefs;
54     }
55
56     @Nonnull
57     @Override
58     protected DestinationType buildDestination(@Nonnull final Collection<MapEntryNode> routes) {
59         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes)).build()).build();
60     }
61
62     @Nonnull
63     @Override
64     protected DestinationType buildWithdrawnDestination(@Nonnull final Collection<MapEntryNode> routes) {
65         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder().setDestinationIpv4(
66                 new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes)).build()).build();
67     }
68 }