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