7b68376ba7fd56476dc2fb236109cd3c2234ae7f
[honeycomb.git] / infra / translate-utils / src / main / java / io / fd / honeycomb / translate / util / write / BindingBrokerWriter.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.util.write;
18
19 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
20
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;
32
33 /**
34  * Simple DataBroker backed writer allowing to delegate writes to different brokers.
35  */
36 public final class BindingBrokerWriter<D extends DataObject> implements Writer<D> {
37     private final InstanceIdentifier<D> instanceIdentifier;
38     private final DataBroker dataBroker;
39
40     public BindingBrokerWriter(final InstanceIdentifier<D> instanceIdentifier, final DataBroker dataBroker) {
41         this.instanceIdentifier = instanceIdentifier;
42         this.dataBroker = dataBroker;
43     }
44
45     @Nonnull
46     @Override
47     public InstanceIdentifier<D> getManagedDataObjectType() {
48         return instanceIdentifier;
49     }
50
51     @Override
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();
58         try {
59             result.checkedGet();
60         } catch (TransactionCommitFailedException e) {
61             throw new WriteFailedException(id, e);
62         }
63     }
64
65     @Override
66     public boolean supportsDirectUpdate() {
67         return false;
68     }
69 }