HONEYCOMB-359 - Wildcarded writers
[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 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;
28
29 import javax.annotation.Nonnull;
30 import javax.annotation.Nullable;
31
32 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
33
34 /**
35  * Simple DataBroker backed writer allowing to delegate writes to different brokers.
36  */
37 public final class BindingBrokerWriter<D extends DataObject> implements Writer<D> {
38     private final InstanceIdentifier<D> instanceIdentifier;
39     private final DataBroker dataBroker;
40
41     public BindingBrokerWriter(final InstanceIdentifier<D> instanceIdentifier, final DataBroker dataBroker) {
42         this.instanceIdentifier = instanceIdentifier;
43         this.dataBroker = dataBroker;
44     }
45
46     @Nonnull
47     @Override
48     public InstanceIdentifier<D> getManagedDataObjectType() {
49         return instanceIdentifier;
50     }
51
52     @Override
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();
59         try {
60             result.checkedGet();
61         } catch (TransactionCommitFailedException e) {
62             throw new WriteFailedException(id, e);
63         }
64     }
65
66     @Override
67     public boolean supportsDirectUpdate() {
68         return false;
69     }
70 }