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.v3po.translate.util.write;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import io.fd.honeycomb.v3po.translate.util.RWUtils;
22 import io.fd.honeycomb.v3po.translate.write.WriteContext;
23 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
24 import io.fd.honeycomb.v3po.translate.write.Writer;
25 import javax.annotation.Nonnull;
26 import javax.annotation.Nullable;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public abstract class AbstractGenericWriter<D extends DataObject> implements Writer<D> {
34 private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericWriter.class);
36 private final InstanceIdentifier<D> instanceIdentifier;
38 protected AbstractGenericWriter(final InstanceIdentifier<D> type) {
39 this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
42 protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx)
43 throws WriteFailedException {
44 LOG.debug("{}: Writing current: {} data: {}", this, id, data);
45 writeCurrentAttributes(id, data, ctx);
46 LOG.debug("{}: Current node written successfully", this);
49 protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter,
50 final WriteContext ctx) throws WriteFailedException {
51 LOG.debug("{}: Updating current: {} dataBefore: {}, datAfter: {}", this, id, dataBefore, dataAfter);
53 if (dataBefore.equals(dataAfter)) {
54 LOG.debug("{}: Skipping current(no update): {}", this, id);
58 updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
59 LOG.debug("{}: Current node updated successfully", this);
62 protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx)
63 throws WriteFailedException {
64 LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore);
65 deleteCurrentAttributes(id, dataBefore, ctx);
68 @SuppressWarnings("unchecked")
70 public void update(@Nonnull final InstanceIdentifier<? extends DataObject> id,
71 @Nullable final DataObject dataBefore,
72 @Nullable final DataObject dataAfter,
73 @Nonnull final WriteContext ctx) throws WriteFailedException {
74 LOG.debug("{}: Updating : {}", this, id);
75 LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter);
77 checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
78 id, getManagedDataObjectType(), this);
80 if (isWrite(dataBefore, dataAfter)) {
81 writeCurrent((InstanceIdentifier<D>) id, castToManaged(dataAfter), ctx);
82 } else if (isDelete(dataBefore, dataAfter)) {
83 deleteCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), ctx);
85 checkArgument(dataBefore != null && dataAfter != null, "No data to process");
86 updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
90 private void checkDataType(@Nonnull final DataObject dataAfter) {
91 checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
94 private D castToManaged(final DataObject data) {
96 return getManagedDataObjectType().getTargetType().cast(data);
99 private static boolean isWrite(final DataObject dataBefore,
100 final DataObject dataAfter) {
101 return dataBefore == null && dataAfter != null;
104 private static boolean isDelete(final DataObject dataBefore,
105 final DataObject dataAfter) {
106 return dataAfter == null && dataBefore != null;
109 private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
110 return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
113 protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
114 @Nonnull final D data,
115 @Nonnull final WriteContext ctx) throws WriteFailedException;
117 protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
118 @Nonnull final D dataBefore,
119 @Nonnull final WriteContext ctx) throws WriteFailedException;
121 protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
122 @Nonnull final D dataBefore,
123 @Nonnull final D dataAfter,
124 @Nonnull final WriteContext ctx) throws WriteFailedException;
128 public InstanceIdentifier<D> getManagedDataObjectType() {
129 return instanceIdentifier;
134 public String toString() {
135 return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());