Bump odlparent/yangtools/mdsal
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Optional;
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.ModifyTransactionRequest;
17 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionSuccess;
18 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
19 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
20 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
21 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
22 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
23 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
24 import org.opendaylight.controller.cluster.access.concepts.RequestException;
25 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Read-only frontend transaction state as observed by the shard leader. This class is NOT thread-safe.
32  *
33  * @author Robert Varga
34  */
35 final class FrontendReadOnlyTransaction extends FrontendTransaction {
36     private static final Logger LOG = LoggerFactory.getLogger(FrontendReadOnlyTransaction.class);
37
38     private final ReadOnlyShardDataTreeTransaction openTransaction;
39
40     private FrontendReadOnlyTransaction(final AbstractFrontendHistory history,
41             final ReadOnlyShardDataTreeTransaction transaction) {
42         super(history, transaction.getIdentifier());
43         this.openTransaction = requireNonNull(transaction);
44     }
45
46     static FrontendReadOnlyTransaction create(final AbstractFrontendHistory history,
47             final ReadOnlyShardDataTreeTransaction transaction) {
48         return new FrontendReadOnlyTransaction(history, transaction);
49     }
50
51     // Sequence has already been checked
52     @Override
53     TransactionSuccess<?> doHandleRequest(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 ModifyTransactionRequest) {
60             handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
61             return null;
62         } else {
63             LOG.warn("Rejecting unsupported request {}", request);
64             throw new UnsupportedRequestException(request);
65         }
66     }
67
68     @Override
69     void retire() {
70         // No-op
71     }
72
73     private void handleModifyTransaction(final ModifyTransactionRequest request, final RequestEnvelope envelope,
74             final long now) {
75         // The only valid request here is with abort protocol
76         final Optional<PersistenceProtocol> optProto = request.getPersistenceProtocol();
77         checkArgument(optProto.isPresent(), "Commit protocol is missing in %s", request);
78         checkArgument(optProto.get() == PersistenceProtocol.ABORT, "Unsupported commit protocol in %s", request);
79         openTransaction.abort(() -> recordAndSendSuccess(envelope, now,
80             new ModifyTransactionSuccess(request.getTarget(), request.getSequence())));
81     }
82
83     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request) {
84         final Optional<NormalizedNode> data = openTransaction.getSnapshot().readNode(request.getPath());
85         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(openTransaction.getIdentifier(),
86             request.getSequence(), data.isPresent()));
87     }
88
89     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request) {
90         final Optional<NormalizedNode> data = openTransaction.getSnapshot().readNode(request.getPath());
91         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(openTransaction.getIdentifier(),
92             request.getSequence(), data));
93     }
94 }