BUG-4167: fall back to unknown module for empty YangInstanceIdentifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardRecoveryCoordinator.java
index 776ab276c612dfd131e8ade7a598123b26c5c7b1..624e68dd8662b6a85fa09380352b46758c0f5732 100644 (file)
@@ -8,16 +8,21 @@
 package org.opendaylight.controller.cluster.datastore;
 
 import com.google.common.base.Preconditions;
+import java.io.File;
 import java.io.IOException;
-import org.opendaylight.controller.cluster.datastore.modification.MutableCompositeModification;
+import java.util.Map.Entry;
+import java.util.Optional;
+import org.opendaylight.controller.cluster.datastore.persisted.DataTreeCandidateSupplier;
+import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
+import org.opendaylight.controller.cluster.datastore.utils.DataTreeModificationOutput;
+import org.opendaylight.controller.cluster.datastore.utils.NormalizedNodeXMLOutput;
 import org.opendaylight.controller.cluster.datastore.utils.PruningDataTreeModification;
 import org.opendaylight.controller.cluster.datastore.utils.SerializationUtils;
 import org.opendaylight.controller.cluster.raft.RaftActorRecoveryCohort;
-import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationByteStringPayload;
-import org.opendaylight.controller.cluster.raft.protobuff.client.messages.CompositeModificationPayload;
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
@@ -62,22 +67,21 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
         Preconditions.checkState(transaction != null, "call startLogRecovery before calling appendRecoveredLogEntry");
 
         try {
-            if (payload instanceof DataTreeCandidatePayload) {
-                DataTreeCandidates.applyToModification(transaction, ((DataTreeCandidatePayload)payload).getCandidate());
-                size++;
-            } else if (payload instanceof CompositeModificationPayload) {
-                MutableCompositeModification.fromSerializable(
-                    ((CompositeModificationPayload) payload).getModification()).apply(transaction);
-                size++;
-            } else if (payload instanceof CompositeModificationByteStringPayload) {
-                MutableCompositeModification.fromSerializable(
-                        ((CompositeModificationByteStringPayload) payload).getModification()).apply(transaction);
+            if (payload instanceof DataTreeCandidateSupplier) {
+                final Entry<Optional<TransactionIdentifier>, DataTreeCandidate> e =
+                        ((DataTreeCandidateSupplier)payload).getCandidate();
+
+                DataTreeCandidates.applyToModification(transaction, e.getValue());
                 size++;
+
+                if (e.getKey().isPresent()) {
+                    // FIXME: BUG-5280: propagate transaction state
+                }
             } else {
                 log.error("{}: Unknown payload {} received during recovery", shardName, payload);
             }
         } catch (IOException e) {
-            log.error("{}: Error extracting ModificationPayload", shardName, e);
+            log.error("{}: Error extracting payload", shardName, e);
         }
     }
 
@@ -95,8 +99,13 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
         log.debug("{}: Applying current log recovery batch with size {}", shardName, size);
         try {
             commitTransaction(transaction);
-        } catch (DataValidationFailedException e) {
-            log.error("{}: Failed to apply recovery batch", shardName, e);
+        } catch (Exception e) {
+            File file = new File(System.getProperty("karaf.data", "."),
+                    "failed-recovery-batch-" + shardName + ".out");
+            DataTreeModificationOutput.toFile(file, transaction.getResultingModification());
+            throw new RuntimeException(String.format(
+                    "%s: Failed to apply recovery batch. Modification data was written to file %s",
+                    shardName, file), e);
         }
         transaction = null;
     }
@@ -116,8 +125,13 @@ class ShardRecoveryCoordinator implements RaftActorRecoveryCohort {
         tx.write(YangInstanceIdentifier.EMPTY, node);
         try {
             commitTransaction(tx);
-        } catch (DataValidationFailedException e) {
-            log.error("{}: Failed to apply recovery snapshot", shardName, e);
+        } catch (Exception e) {
+            File file = new File(System.getProperty("karaf.data", "."),
+                    "failed-recovery-snapshot-" + shardName + ".xml");
+            NormalizedNodeXMLOutput.toFile(file, node);
+            throw new RuntimeException(String.format(
+                    "%s: Failed to apply recovery snapshot. Node data was written to file %s",
+                    shardName, file), e);
         }
     }