Bug 4105: Implement DistributedEntityOwnershipService#registerCandidate
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnershipShard.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.Props;
11 import java.util.Map;
12 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
13 import org.opendaylight.controller.cluster.datastore.Shard;
14 import org.opendaylight.controller.cluster.datastore.entityownership.messages.RegisterCandidateLocal;
15 import org.opendaylight.controller.cluster.datastore.identifiers.ShardIdentifier;
16 import org.opendaylight.controller.cluster.datastore.messages.SuccessReply;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 /**
20  * Special Shard for EntityOwnership.
21  *
22  * @author Thomas Pantelis
23  */
24 public class EntityOwnershipShard extends Shard {
25
26     private static DatastoreContext noPersistenceDatastoreContext(DatastoreContext datastoreContext) {
27         return DatastoreContext.newBuilderFrom(datastoreContext).persistent(false).build();
28     }
29
30     protected EntityOwnershipShard(ShardIdentifier name, Map<String, String> peerAddresses,
31             DatastoreContext datastoreContext, SchemaContext schemaContext) {
32         super(name, peerAddresses, noPersistenceDatastoreContext(datastoreContext), schemaContext);
33     }
34
35     @Override
36     protected void onDatastoreContext(DatastoreContext context) {
37         super.onDatastoreContext(noPersistenceDatastoreContext(context));
38     }
39
40     @Override
41     public void onReceiveCommand(final Object message) throws Exception {
42         if(message instanceof RegisterCandidateLocal) {
43             onRegisterCandidateLocal((RegisterCandidateLocal)message);
44         } else {
45             super.onReceiveCommand(message);
46         }
47     }
48
49     private void onRegisterCandidateLocal(RegisterCandidateLocal registerCandidate) {
50         getSender().tell(SuccessReply.INSTANCE, getSelf());
51     }
52
53     public static Props props(final ShardIdentifier name, final Map<String, String> peerAddresses,
54             final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
55         return Props.create(new Creator(name, peerAddresses, datastoreContext, schemaContext));
56     }
57
58     private static class Creator extends AbstractShardCreator {
59         private static final long serialVersionUID = 1L;
60
61         Creator(final ShardIdentifier name, final Map<String, String> peerAddresses,
62                 final DatastoreContext datastoreContext, final SchemaContext schemaContext) {
63             super(name, peerAddresses, datastoreContext, schemaContext);
64         }
65
66         @Override
67         public Shard create() throws Exception {
68             return new EntityOwnershipShard(name, peerAddresses, datastoreContext, schemaContext);
69         }
70     }
71 }