ec0081d38be2e0469a61aa17ef89919ced413ef5
[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 static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.ENTITY_OWNER_NODE_ID;
11 import static org.opendaylight.controller.cluster.datastore.entityownership.EntityOwnersModel.entityPath;
12 import akka.actor.ActorRef;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.annotations.VisibleForTesting;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.base.Strings;
20 import java.util.Collection;
21 import java.util.concurrent.ConcurrentHashMap;
22 import java.util.concurrent.ConcurrentMap;
23 import java.util.concurrent.TimeUnit;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
26 import org.opendaylight.controller.cluster.datastore.config.Configuration;
27 import org.opendaylight.controller.cluster.datastore.config.ModuleShardConfiguration;
28 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
29 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterListenerLocal;
30 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterCandidateLocal;
31 import org.opendaylight.controller.cluster.datastore.entityownership.messages.UnregisterListenerLocal;
32 import org.opendaylight.controller.cluster.datastore.entityownership.selectionstrategy.EntityOwnerSelectionStrategyConfig;
33 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
34 import org.opendaylight.controller.cluster.datastore.messages.GetShardDataTree;
35 import org.opendaylight.controller.cluster.datastore.shardstrategy.ModuleShardStrategy;
36 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
37 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
38 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
39 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
40 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
41 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
42 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import scala.concurrent.Await;
52 import scala.concurrent.Future;
53 import scala.concurrent.duration.Duration;
54
55 /**
56  * The distributed implementation of the EntityOwnershipService.
57  *
58  * @author Thomas Pantelis
59  */
60 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
61     private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
62     static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
63     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
64
65     private final DistributedDataStore datastore;
66     private final EntityOwnerSelectionStrategyConfig strategyConfig;
67     private final ConcurrentMap<Entity, Entity> registeredEntities = new ConcurrentHashMap<>();
68     private volatile ActorRef localEntityOwnershipShard;
69     private volatile DataTree localEntityOwnershipShardDataTree;
70
71     public DistributedEntityOwnershipService(DistributedDataStore datastore, EntityOwnerSelectionStrategyConfig strategyConfig) {
72         this.datastore = Preconditions.checkNotNull(datastore);
73         this.strategyConfig = Preconditions.checkNotNull(strategyConfig);
74     }
75
76     public void start() {
77         ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
78
79         Configuration configuration = datastore.getActorContext().getConfiguration();
80         Collection<String> entityOwnersMemberNames = configuration.getUniqueMemberNamesForAllShards();
81         CreateShard createShard = new CreateShard(new ModuleShardConfiguration(EntityOwners.QNAME.getNamespace(),
82                 "entity-owners", ENTITY_OWNERSHIP_SHARD_NAME, ModuleShardStrategy.NAME, entityOwnersMemberNames),
83                         newShardBuilder(), null);
84
85         Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
86                 createShard, MESSAGE_TIMEOUT);
87
88         createFuture.onComplete(new OnComplete<Object>() {
89             @Override
90             public void onComplete(Throwable failure, Object response) {
91                 if(failure != null) {
92                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
93                 } else {
94                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
95                 }
96             }
97         }, datastore.getActorContext().getClientDispatcher());
98     }
99
100     private void executeEntityOwnershipShardOperation(final ActorRef shardActor, final Object message) {
101         Future<Object> future = datastore.getActorContext().executeOperationAsync(shardActor, message, MESSAGE_TIMEOUT);
102         future.onComplete(new OnComplete<Object>() {
103             @Override
104             public void onComplete(Throwable failure, Object response) {
105                 if(failure != null) {
106                     LOG.debug("Error sending message {} to {}", message, shardActor, failure);
107                 } else {
108                     LOG.debug("{} message to {} succeeded", message, shardActor, failure);
109                 }
110             }
111         }, datastore.getActorContext().getClientDispatcher());
112     }
113
114     private void executeLocalEntityOwnershipShardOperation(final Object message) {
115         if(localEntityOwnershipShard == null) {
116             Future<ActorRef> future = datastore.getActorContext().findLocalShardAsync(ENTITY_OWNERSHIP_SHARD_NAME);
117             future.onComplete(new OnComplete<ActorRef>() {
118                 @Override
119                 public void onComplete(Throwable failure, ActorRef shardActor) {
120                     if(failure != null) {
121                         LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, failure);
122                     } else {
123                         localEntityOwnershipShard = shardActor;
124                         executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
125                     }
126                 }
127             }, datastore.getActorContext().getClientDispatcher());
128
129         } else {
130             executeEntityOwnershipShardOperation(localEntityOwnershipShard, message);
131         }
132     }
133
134     @Override
135     public EntityOwnershipCandidateRegistration registerCandidate(Entity entity)
136             throws CandidateAlreadyRegisteredException {
137         Preconditions.checkNotNull(entity, "entity cannot be null");
138
139         if(registeredEntities.putIfAbsent(entity, entity) != null) {
140             throw new CandidateAlreadyRegisteredException(entity);
141         }
142
143         RegisterCandidateLocal registerCandidate = new RegisterCandidateLocal(entity);
144
145         LOG.debug("Registering candidate with message: {}", registerCandidate);
146
147         executeLocalEntityOwnershipShardOperation(registerCandidate);
148         return new DistributedEntityOwnershipCandidateRegistration(entity, this);
149     }
150
151     void unregisterCandidate(Entity entity) {
152         LOG.debug("Unregistering candidate for {}", entity);
153
154         executeLocalEntityOwnershipShardOperation(new UnregisterCandidateLocal(entity));
155         registeredEntities.remove(entity);
156     }
157
158     @Override
159     public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
160         Preconditions.checkNotNull(entityType, "entityType cannot be null");
161         Preconditions.checkNotNull(listener, "listener cannot be null");
162
163         RegisterListenerLocal registerListener = new RegisterListenerLocal(listener, entityType);
164
165         LOG.debug("Registering listener with message: {}", registerListener);
166
167         executeLocalEntityOwnershipShardOperation(registerListener);
168         return new DistributedEntityOwnershipListenerRegistration(listener, entityType, this);
169     }
170
171     @Override
172     public Optional<EntityOwnershipState> getOwnershipState(Entity forEntity) {
173         Preconditions.checkNotNull(forEntity, "forEntity cannot be null");
174
175         DataTree dataTree = getLocalEntityOwnershipShardDataTree();
176         if(dataTree == null) {
177             return Optional.absent();
178         }
179
180         Optional<NormalizedNode<?, ?>> entityNode = dataTree.takeSnapshot().readNode(
181                 entityPath(forEntity.getType(), forEntity.getId()));
182         if(!entityNode.isPresent()) {
183             return Optional.absent();
184         }
185
186         String localMemberName = datastore.getActorContext().getCurrentMemberName();
187         Optional<DataContainerChild<? extends PathArgument, ?>> ownerLeaf = ((MapEntryNode)entityNode.get()).
188                 getChild(ENTITY_OWNER_NODE_ID);
189         String owner = ownerLeaf.isPresent() ? ownerLeaf.get().getValue().toString() : null;
190         boolean hasOwner = !Strings.isNullOrEmpty(owner);
191         boolean isOwner = hasOwner && localMemberName.equals(owner);
192
193         return Optional.of(new EntityOwnershipState(isOwner, hasOwner));
194     }
195
196     @Override
197     public boolean isCandidateRegistered(@Nonnull Entity entity) {
198         return registeredEntities.get(entity) != null;
199     }
200
201     private DataTree getLocalEntityOwnershipShardDataTree() {
202         if(localEntityOwnershipShardDataTree == null) {
203             try {
204                 if(localEntityOwnershipShard == null) {
205                     localEntityOwnershipShard = Await.result(datastore.getActorContext().findLocalShardAsync(
206                             ENTITY_OWNERSHIP_SHARD_NAME), Duration.Inf());
207                 }
208
209                 localEntityOwnershipShardDataTree = (DataTree) Await.result(Patterns.ask(localEntityOwnershipShard,
210                         GetShardDataTree.INSTANCE, MESSAGE_TIMEOUT), Duration.Inf());
211             } catch (Exception e) {
212                 LOG.error("Failed to find local {} shard", ENTITY_OWNERSHIP_SHARD_NAME, e);
213             }
214         }
215
216         return localEntityOwnershipShardDataTree;
217     }
218
219     void unregisterListener(String entityType, EntityOwnershipListener listener) {
220         LOG.debug("Unregistering listener {} for entity type {}", listener, entityType);
221
222         executeLocalEntityOwnershipShardOperation(new UnregisterListenerLocal(listener, entityType));
223     }
224
225     @Override
226     public void close() {
227     }
228
229     protected EntityOwnershipShard.Builder newShardBuilder() {
230         return EntityOwnershipShard.newBuilder().localMemberName(datastore.getActorContext().getCurrentMemberName())
231                 .ownerSelectionStrategyConfig(this.strategyConfig);
232     }
233
234     @VisibleForTesting
235     ActorRef getLocalEntityOwnershipShard() {
236         return localEntityOwnershipShard;
237     }
238 }