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