+ if(!recoveryFinished()){
+ // FIXME : Properly handle recovery
+ return;
+ }
+
+ if (message instanceof CreateTransactionChain) {
+ createTransactionChain();
+ } else if (message.getClass().equals(RegisterChangeListener.SERIALIZABLE_CLASS)) {
+ registerChangeListener(RegisterChangeListener.fromSerializable(getContext().system(), message));
+ } else if (message instanceof UpdateSchemaContext) {
+ updateSchemaContext((UpdateSchemaContext) message);
+ } else if (message instanceof ForwardedCommitTransaction) {
+ handleForwardedCommit((ForwardedCommitTransaction) message);
+ } else if (message instanceof Persistent) {
+ commit(((Persistent)message).payload());
+ } else if (message instanceof CreateTransaction) {
+ createTransaction((CreateTransaction) message);
+ } else if(message instanceof NonPersistent){
+ commit(((NonPersistent)message).payload());
+ } else {
+ throw new Exception("Not recognized message in Shard::OnReceive"+message);
+ }
+ }
+
+ private void createTransaction(CreateTransaction createTransaction) {
+ DOMStoreReadWriteTransaction transaction =
+ store.newReadWriteTransaction();
+ ActorRef transactionActor = getContext().actorOf(
+ ShardTransaction.props(transaction, getSelf(), schemaContext), "shard-" + createTransaction.getTransactionId());
+ getSender()
+ .tell(new CreateTransactionReply(transactionActor.path().toString(), createTransaction.getTransactionId()).toSerializable(),
+ getSelf());
+ }
+
+ private void commit(Object serialized) {
+ Modification modification = MutableCompositeModification.fromSerializable(serialized, schemaContext);
+ DOMStoreThreePhaseCommitCohort cohort =
+ modificationToCohort.remove(serialized);
+ if (cohort == null) {
+ log.error(
+ "Could not find cohort for modification : " + modification);
+ return;
+ }
+ final ListenableFuture<Void> future = cohort.commit();
+ final ActorRef sender = getSender();
+ final ActorRef self = getSelf();
+ future.addListener(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ future.get();
+ sender.tell(new CommitTransactionReply(), self);
+ } catch (InterruptedException | ExecutionException e) {
+ // FIXME : Handle this properly
+ log.error(e, "An exception happened when committing");
+ }
+ }
+ }, getContext().dispatcher());
+ }
+
+ private void handleForwardedCommit(ForwardedCommitTransaction message) {
+ Object serializedModification = message.getModification().toSerializable();
+
+ modificationToCohort
+ .put(serializedModification , message.getCohort());
+ if(persistent) {
+ getSelf().forward(Persistent.create(serializedModification),
+ getContext());
+ } else {
+ getSelf().forward(NonPersistent.create(serializedModification),
+ getContext());
+ }
+ }
+
+ private void updateSchemaContext(UpdateSchemaContext message) {
+ this.schemaContext = message.getSchemaContext();
+ store.onGlobalContextUpdated(message.getSchemaContext());
+ }
+
+ private void registerChangeListener(
+ RegisterChangeListener registerChangeListener) {
+
+ ActorSelection dataChangeListenerPath = getContext()
+ .system().actorSelection(registerChangeListener.getDataChangeListenerPath());
+
+ AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>
+ listener = new DataChangeListenerProxy(dataChangeListenerPath);
+
+ org.opendaylight.yangtools.concepts.ListenerRegistration<AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>>
+ registration =
+ store.registerChangeListener(registerChangeListener.getPath(),
+ listener, registerChangeListener.getScope());
+ ActorRef listenerRegistration =
+ getContext().actorOf(
+ DataChangeListenerRegistration.props(registration));
+ getSender()
+ .tell(new RegisterChangeListenerReply(listenerRegistration.path()).toSerializable(),
+ getSelf());
+ }