Merge changes I9865d0cd,Ic71d525f,Ib8faba91,Ia10e5ec9,I35591747,If456a131,I9f8709cc
[controller.git] / opendaylight / md-sal / inventory-manager / src / main / java / org / opendaylight / controller / md / 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.controller.md.inventory.manager;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 class NodeChangeCommiter implements OpendaylightInventoryListener {
41     private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
42
43     private final FlowCapableInventoryProvider manager;
44
45     public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
46         this.manager = Preconditions.checkNotNull(manager);
47     }
48
49     @Override
50     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
51         LOG.debug("Node connector removed notification received.");
52         manager.enqueue(new InventoryOperation() {
53             @Override
54             public void applyOperation(final ReadWriteTransaction tx) {
55                 final NodeConnectorRef ref = connector.getNodeConnectorRef();
56                 LOG.debug("removing node connector {} ", ref.getValue());
57                 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
58             }
59         });
60     }
61
62     @Override
63     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
64         LOG.debug("Node connector updated notification received.");
65         manager.enqueue(new InventoryOperation() {
66             @Override
67             public void applyOperation(final ReadWriteTransaction tx) {
68                 final NodeConnectorRef ref = connector.getNodeConnectorRef();
69                 final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
70                 data.setKey(new NodeConnectorKey(connector.getId()));
71
72                 final FlowCapableNodeConnectorUpdated flowConnector = connector
73                         .getAugmentation(FlowCapableNodeConnectorUpdated.class);
74                 if (flowConnector != null) {
75                     final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
76                     data.addAugmentation(FlowCapableNodeConnector.class, augment);
77                 }
78                 InstanceIdentifier<NodeConnector> value = (InstanceIdentifier<NodeConnector>) ref.getValue();
79                 LOG.debug("updating node connector : {}.", value);
80                 NodeConnector build = data.build();
81                 tx.merge(LogicalDatastoreType.OPERATIONAL, value, build, true);
82             }
83         });
84     }
85
86     @Override
87     public synchronized void onNodeRemoved(final NodeRemoved node) {
88         LOG.debug("Node removed notification received.");
89         manager.enqueue(new InventoryOperation() {
90             @Override
91             public void applyOperation(final ReadWriteTransaction tx) {
92                 final NodeRef ref = node.getNodeRef();
93                 LOG.debug("removing node : {}", ref.getValue());
94                 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
95             }
96         });
97     }
98
99     @Override
100     public synchronized void onNodeUpdated(final NodeUpdated node) {
101         final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
102         if (flowNode == null) {
103             return;
104         }
105         LOG.debug("Node updated notification received.");
106         manager.enqueue(new InventoryOperation() {
107             @Override
108             public void applyOperation(ReadWriteTransaction tx) {
109                 final NodeRef ref = node.getNodeRef();
110                 @SuppressWarnings("unchecked")
111                 InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) ref.getValue()).builder();
112                 InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder.augmentation(FlowCapableNode.class);
113                 final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
114                 CheckedFuture<Optional<FlowCapableNode>, ?> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, path);
115                 Futures.addCallback(readFuture, new FutureCallback<Optional<FlowCapableNode>>() {
116                     @Override
117                     public void onSuccess(Optional<FlowCapableNode> optional) {
118                         enqueueWriteNodeDataTx(node, flowNode, path);
119                         if (!optional.isPresent()) {
120                             enqueuePutTable0Tx(ref);
121                         }
122                     }
123
124                     @Override
125                     public void onFailure(Throwable throwable) {
126                         LOG.debug(String.format("Can't retrieve node data for node %s. Writing node data with table0.", node));
127                         enqueueWriteNodeDataTx(node, flowNode, path);
128                         enqueuePutTable0Tx(ref);
129                     }
130                 });
131             }
132         });
133     }
134
135     private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
136         manager.enqueue(new InventoryOperation() {
137             @Override
138             public void applyOperation(final ReadWriteTransaction tx) {
139                 final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
140                 LOG.debug("updating node :{} ", path);
141                 tx.merge(LogicalDatastoreType.OPERATIONAL, path, augment, true);
142             }
143         });
144     }
145
146     private void enqueuePutTable0Tx(final NodeRef ref) {
147         manager.enqueue(new InventoryOperation() {
148             @Override
149             public void applyOperation(ReadWriteTransaction tx) {
150                 final TableKey tKey = new TableKey((short) 0);
151                 final InstanceIdentifier<Table> tableIdentifier =
152                         ((InstanceIdentifier<Node>) ref.getValue()).augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tKey));
153                 TableBuilder tableBuilder = new TableBuilder();
154                 Table table0 = tableBuilder.setId((short) 0).build();
155                 LOG.debug("writing table :{} ", tableIdentifier);
156                 tx.put(LogicalDatastoreType.OPERATIONAL, tableIdentifier, table0, true);
157             }
158         });
159     }
160 }