2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.inet;
10 import static com.google.common.base.Verify.verify;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
17 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Ipv4PrefixAndPathId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
40 * Class supporting IPv4 unicast RIBs.
42 final class IPv4RIBSupport extends AbstractIPRibSupport<Ipv4RoutesCase, Ipv4Routes, Ipv4Route, Ipv4RouteKey> {
44 private static final Ipv4Routes EMPTY_CONTAINER = new Ipv4RoutesBuilder().build();
45 private static IPv4RIBSupport SINGLETON = null;
47 private IPv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
50 Ipv4PrefixAndPathId.class,
51 Ipv4AddressFamily.class,
55 DestinationIpv4.QNAME,
59 static synchronized IPv4RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
60 if (SINGLETON == null) {
61 SINGLETON = new IPv4RIBSupport(mappingService);
66 private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
67 final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
68 for (final MapEntryNode route : routes) {
69 final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
70 final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
71 prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
72 prefs.add(prefixBuilder.build());
78 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
79 return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
80 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
84 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
85 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
86 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
87 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes))
92 public Ipv4Route createRoute(final Ipv4Route route, final Ipv4RouteKey key, final Attributes attributes) {
93 final Ipv4RouteBuilder builder;
95 builder = new Ipv4RouteBuilder(route);
97 builder = new Ipv4RouteBuilder();
99 builder.withKey(key).setAttributes(attributes);
100 return builder.build();
104 public Ipv4Routes emptyRoutesContainer() {
105 return EMPTY_CONTAINER;
109 public Ipv4RouteKey createRouteListKey(final PathId pathId, final String routeKey) {
110 return new Ipv4RouteKey(pathId, routeKey);
114 public PathId extractPathId(final Ipv4RouteKey routeListKey) {
115 return routeListKey.getPathId();
119 public String extractRouteKey(final Ipv4RouteKey routeListKey) {
120 return routeListKey.getRouteKey();
124 public Map<Ipv4RouteKey, Ipv4Route> extractAdjRibInRoutes(final Routes routes) {
125 verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
126 .bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv4RoutesCase, "Unrecognized routes %s", routes);
127 return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.peer
128 .adj.rib.in.tables.routes.Ipv4RoutesCase) routes).getIpv4Routes().nonnullIpv4Route();