Merge "Bug 4957 RoleContext updated with initialization"
[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.*;
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;
36
37 class NodeChangeCommiter implements OpendaylightInventoryListener {
38     private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
39
40     private final FlowCapableInventoryProvider manager;
41
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();
45
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();
49
50     public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
51         this.manager = Preconditions.checkNotNull(manager);
52     }
53
54     @Override
55     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
56         if(deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) == null){
57             deletedNodeConnectorCache.put(connector.getNodeConnectorRef(), Boolean.TRUE);
58         } else {
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());
63             return;
64         }
65
66         if(!manager.deviceDataDeleteAllowed(getNodeId(connector.getNodeConnectorRef().getValue()))) { return; }
67
68         LOG.debug("Node connector removed notification received, {}", connector.getNodeConnectorRef().getValue());
69         manager.enqueue(new InventoryOperation() {
70             @Override
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());
75             }
76         });
77     }
78
79     @Override
80     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
81         if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) != null){
82             deletedNodeConnectorCache.invalidate(connector.getNodeConnectorRef());
83         }
84
85         LOG.debug("Node connector updated notification received.");
86         manager.enqueue(new InventoryOperation() {
87             @Override
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()));
92
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);
98                 }
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);
103             }
104         });
105     }
106
107     @Override
108     public synchronized void onNodeRemoved(final NodeRemoved node) {
109
110         if(deletedNodeCache.getIfPresent(node.getNodeRef()) == null){
111             deletedNodeCache.put(node.getNodeRef(), Boolean.TRUE);
112         } else {
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());
117             return;
118         }
119
120         if(!manager.deviceDataDeleteAllowed(getNodeId(node.getNodeRef().getValue()))) { return; }
121
122         LOG.debug("Node removed notification received, {}", node.getNodeRef().getValue());
123         manager.enqueue(new InventoryOperation() {
124             @Override
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());
129             }
130         });
131     }
132
133     @Override
134     public synchronized void onNodeUpdated(final NodeUpdated node) {
135         if (deletedNodeCache.getIfPresent(node.getNodeRef()) != null){
136             deletedNodeCache.invalidate(node.getNodeRef());
137         }
138
139         final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
140         if (flowNode == null) {
141             return;
142         }
143         LOG.debug("Node updated notification received,{}", node.getNodeRef().getValue());
144         manager.enqueue(new InventoryOperation() {
145             @Override
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>>() {
154                     @Override
155                     public void onSuccess(Optional<FlowCapableNode> optional) {
156                         enqueueWriteNodeDataTx(node, flowNode, path);
157                         if (!optional.isPresent()) {
158                             enqueuePutTable0Tx(ref);
159                         }
160                     }
161
162                     @Override
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);
167                     }
168                 });
169             }
170         });
171     }
172
173     private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
174         manager.enqueue(new InventoryOperation() {
175             @Override
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);
180             }
181         });
182     }
183
184     private void enqueuePutTable0Tx(final NodeRef ref) {
185         manager.enqueue(new InventoryOperation() {
186             @Override
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);
195             }
196         });
197     }
198
199     private NodeId getNodeId(InstanceIdentifier<?> iid) {
200         return iid.firstKeyOf(Node.class).getId();
201     }
202 }