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