Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[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.Future;
11
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.OpendaylightInventoryListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import com.google.common.base.Objects;
39 import com.google.common.util.concurrent.FutureCallback;
40 import com.google.common.util.concurrent.Futures;
41 import com.google.common.util.concurrent.JdkFutureAdapters;
42
43 public class NodeChangeCommiter implements OpendaylightInventoryListener {
44
45     protected final static Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
46
47     private final FlowCapableInventoryProvider manager;
48
49     public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
50         this.manager = manager;
51     }
52
53     public FlowCapableInventoryProvider getManager() {
54         return this.manager;
55     }
56
57     @Override
58     public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
59
60         final NodeConnectorRef ref = connector.getNodeConnectorRef();
61         final DataModificationTransaction it = this.getManager().startChange();
62         LOG.debug("removing node connector {} ", ref.getValue());
63         it.removeOperationalData(ref.getValue());
64         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
65         listenOnTransactionState(it.getIdentifier(), commitResult, "nodeConnector removal", ref.getValue());
66     }
67
68     @Override
69     public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
70
71         final NodeConnectorRef ref = connector.getNodeConnectorRef();
72         final FlowCapableNodeConnectorUpdated flowConnector = connector
73                 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
74         final DataModificationTransaction it = this.manager.startChange();
75         final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
76         NodeConnectorId id = connector.getId();
77         NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(id);
78         data.setKey(nodeConnectorKey);
79         boolean notEquals = (!Objects.equal(flowConnector, null));
80         if (notEquals) {
81             final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
82             data.addAugmentation(FlowCapableNodeConnector.class, augment);
83         }
84         InstanceIdentifier<? extends Object> value = ref.getValue();
85         LOG.debug("updating node connector : {}.", value);
86         NodeConnector build = data.build();
87         it.putOperationalData((value), build);
88         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
89         listenOnTransactionState(it.getIdentifier(), commitResult, "nodeConnector update", ref.getValue());
90     }
91
92     @Override
93     public synchronized void onNodeRemoved(final NodeRemoved node) {
94
95         final NodeRef ref = node.getNodeRef();
96         final DataModificationTransaction it = this.manager.startChange();
97         LOG.debug("removing node : {}", ref.getValue());
98         it.removeOperationalData((ref.getValue()));
99         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
100         listenOnTransactionState(it.getIdentifier(), commitResult, "node removal", ref.getValue());
101     }
102
103     @Override
104     public synchronized void onNodeUpdated(final NodeUpdated node) {
105
106         final NodeRef ref = node.getNodeRef();
107         final FlowCapableNodeUpdated flowNode = node
108                 .<FlowCapableNodeUpdated> getAugmentation(FlowCapableNodeUpdated.class);
109         final DataModificationTransaction it = this.manager.startChange();
110         final NodeBuilder nodeBuilder = new NodeBuilder(node);
111         nodeBuilder.setKey(new NodeKey(node.getId()));
112         boolean equals = Objects.equal(flowNode, null);
113         if (equals) {
114             return;
115         }
116         final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
117         nodeBuilder.addAugmentation(FlowCapableNode.class, augment);
118         InstanceIdentifier<? extends Object> value = ref.getValue();
119         InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) value).builder();
120         InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder
121                 .<FlowCapableNode> augmentation(FlowCapableNode.class);
122         final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
123         LOG.debug("updating node :{} ", path);
124         it.putOperationalData(path, augment);
125
126         Future<RpcResult<TransactionStatus>> commitResult = it.commit();
127         listenOnTransactionState(it.getIdentifier(), commitResult, "node update", ref.getValue());
128     }
129     
130     /**
131      * @param txId transaction identificator
132      * @param future transaction result
133      * @param action performed by transaction
134      * @param nodeConnectorPath target value
135      */
136     private static void listenOnTransactionState(final Object txId, Future<RpcResult<TransactionStatus>> future,
137             final String action, final InstanceIdentifier<?> nodeConnectorPath) {
138         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future),new FutureCallback<RpcResult<TransactionStatus>>() {
139             
140             @Override
141             public void onFailure(Throwable t) {
142                 LOG.error("Action {} [{}] failed for Tx:{}", action, nodeConnectorPath, txId, t);
143                 
144             }
145             
146             @Override
147             public void onSuccess(RpcResult<TransactionStatus> result) {
148                 if(!result.isSuccessful()) {
149                     LOG.error("Action {} [{}] failed for Tx:{}", action, nodeConnectorPath, txId);
150                 }
151             }
152         });
153     }
154 }

©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.