6ac43e86d2ec7d0fd853eb7728f9677153f43f12
[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
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         ServiceBuilder svcBuilder = new ServiceBuilder();
76         svcBuilder.setServiceStatus(data.get().getServiceStatus());
77         ServiceDataBuilder svcDataBuilder = new ServiceDataBuilder(data.get().getServiceData());
78         svcDataBuilder.setServiceLevelOperStatus(data.get().getServiceData().getServiceLevelOperStatus());
79         svcBuilder.setServiceData(svcDataBuilder.build());
80         svcBuilder.setServiceInstanceId(data.get().getServiceInstanceId());
81         return svcBuilder.build();
82     }
83
84
85     /**
86      * Write the {@link Service} object to the {@link DataBroker}
87      * @param isReplace - false specifies the new data is to be merged into existing data, where as true cause the
88      *                  existing data to be replaced.
89      * @param service - the {@link Service} data object to be presisted in the db.
90      * @param logicalDatastoreType - The logicalDatastoreType
91      */
92     public void write(boolean isReplace,Service service, LogicalDatastoreType logicalDatastoreType) throws Exception {
93         // Each entry will be identifiable by a unique key, we have to create that
94         // identifier
95         InstanceIdentifier.InstanceIdentifierBuilder<Service> serviceBuilder = InstanceIdentifier
96                 .<Services>builder(Services.class).child(Service.class, service.key());
97         InstanceIdentifier<Service> path = serviceBuilder.build();
98
99         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
100         if (!isReplace) {
101             tx.merge(logicalDatastoreType, path, service);
102         } else {
103             tx.put(logicalDatastoreType, path, service);
104         }
105         tx.commit().get();
106
107     }
108
109
110
111
112
113
114
115 }