2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.protocol.bmp.impl.app;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.protocol.bmp.impl.app.TablesUtil.BMP_ATTRIBUTES_QNAME;
13 import static org.opendaylight.protocol.bmp.impl.app.TablesUtil.BMP_ROUTES_QNAME;
15 import com.google.common.base.Preconditions;
17 import javax.annotation.concurrent.NotThreadSafe;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
20 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
21 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
24 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Update;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.Attributes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path.attributes.AttributesBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes1;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.Attributes2;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpReachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.update.attributes.MpUnreachNlri;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.tables.Routes;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
41 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
45 final class TableContext {
47 private static final ContainerNode EMPTY_TABLE_ATTRIBUTES = ImmutableNodes.containerNode(BMP_ATTRIBUTES_QNAME);
49 private static final InstanceIdentifier<MpReachNlri> MP_REACH_NLRI_II = InstanceIdentifier.create(Update.class)
50 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path
51 .attributes.Attributes.class).augmentation(Attributes1.class).child(MpReachNlri.class);
52 private static final InstanceIdentifier<MpUnreachNlri> MP_UNREACH_NLRI_II = InstanceIdentifier.create(Update.class)
53 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.path
54 .attributes.Attributes.class).augmentation(Attributes2.class).child(MpUnreachNlri.class);
55 private static final NodeIdentifier BGP_ROUTES_NODE_ID = new NodeIdentifier(BMP_ROUTES_QNAME);
57 private final YangInstanceIdentifier tableId;
58 private final RIBSupport tableSupport;
59 private final BindingNormalizedNodeCachingCodec<Attributes> attributesCodec;
60 private final BindingNormalizedNodeCachingCodec<MpReachNlri> reachNlriCodec;
61 private final BindingNormalizedNodeCachingCodec<MpUnreachNlri> unreachNlriCodec;
63 @SuppressWarnings({ "unchecked", "rawtypes" })
64 TableContext(final RIBSupport tableSupport, final YangInstanceIdentifier tableId, final BindingCodecTree tree) {
65 this.tableSupport = requireNonNull(tableSupport);
66 this.tableId = requireNonNull(tableId);
67 final BindingCodecTreeNode tableCodecContext = tree.getSubtreeCodec(tableId);
68 Preconditions.checkState(tableCodecContext instanceof BindingDataObjectCodecTreeNode);
69 final BindingDataObjectCodecTreeNode<?> routeListCodec = ((BindingDataObjectCodecTreeNode)tableCodecContext)
70 .streamChild(Routes.class)
71 .streamChild(this.tableSupport.routesCaseClass())
72 .streamChild(this.tableSupport.routesContainerClass())
73 .streamChild(this.tableSupport.routesListClass());
75 this.attributesCodec = routeListCodec.streamChild(Attributes.class)
76 .createCachingCodec(this.tableSupport.cacheableAttributeObjects());
77 this.reachNlriCodec = tree.getSubtreeCodec(MP_REACH_NLRI_II)
78 .createCachingCodec(this.tableSupport.cacheableNlriObjects());
79 this.unreachNlriCodec = tree.getSubtreeCodec(MP_UNREACH_NLRI_II)
80 .createCachingCodec(this.tableSupport.cacheableNlriObjects());
83 YangInstanceIdentifier getTableId() {
87 void createTable(final DOMDataWriteTransaction tx) {
88 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> tb =
89 ImmutableNodes.mapEntryBuilder();
90 tb.withNodeIdentifier((NodeIdentifierWithPredicates) this.tableId.getLastPathArgument());
91 tb.withChild(EMPTY_TABLE_ATTRIBUTES);
93 // tableId is keyed, but that fact is not directly visible from YangInstanceIdentifier, see BUG-2796
94 final NodeIdentifierWithPredicates tableKey =
95 (NodeIdentifierWithPredicates) this.tableId.getLastPathArgument();
96 for (final Map.Entry<QName, Object> e : tableKey.getKeyValues().entrySet()) {
97 tb.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
100 tx.put(LogicalDatastoreType.OPERATIONAL, this.tableId,
101 tb.withChild(ImmutableChoiceNodeBuilder.create().withNodeIdentifier(
102 new NodeIdentifier(TablesUtil.BMP_ROUTES_QNAME)).build()).build());
105 void writeRoutes(final DOMDataWriteTransaction tx, final MpReachNlri nlri, final Attributes attributes) {
106 final ContainerNode domNlri = serializeReachNlri(nlri);
107 final ContainerNode routeAttributes = serializeAttributes(attributes);
108 this.tableSupport.putRoutes(tx, this.tableId, domNlri, routeAttributes, BGP_ROUTES_NODE_ID);
111 void removeRoutes(final DOMDataWriteTransaction tx, final MpUnreachNlri nlri) {
112 this.tableSupport.deleteRoutes(tx, this.tableId, serializeUnreachNlri(nlri), BGP_ROUTES_NODE_ID);
115 private ContainerNode serializeUnreachNlri(final MpUnreachNlri nlri) {
116 Preconditions.checkState(this.unreachNlriCodec != null, "MpUnReachNlri codec not available");
117 return (ContainerNode) this.unreachNlriCodec.serialize(nlri);
120 private ContainerNode serializeReachNlri(final MpReachNlri nlri) {
121 Preconditions.checkState(this.reachNlriCodec != null, "MpReachNlri codec not available");
122 return (ContainerNode) this.reachNlriCodec.serialize(nlri);
125 private ContainerNode serializeAttributes(final Attributes pathAttr) {
126 Preconditions.checkState(this.attributesCodec != null, "Attributes codec not available");
127 final AttributesBuilder a = new AttributesBuilder(pathAttr);
128 a.addAugmentation(Attributes1.class, null);
129 a.addAugmentation(Attributes2.class, null);
130 return (ContainerNode) this.attributesCodec.serialize(a.build());