Remove duplicated code for rib support
[bgpcep.git] / bgp / labeled-unicast / src / main / java / org / opendaylight / protocol / bgp / labeled / unicast / LabeledUnicastRIBSupport.java
1 /*
2  * Copyright (c) 2015 Cisco 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.labeled.unicast;
9
10 import com.google.common.base.Optional;
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.ArrayList;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
21 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
22 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
23 import org.opendaylight.protocol.util.ByteArray;
24 import org.opendaylight.protocol.util.Ipv4Util;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.LabeledUnicastSubsequentAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.bgp.rib.rib.loc.rib.tables.routes.LabeledUnicastRoutesCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStack;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.LabelStackBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestination;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.destination.CLabeledUnicastDestinationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.LabeledUnicastRoutes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.labeled.unicast.routes.labeled.unicast.routes.LabeledUnicastRoute;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLabeledUnicastCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicast;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlriBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlriBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.reach.nlri.AdvertizedRoutesBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.mp.unreach.nlri.WithdrawnRoutesBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.AddressFamily;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.network.concepts.rev131125.MplsLabel;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
55 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
57 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
58 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
63 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
64 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 public final class LabeledUnicastRIBSupport extends AbstractRIBSupport {
69
70     private static final QName PATHID_QNAME = QName.create(LabeledUnicastRoute.QNAME, "path-id").intern();
71     private static final Logger LOG = LoggerFactory.getLogger(LabeledUnicastRIBSupport.class);
72     private static final NodeIdentifier PREFIX_TYPE_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "prefix").intern());
73     private static final NodeIdentifier PATH_ID_LEAF = new NodeIdentifier(PATHID_QNAME);
74     private static final NodeIdentifier LABEL_STACK_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-stack").intern());
75     private static final NodeIdentifier LV_NID = NodeIdentifier.create(QName.create(CLabeledUnicastDestination.QNAME, "label-value").intern());
76
77     private static final QName ROUTE_KEY = QName.create(LabeledUnicastRoute.QNAME, "route-key").intern();
78
79     private static final ChoiceNode EMPTY_ROUTES = Builders.choiceBuilder()
80         .withNodeIdentifier(NodeIdentifier.create(Routes.QNAME))
81         .addChild(Builders.containerBuilder()
82             .withNodeIdentifier(NodeIdentifier.create(LabeledUnicastRoutes.QNAME))
83             .addChild(ImmutableNodes.mapNodeBuilder(LabeledUnicastRoute.QNAME).build()).build()).build();
84     private static final NodeIdentifier DESTINATION = NodeIdentifier.create(DestinationLabeledUnicast.QNAME);
85     private static final NodeIdentifier ROUTE = NodeIdentifier.create(LabeledUnicastRoute.QNAME);
86     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLabeledUnicastDestination.QNAME);
87
88     private final Class<? extends AddressFamily> afiType;
89
90     public LabeledUnicastRIBSupport(final Class<? extends AddressFamily> afiType) {
91         super(LabeledUnicastRoutesCase.class, LabeledUnicastRoutes.class, LabeledUnicastRoute.class);
92         this.afiType = afiType;
93     }
94
95     @Override
96     public ChoiceNode emptyRoutes() {
97         return EMPTY_ROUTES;
98     }
99
100     @Override
101     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
102         return ImmutableSet.of();
103     }
104
105     @Override
106     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
107         return ImmutableSet.of();
108     }
109
110     @Override
111     public boolean isComplexRoute() {
112         return true;
113     }
114
115     @Override
116     protected NodeIdentifier destinationContainerIdentifier() {
117         return DESTINATION;
118     }
119
120     private void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
121         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
122         if (destination != null) {
123             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(NLRI_ROUTES_LIST);
124             if (maybeRoutes.isPresent()) {
125                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
126                 if (routes instanceof UnkeyedListNode) {
127                     final YangInstanceIdentifier base = routesPath.node(routesContainerIdentifier()).node(ROUTE);
128                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode)routes).getValue()) {
129                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
130                         function.apply(tx, base, routeKey, e, attributes);
131                     }
132                 } else {
133                     LOG.warn("Routes {} are not a map", routes);
134                 }
135             }
136         }
137     }
138
139     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode labeledUnicast) {
140         final ByteBuf buffer = Unpooled.buffer();
141
142         final CLabeledUnicastDestination dest = extractCLabeledUnicastDestination(labeledUnicast);
143         LUNlriParser.serializeNlri(Collections.singletonList(dest), buffer);
144         return new NodeIdentifierWithPredicates(LabeledUnicastRoute.QNAME, ROUTE_KEY, ByteArray.readAllBytes(buffer));
145     }
146
147     @Override
148     protected void deleteDestinationRoutes(final DOMDataWriteTransaction tx,
149         final YangInstanceIdentifier tablePath, final ContainerNode destination, final NodeIdentifier routesNodeId) {
150         processDestination(tx, tablePath.node(routesNodeId), destination, null, DELETE_ROUTE);
151     }
152
153     @Override
154     protected void putDestinationRoutes(final DOMDataWriteTransaction tx,
155         final YangInstanceIdentifier tablePath, final ContainerNode destination,
156         final ContainerNode attributes, final NodeIdentifier routesNodeId) {
157         processDestination(tx, tablePath.node(routesNodeId), destination, attributes, this.putRoute);
158     }
159
160     @Override
161     protected MpReachNlri buildReach(final Collection<MapEntryNode> routes,
162         final CNextHop hop) {
163         final MpReachNlriBuilder mb = new MpReachNlriBuilder();
164         mb.setAfi(this.afiType);
165         mb.setSafi(LabeledUnicastSubsequentAddressFamily.class);
166         mb.setCNextHop(hop);
167
168         final List<CLabeledUnicastDestination> dests = new ArrayList<>(routes.size());
169         for (final MapEntryNode route : routes) {
170             dests.add(extractCLabeledUnicastDestination(route));
171         }
172         mb.setAdvertizedRoutes(new AdvertizedRoutesBuilder().setDestinationType(
173             new DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
174                 new DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(dests).build()).build()).build());
175         return mb.build();
176     }
177
178     @Override
179     protected MpUnreachNlri buildUnreach(final Collection<MapEntryNode> routes) {
180         final MpUnreachNlriBuilder mb = new MpUnreachNlriBuilder();
181         mb.setAfi(this.afiType);
182         mb.setSafi(LabeledUnicastSubsequentAddressFamily.class);
183
184         final List<CLabeledUnicastDestination> dests = new ArrayList<>(routes.size());
185         for (final MapEntryNode route : routes) {
186             dests.add(extractCLabeledUnicastDestination(route));
187         }
188         mb.setWithdrawnRoutes(new WithdrawnRoutesBuilder().setDestinationType(
189             new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLabeledUnicastCaseBuilder().setDestinationLabeledUnicast(
190                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.labeled.unicast.rev150525.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type.destination.labeled.unicast._case.DestinationLabeledUnicastBuilder().setCLabeledUnicastDestination(
191                     dests).build()).build()).build());
192         return mb.build();
193     }
194
195     // Conversion from DataContainer to LabeledUnicastDestination Object
196     private static CLabeledUnicastDestination extractCLabeledUnicastDestination(final DataContainerNode<? extends PathArgument> route) {
197         final CLabeledUnicastDestinationBuilder builder = new CLabeledUnicastDestinationBuilder();
198         builder.setPrefix(extractPrefix(route, PREFIX_TYPE_NID));
199         builder.setLabelStack(extractLabel(route, LABEL_STACK_NID, LV_NID));
200         builder.setPathId(PathIdUtil.buildPathId(route, PATH_ID_LEAF));
201         return builder.build();
202     }
203
204     public static IpPrefix extractPrefix(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier prefixTypeNid) {
205         if (route.getChild(prefixTypeNid).isPresent()) {
206             final String prefixType = (String) route.getChild(prefixTypeNid).get().getValue();
207             try {
208                 Ipv4Util.bytesForPrefixBegin(new Ipv4Prefix(prefixType));
209                 return new IpPrefix(new Ipv4Prefix(prefixType));
210             } catch (final IllegalArgumentException e) {
211                 LOG.debug("Creating Ipv6 prefix because", e);
212                 return new IpPrefix(new Ipv6Prefix(prefixType));
213             }
214         }
215         return null;
216     }
217
218     public static List<LabelStack> extractLabel(final DataContainerNode<? extends PathArgument> route, final NodeIdentifier labelStackNid, final NodeIdentifier labelValueNid) {
219         final List<LabelStack> labels = new ArrayList<>();
220         final Optional<DataContainerChild<? extends PathArgument, ?>> labelStacks = route.getChild(labelStackNid);
221         if (labelStacks.isPresent()) {
222             for (final UnkeyedListEntryNode label : ((UnkeyedListNode) labelStacks.get()).getValue()) {
223                 final Optional<DataContainerChild<? extends PathArgument, ?>> labelStack = label.getChild(labelValueNid);
224                 if (labelStack.isPresent()) {
225                     final LabelStackBuilder labelStackbuilder = new LabelStackBuilder();
226                     labelStackbuilder.setLabelValue(new MplsLabel((Long) labelStack.get().getValue()));
227                     labels.add(labelStackbuilder.build());
228                 }
229             }
230         }
231         return labels;
232     }
233
234     @Nonnull
235     @Override
236     public PathArgument getRouteIdAddPath(final long pathId, final PathArgument routeId) {
237         return PathIdUtil.createNiiKey(pathId, routeId, LabeledUnicastRoute.QNAME, PATHID_QNAME, ROUTE_KEY);
238     }
239
240     @Override
241     public Long extractPathId(final NormalizedNode<?, ?> data) {
242         return PathIdUtil.extractPathId(data, PATH_ID_LEAF);
243     }
244 }