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

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