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