2 * Copyright (c) 2017 Inocybe Technologies. 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.aaa.encrypt.impl;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.binding.api.WriteTransaction;
18 import org.opendaylight.mdsal.common.api.CommitInfo;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yang.gen.v1.config.aaa.authn.encrypt.service.config.rev160915.AaaEncryptServiceConfig;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * MdsalUtils manages all the mdsal data operation.
31 public final class MdsalUtils {
32 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtils.class);
34 private MdsalUtils() {
38 public static InstanceIdentifier<AaaEncryptServiceConfig> getEncryptionSrvConfigIid() {
39 return InstanceIdentifier.builder(AaaEncryptServiceConfig.class).build();
43 * initialize the data tree for the given InstanceIdentifier type.
45 * @param type data store type
46 * @param dataBroker Mdsal data Broker
47 * @param iid InstanceIdentifier type
48 * @param object data object
50 public static <T extends DataObject> void initalizeDatastore(final LogicalDatastoreType type,
51 final DataBroker dataBroker, final InstanceIdentifier<T> iid, final T object) {
52 // Put data to MD-SAL data store
53 final WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
54 transaction.put(type, iid, object);
56 // Perform the transaction.submit asynchronously
57 Futures.addCallback(transaction.commit(), new FutureCallback<CommitInfo>() {
59 public void onFailure(final Throwable throwable) {
60 LOG.error("initDatastore: transaction failed");
64 public void onSuccess(final CommitInfo result) {
65 LOG.debug("initDatastore: transaction succeeded");
67 }, MoreExecutors.directExecutor());
68 LOG.info("initDatastore: data populated: {}, {}, {}", type, iid, object);
72 * Executes read as a blocking transaction.
74 * @param store {@link LogicalDatastoreType} to read
75 * @param path {@link InstanceIdentifier} for path to read
76 * @param <D> the data object type
77 * @return the result as the data object requested
79 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
80 final DataBroker dataBroker, final LogicalDatastoreType store, final InstanceIdentifier<D> path) {
81 try (ReadTransaction transaction = dataBroker.newReadOnlyTransaction()) {
82 Optional<D> optionalDataObject = transaction.read(store, path).get();
83 if (optionalDataObject.isPresent()) {
84 return optionalDataObject.get();
86 } catch (InterruptedException | ExecutionException e) {
87 LOG.warn("Failed to read {} ", path, e);