cc0b8a318ddc29778cf17b82da2a78adb0237887
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnerChangeListener.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.cluster.datastore.entityownership;
9
10 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNERS_PATH;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_QNAME;
12 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.createEntity;
13 import com.google.common.base.Objects;
14 import com.google.common.base.Optional;
15 import java.util.Collection;
16 import org.opendaylight.controller.cluster.datastore.ShardDataTree;
17 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
24 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Listens for entity owner changes and notifies the EntityOwnershipListenerSupport appropriately.
30  *
31  * @author Thomas Pantelis
32  */
33 class EntityOwnerChangeListener implements DOMDataTreeChangeListener {
34     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerChangeListener.class);
35
36     private final String localMemberName;
37     private final EntityOwnershipListenerSupport listenerSupport;
38
39     EntityOwnerChangeListener(String localMemberName, EntityOwnershipListenerSupport listenerSupport) {
40         this.localMemberName = localMemberName;
41         this.listenerSupport = listenerSupport;
42     }
43
44     void init(ShardDataTree shardDataTree) {
45         shardDataTree.registerTreeChangeListener(YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).
46                 node(EntityType.QNAME).node(EntityType.QNAME).node(ENTITY_QNAME).node(ENTITY_QNAME).node(EntityOwnersModel.ENTITY_OWNER_QNAME).build(), this);
47     }
48
49     @Override
50     public void onDataTreeChanged(Collection<DataTreeCandidate> changes) {
51         for(DataTreeCandidate change: changes) {
52             DataTreeCandidateNode changeRoot = change.getRootNode();
53             LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get();
54
55             LOG.debug("{}: Entity node changed: {}, {}", logId(), changeRoot.getModificationType(), change.getRootPath());
56
57             String newOwner = extractOwner(ownerLeaf);
58
59             String origOwner = null;
60             Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore();
61             if(dataBefore.isPresent()) {
62                 origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get());
63             }
64
65             LOG.debug("{}: New owner: {}, Original owner: {}", logId(), newOwner, origOwner);
66
67             if(!Objects.equal(origOwner, newOwner)) {
68                 boolean isOwner = Objects.equal(localMemberName, newOwner);
69                 boolean wasOwner = Objects.equal(localMemberName, origOwner);
70                 boolean hasOwner = newOwner != null && !newOwner.toString().isEmpty();
71
72                 Entity entity = createEntity(change.getRootPath());
73
74                 LOG.debug("{}: Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}, hasOwner: {}",
75                         logId(), entity, wasOwner, isOwner, hasOwner);
76
77                 listenerSupport.notifyEntityOwnershipListeners(entity, wasOwner, isOwner, hasOwner);
78             }
79         }
80     }
81
82     private String extractOwner(LeafNode<?> ownerLeaf) {
83         Object value = ownerLeaf.getValue();
84         return value != null ? value.toString() : null;
85     }
86
87     private String logId() {
88         return listenerSupport.getLogId();
89     }
90 }

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