Remove code repetitions from GenericListWriter
[honeycomb.git] / infra / translate-impl / src / main / java / io / fd / honeycomb / translate / impl / write / GenericListWriter.java
1 /*
2  * Copyright (c) 2016 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.impl.write;
18
19 import static io.fd.honeycomb.translate.impl.write.GenericWriter.isUpdateSupported;
20
21 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
22 import io.fd.honeycomb.translate.util.RWUtils;
23 import io.fd.honeycomb.translate.util.write.AbstractGenericWriter;
24 import io.fd.honeycomb.translate.write.ListWriter;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.Identifiable;
31 import org.opendaylight.yangtools.yang.binding.Identifier;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 /**
35  * Generic list node writer with customizable behavior thanks to injected customizer.
36  */
37 public final class GenericListWriter<D extends DataObject & Identifiable<K>, K extends Identifier<D>> extends
38         AbstractGenericWriter<D> implements ListWriter<D, K> {
39
40     private final WriterCustomizer<D> customizer;
41
42     public GenericListWriter(@Nonnull final InstanceIdentifier<D> type,
43                              @Nonnull final ListWriterCustomizer<D, K> customizer) {
44         super(type, isUpdateSupported(customizer));
45         this.customizer = customizer;
46     }
47
48     @Override
49     protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D data,
50                                           @Nonnull final WriteContext ctx) throws WriteFailedException {
51         try {
52             customizer.writeCurrentAttributes(id, data, ctx);
53         } catch (RuntimeException e) {
54             throw new WriteFailedException.CreateFailedException(id, data, e);
55         }
56     }
57
58     @Override
59     protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore,
60                                            @Nonnull final WriteContext ctx) throws WriteFailedException {
61         try {
62             customizer.deleteCurrentAttributes(id, dataBefore, ctx);
63         } catch (RuntimeException e) {
64             throw new WriteFailedException.DeleteFailedException(id, e);
65         }
66     }
67
68     @Override
69     protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore,
70                                            @Nonnull final D dataAfter, @Nonnull final WriteContext ctx)
71         throws WriteFailedException {
72         try {
73             customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
74         } catch (RuntimeException e) {
75             throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);
76         }
77     }
78
79     @Override
80     protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx)
81         throws WriteFailedException {
82         super.writeCurrent(getId(id, data), data, ctx);
83     }
84
85     @Override
86     protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter,
87                                  final WriteContext ctx) throws WriteFailedException {
88         super.updateCurrent(getId(id, dataBefore), dataBefore, dataAfter, ctx);
89     }
90
91     @Override
92     protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx)
93         throws WriteFailedException {
94         super.deleteCurrent(getId(id, dataBefore), dataBefore, ctx);
95     }
96
97     private InstanceIdentifier<D> getId(final InstanceIdentifier<D> id, final D current) {
98         // Make sure the key is present
99         if (isWildcarded(id)) {
100             return getSpecificId(id, current);
101         } else {
102             return id;
103         }
104     }
105
106     private boolean isWildcarded(final InstanceIdentifier<D> id) {
107         return id.firstIdentifierOf(getManagedDataObjectType().getTargetType()).isWildcarded();
108     }
109
110     private InstanceIdentifier<D> getSpecificId(final InstanceIdentifier<D> currentId, final D current) {
111         return RWUtils.replaceLastInId(currentId,
112             new InstanceIdentifier.IdentifiableItem<>(currentId.getTargetType(), current.getKey()));
113     }
114 }