cd8cef4b1856c1c094181b06082800b4b5636150
[bgpcep.git] / bgp / 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
9 package org.opendaylight.protocol.bmp.impl.app;
10
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 com.google.common.base.Verify;
16 import java.util.Map;
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.protocol.bgp.rib.spi.RIBSupport;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.Update;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AttributesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes1;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.Attributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpReachNlri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.attributes.MpUnreachNlri;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
30 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTree;
31 import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
32 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeCachingCodec;
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.schema.ChoiceNode;
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;
43
44 /**
45  * Created by cgasparini on 22.5.2015.
46  */
47 @NotThreadSafe
48 final class TableContext {
49
50     private static final ContainerNode EMPTY_TABLE_ATTRIBUTES = ImmutableNodes.containerNode(BMP_ATTRIBUTES_QNAME);
51
52     private static final InstanceIdentifier<MpReachNlri> MP_REACH_NLRI_II = InstanceIdentifier.create(Update.class)
53                 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes.class)
54                 .augmentation(Attributes1.class)
55                 .child(MpReachNlri.class);
56     private static final InstanceIdentifier<MpUnreachNlri> MP_UNREACH_NLRI_II = InstanceIdentifier.create(Update.class)
57             .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Attributes.class)
58             .augmentation(Attributes2.class)
59             .child(MpUnreachNlri.class);
60     private static final NodeIdentifier ROUTES_NODE_ID = new NodeIdentifier(BMP_ROUTES_QNAME);
61
62     private final YangInstanceIdentifier tableId;
63     private final RIBSupport tableSupport;
64     private final BindingNormalizedNodeCachingCodec<Attributes> attributesCodec;
65     private final BindingNormalizedNodeCachingCodec<MpReachNlri> reachNlriCodec;
66     private final BindingNormalizedNodeCachingCodec<MpUnreachNlri> unreachNlriCodec;
67
68     @SuppressWarnings({ "unchecked", "rawtypes" })
69     TableContext(final RIBSupport tableSupport, final YangInstanceIdentifier tableId, final BindingCodecTree tree) {
70         this.tableSupport = Preconditions.checkNotNull(tableSupport);
71         this.tableId = Preconditions.checkNotNull(tableId);
72         final BindingCodecTreeNode tableCodecContext = tree.getSubtreeCodec(tableId);
73         final BindingCodecTreeNode<? extends Route> routeListCodec = tableCodecContext
74             .streamChild(Routes.class)
75             .streamChild(this.tableSupport.routesCaseClass())
76             .streamChild(this.tableSupport.routesContainerClass())
77             .streamChild(this.tableSupport.routesListClass());
78
79         this.attributesCodec = routeListCodec.streamChild(Attributes.class).createCachingCodec(this.tableSupport.cacheableAttributeObjects());
80         this.reachNlriCodec = tree.getSubtreeCodec(MP_REACH_NLRI_II).createCachingCodec(this.tableSupport.cacheableNlriObjects());
81         this.unreachNlriCodec = tree.getSubtreeCodec(MP_UNREACH_NLRI_II).createCachingCodec(this.tableSupport.cacheableNlriObjects());
82     }
83
84     YangInstanceIdentifier getTableId() {
85         return this.tableId;
86     }
87
88     void createTable(final DOMDataWriteTransaction tx) {
89         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> tb = ImmutableNodes.mapEntryBuilder();
90         tb.withNodeIdentifier((YangInstanceIdentifier.NodeIdentifierWithPredicates)tableId.getLastPathArgument());
91         tb.withChild(EMPTY_TABLE_ATTRIBUTES);
92
93         // tableId is keyed, but that fact is not directly visible from YangInstanceIdentifier, see BUG-2796
94         final YangInstanceIdentifier.NodeIdentifierWithPredicates tableKey = (YangInstanceIdentifier.NodeIdentifierWithPredicates) tableId.getLastPathArgument();
95         for (final Map.Entry<QName, Object> e : tableKey.getKeyValues().entrySet()) {
96             tb.withChild(ImmutableNodes.leafNode(e.getKey(), e.getValue()));
97         }
98
99         final ChoiceNode routes = this.tableSupport.emptyRoutes();
100         Verify.verifyNotNull(routes, "Null empty routes in %s", this.tableSupport);
101
102         tx.put(LogicalDatastoreType.OPERATIONAL, this.tableId,
103                 tb.withChild(ImmutableChoiceNodeBuilder.create(routes).withNodeIdentifier(
104                         new NodeIdentifier(TablesUtil.BMP_ROUTES_QNAME)).build()).build());
105     }
106
107     void removeTable(final DOMDataWriteTransaction tx) {
108         tx.delete(LogicalDatastoreType.OPERATIONAL, this.tableId);
109     }
110
111     void writeRoutes(final DOMDataWriteTransaction tx, final MpReachNlri nlri, final Attributes attributes) {
112         final ContainerNode domNlri = serializeReachNlri(nlri);
113         final ContainerNode routeAttributes = serializeAttributes(attributes);
114         this.tableSupport.putRoutes(tx, this.tableId, domNlri, routeAttributes, ROUTES_NODE_ID);
115     }
116
117     void removeRoutes(final DOMDataWriteTransaction tx, final MpUnreachNlri nlri) {
118         this.tableSupport.deleteRoutes(tx, this.tableId, serializeUnreachNlri(nlri), ROUTES_NODE_ID);
119     }
120
121     private ContainerNode serializeUnreachNlri(final MpUnreachNlri nlri) {
122         Preconditions.checkState(this.unreachNlriCodec != null, "MpUnReachNlri codec not available");
123         return (ContainerNode) this.unreachNlriCodec.serialize(nlri);
124     }
125
126     private ContainerNode serializeReachNlri(final MpReachNlri nlri) {
127         Preconditions.checkState(this.reachNlriCodec != null, "MpReachNlri codec not available");
128         return (ContainerNode) this.reachNlriCodec.serialize(nlri);
129     }
130
131     private ContainerNode serializeAttributes(final Attributes pathAttr) {
132         Preconditions.checkState(this.attributesCodec != null, "Attributes codec not available");
133         final AttributesBuilder a = new AttributesBuilder(pathAttr);
134         a.addAugmentation(Attributes1.class, null);
135         a.addAugmentation(Attributes2.class, null);
136         return (ContainerNode) this.attributesCodec.serialize(a.build());
137     }
138 }