Bug 7521: Convert byte[] to ShardManagerSnapshot in DatastoreSnapshot
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / DataTreeCohortActorRegistry.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.PoisonPill;
13 import akka.actor.Status;
14 import akka.util.Timeout;
15 import com.google.common.base.Preconditions;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import javax.annotation.concurrent.NotThreadSafe;
22 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.CanCommit;
24 import org.opendaylight.controller.md.sal.dom.spi.AbstractRegistrationTree;
25 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeNode;
26 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeSnapshot;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Registry of user commit cohorts, which is responsible for handling registration and calculation
41  * of affected cohorts based on {@link DataTreeCandidate}.
42  *
43  */
44 @NotThreadSafe
45 class DataTreeCohortActorRegistry extends AbstractRegistrationTree<ActorRef> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCohortActorRegistry.class);
48
49     private final Map<ActorRef, RegistrationTreeNode<ActorRef>> cohortToNode = new HashMap<>();
50
51     Collection<ActorRef> getCohortActors() {
52         return new ArrayList<>(cohortToNode.keySet());
53     }
54
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     void registerCohort(final ActorRef sender, final RegisterCohort cohort) {
57         takeLock();
58         try {
59             final ActorRef cohortRef = cohort.getCohort();
60             final RegistrationTreeNode<ActorRef> node =
61                     findNodeFor(cohort.getPath().getRootIdentifier().getPathArguments());
62             addRegistration(node, cohort.getCohort());
63             cohortToNode.put(cohortRef, node);
64         } catch (final Exception e) {
65             sender.tell(new Status.Failure(e), ActorRef.noSender());
66             return;
67         } finally {
68             releaseLock();
69         }
70         sender.tell(new Status.Success(null), ActorRef.noSender());
71     }
72
73     void removeCommitCohort(final ActorRef sender, final RemoveCohort message) {
74         final ActorRef cohort = message.getCohort();
75         final RegistrationTreeNode<ActorRef> node = cohortToNode.get(cohort);
76         if (node != null) {
77             removeRegistration(node, cohort);
78             cohortToNode.remove(cohort);
79         }
80         sender.tell(new Status.Success(null), ActorRef.noSender());
81         cohort.tell(PoisonPill.getInstance(), cohort);
82     }
83
84     Collection<DataTreeCohortActor.CanCommit> createCanCommitMessages(final TransactionIdentifier txId,
85             final DataTreeCandidate candidate, final SchemaContext schema) {
86         try (RegistrationTreeSnapshot<ActorRef> cohorts = takeSnapshot()) {
87             return new CanCommitMessageBuilder(txId, candidate, schema).perform(cohorts.getRootNode());
88         }
89     }
90
91     void process(final ActorRef sender, final CohortRegistryCommand message) {
92         if (message instanceof RegisterCohort) {
93             registerCohort(sender, (RegisterCohort) message);
94         } else if (message instanceof RemoveCohort) {
95             removeCommitCohort(sender, (RemoveCohort) message);
96         }
97     }
98
99     abstract static class CohortRegistryCommand {
100
101         private final ActorRef cohort;
102
103         CohortRegistryCommand(final ActorRef cohort) {
104             this.cohort = Preconditions.checkNotNull(cohort);
105         }
106
107         ActorRef getCohort() {
108             return cohort;
109         }
110     }
111
112     static class RegisterCohort extends CohortRegistryCommand {
113
114         private final DOMDataTreeIdentifier path;
115
116         RegisterCohort(final DOMDataTreeIdentifier path, final ActorRef cohort) {
117             super(cohort);
118             this.path = path;
119
120         }
121
122         public DOMDataTreeIdentifier getPath() {
123             return path;
124         }
125
126     }
127
128     static class RemoveCohort extends CohortRegistryCommand {
129
130         RemoveCohort(final ActorRef cohort) {
131             super(cohort);
132         }
133
134     }
135
136     private static class CanCommitMessageBuilder {
137
138         private final TransactionIdentifier txId;
139         private final DataTreeCandidate candidate;
140         private final SchemaContext schema;
141         private final Collection<DataTreeCohortActor.CanCommit> messages =
142                 new ArrayList<>();
143
144         CanCommitMessageBuilder(final TransactionIdentifier txId, final DataTreeCandidate candidate,
145                 final SchemaContext schema) {
146             this.txId = Preconditions.checkNotNull(txId);
147             this.candidate = Preconditions.checkNotNull(candidate);
148             this.schema = schema;
149         }
150
151         private void lookupAndCreateCanCommits(final List<PathArgument> args, final int offset,
152                 final RegistrationTreeNode<ActorRef> node) {
153
154             if (args.size() != offset) {
155                 final PathArgument arg = args.get(offset);
156                 final RegistrationTreeNode<ActorRef> exactChild = node.getExactChild(arg);
157                 if (exactChild != null) {
158                     lookupAndCreateCanCommits(args, offset + 1, exactChild);
159                 }
160                 for (final RegistrationTreeNode<ActorRef> c : node.getInexactChildren(arg)) {
161                     lookupAndCreateCanCommits(args, offset + 1, c);
162                 }
163             } else {
164                 lookupAndCreateCanCommits(candidate.getRootPath(), node, candidate.getRootNode());
165             }
166         }
167
168         private void lookupAndCreateCanCommits(final YangInstanceIdentifier path,
169                 final RegistrationTreeNode<ActorRef> regNode, final DataTreeCandidateNode candNode) {
170             if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
171                 LOG.debug("Skipping unmodified candidate {}", path);
172                 return;
173             }
174             final Collection<ActorRef> regs = regNode.getRegistrations();
175             if (!regs.isEmpty()) {
176                 createCanCommits(regs, path, candNode);
177             }
178
179             for (final DataTreeCandidateNode candChild : candNode.getChildNodes()) {
180                 if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
181                     final RegistrationTreeNode<ActorRef> regChild =
182                             regNode.getExactChild(candChild.getIdentifier());
183                     if (regChild != null) {
184                         lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), regChild, candChild);
185                     }
186
187                     for (final RegistrationTreeNode<ActorRef> rc : regNode
188                             .getInexactChildren(candChild.getIdentifier())) {
189                         lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), rc, candChild);
190                     }
191                 }
192             }
193         }
194
195         private void createCanCommits(final Collection<ActorRef> regs, final YangInstanceIdentifier path,
196                 final DataTreeCandidateNode node) {
197             final DOMDataTreeCandidate domCandidate = DOMDataTreeCandidateTO.create(treeIdentifier(path), node);
198             for (final ActorRef reg : regs) {
199                 final CanCommit message = new DataTreeCohortActor.CanCommit(txId, domCandidate, schema, reg);
200                 messages.add(message);
201             }
202         }
203
204         private static DOMDataTreeIdentifier treeIdentifier(final YangInstanceIdentifier path) {
205             return new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, path);
206         }
207
208         private Collection<DataTreeCohortActor.CanCommit> perform(final RegistrationTreeNode<ActorRef> rootNode) {
209             final List<PathArgument> toLookup = candidate.getRootPath().getPathArguments();
210             lookupAndCreateCanCommits(toLookup, 0, rootNode);
211             return messages;
212         }
213     }
214
215     CompositeDataTreeCohort createCohort(final SchemaContext schemaContext, final TransactionIdentifier txId,
216             final Timeout commitStepTimeout) {
217         return new CompositeDataTreeCohort(this, txId, schemaContext, commitStepTimeout);
218     }
219 }