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
8 package org.opendaylight.controller.cluster.datastore;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nullable;
13 import javax.annotation.concurrent.NotThreadSafe;
14 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
15 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
16 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
17 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
18 import org.opendaylight.controller.cluster.access.commands.TransactionAbortRequest;
19 import org.opendaylight.controller.cluster.access.commands.TransactionAbortSuccess;
20 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
22 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
23 import org.opendaylight.controller.cluster.access.concepts.RequestException;
24 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Read-only frontend transaction state as observed by the shard leader.
32 * @author Robert Varga
35 final class FrontendReadOnlyTransaction extends FrontendTransaction {
36 private static final Logger LOG = LoggerFactory.getLogger(FrontendReadOnlyTransaction.class);
38 private final ReadOnlyShardDataTreeTransaction openTransaction;
40 private FrontendReadOnlyTransaction(final AbstractFrontendHistory history,
41 final ReadOnlyShardDataTreeTransaction transaction) {
42 super(history, transaction.getIdentifier());
43 this.openTransaction = Preconditions.checkNotNull(transaction);
46 static FrontendReadOnlyTransaction create(final AbstractFrontendHistory history,
47 final ReadOnlyShardDataTreeTransaction transaction) {
48 return new FrontendReadOnlyTransaction(history, transaction);
51 // Sequence has already been checked
53 @Nullable TransactionSuccess<?> handleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
54 final long now) throws RequestException {
55 if (request instanceof ExistsTransactionRequest) {
56 return handleExistsTransaction((ExistsTransactionRequest) request);
57 } else if (request instanceof ReadTransactionRequest) {
58 return handleReadTransaction((ReadTransactionRequest) request);
59 } else if (request instanceof TransactionAbortRequest) {
60 handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
63 LOG.warn("Rejecting unsupported request {}", request);
64 throw new UnsupportedRequestException(request);
68 private void handleTransactionAbort(final TransactionAbortRequest request, final RequestEnvelope envelope,
69 final long now) throws RequestException {
70 openTransaction.abort(() -> recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(request.getTarget(),
71 request.getSequence())));
74 private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
75 throws RequestException {
76 final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
77 return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(openTransaction.getIdentifier(),
78 request.getSequence(), data.isPresent()));
81 private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
82 throws RequestException {
83 final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
84 return recordSuccess(request.getSequence(), new ReadTransactionSuccess(openTransaction.getIdentifier(),
85 request.getSequence(), data));