56eb5ee73e809a108c25452518f4de5ccd2aaf96
[bgpcep.git] / bgp / inet / src / main / java / org / opendaylight / protocol / bgp / inet / 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.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.protocol.bgp.parser.spi.PathIdUtil;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.DestinationIpv6;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.DestinationIpv6Builder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.Ipv6Routes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6Route;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6RouteBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.ipv6.routes.ipv6.routes.Ipv6RouteKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationIpv6CaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.PathId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.destination.DestinationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv6AddressFamily;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
32
33 /**
34  * Class supporting IPv6 unicast RIBs.
35  */
36 final class IPv6RIBSupport extends AbstractIPRibSupport<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns
37         .yang.bgp.inet.rev171207.bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv6RoutesCase, Ipv6Route, Ipv6RouteKey> {
38
39     private static final IPv6RIBSupport SINGLETON = new IPv6RIBSupport();
40
41     private IPv6RIBSupport() {
42         super(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.Ipv6Prefix.class,
43                 Ipv6AddressFamily.class, Ipv6RoutesCase.class, Ipv6Routes.class, Ipv6Route.class,
44                 DestinationIpv6.QNAME, Ipv6Prefixes.QNAME);
45     }
46
47     static IPv6RIBSupport getInstance() {
48         return SINGLETON;
49     }
50
51     @Override
52     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
53         return new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder()
54                 .setIpv6Prefixes(extractPrefixes(routes)).build()).build();
55     }
56
57     @Override
58     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
59         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev171207.update
60                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder()
61                 .setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes))
62                         .build()).build();
63     }
64
65     private List<Ipv6Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
66         final List<Ipv6Prefixes> prefs = new ArrayList<>(routes.size());
67         for (final MapEntryNode route : routes) {
68             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
69             prefs.add(new Ipv6PrefixesBuilder().setPathId(PathIdUtil.buildPathId(route, routePathIdNid()))
70                     .setPrefix(new Ipv6Prefix(prefix)).build());
71         }
72         return prefs;
73     }
74
75     @Override
76     public Ipv6RouteKey extractRouteKey(final Ipv6Route route) {
77         return route.getKey();
78     }
79
80     @Override
81     public Ipv6Route createRoute(final Ipv6Route route, final Ipv6RouteKey routeKey, final PathId pathId,
82             final Attributes attributes) {
83         final Ipv6RouteBuilder builder;
84         if (route != null) {
85             builder = new Ipv6RouteBuilder(route);
86         } else {
87             builder = new Ipv6RouteBuilder();
88         }
89         return builder.setPrefix(routeKey.getPrefix()).setPathId(pathId).setAttributes(attributes).build();
90     }
91
92     @Override
93     public Collection<Ipv6Route> changedRoutes(final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang
94             .bgp.inet.rev171207.bgp.rib.rib.peer.adj.rib.in.tables.routes.Ipv6RoutesCase routes) {
95         final Ipv6Routes routeCont = routes.getIpv6Routes();
96         if (routeCont == null) {
97             return Collections.emptyList();
98         }
99         return routeCont.getIpv6Route();
100     }
101
102     @Override
103     public Ipv6RouteKey createNewRouteKey(final PathId pathId, final Ipv6RouteKey routeKey) {
104         return new Ipv6RouteKey(pathId, routeKey.getPrefix());
105     }
106 }