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