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.NodeConnectorRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 class NodeChangeCommiter implements OpendaylightInventoryListener {
44 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
46 private final FlowCapableInventoryProvider manager;
48 // cache for nodes which were deleted, we get more than one nodeRemoved notification
49 private Cache<NodeRef, Boolean> deletedNodeCache =
50 CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.SECONDS).build();
52 // cache for node-connectors which were deleted, we get more than one nodeConnectorRemoved notification
53 private Cache<NodeConnectorRef, Boolean> deletedNodeConnectorCache =
54 CacheBuilder.newBuilder().maximumSize(1000000).expireAfterWrite(10, TimeUnit.SECONDS).build();
56 public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
57 this.manager = Preconditions.checkNotNull(manager);
61 public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
62 if(deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) == null){
63 deletedNodeConnectorCache.put(connector.getNodeConnectorRef(), Boolean.TRUE);
65 //its been noted that creating an operation for already removed node-connectors, fails
66 // the entire transaction chain, there by failing deserving removals
67 LOG.debug("Already received notification to remove nodeConnector, {} - Ignored",
68 connector.getNodeConnectorRef().getValue());
72 LOG.debug("Node connector removed notification received, {}", connector.getNodeConnectorRef().getValue());
73 manager.enqueue(new InventoryOperation() {
75 public void applyOperation(final ReadWriteTransaction tx) {
76 final NodeConnectorRef ref = connector.getNodeConnectorRef();
77 LOG.debug("removing node connector {} ", ref.getValue());
78 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
84 public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
85 if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) != null){
86 deletedNodeConnectorCache.invalidate(connector.getNodeConnectorRef());
89 LOG.debug("Node connector updated notification received.");
90 manager.enqueue(new InventoryOperation() {
92 public void applyOperation(final ReadWriteTransaction tx) {
93 final NodeConnectorRef ref = connector.getNodeConnectorRef();
94 final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
95 data.setKey(new NodeConnectorKey(connector.getId()));
97 final FlowCapableNodeConnectorUpdated flowConnector = connector
98 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
99 if (flowConnector != null) {
100 final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
101 data.addAugmentation(FlowCapableNodeConnector.class, augment);
103 InstanceIdentifier<NodeConnector> value = (InstanceIdentifier<NodeConnector>) ref.getValue();
104 LOG.debug("updating node connector : {}.", value);
105 NodeConnector build = data.build();
106 tx.merge(LogicalDatastoreType.OPERATIONAL, value, build, true);
112 public synchronized void onNodeRemoved(final NodeRemoved node) {
114 if(deletedNodeCache.getIfPresent(node.getNodeRef()) == null){
115 deletedNodeCache.put(node.getNodeRef(), Boolean.TRUE);
117 //its been noted that creating an operation for already removed node, fails
118 // the entire transaction chain, there by failing deserving removals
119 LOG.debug("Already received notification to remove node, {} - Ignored",
120 node.getNodeRef().getValue());
124 LOG.debug("Node removed notification received, {}", node.getNodeRef().getValue());
125 manager.enqueue(new InventoryOperation() {
127 public void applyOperation(final ReadWriteTransaction tx) {
128 final NodeRef ref = node.getNodeRef();
129 LOG.debug("removing node : {}", ref.getValue());
130 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
136 public synchronized void onNodeUpdated(final NodeUpdated node) {
137 if (deletedNodeCache.getIfPresent(node.getNodeRef()) != null){
138 deletedNodeCache.invalidate(node.getNodeRef());
141 final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
142 if (flowNode == null) {
145 LOG.debug("Node updated notification received,{}", node.getNodeRef().getValue());
146 manager.enqueue(new InventoryOperation() {
148 public void applyOperation(ReadWriteTransaction tx) {
149 final NodeRef ref = node.getNodeRef();
150 @SuppressWarnings("unchecked")
151 InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) ref.getValue()).builder();
152 InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder.augmentation(FlowCapableNode.class);
153 final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
154 CheckedFuture<Optional<FlowCapableNode>, ?> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, path);
155 Futures.addCallback(readFuture, new FutureCallback<Optional<FlowCapableNode>>() {
157 public void onSuccess(Optional<FlowCapableNode> optional) {
158 enqueueWriteNodeDataTx(node, flowNode, path);
159 if (!optional.isPresent()) {
160 enqueuePutTable0Tx(ref);
165 public void onFailure(Throwable throwable) {
166 LOG.debug(String.format("Can't retrieve node data for node %s. Writing node data with table0.", node));
167 enqueueWriteNodeDataTx(node, flowNode, path);
168 enqueuePutTable0Tx(ref);
175 private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
176 manager.enqueue(new InventoryOperation() {
178 public void applyOperation(final ReadWriteTransaction tx) {
179 final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
180 LOG.debug("updating node :{} ", path);
181 tx.merge(LogicalDatastoreType.OPERATIONAL, path, augment, true);
186 private void enqueuePutTable0Tx(final NodeRef ref) {
187 manager.enqueue(new InventoryOperation() {
189 public void applyOperation(ReadWriteTransaction tx) {
190 final TableKey tKey = new TableKey((short) 0);
191 final InstanceIdentifier<Table> tableIdentifier =
192 ((InstanceIdentifier<Node>) ref.getValue()).augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tKey));
193 TableBuilder tableBuilder = new TableBuilder();
194 Table table0 = tableBuilder.setId((short) 0).build();
195 LOG.debug("writing table :{} ", tableIdentifier);
196 tx.put(LogicalDatastoreType.OPERATIONAL, tableIdentifier, table0, true);