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