Remove duplicated code
[bgpcep.git] / bgp / l3vpn / src / main / java / org / opendaylight / protocol / bgp / l3vpn / AbstractVpnRIBSupport.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.l3vpn;
9
10 import com.google.common.base.Preconditions;
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.List;
16 import java.util.Optional;
17 import java.util.stream.Collectors;
18 import org.opendaylight.bgp.concepts.RouteDistinguisherUtil;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.protocol.bgp.labeled.unicast.LUNlriParser;
21 import org.opendaylight.protocol.bgp.labeled.unicast.LabeledUnicastIpv4RIBSupport;
22 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
23 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
24 import org.opendaylight.protocol.util.ByteArray;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.MplsLabeledVpnSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisher;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.RouteDistinguisherBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestination;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.destination.type.VpnDestinationBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRoute;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRouteBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.vpn.rev180329.l3vpn.ip.route.VpnRouteKey;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
47 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public abstract class AbstractVpnRIBSupport extends AbstractRIBSupport<VpnRoute, VpnRouteKey> {
57     private static final Logger LOG = LoggerFactory.getLogger(AbstractVpnRIBSupport.class);
58     private final NodeIdentifier nlriRoutesListNid;
59     private final NodeIdentifier prefixTypeNid;
60     private final NodeIdentifier labelStackNid;
61     private final NodeIdentifier lvNid;
62     private final NodeIdentifier rdNid;
63     private final QName routeKey;
64
65     /**
66      * Default constructor. Requires the QName of the container augmented under the routes choice
67      * node in instantiations of the rib grouping. It is assumed that this container is defined by
68      * the same model which populates it with route grouping instantiation, and by extension with
69      * the route attributes container.
70      *
71      * @param cazeClass      Binding class of the AFI/SAFI-specific case statement, must not be null
72      * @param containerClass Binding class of the container in routes choice, must not be null.
73      * @param listClass      Binding class of the route list, nust not be null;
74      */
75     protected AbstractVpnRIBSupport(final Class<? extends Routes> cazeClass,
76             final Class<? extends DataObject> containerClass, final Class<? extends Route> listClass,
77         final Class<? extends AddressFamily> afiClass, final QName vpnDstContainerClassQname) {
78         super(cazeClass, containerClass, listClass, afiClass,
79                 MplsLabeledVpnSubsequentAddressFamily.class, vpnDstContainerClassQname);
80         final QName classQname = BindingReflections.findQName(containerClass).intern();
81         this.routeKey = QName.create(routeQName(), ROUTE_KEY).intern();
82         final QName vpnDstClassQname = QName.create(classQname, VpnDestination.QNAME.getLocalName());
83         this.nlriRoutesListNid = NodeIdentifier.create(vpnDstClassQname);
84         this.prefixTypeNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "prefix").intern());
85         this.labelStackNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-stack").intern());
86         this.lvNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "label-value").intern());
87         this.rdNid = NodeIdentifier.create(QName.create(vpnDstClassQname, "route-distinguisher").intern());
88     }
89
90     private VpnDestination extractVpnDestination(final DataContainerNode<? extends PathArgument> route) {
91         final VpnDestination dst = new VpnDestinationBuilder()
92             .setPrefix(extractPrefix(route, this.prefixTypeNid))
93             .setLabelStack(LabeledUnicastIpv4RIBSupport.extractLabel(route, this.labelStackNid, this.lvNid))
94             .setRouteDistinguisher(extractRouteDistinguisher(route))
95             .build();
96         return dst;
97     }
98
99     protected abstract IpPrefix extractPrefix(DataContainerNode<? extends PathArgument> route,
100             NodeIdentifier prefixTypeNid);
101
102     private RouteDistinguisher extractRouteDistinguisher(
103             final DataContainerNode<? extends YangInstanceIdentifier.PathArgument> route) {
104         if (route.getChild(this.rdNid).isPresent()) {
105             return RouteDistinguisherBuilder.getDefaultInstance((String) route.getChild(this.rdNid).get().getValue());
106         }
107         return null;
108     }
109
110     protected abstract DestinationType getAdvertisedDestinationType(List<VpnDestination> dests);
111
112     protected abstract DestinationType getWithdrawnDestinationType(List<VpnDestination> dests);
113
114     @Override
115     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
116         return getAdvertisedDestinationType(extractRoutes(routes));
117     }
118
119     @Override
120     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
121         return getWithdrawnDestinationType(extractRoutes(routes));
122     }
123
124     private List<VpnDestination> extractRoutes(final Collection<MapEntryNode> routes) {
125         return routes.stream().map(this::extractVpnDestination).collect(Collectors.toList());
126     }
127
128     @Override
129     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
130         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
131         if (destination != null) {
132             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes =
133                     destination.getChild(this.nlriRoutesListNid);
134             if (maybeRoutes.isPresent()) {
135                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
136                 if (routes instanceof UnkeyedListNode) {
137                     final UnkeyedListNode routeListNode = (UnkeyedListNode) routes;
138                     LOG.debug("{} routes are found", routeListNode.getSize());
139                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(routeNid());
140                     for (final UnkeyedListEntryNode e : routeListNode.getValue()) {
141                         final NodeIdentifierWithPredicates key = createRouteKey(e);
142                         LOG.debug("Route {} is processed.", key);
143                         function.apply(tx, base, key, e, attributes);
144                     }
145                 } else {
146                     LOG.warn("Routes {} are not a map", routes);
147                 }
148             }
149         } else {
150             LOG.debug("Destination is null.");
151         }
152     }
153
154     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode l3vpn) {
155         final ByteBuf buffer = Unpooled.buffer();
156         final VpnDestination dests = new VpnDestinationBuilder().setPrefix(extractPrefix(l3vpn, this.prefixTypeNid))
157             .setRouteDistinguisher(extractRouteDistinguisher(l3vpn)).build();
158         final ByteBuf nlriByteBuf = Unpooled.buffer();
159
160         for (final VpnDestination dest : Collections.singletonList(dests)) {
161             final IpPrefix prefix = dest.getPrefix();
162             LOG.debug("Serializing Nlri: VpnDestination={}, IpPrefix={}", dest, prefix);
163             AbstractVpnNlriParser.serializeLengtField(prefix, null, nlriByteBuf);
164             RouteDistinguisherUtil.serializeRouteDistinquisher(dest.getRouteDistinguisher(), nlriByteBuf);
165             Preconditions.checkArgument(prefix.getIpv6Prefix() != null || prefix.getIpv4Prefix() != null,
166                     "Ipv6 or Ipv4 prefix is missing.");
167             LUNlriParser.serializePrefixField(prefix, nlriByteBuf);
168         }
169         buffer.writeBytes(nlriByteBuf);
170
171         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
172                 l3vpn.getChild(routePathIdNid());
173         return PathIdUtil.createNidKey(routeQName(), this.routeKey,
174                 pathIdQName(), ByteArray.encodeBase64(buffer), maybePathIdLeaf);
175     }
176
177     @Override
178     public final VpnRoute createRoute(final VpnRoute route, final String vpnRouteKey,
179             final long pathId, final Attributes attributes) {
180         final VpnRouteBuilder builder;
181         if (route != null) {
182             builder = new VpnRouteBuilder(route);
183         } else {
184             builder = new VpnRouteBuilder();
185         }
186         return builder.setKey(new VpnRouteKey(new PathId(pathId), vpnRouteKey)).setAttributes(attributes).build();
187     }
188
189     @Override
190     public VpnRouteKey createRouteListKey(final long pathId, final String vpnRouteKey) {
191         return new VpnRouteKey(new PathId(pathId), vpnRouteKey);
192     }
193 }