2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.inventory.manager;
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;
40 class NodeChangeCommiter implements OpendaylightInventoryListener {
41 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
43 private final FlowCapableInventoryProvider manager;
45 public NodeChangeCommiter(final FlowCapableInventoryProvider manager) {
46 this.manager = Preconditions.checkNotNull(manager);
50 public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
51 LOG.debug("Node connector removed notification received.");
52 manager.enqueue(new InventoryOperation() {
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());
63 public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
64 LOG.debug("Node connector updated notification received.");
65 manager.enqueue(new InventoryOperation() {
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()));
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);
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);
87 public synchronized void onNodeRemoved(final NodeRemoved node) {
88 LOG.debug("Node removed notification received.");
89 manager.enqueue(new InventoryOperation() {
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());
100 public synchronized void onNodeUpdated(final NodeUpdated node) {
101 final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
102 if (flowNode == null) {
105 LOG.debug("Node updated notification received.");
106 manager.enqueue(new InventoryOperation() {
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>>() {
117 public void onSuccess(Optional<FlowCapableNode> optional) {
118 enqueueWriteNodeDataTx(node, flowNode, path);
119 if (!optional.isPresent()) {
120 enqueuePutTable0Tx(ref);
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);
135 private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
136 manager.enqueue(new InventoryOperation() {
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);
146 private void enqueuePutTable0Tx(final NodeRef ref) {
147 manager.enqueue(new InventoryOperation() {
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);