2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.sdnc.vnfapi.util;
24 import com.google.common.base.Optional;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
31 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.Vnfs;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.model.infrastructure.VnfList;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnctl.vnf.rev150720.vnf.model.infrastructure.VnfListKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 * This util class provides utility to read and write {@link VnfList} data objects from the {@link DataBroker}
41 public class DataBrokerUtil {
44 private final DataBroker dataBroker;
46 public DataBrokerUtil(DataBroker dataBroker) {
47 this.dataBroker = dataBroker;
50 /** @return VnfList - the VnfList object read from the DataBroker or null if none was found */
51 public VnfList read(String VnfListKey, LogicalDatastoreType logicalDatastoreType) throws Exception {
52 InstanceIdentifier VnfListInstanceIdentifier = InstanceIdentifier.<Vnfs>builder(Vnfs.class)
53 .child(VnfList.class, new VnfListKey(VnfListKey)).build();
54 ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
55 Optional<VnfList> data = (Optional<VnfList>) readTx.read(logicalDatastoreType, VnfListInstanceIdentifier).get();
56 if(!data.isPresent()){
64 * Write the {@link VnfList} object to the {@link DataBroker}
65 * @param isReplace - false specifies the new data is to be merged into existing data, where as true cause the
66 * existing data to be replaced.
67 * @param VnfList - the {@link VnfList} data object to be presisted in the db.
68 * @param logicalDatastoreType - The logicalDatastoreType
70 public void write(boolean isReplace,VnfList VnfList, LogicalDatastoreType logicalDatastoreType) throws Exception {
71 // Each entry will be identifiable by a unique key, we have to create that
73 InstanceIdentifier.InstanceIdentifierBuilder<VnfList> VnfListBuilder = InstanceIdentifier
74 .<Vnfs>builder(Vnfs.class).child(VnfList.class, VnfList.key());
75 InstanceIdentifier<VnfList> path = VnfListBuilder.build();
77 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
79 tx.merge(logicalDatastoreType, path, VnfList);
81 tx.put(logicalDatastoreType, path, VnfList);
83 CheckedFuture<Void,TransactionCommitFailedException> cf = tx.submit();