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