Release version 2.1.1 and roll versions
[sdnc/northbound.git] / vnfapi / provider / src / test / java / org / onap / sdnc / vnfapi / util / DataBrokerUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.sdnc.vnfapi.util;
23
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;
35
36
37 /**
38  * This util class provides utility to read and write {@link VnfList} data objects from the {@link DataBroker}
39  *
40  */
41 public class DataBrokerUtil {
42
43
44     private final DataBroker dataBroker;
45
46     public DataBrokerUtil(DataBroker dataBroker) {
47         this.dataBroker = dataBroker;
48     }
49
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()){
57             return null;
58         }
59         return data.get();
60     }
61
62
63     /**
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
69      */
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
72         // identifier
73         InstanceIdentifier.InstanceIdentifierBuilder<VnfList> VnfListBuilder = InstanceIdentifier
74                 .<Vnfs>builder(Vnfs.class).child(VnfList.class, VnfList.key());
75         InstanceIdentifier<VnfList> path = VnfListBuilder.build();
76
77         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
78         if (!isReplace) {
79             tx.merge(logicalDatastoreType, path, VnfList);
80         } else {
81             tx.put(logicalDatastoreType, path, VnfList);
82         }
83         CheckedFuture<Void,TransactionCommitFailedException> cf = tx.submit();
84         cf.checkedGet();
85
86     }
87
88
89
90
91
92
93
94 }