Bug 4957 No empty transaction for every connection fix
[openflowplugin.git] / applications / inventory-manager / src / main / java / org / opendaylight / openflowplugin / applications / inventory / manager / NodeChangeCommiter.java
1 /**
2  * Copyright (c) 2013 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 package org.opendaylight.openflowplugin.applications.inventory.manager;
9
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;
42
43 class NodeChangeCommiter implements OpendaylightInventoryListener {
44     private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
45
46     private final FlowCapableInventoryProvider manager;
47
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();
51
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();
55
56     public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
57         this.manager = Preconditions.checkNotNull(manager);
58     }
59
60     @Override
61     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
62         if(deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) == null){
63             deletedNodeConnectorCache.put(connector.getNodeConnectorRef(), Boolean.TRUE);
64         } else {
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());
69             return;
70         }
71
72         LOG.debug("Node connector removed notification received, {}", connector.getNodeConnectorRef().getValue());
73         manager.enqueue(new InventoryOperation() {
74             @Override
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());
79             }
80         });
81     }
82
83     @Override
84     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
85         if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) != null){
86             deletedNodeConnectorCache.invalidate(connector.getNodeConnectorRef());
87         }
88
89         LOG.debug("Node connector updated notification received.");
90         manager.enqueue(new InventoryOperation() {
91             @Override
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()));
96
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);
102                 }
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);
107             }
108         });
109     }
110
111     @Override
112     public synchronized void onNodeRemoved(final NodeRemoved node) {
113
114         if(deletedNodeCache.getIfPresent(node.getNodeRef()) == null){
115             deletedNodeCache.put(node.getNodeRef(), Boolean.TRUE);
116         } else {
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());
121             return;
122         }
123
124         LOG.debug("Node removed notification received, {}", node.getNodeRef().getValue());
125         manager.enqueue(new InventoryOperation() {
126             @Override
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());
131             }
132         });
133     }
134
135     @Override
136     public synchronized void onNodeUpdated(final NodeUpdated node) {
137         if (deletedNodeCache.getIfPresent(node.getNodeRef()) != null){
138             deletedNodeCache.invalidate(node.getNodeRef());
139         }
140
141         final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
142         if (flowNode == null) {
143             return;
144         }
145         LOG.debug("Node updated notification received,{}", node.getNodeRef().getValue());
146         manager.enqueue(new InventoryOperation() {
147             @Override
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>>() {
156                     @Override
157                     public void onSuccess(Optional<FlowCapableNode> optional) {
158                         enqueueWriteNodeDataTx(node, flowNode, path);
159                         if (!optional.isPresent()) {
160                             enqueuePutTable0Tx(ref);
161                         }
162                     }
163
164                     @Override
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);
169                     }
170                 });
171             }
172         });
173     }
174
175     private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
176         manager.enqueue(new InventoryOperation() {
177             @Override
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);
182             }
183         });
184     }
185
186     private void enqueuePutTable0Tx(final NodeRef ref) {
187         manager.enqueue(new InventoryOperation() {
188             @Override
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);
197             }
198         });
199     }
200 }