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.northbound.util;
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;
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;
45 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
49 * This util class provides utility to read and write {@link Service} data objects from the {@link DataBroker}
52 public class DataBrokerUtil {
55 private final DataBroker dataBroker;
57 public DataBrokerUtil(DataBroker dataBroker) {
58 this.dataBroker = dataBroker;
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()){
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()))
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
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
94 InstanceIdentifier.InstanceIdentifierBuilder<Service> serviceBuilder = InstanceIdentifier
95 .<Services>builder(Services.class).child(Service.class, service.key());
96 InstanceIdentifier<Service> path = serviceBuilder.build();
98 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
100 tx.merge(logicalDatastoreType, path, service);
102 tx.put(logicalDatastoreType, path, service);