Extract routeKey/pathId from the identifier
[bgpcep.git] / bgp / extensions / 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.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.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCase;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.bgp.rib.rib.loc.rib.tables.routes.Ipv6RoutesCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.DestinationIpv6Builder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6Prefixes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.prefixes.destination.ipv6.Ipv6PrefixesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6Routes;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.Ipv6RoutesBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6Route;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.ipv6.routes.ipv6.routes.Ipv6RouteKey;
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.DestinationIpv6CaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv6AddressFamily;
33 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35
36 /**
37  * Class supporting IPv6 unicast RIBs.
38  */
39 final class IPv6RIBSupport extends AbstractIPRibSupport<Ipv6RoutesCase, Ipv6Routes, Ipv6Route, Ipv6RouteKey> {
40
41     private static final Ipv6Routes EMPTY_CONTAINER
42             = new Ipv6RoutesBuilder().setIpv6Route(Collections.emptyList()).build();
43     private static final Ipv6RoutesCase EMPTY_CASE = new Ipv6RoutesCaseBuilder().setIpv6Routes(EMPTY_CONTAINER).build();
44     private static IPv6RIBSupport SINGLETON;
45
46     private IPv6RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
47         super(mappingService,
48                 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.Ipv6Prefix.class,
49                 Ipv6AddressFamily.class,
50                 Ipv6RoutesCase.class,
51                 Ipv6Routes.class,
52                 Ipv6Route.class,
53                 DestinationIpv6.QNAME,
54                 Ipv6Prefixes.QNAME);
55     }
56
57     static synchronized IPv6RIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
58         if (SINGLETON == null) {
59             SINGLETON = new IPv6RIBSupport(mappingService);
60         }
61         return SINGLETON;
62     }
63
64     @Override
65     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
66         return new DestinationIpv6CaseBuilder().setDestinationIpv6(new DestinationIpv6Builder()
67                 .setIpv6Prefixes(extractPrefixes(routes)).build()).build();
68     }
69
70     @Override
71     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
72         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.inet.rev180329.update
73                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationIpv6CaseBuilder()
74                 .setDestinationIpv6(new DestinationIpv6Builder().setIpv6Prefixes(extractPrefixes(routes))
75                         .build()).build();
76     }
77
78     private List<Ipv6Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) {
79         final List<Ipv6Prefixes> prefs = new ArrayList<>(routes.size());
80         for (final MapEntryNode route : routes) {
81             final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue();
82             prefs.add(new Ipv6PrefixesBuilder().setPathId(PathIdUtil.buildPathId(route, routePathIdNid()))
83                     .setPrefix(new Ipv6Prefix(prefix)).build());
84         }
85         return prefs;
86     }
87
88     @Override
89     public Ipv6Route createRoute(final Ipv6Route route, final Ipv6RouteKey key, final Attributes attributes) {
90         final Ipv6RouteBuilder builder;
91         if (route != null) {
92             builder = new Ipv6RouteBuilder(route);
93         } else {
94             builder = new Ipv6RouteBuilder();
95         }
96         return builder.withKey(key).setAttributes(attributes).build();
97     }
98
99     @Override
100     public Ipv6RoutesCase emptyRoutesCase() {
101         return EMPTY_CASE;
102     }
103
104     @Override
105     public Ipv6Routes emptyRoutesContainer() {
106         return EMPTY_CONTAINER;
107     }
108
109     @Override
110     public Ipv6RouteKey createRouteListKey(final PathId pathId, final String routeKey) {
111         return new Ipv6RouteKey(pathId, routeKey);
112     }
113
114     @Override
115     public List<Ipv6Route> routesFromContainer(final Ipv6Routes container) {
116         return container.getIpv6Route();
117     }
118
119     @Override
120     public PathId extractPathId(final Ipv6RouteKey routeListKey) {
121         return routeListKey.getPathId();
122     }
123
124     @Override
125     public String extractRouteKey(final Ipv6RouteKey routeListKey) {
126         return routeListKey.getRouteKey();
127     }
128 }