Bug 4105: Implement EntityOwnershipListener registration/notification
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / DistributedEntityOwnershipService.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 akka.actor.ActorRef;
11 import akka.dispatch.OnComplete;
12 import akka.util.Timeout;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
20 import org.opendaylight.controller.cluster.datastore.config.Configuration;
21 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
22 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
23 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
24 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
25 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
26 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
27 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
28 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
32 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
33 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import scala.concurrent.Future;
39
40 /**
41  * The distributed implementation of the EntityOwnershipService.
42  *
43  * @author Thomas Pantelis
44  */
45 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
46     private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
47     static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
48     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
49
50     private final DistributedDataStore datastore;
51     private final ConcurrentMap<Entity, EntityOwnershipCandidate> registeredEntities = new ConcurrentHashMap<>();
52     private volatile ActorRef localEntityOwnershipShard;
53
54     public DistributedEntityOwnershipService(DistributedDataStore datastore) {
55         this.datastore = datastore;
56     }
57
58     public void start() {
59         ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
60
61         Configuration configuration = datastore.getActorContext().getConfiguration();
62         Collection<String> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
63         CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
64                 "entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
65                         newShardPropsCreator(), null);
66
67         Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
68                 createShard, MESSAGE_TIMEOUT);
69
70         createFuture.onComplete(new OnComplete<Object>() {
71             @Override
72             public void onComplete(Throwable failure, Object response) {
73                 if(failure != null) {
74                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
75                 } else {
76                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
77                 }
78             }
79         }, datastore.getActorContext().getClientDispatcher());
80     }
81
82     private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
83         Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
84         future.onComplete(new OnComplete<Object>() {
85             @Override
86             public void onComplete(Throwable failure, Object response) {
87                 if(failure != null) {
88                     LOG.debug("Error sending message {} to {}", message, shardActor, failure);
89                 } else {
90                     LOG.debug("{} message to {} succeeded", message, shardActor, failure);
91                 }
92             }
93         }, datastore.getActorContext().getClientDispatcher());
94     }
95
96     private void executeLocalEntityOwnershipShardOperation(final Object message) {
97         if(localEntityOwnershipShard == null) {
98             Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
99             future.onComplete(new OnComplete<ActorRef>() {
100                 @Override
101                 public void onComplete(Throwable failure, ActorRef shardActor) {
102                     if(failure != null) {
103                         LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
104                     } else {
105                         localEntityOwnershipShard = shardActor;
106                         executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
107                     }
108                 }
109             }, datastore.getActorContext().getClientDispatcher());
110
111         } else {
112             executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
113         }
114     }
115
116     @Override
117     public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
118             throws CandidateAlreadyRegisteredException {
119         Preconditions.checkNotNull(entity, "entity cannot be null");
120         Preconditions.checkNotNull(candidate, "candidate cannot be null");
121
122         EntityOwnershipCandidate currentCandidate = registeredEntities.putIfAbsent(entity, candidate);
123         if(currentCandidate != null) {
124             throw new CandidateAlreadyRegisteredException(entity, currentCandidate);
125         }
126
127         RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(candidate, entity);
128
129         LOG.debug("Registering candidate with message: {}", registerCandidate);
130
131         executeLocalEntityOwnershipShardOperation(registerCandidate);
132         return new DistributedEntityOwnershipCandidateRegistration(candidate, entity, this);
133     }
134
135     void unregisterCandidate(Entity entity, EntityOwnershipCandidate entityOwnershipCandidate) {
136         LOG.debug("Unregistering candidate {} for {}", entityOwnershipCandidate, entity);
137
138         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entityOwnershipCandidate, entity));
139         registeredEntities.remove(entity);
140     }
141
142     @Override
143     public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
144         Preconditions.checkNotNull(entityType, "entityType cannot be null");
145         Preconditions.checkNotNull(listener, "listener cannot be null");
146
147         RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
148
149         LOG.debug("Registering listener with message: {}", registerListener);
150
151         executeLocalEntityOwnershipShardOperation(registerListener);
152         return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
153     }
154
155     void unregisterListener(String entityType, EntityOwnershipListener listener) {
156         LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
157
158         executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
159     }
160
161     @Override
162     public void close() {
163     }
164
165     protected EntityOwnershipShardPropsCreator newShardPropsCreator() {
166         return new EntityOwnershipShardPropsCreator(datastore.getActorContext().getCurrentMemberName());
167     }
168
169     @VisibleForTesting
170     ActorRef getLocalEntityOwnershipShard() {
171         return localEntityOwnershipShard;
172     }
173 }