2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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 com.google.common.collect.ArrayListMultimap;
17 import com.google.common.collect.Multimap;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.concurrent.Executor;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
28 import org.opendaylight.mdsal.dom.spi.AbstractRegistrationTree;
29 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
30 import org.opendaylight.mdsal.dom.spi.RegistrationTreeSnapshot;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Registry of user commit cohorts, which is responsible for handling registration and calculation
42 * of affected cohorts based on {@link DataTreeCandidate}. This class is NOT thread-safe.
45 class DataTreeCohortActorRegistry extends AbstractRegistrationTree<ActorRef> {
47 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCohortActorRegistry.class);
49 private final Map<ActorRef, RegistrationTreeNode<ActorRef>> cohortToNode = new HashMap<>();
51 Collection<ActorRef> getCohortActors() {
52 return new ArrayList<>(cohortToNode.keySet());
55 @SuppressWarnings("checkstyle:IllegalCatch")
56 void registerCohort(final ActorRef sender, final RegisterCohort cohort) {
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());
70 sender.tell(new Status.Success(null), ActorRef.noSender());
73 void removeCommitCohort(final ActorRef sender, final RemoveCohort message) {
74 final ActorRef cohort = message.getCohort();
75 final RegistrationTreeNode<ActorRef> node = cohortToNode.get(cohort);
77 removeRegistration(node, cohort);
78 cohortToNode.remove(cohort);
80 sender.tell(new Status.Success(null), ActorRef.noSender());
81 cohort.tell(PoisonPill.getInstance(), cohort);
84 List<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());
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);
99 abstract static class CohortRegistryCommand {
101 private final ActorRef cohort;
103 CohortRegistryCommand(final ActorRef cohort) {
104 this.cohort = Preconditions.checkNotNull(cohort);
107 ActorRef getCohort() {
112 static class RegisterCohort extends CohortRegistryCommand {
114 private final DOMDataTreeIdentifier path;
116 RegisterCohort(final DOMDataTreeIdentifier path, final ActorRef cohort) {
122 public DOMDataTreeIdentifier getPath() {
128 static class RemoveCohort extends CohortRegistryCommand {
130 RemoveCohort(final ActorRef cohort) {
136 private static class CanCommitMessageBuilder {
138 private final TransactionIdentifier txId;
139 private final DataTreeCandidate candidate;
140 private final SchemaContext schema;
141 private final Multimap<ActorRef, DOMDataTreeCandidate> actorToCandidates = ArrayListMultimap.create();
143 CanCommitMessageBuilder(final TransactionIdentifier txId, final DataTreeCandidate candidate,
144 final SchemaContext schema) {
145 this.txId = Preconditions.checkNotNull(txId);
146 this.candidate = Preconditions.checkNotNull(candidate);
147 this.schema = schema;
150 private void lookupAndCreateCanCommits(final List<PathArgument> args, final int offset,
151 final RegistrationTreeNode<ActorRef> node) {
153 if (args.size() != offset) {
154 final PathArgument arg = args.get(offset);
155 final RegistrationTreeNode<ActorRef> exactChild = node.getExactChild(arg);
156 if (exactChild != null) {
157 lookupAndCreateCanCommits(args, offset + 1, exactChild);
159 for (final RegistrationTreeNode<ActorRef> c : node.getInexactChildren(arg)) {
160 lookupAndCreateCanCommits(args, offset + 1, c);
163 lookupAndCreateCanCommits(candidate.getRootPath(), node, candidate.getRootNode());
167 private void lookupAndCreateCanCommits(final YangInstanceIdentifier path,
168 final RegistrationTreeNode<ActorRef> regNode, final DataTreeCandidateNode candNode) {
169 if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
170 LOG.debug("Skipping unmodified candidate {}", path);
173 final Collection<ActorRef> regs = regNode.getRegistrations();
174 if (!regs.isEmpty()) {
175 createCanCommits(regs, path, candNode);
178 for (final DataTreeCandidateNode candChild : candNode.getChildNodes()) {
179 if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
180 final RegistrationTreeNode<ActorRef> regChild =
181 regNode.getExactChild(candChild.getIdentifier());
182 if (regChild != null) {
183 lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), regChild, candChild);
186 for (final RegistrationTreeNode<ActorRef> rc : regNode
187 .getInexactChildren(candChild.getIdentifier())) {
188 lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), rc, candChild);
194 private void createCanCommits(final Collection<ActorRef> regs, final YangInstanceIdentifier path,
195 final DataTreeCandidateNode node) {
196 final DOMDataTreeCandidate domCandidate = DOMDataTreeCandidateTO.create(treeIdentifier(path), node);
197 for (final ActorRef reg : regs) {
198 actorToCandidates.put(reg, domCandidate);
202 private static DOMDataTreeIdentifier treeIdentifier(final YangInstanceIdentifier path) {
203 return new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, path);
206 List<DataTreeCohortActor.CanCommit> perform(final RegistrationTreeNode<ActorRef> rootNode) {
207 final List<PathArgument> toLookup = candidate.getRootPath().getPathArguments();
208 lookupAndCreateCanCommits(toLookup, 0, rootNode);
210 final Map<ActorRef, Collection<DOMDataTreeCandidate>> mapView = actorToCandidates.asMap();
211 final List<DataTreeCohortActor.CanCommit> messages = new ArrayList<>(mapView.size());
212 for (Map.Entry<ActorRef, Collection<DOMDataTreeCandidate>> entry: mapView.entrySet()) {
213 messages.add(new DataTreeCohortActor.CanCommit(txId, entry.getValue(), schema, entry.getKey()));
220 CompositeDataTreeCohort createCohort(final SchemaContext schemaContext, final TransactionIdentifier txId,
221 final Executor callbackExecutor, final Timeout commitStepTimeout) {
222 return new CompositeDataTreeCohort(this, txId, schemaContext, callbackExecutor, commitStepTimeout);