Create common parent for extensions families
[bgpcep.git] / bgp / extensions / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / mcast / L3VpnMcastIpv4RIBSupport.java
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property. 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
9 package org.opendaylight.protocol.bgp.l3vpn.mcast;
10
11 import io.netty.buffer.ByteBuf;
12 import io.netty.buffer.Unpooled;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
17 import org.opendaylight.protocol.bgp.l3vpn.mcast.nlri.L3vpnMcastNlriSerializer;
18 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv4Case;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.bgp.rib.rib.loc.rib.tables.routes.L3vpnMcastRoutesIpv4CaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.destination.L3vpnMcastDestination;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.ipv4.L3vpnMcastRoutesIpv4;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.l3vpn.mcast.routes.ipv4.L3vpnMcastRoutesIpv4Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationL3vpnMcastIpv4AdvertizedCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.l3vpn.mcast.ipv4.advertized._case.DestinationIpv4L3vpnMcast;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.l3vpn.mcast.ipv4.advertized._case.DestinationIpv4L3vpnMcastBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationL3vpnMcastIpv4WithdrawnCaseBuilder;
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.YangInstanceIdentifier.NodeIdentifierWithPredicates;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
36 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
37 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
39
40 /**
41  * Ipv4 L3VPN Multicast RIBSupport.
42  *
43  * @author Claudio D. Gasparini
44  */
45 public final class L3VpnMcastIpv4RIBSupport
46         extends AbstractL3vpnMcastIpRIBSupport<L3vpnMcastRoutesIpv4Case, L3vpnMcastRoutesIpv4> {
47     private static final L3vpnMcastRoutesIpv4 EMPTY_CONTAINER
48             = new L3vpnMcastRoutesIpv4Builder().setL3vpnMcastRoute(Collections.emptyList()).build();
49     private static final L3vpnMcastRoutesIpv4Case EMPTY_CASE
50             = new L3vpnMcastRoutesIpv4CaseBuilder().setL3vpnMcastRoutesIpv4(EMPTY_CONTAINER).build();
51     private static L3VpnMcastIpv4RIBSupport SINGLETON;
52
53
54     private L3VpnMcastIpv4RIBSupport(final BindingNormalizedNodeSerializer mappingService) {
55         super(mappingService,
56                 L3vpnMcastRoutesIpv4Case.class,
57                 L3vpnMcastRoutesIpv4.class,
58                 Ipv4AddressFamily.class,
59                 DestinationIpv4L3vpnMcast.QNAME,
60                 L3vpnMcastDestination.QNAME);
61     }
62
63     public static synchronized L3VpnMcastIpv4RIBSupport getInstance(
64             final BindingNormalizedNodeSerializer mappingService) {
65         if (SINGLETON == null) {
66             SINGLETON = new L3VpnMcastIpv4RIBSupport(mappingService);
67         }
68         return SINGLETON;
69     }
70
71
72     @Override
73     protected DestinationL3vpnMcastIpv4AdvertizedCase buildDestination(final Collection<MapEntryNode> routes) {
74         return new DestinationL3vpnMcastIpv4AdvertizedCaseBuilder().setDestinationIpv4L3vpnMcast(
75                 new DestinationIpv4L3vpnMcastBuilder().setL3vpnMcastDestination(extractRoutes(routes)).build()).build();
76     }
77
78     @Override
79     protected DestinationL3vpnMcastIpv4WithdrawnCase buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
80         return new DestinationL3vpnMcastIpv4WithdrawnCaseBuilder().setDestinationIpv4L3vpnMcast(
81                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.l3vpn.mcast.rev180417.update
82                         .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.l3vpn.mcast.ipv4
83                         .withdrawn._case.DestinationIpv4L3vpnMcastBuilder()
84                         .setL3vpnMcastDestination(extractRoutes(routes)).build()).build();
85     }
86
87     @Override
88     public L3vpnMcastRoutesIpv4Case emptyRoutesCase() {
89         return EMPTY_CASE;
90     }
91
92     @Override
93     public L3vpnMcastRoutesIpv4 emptyRoutesContainer() {
94         return EMPTY_CONTAINER;
95     }
96
97     @Override
98     protected IpPrefix createPrefix(final String prefix) {
99         return new IpPrefix(new Ipv4Prefix(prefix));
100     }
101
102     @Override
103     public NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
104         final ByteBuf buffer = Unpooled.buffer();
105         final L3vpnMcastDestination dest = extractDestinations(l3vpn);
106         L3vpnMcastNlriSerializer.serializeNlri(Collections.singletonList(dest), buffer);
107         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
108                 l3vpn.getChild(routePathIdNid());
109         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
110                 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
111     }
112 }