HONEYCOMB-359 - Wildcarded writers
[honeycomb.git] / infra / translate-utils / src / main / java / io / fd / honeycomb / translate / util / write / AbstractGenericWriter.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.util.write;
18
19 import io.fd.honeycomb.translate.util.RWUtils;
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.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30
31 import static com.google.common.base.Preconditions.checkArgument;
32
33 public abstract class AbstractGenericWriter<D extends DataObject> implements Writer<D> {
34
35     private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericWriter.class);
36
37     private final InstanceIdentifier<D> instanceIdentifier;
38     private final boolean supportsUpdate;
39
40     protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate) {
41         this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
42         this.supportsUpdate = supportsUpdate;
43     }
44
45     protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx)
46         throws WriteFailedException {
47         LOG.debug("{}: Writing current: {} data: {}", this, id, data);
48         writeCurrentAttributes(id, data, ctx);
49         LOG.debug("{}: Current node written successfully", this);
50     }
51
52     protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter,
53                                  final WriteContext ctx) throws WriteFailedException {
54         LOG.debug("{}: Updating current: {} dataBefore: {}, datAfter: {}", this, id, dataBefore, dataAfter);
55
56         if (dataBefore.equals(dataAfter)) {
57             LOG.debug("{}: Skipping current(no update): {}", this, id);
58             // No change, ignore
59             return;
60         }
61         updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
62         LOG.debug("{}: Current node updated successfully", this);
63     }
64
65     protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx)
66         throws WriteFailedException {
67         LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore);
68         deleteCurrentAttributes(id, dataBefore, ctx);
69     }
70
71     @SuppressWarnings("unchecked")
72     @Override
73     public void processModification(@Nonnull final InstanceIdentifier<? extends DataObject> id,
74                                     @Nullable final DataObject dataBefore,
75                                     @Nullable final DataObject dataAfter,
76                                     @Nonnull final WriteContext ctx) throws WriteFailedException {
77         LOG.debug("{}: Updating : {}", this, id);
78         LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter);
79
80         checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
81                 id, getManagedDataObjectType(), this);
82
83         if (isWrite(dataBefore, dataAfter)) {
84             writeCurrent((InstanceIdentifier<D>) id, castToManaged(dataAfter), ctx);
85         } else if (isDelete(dataBefore, dataAfter)) {
86             deleteCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), ctx);
87         } else {
88             checkArgument(dataBefore != null && dataAfter != null, "No data to process");
89             updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
90         }
91     }
92
93     private void checkDataType(@Nonnull final DataObject dataAfter) {
94         checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
95     }
96
97     private D castToManaged(final DataObject data) {
98         checkDataType(data);
99         return getManagedDataObjectType().getTargetType().cast(data);
100     }
101
102     private static boolean isWrite(final DataObject dataBefore,
103                                     final DataObject dataAfter) {
104         return dataBefore == null && dataAfter != null;
105     }
106
107     private static boolean isDelete(final DataObject dataBefore,
108                                     final DataObject dataAfter) {
109         return dataAfter == null && dataBefore != null;
110     }
111
112     private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
113         return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
114     }
115
116     protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
117                                                    @Nonnull final D data,
118                                                    @Nonnull final WriteContext ctx) throws WriteFailedException;
119
120     protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
121                                                     @Nonnull final D dataBefore,
122                                                     @Nonnull final WriteContext ctx) throws WriteFailedException;
123
124     protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
125                                                     @Nonnull final D dataBefore,
126                                                     @Nonnull final D dataAfter,
127                                                     @Nonnull final WriteContext ctx) throws WriteFailedException;
128
129     @Nonnull
130     @Override
131     public InstanceIdentifier<D> getManagedDataObjectType() {
132         return instanceIdentifier;
133     }
134
135
136     @Override
137     public String toString() {
138         return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
139     }
140
141     @Override
142     public boolean supportsDirectUpdate() {
143         return supportsUpdate;
144     }
145 }