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.Collections;
15 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
45 = new Ipv4RoutesBuilder().setIpv4Route(Collections.emptyList()).build();
46 private static IPv4RIBSupport SINGLETON = null;
48 private IPv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
51 Ipv4PrefixAndPathId.class,
52 Ipv4AddressFamily.class,
56 DestinationIpv4.QNAME,
60 static synchronized IPv4RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
61 if (SINGLETON == null) {
62 SINGLETON = new IPv4RIBSupport(mappingService);
67 private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
68 final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
69 for (final MapEntryNode route : routes) {
70 final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
71 final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
72 prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
73 prefs.add(prefixBuilder.build());
79 protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
80 return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
81 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
85 protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
86 return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
87 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
88 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes))
93 public Ipv4Route createRoute(final Ipv4Route route, final Ipv4RouteKey key, final Attributes attributes) {
94 final Ipv4RouteBuilder builder;
96 builder = new Ipv4RouteBuilder(route);
98 builder = new Ipv4RouteBuilder();
100 builder.withKey(key).setAttributes(attributes);
101 return builder.build();
105 public Ipv4Routes emptyRoutesContainer() {
106 return EMPTY_CONTAINER;
110 public Ipv4RouteKey createRouteListKey(final PathId pathId, final String routeKey) {
111 return new Ipv4RouteKey(pathId, routeKey);
115 public PathId extractPathId(final Ipv4RouteKey routeListKey) {
116 return routeListKey.getPathId();
120 public String extractRouteKey(final Ipv4RouteKey routeListKey) {
121 return routeListKey.getRouteKey();
125 public List<Ipv4Route> extractAdjRibInRoutes(final Routes routes) {
126 verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
127 .bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv4RoutesCase, "Unrecognized routes %s", routes);
128 return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.peer
129 .adj.rib.in.tables.routes.Ipv4RoutesCase) routes).getIpv4Routes().nonnullIpv4Route();