2 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.util.write;
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import io.fd.honeycomb.translate.write.WriteContext;
23 import io.fd.honeycomb.translate.write.WriteFailedException;
24 import io.fd.honeycomb.translate.write.Writer;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
29 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 * Simple DataBroker backed writer allowing to delegate writes to different brokers.
36 public final class BindingBrokerWriter<D extends DataObject> implements Writer<D> {
37 private final InstanceIdentifier<D> instanceIdentifier;
38 private final DataBroker dataBroker;
40 public BindingBrokerWriter(final InstanceIdentifier<D> instanceIdentifier, final DataBroker dataBroker) {
41 this.instanceIdentifier = instanceIdentifier;
42 this.dataBroker = dataBroker;
47 public InstanceIdentifier<D> getManagedDataObjectType() {
48 return instanceIdentifier;
52 public void processModification(@Nonnull final InstanceIdentifier<? extends DataObject> id,
53 @Nullable final DataObject dataBefore, @Nullable final DataObject dataAfter,
54 @Nonnull final WriteContext ctx) throws WriteFailedException {
55 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
56 writeTransaction.put(CONFIGURATION, (InstanceIdentifier<DataObject>) id, dataAfter);
57 final CheckedFuture<Void, TransactionCommitFailedException> result = writeTransaction.submit();
60 } catch (TransactionCommitFailedException e) {
61 throw new WriteFailedException(id, e);
66 public boolean supportsDirectUpdate() {