aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/netconfnode-state-service/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/netconfnodestateservice/impl/GenericTransactionUtils.java
blob: 6a811ea59aa1731086e459bc196b43bef5e0595f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 ******************************************************************************/
package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl;

import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.FluentFuture;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.onap.ccsdk.features.sdnr.wt.common.util.StackTrace;
import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.TransactionUtils;
import org.opendaylight.mdsal.binding.api.DataBroker;
import org.opendaylight.mdsal.binding.api.ReadTransaction;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class GenericTransactionUtils implements TransactionUtils {
    static final Logger LOG = LoggerFactory.getLogger(GenericTransactionUtils.class);

    /**
     * Deliver the data back or null. Warning
     *
     * @param <T> SubType of the DataObject to be handled
     * @param dataBroker for accessing data
     * @param dataStoreType to address datastore
     * @param iid id to access data
     * @return null or object
     */
    @Override
    @Nullable
    public <T extends DataObject> T readData(DataBroker dataBroker, LogicalDatastoreType dataStoreType,
            InstanceIdentifier<T> iid) {

        AtomicBoolean noErrorIndication = new AtomicBoolean();
        AtomicReference<String> statusText = new AtomicReference<>();

        @Nullable T obj = readDataOptionalWithStatus(dataBroker, dataStoreType, iid, noErrorIndication, statusText);

        if (!noErrorIndication.get()) {
            LOG.warn("Read transaction for identifier " + iid + " failed with status " + statusText.get());
        }

        return obj;
    }

    /**
     * Deliver the data back or null
     *
     * @param <T> SubType of the DataObject to be handled
     * @param dataBroker for accessing data
     * @param dataStoreType to address datastore
     * @param iid id to access data
     * @param noErrorIndication (Output) true if data could be read and are available and is not null
     * @param statusIndicator (Output) String with status indications during the read.
     * @return null or object
     */
    @Override
    @SuppressWarnings("null")
    public @Nullable <T extends DataObject> T readDataOptionalWithStatus(DataBroker dataBroker,
            LogicalDatastoreType dataStoreType, InstanceIdentifier<T> iid, AtomicBoolean noErrorIndication,
            AtomicReference<String> statusIndicator) {

        @Nullable T data = null;
        noErrorIndication.set(false);

        statusIndicator.set("Preconditions");
        Preconditions.checkNotNull(dataBroker);

        int retry = 0;
        int retryDelayMilliseconds = 2000;
        int maxRetries = 0; // 0 no Retry

        do {
            if (retry > 0) {
                try {
                    LOG.debug("Sleep {}ms", retryDelayMilliseconds);
                    Thread.sleep(retryDelayMilliseconds);
                } catch (InterruptedException e) {
                    LOG.debug("Sleep interrupted", e);
                    Thread.currentThread().interrupt();
                }
            }

            LOG.debug("Sending message with retry {} ", retry);
            statusIndicator.set("Create Read Transaction");
            ReadTransaction readTransaction = dataBroker.newReadOnlyTransaction();
            try {
                @NonNull FluentFuture<Optional<T>> od = readTransaction.read(dataStoreType, iid);
                statusIndicator.set("Read done");
                if (od != null) {
                    statusIndicator.set("Unwrap checkFuture done");
                    Optional<T> optionalData = od.get();
                    if (optionalData != null) {
                        statusIndicator.set("Unwrap optional done");
                        data = optionalData.orElse(null);
                        statusIndicator.set("Read transaction done");
                        noErrorIndication.set(true);
                    } else {
                        statusIndicator.set("optional Data is null");
                    }
                } else {
                    statusIndicator.set("od feature is null");
                }
            } catch (CancellationException | ExecutionException | InterruptedException | NoSuchElementException e) {
                statusIndicator.set(StackTrace.toString(e));
                if (e instanceof InterruptedException) {
                    Thread.currentThread().interrupt();
                }
                LOG.debug("Exception during read", e);
            }

        } while (noErrorIndication.get() == false && retry++ < maxRetries);

        LOG.debug("stage 2 noErrorIndication {} status text {}", noErrorIndication.get(), statusIndicator.get());

        return data;
    }


}