02ea19bca16711c3a60aaba6934ab8d99cb59592
[bgpcep.git] / bgp / extensions / 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 static com.google.common.base.Verify.verify;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
30 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;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.Ipv4PrefixAndPathId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
39
40 /**
41  * Class supporting IPv4 unicast RIBs.
42  */
43 final class IPv4RIBSupport extends AbstractIPRibSupport<Ipv4RoutesCase, Ipv4Routes, Ipv4Route, Ipv4RouteKey> {
44
45     private static final Ipv4Routes EMPTY_CONTAINER
46             = new Ipv4RoutesBuilder().setIpv4Route(Collections.emptyList()).build();
47     private static IPv4RIBSupport SINGLETON = null;
48
49     private IPv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
50         super(
51                 mappingService,
52                 Ipv4PrefixAndPathId.class,
53                 Ipv4AddressFamily.class,
54                 Ipv4RoutesCase.class,
55                 Ipv4Routes.class,
56                 Ipv4Route.class,
57                 DestinationIpv4.QNAME,
58                 Ipv4Prefixes.QNAME);
59     }
60
61     static synchronized IPv4RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
62         if (SINGLETON == null) {
63             SINGLETON = new IPv4RIBSupport(mappingService);
64         }
65         return SINGLETON;
66     }
67
68     private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
69         final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size());
70         for (final MapEntryNode route : routes) {
71             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
72             final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix));
73             prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid()));
74             prefs.add(prefixBuilder.build());
75         }
76         return prefs;
77     }
78
79     @Override
80     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
81         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
82                 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
83     }
84
85     @Override
86     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
87         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
88                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv4CaseBuilder()
89                 .setDestinationIpv4(new DestinationIpv4Builder().setIpv4Prefixes(extractPrefixes(routes))
90                         .build()).build();
91     }
92
93     @Override
94     public Ipv4Route createRoute(final Ipv4Route route, final Ipv4RouteKey key, final Attributes attributes) {
95         final Ipv4RouteBuilder builder;
96         if (route != null) {
97             builder = new Ipv4RouteBuilder(route);
98         } else {
99             builder = new Ipv4RouteBuilder();
100         }
101         builder.withKey(key).setAttributes(attributes);
102         return builder.build();
103     }
104
105     @Override
106     public Ipv4Routes emptyRoutesContainer() {
107         return EMPTY_CONTAINER;
108     }
109
110     @Override
111     public Ipv4RouteKey createRouteListKey(final PathId pathId, final String routeKey) {
112         return new Ipv4RouteKey(pathId, routeKey);
113     }
114
115     @Override
116     public PathId extractPathId(final Ipv4RouteKey routeListKey) {
117         return routeListKey.getPathId();
118     }
119
120     @Override
121     public String extractRouteKey(final Ipv4RouteKey routeListKey) {
122         return routeListKey.getRouteKey();
123     }
124
125     @Override
126     public Map<Ipv4RouteKey, Ipv4Route> extractAdjRibInRoutes(final Routes routes) {
127         verify(routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329
128             .bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv4RoutesCase, "Unrecognized routes %s", routes);
129         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.peer
130                 .adj.rib.in.tables.routes.Ipv4RoutesCase) routes).getIpv4Routes().nonnullIpv4Route();
131     }
132 }