04037b35ea7ff81d8ce25fe02da8e15b16c9156b
[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.createEntity;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Strings;
15 import com.google.common.base.Verify;
16 import java.util.Collection;
17 import java.util.Objects;
18 import org.opendaylight.controller.cluster.access.concepts.MemberName;
19 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
20 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Listens for entity owner changes and notifies the EntityOwnershipListenerSupport appropriately.
29  *
30  * @author Thomas Pantelis
31  */
32 class EntityOwnerChangeListener extends AbstractEntityOwnerChangeListener {
33     private static final Logger LOG = LoggerFactory.getLogger(EntityOwnerChangeListener.class);
34
35     private final String localMemberName;
36     private final EntityOwnershipChangePublisher publisher;
37
38     EntityOwnerChangeListener(final MemberName localMemberName, final EntityOwnershipChangePublisher publisher) {
39         this.localMemberName = Verify.verifyNotNull(localMemberName.getName());
40         this.publisher = Preconditions.checkNotNull(publisher);
41     }
42
43     @Override
44     public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
45         for (DataTreeCandidate change: changes) {
46             DataTreeCandidateNode changeRoot = change.getRootNode();
47             LeafNode<?> ownerLeaf = (LeafNode<?>) changeRoot.getDataAfter().get();
48
49             LOG.debug("{}: Entity node changed: {}, {}", logId(), changeRoot.getModificationType(),
50                     change.getRootPath());
51
52             String newOwner = extractOwner(ownerLeaf);
53
54             String origOwner = null;
55             Optional<NormalizedNode<?, ?>> dataBefore = changeRoot.getDataBefore();
56             if (dataBefore.isPresent()) {
57                 origOwner = extractOwner((LeafNode<?>) changeRoot.getDataBefore().get());
58             }
59
60             LOG.debug("{}: New owner: {}, Original owner: {}", logId(), newOwner, origOwner);
61
62             if (!Objects.equals(origOwner, newOwner)) {
63                 boolean isOwner = localMemberName.equals(newOwner);
64                 boolean wasOwner = localMemberName.equals(origOwner);
65                 boolean hasOwner = !Strings.isNullOrEmpty(newOwner);
66
67                 DOMEntity entity = createEntity(change.getRootPath());
68
69                 LOG.debug(
70                     "{}: Calling notifyEntityOwnershipListeners: entity: {}, wasOwner: {}, isOwner: {}, hasOwner: {}",
71                     logId(), entity, wasOwner, isOwner, hasOwner);
72
73                 publisher.notifyEntityOwnershipListeners(entity, wasOwner, isOwner, hasOwner);
74             }
75         }
76     }
77
78     private String logId() {
79         return publisher.getLogId();
80     }
81 }

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