Bug 4105: Add EntityOwnershipShard
[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 java.util.concurrent.TimeUnit;
14 import org.opendaylight.controller.cluster.datastore.DistributedDataStore;
15 import org.opendaylight.controller.cluster.datastore.messages.CreateShard;
16 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
17 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidate;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import scala.concurrent.Future;
26
27 /**
28  * The distributed implementation of the EntityOwnershipService.
29  *
30  * @author Thomas Pantelis
31  */
32 public class DistributedEntityOwnershipService implements EntityOwnershipService, AutoCloseable {
33     private static final Logger LOG = LoggerFactory.getLogger(DistributedEntityOwnershipService.class);
34     static final String ENTITY_OWNERSHIP_SHARD_NAME = "entity-ownership";
35     private static final Timeout MESSAGE_TIMEOUT = new Timeout(1, TimeUnit.MINUTES);
36
37     private final DistributedDataStore datastore;
38
39     public DistributedEntityOwnershipService(DistributedDataStore datastore) {
40         this.datastore = datastore;
41     }
42
43     public void start() {
44         ActorRef shardManagerActor = datastore.getActorContext().getShardManager();
45
46         CreateShard createShard = new CreateShard(ENTITY_OWNERSHIP_SHARD_NAME,
47                 datastore.getActorContext().getConfiguration().getUniqueMemberNamesForAllShards(),
48                 new EntityOwnershipShardPropsCreator(), null);
49
50         Future<Object> createFuture = datastore.getActorContext().executeOperationAsync(shardManagerActor,
51                 createShard, MESSAGE_TIMEOUT);
52
53         createFuture.onComplete(new OnComplete<Object>() {
54             @Override
55             public void onComplete(Throwable failure, Object response) {
56                 if(failure != null) {
57                     LOG.error("Failed to create {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
58                 } else {
59                     LOG.info("Successfully created {} shard", ENTITY_OWNERSHIP_SHARD_NAME);
60                 }
61             }
62         }, datastore.getActorContext().getClientDispatcher());
63     }
64
65     @Override
66     public EntityOwnershipCandidateRegistration registerCandidate(Entity entity, EntityOwnershipCandidate candidate)
67             throws CandidateAlreadyRegisteredException {
68         // TODO Auto-generated method stub
69         return null;
70     }
71
72     @Override
73     public EntityOwnershipListenerRegistration registerListener(Entity entity, EntityOwnershipListener listener) {
74         // TODO Auto-generated method stub
75         return null;
76     }
77
78     @Override
79     public void close() {
80     }
81 }