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 com.google.common.base.Preconditions;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
20 import javax.annotation.concurrent.NotThreadSafe;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.datastore.DataTreeCohortActor.CanCommit;
23 import org.opendaylight.controller.md.sal.dom.spi.AbstractRegistrationTree;
24 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeNode;
25 import org.opendaylight.controller.md.sal.dom.spi.RegistrationTreeSnapshot;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Registry of user commit cohorts, which is responsible for handling registration and calculation
40 * of affected cohorts based on {@link DataTreeCandidate}.
44 class DataTreeCohortActorRegistry extends AbstractRegistrationTree<ActorRef> {
46 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCohortActorRegistry.class);
48 private final Map<ActorRef, RegistrationTreeNode<ActorRef>> cohortToNode = new HashMap<>();
51 void registerCohort(ActorRef sender, RegisterCohort cohort) {
54 final ActorRef cohortRef = cohort.getCohort();
55 final RegistrationTreeNode<ActorRef> node =
56 findNodeFor(cohort.getPath().getRootIdentifier().getPathArguments());
57 addRegistration(node, cohort.getCohort());
58 cohortToNode.put(cohortRef, node);
59 } catch (final Exception e) {
60 sender.tell(new Status.Failure(e), ActorRef.noSender());
65 sender.tell(new Status.Success(null), ActorRef.noSender());
68 void removeCommitCohort(ActorRef sender, RemoveCohort message) {
69 final ActorRef cohort = message.getCohort();
70 final RegistrationTreeNode<ActorRef> node = cohortToNode.get(cohort);
72 removeRegistration(node, cohort);
73 cohortToNode.remove(cohort);
75 sender.tell(new Status.Success(null), ActorRef.noSender());
76 cohort.tell(PoisonPill.getInstance(), cohort);
79 Collection<DataTreeCohortActor.CanCommit> createCanCommitMessages(TransactionIdentifier txId,
80 DataTreeCandidate candidate, SchemaContext schema) {
81 try (RegistrationTreeSnapshot<ActorRef> cohorts = takeSnapshot()) {
82 return new CanCommitMessageBuilder(txId, candidate, schema).perform(cohorts.getRootNode());
86 void process(ActorRef sender, CohortRegistryCommand message) {
87 if (message instanceof RegisterCohort) {
88 registerCohort(sender, (RegisterCohort) message);
89 } else if (message instanceof RemoveCohort) {
90 removeCommitCohort(sender, (RemoveCohort) message);
94 static abstract class CohortRegistryCommand {
96 private final ActorRef cohort;
98 CohortRegistryCommand(ActorRef cohort) {
99 this.cohort = Preconditions.checkNotNull(cohort);
102 ActorRef getCohort() {
107 static class RegisterCohort extends CohortRegistryCommand {
109 private final DOMDataTreeIdentifier path;
111 RegisterCohort(DOMDataTreeIdentifier path, ActorRef cohort) {
117 public DOMDataTreeIdentifier getPath() {
123 static class RemoveCohort extends CohortRegistryCommand {
125 RemoveCohort(ActorRef cohort) {
131 private static class CanCommitMessageBuilder {
133 private final TransactionIdentifier txId;
134 private final DataTreeCandidate candidate;
135 private final SchemaContext schema;
136 private final Collection<DataTreeCohortActor.CanCommit> messages =
139 CanCommitMessageBuilder(TransactionIdentifier txId, DataTreeCandidate candidate, SchemaContext schema) {
140 this.txId = Preconditions.checkNotNull(txId);
141 this.candidate = Preconditions.checkNotNull(candidate);
142 this.schema = schema;
145 private void lookupAndCreateCanCommits(List<PathArgument> args, int offset,
146 RegistrationTreeNode<ActorRef> node) {
148 if (args.size() != offset) {
149 final PathArgument arg = args.get(offset);
150 final RegistrationTreeNode<ActorRef> exactChild = node.getExactChild(arg);
151 if (exactChild != null) {
152 lookupAndCreateCanCommits(args, offset + 1, exactChild);
154 for (final RegistrationTreeNode<ActorRef> c : node.getInexactChildren(arg)) {
155 lookupAndCreateCanCommits(args, offset + 1, c);
158 lookupAndCreateCanCommits(candidate.getRootPath(), node, candidate.getRootNode());
162 private void lookupAndCreateCanCommits(YangInstanceIdentifier path, RegistrationTreeNode<ActorRef> regNode,
163 DataTreeCandidateNode candNode) {
164 if (candNode.getModificationType() == ModificationType.UNMODIFIED) {
165 LOG.debug("Skipping unmodified candidate {}", path);
168 final Collection<ActorRef> regs = regNode.getRegistrations();
169 if (!regs.isEmpty()) {
170 createCanCommits(regs, path, candNode);
173 for (final DataTreeCandidateNode candChild : candNode.getChildNodes()) {
174 if (candChild.getModificationType() != ModificationType.UNMODIFIED) {
175 final RegistrationTreeNode<ActorRef> regChild =
176 regNode.getExactChild(candChild.getIdentifier());
177 if (regChild != null) {
178 lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), regChild, candChild);
181 for (final RegistrationTreeNode<ActorRef> rc : regNode
182 .getInexactChildren(candChild.getIdentifier())) {
183 lookupAndCreateCanCommits(path.node(candChild.getIdentifier()), rc, candChild);
189 private void createCanCommits(Collection<ActorRef> regs, YangInstanceIdentifier path,
190 DataTreeCandidateNode node) {
191 final DOMDataTreeCandidate candidate = DOMDataTreeCandidateTO.create(treeIdentifier(path), node);
192 for (final ActorRef reg : regs) {
193 final CanCommit message = new DataTreeCohortActor.CanCommit(txId, candidate, schema, reg);
194 messages.add(message);
198 private static DOMDataTreeIdentifier treeIdentifier(YangInstanceIdentifier path) {
199 return new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, path);
202 private Collection<DataTreeCohortActor.CanCommit> perform(RegistrationTreeNode<ActorRef> rootNode) {
203 final List<PathArgument> toLookup = candidate.getRootPath().getPathArguments();
204 lookupAndCreateCanCommits(toLookup, 0, rootNode);