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