Bug 508: Fixed concurrent conflicting commits by InventoryManager.
[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 java.util.concurrent.ExecutionException;
11 import java.util.concurrent.Future;
12
13 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
14 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
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.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Objects;
41
42 @SuppressWarnings("all")
43 public class NodeChangeCommiter implements OpendaylightInventoryListener {
44     private final static Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
45
46     private final FlowCapableInventoryProvider manager;
47
48     public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
49         this.manager = manager;
50     }
51
52     public FlowCapableInventoryProvider getManager() {
53         return this.manager;
54     }
55
56     @Override
57     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
58
59         final NodeConnectorRef ref = connector.getNodeConnectorRef();
60         final DataModificationTransaction it = this.getManager().startChange();
61         NodeChangeCommiter.LOG.debug("removing node connector {} ", ref.getValue());
62         it.removeOperationalData(ref.getValue());
63         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
64         try {
65             commitResult.get();
66         } catch (InterruptedException | ExecutionException e) {
67             LOG.error("Node Connector {} not removed.", ref.getValue(), e);
68         }
69
70     }
71
72     @Override
73     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
74
75         final NodeConnectorRef ref = connector.getNodeConnectorRef();
76         final FlowCapableNodeConnectorUpdated flowConnector = connector
77                 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
78         final DataModificationTransaction it = this.getManager().startChange();
79         final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
80         NodeConnectorId id = connector.getId();
81         NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(id);
82         data.setKey(nodeConnectorKey);
83         boolean notEquals = (!Objects.equal(flowConnector, null));
84         if (notEquals) {
85             final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
86             data.addAugmentation(FlowCapableNodeConnector.class, augment);
87         }
88         InstanceIdentifier<? extends Object> value = ref.getValue();
89         String string = value.toString();
90         String plus = ("updating node connector : " + string);
91         NodeChangeCommiter.LOG.debug(plus);
92         InstanceIdentifier<? extends Object> value1 = ref.getValue();
93         NodeConnector build = data.build();
94         it.putOperationalData((value1), build);
95         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
96         try {
97             commitResult.get();
98         } catch (InterruptedException | ExecutionException e) {
99             LOG.error("Node Connector {} not updated.", ref.getValue(), e);
100         }
101
102     }
103
104     @Override
105     public synchronized void onNodeRemoved(final NodeRemoved node) {
106
107         final NodeRef ref = node.getNodeRef();
108         FlowCapableInventoryProvider manager = this.getManager();
109         final DataModificationTransaction it = manager.startChange();
110         InstanceIdentifier<? extends Object> value = ref.getValue();
111         String string = value.toString();
112         String plus = ("removing node : " + string);
113         NodeChangeCommiter.LOG.debug(plus);
114         InstanceIdentifier<? extends Object> value1 = ref.getValue();
115         it.removeOperationalData((value1));
116         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
117         try {
118             commitResult.get();
119         } catch (InterruptedException | ExecutionException e) {
120             LOG.error("Node {} not removed.", ref.getValue(), e);
121         }
122     }
123
124     @Override
125     public synchronized void onNodeUpdated(final NodeUpdated node) {
126
127         final NodeRef ref = node.getNodeRef();
128         final FlowCapableNodeUpdated flowNode = node
129                 .<FlowCapableNodeUpdated> getAugmentation(FlowCapableNodeUpdated.class);
130         FlowCapableInventoryProvider manager = this.getManager();
131         final DataModificationTransaction it = manager.startChange();
132         NodeBuilder nodeBuilder = new NodeBuilder(node);
133         final NodeBuilder data = nodeBuilder;
134         NodeId id = node.getId();
135         NodeKey nodeKey = new NodeKey(id);
136         data.setKey(nodeKey);
137         boolean equals = Objects.equal(flowNode, null);
138         if (equals) {
139             return;
140         }
141         final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
142         data.addAugmentation(FlowCapableNode.class, augment);
143         InstanceIdentifier<? extends Object> value = ref.getValue();
144         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.<Node> builder(((InstanceIdentifier<Node>) value));
145         InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder
146                 .<FlowCapableNode> augmentation(FlowCapableNode.class);
147         final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
148         NodeChangeCommiter.LOG.debug("updating node :{} ", path);
149         it.putOperationalData(path, augment);
150
151         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
152         try {
153             commitResult.get();
154         } catch (InterruptedException | ExecutionException e) {
155             LOG.error("Node  {} not updated.", ref.getValue(), e);
156         }
157
158     }
159 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.