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.openflowplugin.applications.inventory.manager;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
19 import java.util.concurrent.TimeUnit;
21 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
26 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.*;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
43 import org.opendaylight.yangtools.yang.common.QName;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 class NodeChangeCommiter implements OpendaylightInventoryListener,EntityOwnershipListener {
49 private static final Logger LOG = LoggerFactory.getLogger(NodeChangeCommiter.class);
51 private final FlowCapableInventoryProvider manager;
52 private final EntityOwnershipService entityOwnershipService;
54 private final String DEVICE_TYPE = "openflow";
56 // cache for nodes which were deleted, we get more than one nodeRemoved notification
57 private Cache<NodeRef, Boolean> deletedNodeCache =
58 CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(10, TimeUnit.SECONDS).build();
60 // cache for node-connectors which were deleted, we get more than one nodeConnectorRemoved notification
61 private Cache<NodeConnectorRef, Boolean> deletedNodeConnectorCache =
62 CacheBuilder.newBuilder().maximumSize(1000000).expireAfterWrite(10, TimeUnit.SECONDS).build();
64 public NodeChangeCommiter(final FlowCapableInventoryProvider manager, final EntityOwnershipService entityOwnershipService) {
65 this.manager = Preconditions.checkNotNull(manager);
66 this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
70 registerEntityOwnershipChangeListener();
73 public void registerEntityOwnershipChangeListener() {
74 if(entityOwnershipService!=null) {
75 if(LOG.isDebugEnabled()) {
76 LOG.debug("registerEntityOwnershipChangeListener:" +
77 " Registering entity ownership change listener for entities of type {}", DEVICE_TYPE);
79 entityOwnershipService.registerListener(DEVICE_TYPE, this);
84 public synchronized void onNodeConnectorRemoved(final NodeConnectorRemoved connector) {
85 if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) == null) {
86 deletedNodeConnectorCache.put(connector.getNodeConnectorRef(), Boolean.TRUE);
88 //its been noted that creating an operation for already removed node-connectors, fails
89 // the entire transaction chain, there by failing deserving removals
90 LOG.debug("Already received notification to remove nodeConnector, {} - Ignored",
91 connector.getNodeConnectorRef().getValue());
95 if (!manager.deviceDataDeleteAllowed(getNodeId(connector.getNodeConnectorRef().getValue()))) {
99 LOG.debug("Node connector removed notification received, {}", connector.getNodeConnectorRef().getValue());
100 manager.enqueue(new InventoryOperation() {
102 public void applyOperation(final ReadWriteTransaction tx) {
103 final NodeConnectorRef ref = connector.getNodeConnectorRef();
104 LOG.debug("removing node connector {} ", ref.getValue());
105 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
111 public synchronized void onNodeConnectorUpdated(final NodeConnectorUpdated connector) {
112 if (deletedNodeConnectorCache.getIfPresent(connector.getNodeConnectorRef()) != null) {
113 deletedNodeConnectorCache.invalidate(connector.getNodeConnectorRef());
116 LOG.debug("Node connector updated notification received.");
117 manager.enqueue(new InventoryOperation() {
119 public void applyOperation(final ReadWriteTransaction tx) {
120 final NodeConnectorRef ref = connector.getNodeConnectorRef();
121 final NodeConnectorBuilder data = new NodeConnectorBuilder(connector);
122 data.setKey(new NodeConnectorKey(connector.getId()));
124 final FlowCapableNodeConnectorUpdated flowConnector = connector
125 .getAugmentation(FlowCapableNodeConnectorUpdated.class);
126 if (flowConnector != null) {
127 final FlowCapableNodeConnector augment = InventoryMapping.toInventoryAugment(flowConnector);
128 data.addAugmentation(FlowCapableNodeConnector.class, augment);
130 InstanceIdentifier<NodeConnector> value = (InstanceIdentifier<NodeConnector>) ref.getValue();
131 LOG.debug("updating node connector : {}.", value);
132 NodeConnector build = data.build();
133 tx.merge(LogicalDatastoreType.OPERATIONAL, value, build, true);
139 public synchronized void onNodeRemoved(final NodeRemoved node) {
141 if (deletedNodeCache.getIfPresent(node.getNodeRef()) == null) {
142 deletedNodeCache.put(node.getNodeRef(), Boolean.TRUE);
144 //its been noted that creating an operation for already removed node, fails
145 // the entire transaction chain, there by failing deserving removals
146 LOG.debug("Already received notification to remove node, {} - Ignored",
147 node.getNodeRef().getValue());
151 if (!manager.deviceDataDeleteAllowed(getNodeId(node.getNodeRef().getValue()))) {
155 LOG.debug("Node removed notification received, {}", node.getNodeRef().getValue());
156 manager.enqueue(new InventoryOperation() {
158 public void applyOperation(final ReadWriteTransaction tx) {
159 final NodeRef ref = node.getNodeRef();
160 LOG.debug("removing node : {}", ref.getValue());
161 tx.delete(LogicalDatastoreType.OPERATIONAL, ref.getValue());
167 public synchronized void onNodeUpdated(final NodeUpdated node) {
168 if (deletedNodeCache.getIfPresent(node.getNodeRef()) != null) {
169 deletedNodeCache.invalidate(node.getNodeRef());
172 final FlowCapableNodeUpdated flowNode = node.getAugmentation(FlowCapableNodeUpdated.class);
173 if (flowNode == null) {
176 LOG.debug("Node updated notification received,{}", node.getNodeRef().getValue());
177 manager.enqueue(new InventoryOperation() {
179 public void applyOperation(ReadWriteTransaction tx) {
180 final NodeRef ref = node.getNodeRef();
181 @SuppressWarnings("unchecked")
182 InstanceIdentifierBuilder<Node> builder = ((InstanceIdentifier<Node>) ref.getValue()).builder();
183 InstanceIdentifierBuilder<FlowCapableNode> augmentation = builder.augmentation(FlowCapableNode.class);
184 final InstanceIdentifier<FlowCapableNode> path = augmentation.build();
185 CheckedFuture<Optional<FlowCapableNode>, ?> readFuture = tx.read(LogicalDatastoreType.OPERATIONAL, path);
186 Futures.addCallback(readFuture, new FutureCallback<Optional<FlowCapableNode>>() {
188 public void onSuccess(Optional<FlowCapableNode> optional) {
189 enqueueWriteNodeDataTx(node, flowNode, path);
190 if (!optional.isPresent()) {
191 enqueuePutTable0Tx(ref);
196 public void onFailure(Throwable throwable) {
197 LOG.debug(String.format("Can't retrieve node data for node %s. Writing node data with table0.", node));
198 enqueueWriteNodeDataTx(node, flowNode, path);
199 enqueuePutTable0Tx(ref);
206 private void enqueueWriteNodeDataTx(final NodeUpdated node, final FlowCapableNodeUpdated flowNode, final InstanceIdentifier<FlowCapableNode> path) {
207 manager.enqueue(new InventoryOperation() {
209 public void applyOperation(final ReadWriteTransaction tx) {
210 final FlowCapableNode augment = InventoryMapping.toInventoryAugment(flowNode);
211 LOG.debug("updating node :{} ", path);
212 tx.merge(LogicalDatastoreType.OPERATIONAL, path, augment, true);
217 private void enqueuePutTable0Tx(final NodeRef ref) {
218 manager.enqueue(new InventoryOperation() {
220 public void applyOperation(ReadWriteTransaction tx) {
221 final TableKey tKey = new TableKey((short) 0);
222 final InstanceIdentifier<Table> tableIdentifier =
223 ((InstanceIdentifier<Node>) ref.getValue()).augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tKey));
224 TableBuilder tableBuilder = new TableBuilder();
225 Table table0 = tableBuilder.setId((short) 0).build();
226 LOG.debug("writing table :{} ", tableIdentifier);
227 tx.merge(LogicalDatastoreType.OPERATIONAL, tableIdentifier, table0, true);
233 public void ownershipChanged(final EntityOwnershipChange entityOwnershipChange) {
234 final Entity entity = entityOwnershipChange.getEntity();
235 NodeId nodeId = getNodeID(entity.getId());
236 InstanceIdentifier<Node> identifier = identifierFromNodeId(nodeId);
238 if (!entityOwnershipChange.hasOwner()) {
239 //NodeChangeCommitter : entityOwnershipChange - hasOwner,is false for node
240 LOG.debug("Node: {} is not owned by any controller instance, cleaning up the inventory data store ",nodeId);
241 NodeRef nodeRef = new NodeRef(identifier);
242 NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
243 onNodeRemoved(nodeRemoved);
247 LOG.debug("Node {} is currently owned by other controller instance", nodeId);
253 private static NodeId getNodeID(YangInstanceIdentifier yangInstanceIdentifier) {
256 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.
257 controller.md.sal.core.general.entity.rev150820.Entity.QNAME;
258 QName ENTITY_NAME = QName.create(ENTITY_QNAME, "name");
260 YangInstanceIdentifier.NodeIdentifierWithPredicates niWPredicates =
261 (YangInstanceIdentifier.NodeIdentifierWithPredicates) yangInstanceIdentifier.getLastPathArgument();
262 Map<QName, Object> keyValMap = niWPredicates.getKeyValues();
263 String nodeIdStr = (String) (keyValMap.get(ENTITY_NAME));
264 return new NodeId(nodeIdStr);
267 private static InstanceIdentifier<Node> identifierFromNodeId(NodeId nodeId) {
268 NodeKey nodeKey = new NodeKey(nodeId);
269 InstanceIdentifier.InstanceIdentifierBuilder<Node> builder =
270 InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
271 return builder.build();
274 private static NodeRemoved nodeRemoved(final NodeRef nodeRef) {
275 NodeRemovedBuilder builder = new NodeRemovedBuilder();
276 builder.setNodeRef(nodeRef);
277 return builder.build();
279 private NodeId getNodeId(InstanceIdentifier<?> iid) {
280 return iid.firstKeyOf(Node.class).getId();