BUG-2982 : moved path-attributes container to grouping
[bgpcep.git] / bgp / linkstate / src / main / java / org / opendaylight / protocol / bgp / linkstate / LinkstateRIBSupport.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.linkstate;
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 org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
17 import org.opendaylight.protocol.bgp.linkstate.nlri.LinkstateNlriParser;
18 import org.opendaylight.protocol.bgp.rib.spi.AbstractRIBSupport;
19 import org.opendaylight.protocol.util.ByteArray;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.bgp.rib.rib.loc.rib.tables.routes.LinkstateRoutesCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.destination.CLinkstateDestination;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.LinkstateRoutes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.linkstate.routes.linkstate.routes.LinkstateRoute;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.linkstate.rev150210.update.attributes.mp.reach.nlri.advertized.routes.destination.type.destination.linkstate._case.DestinationLinkstate;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
39 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 final class LinkstateRIBSupport extends AbstractRIBSupport {
47     private static abstract class ApplyRoute {
48         abstract void apply(DOMDataWriteTransaction tx, YangInstanceIdentifier base, NodeIdentifierWithPredicates routeKey, DataContainerNode<?> route, final ContainerNode attributes);
49     }
50
51     private static final class DeleteRoute extends ApplyRoute {
52         @Override
53         void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey, final DataContainerNode<?> route, final ContainerNode attributes) {
54             tx.delete(LogicalDatastoreType.OPERATIONAL, base.node(routeKey));
55         }
56     }
57
58     private final class PutRoute extends ApplyRoute {
59         @Override
60         void apply(final DOMDataWriteTransaction tx, final YangInstanceIdentifier base, final NodeIdentifierWithPredicates routeKey,
61             final DataContainerNode<?> route, final ContainerNode attributes) {
62             final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> b = ImmutableNodes.mapEntryBuilder();
63             b.withNodeIdentifier(routeKey);
64
65             // FIXME: All route children, there should be a utility somewhere to do this
66             for (final DataContainerChild<? extends PathArgument, ?> child : route.getValue()) {
67                 b.withChild(child);
68             }
69             // Add attributes
70             final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> cb = Builders.containerBuilder(attributes);
71             cb.withNodeIdentifier(routeAttributesIdentifier());
72             b.withChild(cb.build());
73             tx.put(LogicalDatastoreType.OPERATIONAL, base.node(routeKey), b.build());
74         }
75     }
76
77     private static final Logger LOG = LoggerFactory.getLogger(LinkstateRIBSupport.class);
78
79     private static final QName ROUTE_KEY = QName.cachedReference(QName.create(LinkstateRoute.QNAME, "route-key"));
80     private static final LinkstateRIBSupport SINGLETON = new LinkstateRIBSupport();
81     private static final ApplyRoute DELETE_ROUTE = new DeleteRoute();
82
83     private final ChoiceNode emptyRoutes = Builders.choiceBuilder()
84         .withNodeIdentifier(new NodeIdentifier(Routes.QNAME))
85         .addChild(Builders.containerBuilder()
86             .withNodeIdentifier(new NodeIdentifier(LinkstateRoutes.QNAME))
87             .addChild(ImmutableNodes.mapNodeBuilder(LinkstateRoute.QNAME).build()).build()).build();
88     private final NodeIdentifier destination = new NodeIdentifier(DestinationLinkstate.QNAME);
89     private final NodeIdentifier route = new NodeIdentifier(LinkstateRoute.QNAME);
90     private final NodeIdentifier nlriRoutesList = new NodeIdentifier(CLinkstateDestination.QNAME);
91     private final ApplyRoute putRoute = new PutRoute();
92
93     private LinkstateRIBSupport() {
94        super(LinkstateRoutesCase.class, LinkstateRoutes.class, LinkstateRoute.class);
95     }
96
97     static LinkstateRIBSupport getInstance() {
98         return SINGLETON;
99     }
100
101     @Override
102     public ChoiceNode emptyRoutes() {
103         return this.emptyRoutes;
104     }
105
106     @Override
107     public ImmutableCollection<Class<? extends DataObject>> cacheableAttributeObjects() {
108         return ImmutableSet.of();
109     }
110
111     @Override
112     public ImmutableCollection<Class<? extends DataObject>> cacheableNlriObjects() {
113         return ImmutableSet.of();
114     }
115
116     @Override
117     protected void deleteDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
118         final ContainerNode destination) {
119         processDestination(tx, tablePath, destination, null, DELETE_ROUTE);
120     }
121
122     private void processDestination(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
123         final ContainerNode destination, final ContainerNode attributes, final ApplyRoute function) {
124         if (destination != null) {
125             final Optional<DataContainerChild<? extends PathArgument, ?>> maybeRoutes = destination.getChild(this.nlriRoutesList);
126             if (maybeRoutes.isPresent()) {
127                 final DataContainerChild<? extends PathArgument, ?> routes = maybeRoutes.get();
128                 if (routes instanceof UnkeyedListNode) {
129                     final YangInstanceIdentifier base = tablePath.node(ROUTES).node(routesContainerIdentifier()).node(this.route);
130                     for (final UnkeyedListEntryNode e : ((UnkeyedListNode)routes).getValue()) {
131                         final NodeIdentifierWithPredicates routeKey = createRouteKey(e);
132                         function.apply(tx, base, routeKey,  e, attributes);
133                     }
134                 } else {
135                     LOG.warn("Routes {} are not a map", routes);
136                 }
137             }
138         }
139     }
140
141     @Override
142     protected void putDestinationRoutes(final DOMDataWriteTransaction tx, final YangInstanceIdentifier tablePath,
143         final ContainerNode destination, final ContainerNode attributes) {
144         processDestination(tx, tablePath, destination, attributes, this.putRoute);
145     }
146
147     private NodeIdentifierWithPredicates createRouteKey(final UnkeyedListEntryNode linkstate) {
148         final ByteBuf buffer = Unpooled.buffer();
149         LinkstateNlriParser.serializeNlri(linkstate, buffer);
150         return new NodeIdentifierWithPredicates(LinkstateRoute.QNAME, ROUTE_KEY, ByteArray.readAllBytes(buffer));
151     }
152
153     @Override
154     protected NodeIdentifier destinationContainerIdentifier() {
155         return this.destination;
156     }
157 }