Create common parent for extensions families
[bgpcep.git] / bgp / extensions / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / impl / LinkstateRIBSupport.java
1 /*
2  * Copyright (c) 2016 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.linkstate.impl;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.Unpooled;
12 import java.util.Arrays;
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.controller.md.sal.dom.api.DOMDataWriteTransaction;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.protocol.bgp.linkstate.impl.nlri.LinkstateNlriParser;
21 import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry;
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.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateAddressFamily;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.LinkstateSubsequentAddressFamily;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.destination.CLinkstateDestination;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.LinkstateRoutesBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRoute;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.linkstate.routes.linkstate.routes.LinkstateRouteKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstate;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.PathId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public final class LinkstateRIBSupport
53         extends AbstractRIBSupport<LinkstateRoutesCase, LinkstateRoutes, LinkstateRoute, LinkstateRouteKey> {
54     private static final Logger LOG = LoggerFactory.getLogger(LinkstateRIBSupport.class);
55
56     private static final LinkstateRoutes EMPTY_CONTAINER
57             = new LinkstateRoutesBuilder().setLinkstateRoute(Collections.emptyList()).build();
58     private static final LinkstateRoutesCase EMPTY_CASE
59             = new LinkstateRoutesCaseBuilder().setLinkstateRoutes(EMPTY_CONTAINER).build();
60     private static LinkstateRIBSupport SINGLETON;
61     private final YangInstanceIdentifier.NodeIdentifier nlriRoutesList
62             = new YangInstanceIdentifier.NodeIdentifier(CLinkstateDestination.QNAME);
63
64     private LinkstateRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
65         super(
66                 mappingService,
67                 LinkstateRoutesCase.class,
68                 LinkstateRoutes.class,
69                 LinkstateRoute.class,
70                 LinkstateAddressFamily.class,
71                 LinkstateSubsequentAddressFamily.class,
72                 DestinationLinkstate.QNAME);
73     }
74
75     public synchronized static LinkstateRIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
76         if(SINGLETON == null){
77             SINGLETON = new LinkstateRIBSupport(mappingService);
78         }
79         return SINGLETON;
80     }
81
82     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode linkstate) {
83         final ByteBuf buffer = Unpooled.buffer();
84         final CLinkstateDestination cLinkstateDestination = LinkstateNlriParser.extractLinkstateDestination(linkstate);
85         SimpleNlriTypeRegistry.getInstance().serializeNlriType(cLinkstateDestination, buffer);
86         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
87                 linkstate.getChild(routePathIdNid());
88         return PathIdUtil.createNidKey(routeQName(), routeKeyQName(),
89                 pathIdQName(), Arrays.toString(ByteArray.readAllBytes(buffer)), maybePathIdLeaf);
90     }
91
92     private static List<CLinkstateDestination> extractRoutes(final Collection<MapEntryNode> routes) {
93         return routes.stream().map(LinkstateNlriParser::extractLinkstateDestination).collect(Collectors.toList());
94     }
95
96     @Override
97     protected void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier routesPath,
98             final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
99         if (destination != null) {
100             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes
101                     = destination.getChild(this.nlriRoutesList);
102             processRoute(maybeRoutes, routesPath, attributes, function, tx);
103         }
104     }
105
106     private void processRoute(final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes,
107             final YangInstanceIdentifier routesPath,
108             final ContainerNode attributes, final ApplyRoute function, final DOMDataWriteTransaction tx) {
109         if (maybeRoutes.isPresent()) {
110             final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
111             if (routes instanceof UnkeyedListNode) {
112                 final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
113                 for (final UnkeyedListEntryNode e : ((UnkeyedListNode) routes).getValue()) {
114                     final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
115                     function.apply(tx, base, routeKey, e, attributes);
116                 }
117             } else {
118                 LOG.warn("Routes {} are not a map", routes);
119             }
120         }
121     }
122
123     @Override
124     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
125         return new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
126                 new DestinationLinkstateBuilder().setCLinkstateDestination(extractRoutes(routes)).build()).build();
127     }
128
129     @Override
130     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
131         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev180329.update
132                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder()
133                 .setDestinationLinkstate(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp
134                         .linkstate.rev180329.update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
135                         .destination.linkstate._case.DestinationLinkstateBuilder()
136                         .setCLinkstateDestination(extractRoutes(routes)).build()).build();
137     }
138
139     @Override
140     public LinkstateRoute createRoute(final LinkstateRoute route, final String routeKey,
141             final long pathId, final Attributes attributes) {
142         final LinkstateRouteBuilder builder;
143         if (route != null) {
144             builder = new LinkstateRouteBuilder(route);
145         } else {
146             builder = new LinkstateRouteBuilder();
147         }
148         return builder.withKey(createRouteListKey(pathId, routeKey)).setAttributes(attributes).build();
149     }
150
151     @Override
152     public LinkstateRoutesCase emptyRoutesCase() {
153         return EMPTY_CASE;
154     }
155
156     @Override
157     public LinkstateRoutes emptyRoutesContainer() {
158         return EMPTY_CONTAINER;
159     }
160
161     @Override
162     public LinkstateRouteKey createRouteListKey(final long pathId, final String routeKey) {
163         return new LinkstateRouteKey(new PathId(pathId), routeKey);
164     }
165 }