Cleanup deprecation warnings in bgp/extensions/linkstate
[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 static com.google.common.base.Verify.verify;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.Unpooled;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
23 import org.opendaylight.protocol.bgp.linkstate.impl.nlri.LinkstateNlriParser;
24 import org.opendaylight.protocol.bgp.linkstate.spi.pojo.SimpleNlriTypeRegistry;
25 import org.opendaylight.protocol.bgp.parser.spi.PathIdUtil;
26 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateAddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.LinkstateSubsequentAddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.destination.CLinkstateDestination;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.LinkstateRoutes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.LinkstateRoutesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.LinkstateRoute;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.LinkstateRouteBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.linkstate.routes.linkstate.routes.LinkstateRouteKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.DestinationLinkstateCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstate;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstateBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.PathId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev200120.path.attributes.Attributes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.destination.DestinationType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
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 final class LinkstateRIBSupport
57         extends AbstractRIBSupport<LinkstateRoutesCase, LinkstateRoutes, LinkstateRoute, LinkstateRouteKey> {
58     private static final Logger LOG = LoggerFactory.getLogger(LinkstateRIBSupport.class);
59     private static final LinkstateRoutes EMPTY_CONTAINER = new LinkstateRoutesBuilder().build();
60     private static final NodeIdentifier NLRI_ROUTES_LIST = NodeIdentifier.create(CLinkstateDestination.QNAME);
61     private static LinkstateRIBSupport SINGLETON;
62
63     private LinkstateRIBSupport(final BindingNormalizedNodeSerializer mappingService) {
64         super(
65                 mappingService,
66                 LinkstateRoutesCase.class,
67                 LinkstateRoutes.class,
68                 LinkstateRoute.class,
69                 LinkstateAddressFamily.class,
70                 LinkstateSubsequentAddressFamily.class,
71                 DestinationLinkstate.QNAME);
72     }
73
74     public static synchronized LinkstateRIBSupport getInstance(final BindingNormalizedNodeSerializer mappingService) {
75         if (SINGLETON == null) {
76             SINGLETON = new LinkstateRIBSupport(mappingService);
77         }
78         return SINGLETON;
79     }
80
81     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode linkstate) {
82         final ByteBuf buffer = Unpooled.buffer();
83         final CLinkstateDestination cLinkstateDestination = LinkstateNlriParser.extractLinkstateDestination(linkstate);
84         SimpleNlriTypeRegistry.getInstance().serializeNlriType(cLinkstateDestination, buffer);
85         final Optional<DataContainerChild<? extends PathArgument, ?>> maybePathIdLeaf =
86                 linkstate.getChild(routePathIdNid());
87         return PathIdUtil.createNidKey(routeQName(), routeKeyTemplate(),
88                 ByteArray.encodeBase64(buffer), maybePathIdLeaf);
89     }
90
91     private static List<CLinkstateDestination> extractRoutes(final Collection<MapEntryNode> routes) {
92         return routes.stream().map(LinkstateNlriParser::extractLinkstateDestination).collect(Collectors.toList());
93     }
94
95     @Override
96     protected Collection<NodeIdentifierWithPredicates> processDestination(final DOMDataTreeWriteTransaction tx,
97                                                                           final YangInstanceIdentifier routesPath,
98                                                                           final ContainerNode destination,
99                                                                           final ContainerNode attributes,
100                                                                           final ApplyRoute function) {
101         if (destination != null) {
102             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes
103                     = destination.getChild(LinkstateRIBSupport.NLRI_ROUTES_LIST);
104             return processRoute(maybeRoutes, routesPath, attributes, function, tx);
105         }
106         return Collections.emptyList();
107     }
108
109     private List<NodeIdentifierWithPredicates> processRoute(
110             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes,
111             final YangInstanceIdentifier routesPath,
112             final ContainerNode attributes, final ApplyRoute function, final DOMDataTreeWriteTransaction tx) {
113         if (maybeRoutes.isPresent()) {
114             final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
115             if (routes instanceof UnkeyedListNode) {
116                 final YangInstanceIdentifier base = routesYangInstanceIdentifier(routesPath);
117                 final Collection<UnkeyedListEntryNode> routesList = ((UnkeyedListNode) routes).getValue();
118                 final List<NodeIdentifierWithPredicates> keys = new ArrayList<>(routesList.size());
119                 for (final UnkeyedListEntryNode linkstateDest : routesList) {
120                     final NodeIdentifierWithPredicates routeKey = createRouteKey(linkstateDest);
121                     function.apply(tx, base, routeKey, linkstateDest, attributes);
122                     keys.add(routeKey);
123                 }
124                 return keys;
125             }
126             LOG.warn("Routes {} are not a map", routes);
127         }
128         return Collections.emptyList();
129     }
130
131     @Override
132     protected DestinationType buildDestination(final Collection<MapEntryNode> routes) {
133         return new DestinationLinkstateCaseBuilder().setDestinationLinkstate(
134                 new DestinationLinkstateBuilder().setCLinkstateDestination(extractRoutes(routes)).build()).build();
135     }
136
137     @Override
138     protected DestinationType buildWithdrawnDestination(final Collection<MapEntryNode> routes) {
139         return new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120.update
140                 .attributes.mp.unreach.nlri.withdrawn.routes.destination.type.DestinationLinkstateCaseBuilder()
141                 .setDestinationLinkstate(
142                     new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
143                         .update.attributes.mp.unreach.nlri.withdrawn.routes.destination.type
144                         .destination.linkstate._case.DestinationLinkstateBuilder()
145                         .setCLinkstateDestination(extractRoutes(routes)).build())
146                 .build();
147     }
148
149     @Override
150     public LinkstateRoute createRoute(final LinkstateRoute route, final LinkstateRouteKey key,
151             final Attributes attributes) {
152         final LinkstateRouteBuilder builder;
153         if (route != null) {
154             builder = new LinkstateRouteBuilder(route);
155         } else {
156             builder = new LinkstateRouteBuilder();
157         }
158         return builder.withKey(key).setAttributes(attributes).build();
159     }
160
161     @Override
162     public LinkstateRoutes emptyRoutesContainer() {
163         return EMPTY_CONTAINER;
164     }
165
166     @Override
167     public LinkstateRouteKey createRouteListKey(final PathId pathId, final String routeKey) {
168         return new LinkstateRouteKey(pathId, routeKey);
169     }
170
171     @Override
172     public PathId extractPathId(final LinkstateRouteKey routeListKey) {
173         return routeListKey.getPathId();
174     }
175
176     @Override
177     public String extractRouteKey(final LinkstateRouteKey routeListKey) {
178         return routeListKey.getRouteKey();
179     }
180
181     @Override
182     public Map<LinkstateRouteKey, LinkstateRoute> extractAdjRibInRoutes(final Routes routes) {
183         verify(
184             routes instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
185                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.LinkstateRoutesCase, "Unrecognized routes %s", routes);
186         return ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev200120
187                 .bgp.rib.rib.peer.adj.rib.in.tables.routes.LinkstateRoutesCase) routes).getLinkstateRoutes()
188                 .nonnullLinkstateRoute();
189     }
190 }