2 * Copyright (c) 2013 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.openflowplugin.applications.inventory.manager;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import java.util.concurrent.TimeUnit;
18 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.*;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 class NodeChangeCommiter implements OpendaylightInventoryListener {
38 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
40 private final FlowCapableInventoryProvider manager;
42 // cache for nodes which were deleted, we get more than one nodeRemoved notification
43 private Cache<NodeRef, Boolean> deletedNodeCache =
44 CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.SECONDS).build();
46 // cache for node-connectors which were deleted, we get more than one nodeConnectorRemoved notification
47 private Cache<NodeConnectorRef, Boolean> deletedNodeConnectorCache =
48 CacheBuilder.newBuilder().maximumSize(1000000).expireAfterWrite(10, TimeUnit.SECONDS).build();
50 public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
51 this.manager = Preconditions.checkNotNull(manager);
55 public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
56 if(deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) == null){
57 deletedNodeConnectorCache.put(connector.getNodeConnectorRef(), Boolean.TRUE);
59 //its been noted that creating an operation for already removed node-connectors, fails
60 // the entire transaction chain, there by failing deserving removals
61 LOG.debug("Already received notification to remove nodeConnector, {} - Ignored",
62 connector.getNodeConnectorRef().getValue());
66 if(!manager.deviceDataDeleteAllowed(getNodeId(connector.getNodeConnectorRef().getValue()))) { return; }
68 LOG.debug("Node connector removed notification received, {}", connector.getNodeConnectorRef().getValue());
69 manager.enqueue(new InventoryOperation() {
71 public void applyOperation(final ReadWriteTransaction tx) {
72 final NodeConnectorRef ref = connector.getNodeConnectorRef();
73 LOG.debug("removing node connector {} ", ref.getValue());
74 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
80 public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
81 if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) != null){
82 deletedNodeConnectorCache.invalidate(connector.getNodeConnectorRef());
85 LOG.debug("Node connector updated notification received.");
86 manager.enqueue(new InventoryOperation() {
88 public void applyOperation(final ReadWriteTransaction tx) {
89 final NodeConnectorRef ref = connector.getNodeConnectorRef();
90 final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
91 data.setKey(new NodeConnectorKey(connector.getId()));
93 final FlowCapableNodeConnectorUpdated flowConnector = connector
94 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
95 if (flowConnector != null) {
96 final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
97 data.addAugmentation(FlowCapableNodeConnector.class, augment);
99 InstanceIdentifier<NodeConnector> value = (InstanceIdentifier<NodeConnector>) ref.getValue();
100 LOG.debug("updating node connector : {}.", value);
101 NodeConnector build = data.build();
102 tx.merge(LogicalDatastoreType.OPERATIONAL, value, build, true);
108 public synchronized void onNodeRemoved(final NodeRemoved node) {
110 if(deletedNodeCache.getIfPresent(node.getNodeRef()) == null){
111 deletedNodeCache.put(node.getNodeRef(), Boolean.TRUE);
113 //its been noted that creating an operation for already removed node, fails
114 // the entire transaction chain, there by failing deserving removals
115 LOG.debug("Already received notification to remove node, {} - Ignored",
116 node.getNodeRef().getValue());
120 if(!manager.deviceDataDeleteAllowed(getNodeId(node.getNodeRef().getValue()))) { return; }
122 LOG.debug("Node removed notification received, {}", node.getNodeRef().getValue());
123 manager.enqueue(new InventoryOperation() {
125 public void applyOperation(final ReadWriteTransaction tx) {
126 final NodeRef ref = node.getNodeRef();
127 LOG.debug("removing node : {}", ref.getValue());
128 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
134 public synchronized void onNodeUpdated(final NodeUpdated node) {
135 if (deletedNodeCache.getIfPresent(node.getNodeRef()) != null){
136 deletedNodeCache.invalidate(node.getNodeRef());
139 final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
140 if (flowNode == null) {
143 LOG.debug("Node updated notification received,{}", node.getNodeRef().getValue());
144 manager.enqueue(new InventoryOperation() {
146 public void applyOperation(ReadWriteTransaction tx) {
147 final NodeRef ref = node.getNodeRef();
148 @SuppressWarnings("unchecked")
149 InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) ref.getValue()).builder();
150 InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder.augmentation(FlowCapableNode.class);
151 final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
152 CheckedFuture<Optional<FlowCapableNode>, ?> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, path);
153 Futures.addCallback(readFuture, new FutureCallback<Optional<FlowCapableNode>>() {
155 public void onSuccess(Optional<FlowCapableNode> optional) {
156 enqueueWriteNodeDataTx(node, flowNode, path);
157 if (!optional.isPresent()) {
158 enqueuePutTable0Tx(ref);
163 public void onFailure(Throwable throwable) {
164 LOG.debug(String.format("Can't retrieve node data for node %s. Writing node data with table0.", node));
165 enqueueWriteNodeDataTx(node, flowNode, path);
166 enqueuePutTable0Tx(ref);
173 private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
174 manager.enqueue(new InventoryOperation() {
176 public void applyOperation(final ReadWriteTransaction tx) {
177 final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
178 LOG.debug("updating node :{} ", path);
179 tx.merge(LogicalDatastoreType.OPERATIONAL, path, augment, true);
184 private void enqueuePutTable0Tx(final NodeRef ref) {
185 manager.enqueue(new InventoryOperation() {
187 public void applyOperation(ReadWriteTransaction tx) {
188 final TableKey tKey = new TableKey((short) 0);
189 final InstanceIdentifier<Table> tableIdentifier =
190 ((InstanceIdentifier<Node>) ref.getValue()).augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tKey));
191 TableBuilder tableBuilder = new TableBuilder();
192 Table table0 = tableBuilder.setId((short) 0).build();
193 LOG.debug("writing table :{} ", tableIdentifier);
194 tx.merge(LogicalDatastoreType.OPERATIONAL, tableIdentifier, table0, true);
199 private NodeId getNodeId(InstanceIdentifier<?> iid) {
200 return iid.firstKeyOf(Node.class).getId();