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

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