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 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;
40 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
44 * This util class provides utility to read and write {@link Service} data objects from the {@link DataBroker}
47 public class DataBrokerUtil {
50 private final DataBroker dataBroker;
52 public DataBrokerUtil(DataBroker dataBroker) {
53 this.dataBroker = dataBroker;
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()){
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()))
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
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
89 InstanceIdentifier.InstanceIdentifierBuilder<Service> serviceBuilder = InstanceIdentifier
90 .<Services>builder(Services.class).child(Service.class, service.key());
91 InstanceIdentifier<Service> path = serviceBuilder.build();
93 WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
95 tx.merge(logicalDatastoreType, path, service);
97 tx.put(logicalDatastoreType, path, service);
99 CheckedFuture<Void,TransactionCommitFailedException> cf = tx.submit();