Fixed some sonar issues
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / IPv6RIBSupport.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.rib.impl;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.DestinationIpv6Builder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.Ipv6Routes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.ipv6.routes.ipv6.routes.Ipv6Route;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
38
39 /**
40  * Class supporting IPv6 unicast RIBs.
41  */
42 final class IPv6RIBSupport extends AbstractIPRIBSupport {
43     private static final QName PREFIX_QNAME = QName.cachedReference(QName.create(Ipv6Route.QNAME, "prefix"));
44     private static final IPv6RIBSupport SINGLETON = new IPv6RIBSupport();
45     private final ChoiceNode emptyRoutes = Builders.choiceBuilder()
46             .withNodeIdentifier(new NodeIdentifier(Routes.QNAME))
47             .addChild(Builders.containerBuilder()
48                 .withNodeIdentifier(new NodeIdentifier(Ipv6Routes.QNAME))
49                 .withChild(ImmutableNodes.mapNodeBuilder(Ipv6Route.QNAME).build()).build()).build();
50     private final NodeIdentifier destination = new NodeIdentifier(DestinationIpv6.QNAME);
51     private final NodeIdentifier route = new NodeIdentifier(Ipv6Route.QNAME);
52     private final NodeIdentifier nlriRoutesList = new NodeIdentifier(Ipv6Prefixes.QNAME);
53     private final NodeIdentifier routeKeyLeaf = new NodeIdentifier(PREFIX_QNAME);
54
55     private IPv6RIBSupport() {
56         super(Ipv6RoutesCase.class, Ipv6Routes.class, Ipv6Route.class);
57     }
58
59     static IPv6RIBSupport getInstance() {
60         return SINGLETON;
61     }
62
63     @Override
64     public ChoiceNode emptyRoutes() {
65         return this.emptyRoutes;
66     }
67
68     @Override
69     protected NodeIdentifier destinationContainerIdentifier() {
70         return this.destination;
71     }
72
73     @Override
74     protected NodeIdentifier routeIdentifier() {
75         return this.route;
76     }
77
78     @Override
79     protected NodeIdentifier routeKeyLeafIdentifier() {
80         return this.routeKeyLeaf;
81     }
82
83     @Override
84     protected NodeIdentifier nlriRoutesListIdentifier() {
85         return this.nlriRoutesList;
86     }
87
88     @Override
89     protected QName keyLeafQName() {
90         return PREFIX_QNAME;
91     }
92
93     @Override
94     protected QName routeQName() {
95         return Ipv6Route.QNAME;
96     }
97
98     private List<Ipv6Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
99         final List<Ipv6Prefixes> prefs = new ArrayList<>(routes.size());
100         for (final MapEntryNode ipv6Route : routes) {
101             final String prefix = (String) ipv6Route.getChild(this.routeKeyLeaf).get().getValue();
102             prefs.add(new Ipv6PrefixesBuilder().setPrefix(new Ipv6Prefix(prefix)).build());
103         }
104         return prefs;
105     }
106
107     @Override
108     protected MpReachNlri buildReach(final Collection<MapEntryNode> routes, final CNextHop hop) {
109         final MpReachNlriBuilder mb = new MpReachNlriBuilder();
110         mb.setAfi(Ipv6AddressFamily.class);
111         mb.setSafi(UnicastSubsequentAddressFamily.class);
112         mb.setCNextHop(hop);
113         mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
114             new DestinationIpv6CaseBuilder().setDestinationIpv6(
115                 new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes)).build()).build()).build());
116         return mb.build();
117     }
118
119     @Override
120     protected MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
121         final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
122         mb.setAfi(Ipv6AddressFamily.class);
123         mb.setSafi(UnicastSubsequentAddressFamily.class);
124         mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
125             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev150305.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder().setDestinationIpv6(
126                 new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes)).build()).build()).build());
127         return mb.build();
128     }
129 }