878487cf0c3ec3870bc79875b1eb98071c8425b3
[sdnc/northbound.git] / generic-resource-api / provider / src / test / java / org / onap / sdnc / northbound / 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.northbound.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.sdnc.northbound.generic.resource.rev170824.Services;
32 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceDataBuilder;
33 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.level.oper.status.ServiceLevelOperStatusBuilder;
34 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
35 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.ServiceBuilder;
36 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.ServiceKey;
37 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatusBuilder;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
41
42
43 /**
44  * This util class provides utility to read and write {@link Service} data objects from the {@link DataBroker}
45  *
46  */
47 public class DataBrokerUtil {
48
49
50     private final DataBroker dataBroker;
51
52     public DataBrokerUtil(DataBroker dataBroker) {
53         this.dataBroker = dataBroker;
54     }
55
56     /** @return Service - the Service object read from the DataBroker or null if none was found */
57     public Service read(String serviceKey, LogicalDatastoreType logicalDatastoreType) throws Exception {
58         InstanceIdentifier serviceInstanceIdentifier = InstanceIdentifier.<Services>builder(Services.class)
59                 .child(Service.class, new ServiceKey(serviceKey)).build();
60         ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
61         Optional<Service> data = (Optional<Service>) readTx.read(logicalDatastoreType, serviceInstanceIdentifier).get();
62         if(!data.isPresent()){
63             return null;
64         }
65
66
67         //The toString() value from a Service object returned form data.get() is different than the toString() value
68         //from a Service Object constructed from a Builder. This makes it difficult to compare deltas when doing a
69         // assertEquals.  That why we rebuild it her to solve that problem.
70         return build(ServiceBuilder::new,data.get(),(service) -> service
71                 .setServiceStatus(build(ServiceStatusBuilder::new,service.getServiceStatus()))
72                 .setServiceData(build(ServiceDataBuilder::new,service.getServiceData(),(serviceStatus)->serviceStatus
73                                 .setServiceLevelOperStatus(build(ServiceLevelOperStatusBuilder::new,serviceStatus.getServiceLevelOperStatus()))
74                                 ))
75         );
76     }
77
78
79     /**
80      * Write the {@link Service} object to the {@link DataBroker}
81      * @param isReplace - false specifies the new data is to be merged into existing data, where as true cause the
82      *                  existing data to be replaced.
83      * @param service - the {@link Service} data object to be presisted in the db.
84      * @param logicalDatastoreType - The logicalDatastoreType
85      */
86     public void write(boolean isReplace,Service service, LogicalDatastoreType logicalDatastoreType) throws Exception {
87         // Each entry will be identifiable by a unique key, we have to create that
88         // identifier
89         InstanceIdentifier.InstanceIdentifierBuilder<Service> serviceBuilder = InstanceIdentifier
90                 .<Services>builder(Services.class).child(Service.class, service.key());
91         InstanceIdentifier<Service> path = serviceBuilder.build();
92
93         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
94         if (!isReplace) {
95             tx.merge(logicalDatastoreType, path, service);
96         } else {
97             tx.put(logicalDatastoreType, path, service);
98         }
99         CheckedFuture<Void,TransactionCommitFailedException> cf = tx.submit();
100         cf.checkedGet();
101
102     }
103
104
105
106
107
108
109
110 }