2 * Copyright (c) 2016 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.impl.write;
19 import io.fd.honeycomb.translate.write.WriteContext;
20 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
21 import io.fd.honeycomb.translate.util.write.AbstractGenericWriter;
22 import io.fd.honeycomb.translate.write.WriteFailedException;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Generic writer with customizable behavior thanks to injected customizer.
32 public final class GenericWriter<D extends DataObject> extends AbstractGenericWriter<D> {
34 private static final Logger LOG = LoggerFactory.getLogger(GenericWriter.class);
35 private static final String UPDATE_M = "updateCurrentAttributes";
36 private final WriterCustomizer<D> customizer;
38 public GenericWriter(@Nonnull final InstanceIdentifier<D> type,
39 @Nonnull final WriterCustomizer<D> customizer) {
40 super(type, isUpdateSupported(customizer));
41 this.customizer = customizer;
45 static boolean isUpdateSupported(final @Nonnull WriterCustomizer<?> customizer) {
47 // if customizer overrides updateCurrentAttributes method, it will be used, otherwise updates will be broken into individual
48 // delete + create pairs
49 final Class<? extends WriterCustomizer> customizerClass = customizer.getClass();
50 final Class<?> updateDeclaringClass = customizerClass
51 .getMethod(UPDATE_M, InstanceIdentifier.class, DataObject.class, DataObject.class, WriteContext.class)
53 final boolean supportsUpdate = !WriterCustomizer.class.equals(updateDeclaringClass);
54 LOG.debug("Customizer {} update support : {}|Update declaring class {}", customizerClass, supportsUpdate,
55 updateDeclaringClass);
56 return supportsUpdate;
57 } catch (NoSuchMethodException e) {
58 throw new IllegalStateException("Unable to detect if customizer supports update", e);
63 protected void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D data,
64 @Nonnull final WriteContext ctx) throws WriteFailedException {
66 customizer.writeCurrentAttributes(id, data, ctx);
67 } catch (RuntimeException e) {
68 throw new WriteFailedException.CreateFailedException(id, data, e);
73 protected void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id, @Nonnull final D dataBefore,
74 @Nonnull final WriteContext ctx) throws WriteFailedException {
76 customizer.deleteCurrentAttributes(id, dataBefore, ctx);
77 } catch (RuntimeException e) {
78 throw new WriteFailedException.DeleteFailedException(id, e);
83 protected void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
84 @Nonnull final D dataBefore,
85 @Nonnull final D dataAfter,
86 @Nonnull final WriteContext ctx) throws WriteFailedException {
88 customizer.updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
89 } catch (RuntimeException e) {
90 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e);