a3cffd1d5ee9f43fdd0510fa5899b0c525ac326d
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendReadOnlyTransaction.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 package org.opendaylight.controller.cluster.datastore;
9
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
27 /**
28  * Read-only frontend transaction state as observed by the shard leader.
29  *
30  * @author Robert Varga
31  */
32 @NotThreadSafe
33 final class FrontendReadOnlyTransaction extends FrontendTransaction {
34     private final ReadOnlyShardDataTreeTransaction openTransaction;
35
36     private FrontendReadOnlyTransaction(final AbstractFrontendHistory history,
37             final ReadOnlyShardDataTreeTransaction transaction) {
38         super(history, transaction.getIdentifier());
39         this.openTransaction = Preconditions.checkNotNull(transaction);
40     }
41
42     static FrontendReadOnlyTransaction create(final AbstractFrontendHistory history,
43             final ReadOnlyShardDataTreeTransaction transaction) {
44         return new FrontendReadOnlyTransaction(history, transaction);
45     }
46
47     // Sequence has already been checked
48     @Override
49     @Nullable TransactionSuccess<?> handleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
50             final long now) throws RequestException {
51         if (request instanceof ExistsTransactionRequest) {
52             return handleExistsTransaction((ExistsTransactionRequest) request);
53         } else if (request instanceof ReadTransactionRequest) {
54             return handleReadTransaction((ReadTransactionRequest) request);
55         } else if (request instanceof TransactionAbortRequest) {
56             handleTransactionAbort((TransactionAbortRequest) request, envelope, now);
57             return null;
58         } else {
59             throw new UnsupportedRequestException(request);
60         }
61     }
62
63     private void handleTransactionAbort(final TransactionAbortRequest request, final RequestEnvelope envelope,
64             final long now) throws RequestException {
65         openTransaction.abort(() -> recordAndSendSuccess(envelope, now, new TransactionAbortSuccess(request.getTarget(),
66             request.getSequence())));
67     }
68
69     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request)
70             throws RequestException {
71         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
72         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(openTransaction.getIdentifier(),
73             request.getSequence(), data.isPresent()));
74     }
75
76     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request)
77             throws RequestException {
78         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
79         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(openTransaction.getIdentifier(),
80             request.getSequence(), data));
81     }
82 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.