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 com.google.common.util.concurrent.CheckedFuture;
20 import io.fd.honeycomb.translate.write.WriteContext;
21 import io.fd.honeycomb.translate.write.WriteFailedException;
22 import io.fd.honeycomb.translate.write.Writer;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
32 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
35 * Simple DataBroker backed writer allowing to delegate writes to different brokers.
37 public final class BindingBrokerWriter<D extends DataObject> implements Writer<D> {
38 private final InstanceIdentifier<D> instanceIdentifier;
39 private final DataBroker dataBroker;
41 public BindingBrokerWriter(final InstanceIdentifier<D> instanceIdentifier, final DataBroker dataBroker) {
42 this.instanceIdentifier = instanceIdentifier;
43 this.dataBroker = dataBroker;
48 public InstanceIdentifier<D> getManagedDataObjectType() {
49 return instanceIdentifier;
53 public void processModification(@Nonnull final InstanceIdentifier<? extends DataObject> id,
54 @Nullable final DataObject dataBefore, @Nullable final DataObject dataAfter,
55 @Nonnull final WriteContext ctx) throws WriteFailedException {
56 final WriteTransaction writeTransaction = dataBroker.newWriteOnlyTransaction();
57 writeTransaction.put(CONFIGURATION, (InstanceIdentifier<DataObject>) id, dataAfter);
58 final CheckedFuture<Void, TransactionCommitFailedException> result = writeTransaction.submit();
61 } catch (TransactionCommitFailedException e) {
62 throw new WriteFailedException(id, e);
67 public boolean supportsDirectUpdate() {