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
8 package org.opendaylight.protocol.bgp.rib.impl;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.ImmutableSet.Builder;
15 import javax.annotation.concurrent.NotThreadSafe;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
18 import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.ClusterId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.OriginatorId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.PathAttributes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Aggregator;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.AsPath;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Communities;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.ExtendedCommunities;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.LocalPref;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.MultiExitDisc;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.path.attributes.Origin;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlri;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlri;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Attributes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.tables.Routes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.BgpAggregator;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Community;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.ExtendedCommunity;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
39 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
47 * A context for a single RIB table instance. It is always bound to a particular {@link AdjRibInWriter}.
49 * FIXME: need a better name once we local-rib and rib-out contexts
52 final class TableContext {
53 private static final ContainerNode EMPTY_TABLE_ATTRIBUTES = ImmutableNodes.containerNode(Attributes.QNAME);
54 private static final ContainerNode EMPTY_ROUTE_ATTRIBUTES = ImmutableNodes.containerNode(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes.QNAME);
55 private static final Set<Class<? extends DataObject>> ATTRIBUTE_CACHEABLES;
58 final Builder<Class<? extends DataObject>> acb = ImmutableSet.builder();
59 acb.add(Aggregator.class);
60 acb.add(BgpAggregator.class);
61 acb.add(AsPath.class);
62 acb.add(ClusterId.class);
63 acb.add(Community.class);
64 acb.add(Communities.class);
65 acb.add(ExtendedCommunity.class);
66 acb.add(ExtendedCommunities.class);
67 acb.add(LocalPref.class);
68 acb.add(MultiExitDisc.class);
69 acb.add(Origin.class);
70 acb.add(OriginatorId.class);
71 ATTRIBUTE_CACHEABLES = acb.build();
74 private final YangInstanceIdentifier tableId;
75 private final RIBSupport tableSupport;
76 private final Object attributeCodec;
77 private final Object nlriCodec;
79 TableContext(final RIBSupport tableSupport, final YangInstanceIdentifier tableId) {
80 this.tableSupport = Preconditions.checkNotNull(tableSupport);
81 this.tableId = Preconditions.checkNotNull(tableId);
83 final Builder<Class<? extends DataObject>> acb = ImmutableSet.builder();
84 acb.addAll(ATTRIBUTE_CACHEABLES);
85 acb.addAll(tableSupport.cacheableAttributeObjects());
87 // FIXME: new Codec.create(acb.build(), tableSupport.cacheableNlriObjects());
88 attributeCodec = null;
90 // FIXME: new Codec.create(tableSupport.cacheableNlriObjects());
94 YangInstanceIdentifier getTableId() {
98 static void clearTable(final DOMDataWriteTransaction tx, final RIBSupport tableSupport, final YangInstanceIdentifier tableId) {
99 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> tb =
100 ImmutableNodes.mapEntryBuilder().withNodeIdentifier((NodeIdentifierWithPredicates)tableId.getLastPathArgument()).withChild(EMPTY_TABLE_ATTRIBUTES);
102 final ChoiceNode routes = tableSupport.emptyRoutes();
103 Verify.verifyNotNull(routes, "Null empty routes in %s", tableSupport);
104 Verify.verify(Routes.QNAME.equals(routes.getNodeType()), "Empty routes have unexpected identifier %s, expected %s", routes.getNodeType(), Routes.QNAME);
106 tx.put(LogicalDatastoreType.OPERATIONAL, tableId, tb.withChild(routes).build());
109 void clearTable(final DOMDataWriteTransaction tx) {
110 clearTable(tx, tableSupport, tableId);
113 void removeTable(final DOMDataWriteTransaction tx) {
114 tx.delete(LogicalDatastoreType.OPERATIONAL, tableId);
117 void writeRoutes(final Object codecFactory, final DOMDataWriteTransaction tx, final MpReachNlri nlri, final PathAttributes attributes) {
119 // FIXME: run the decoder process
120 final ContainerNode domNlri = (ContainerNode) nlriCodec;
122 // FIXME: run the decoder process
123 final ContainerNode domAttributes = (ContainerNode) attributeCodec;
124 final ContainerNode routeAttributes = Builders.containerBuilder(EMPTY_ROUTE_ATTRIBUTES).withValue(domAttributes.getValue()).build();
126 tableSupport.putRoutes(tx, tableId, domNlri, routeAttributes);
129 void removeRoutes(final Object object, final DOMDataWriteTransaction tx, final MpUnreachNlri nlri) {
130 // FIXME: run the decoder process
131 final ContainerNode domNlri = (ContainerNode) nlriCodec;
133 tableSupport.deleteRoutes(tx, tableId, domNlri);