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