Code clean up
[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.Collections;
13 import java.util.List;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv4RoutesCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.DestinationIpv4Builder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4Prefixes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.prefixes.destination.ipv4.Ipv4PrefixesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4Routes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.Ipv4RoutesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4Route;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv4.routes.ipv4.routes.Ipv4RouteKey;
28 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;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Ipv4PrefixAndPathId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
34 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36
37 /**
38  * Class supporting IPv4 unicast RIBs.
39  */
40 final class IPv4RIBSupport extends AbstractIPRibSupport<Ipv4RoutesCase, Ipv4Routes, Ipv4Route, Ipv4RouteKey> {
41
42     private static final Ipv4Routes EMPTY_CONTAINER
43             = new Ipv4RoutesBuilder().setIpv4Route(Collections.emptyList()).build();
44     private static final Ipv4RoutesCase EMPTY_CASE = new Ipv4RoutesCaseBuilder().setIpv4Routes(EMPTY_CONTAINER).build();
45     private static IPv4RIBSupport SINGLETON = null;
46
47     private IPv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
48         super(
49                 mappingService,
50                 Ipv4PrefixAndPathId.class,
51                 Ipv4AddressFamily.class,
52                 Ipv4RoutesCase.class,
53                 Ipv4Routes.class,
54                 Ipv4Route.class,
55                 DestinationIpv4.QNAME,
56                 Ipv4Prefixes.QNAME);
57     }
58
59     static synchronized IPv4RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
60         if (SINGLETON == null) {
61             SINGLETON = new IPv4RIBSupport(mappingService);
62         }
63         return SINGLETON;
64     }
65
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());
73         }
74         return prefs;
75     }
76
77     @Override
78     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
79         return new DestinationIpv4CaseBuilder().setDestinationIpv4(new DestinationIpv4Builder()
80                 .setIpv4Prefixes(extractPrefixes(routes)).build()).build();
81     }
82
83     @Override
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))
88                         .build()).build();
89     }
90
91     @Override
92     public Ipv4Route createRoute(final Ipv4Route route, final String routeKey, final long pathId,
93             final Attributes attributes) {
94         final Ipv4RouteBuilder builder;
95         if (route != null) {
96             builder = new Ipv4RouteBuilder(route);
97         } else {
98             builder = new Ipv4RouteBuilder();
99         }
100         builder.withKey(createRouteListKey(pathId, routeKey)).setAttributes(attributes);
101         return builder.build();
102     }
103
104     @Override
105     public Ipv4RoutesCase emptyRoutesCase() {
106         return EMPTY_CASE;
107     }
108
109     @Override
110     public Ipv4Routes emptyRoutesContainer() {
111         return EMPTY_CONTAINER;
112     }
113
114     @Override
115     public Ipv4RouteKey createRouteListKey(final long pathId, final String routeKey) {
116         return new Ipv4RouteKey(new PathId(pathId), routeKey);
117     }
118 }