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.impl.write;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import io.fd.honeycomb.v3po.translate.write.WriteContext;
22 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
23 import io.fd.honeycomb.v3po.translate.write.Writer;
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 abstract class AbstractCompositeWriter<D extends DataObject> implements Writer<D> {
33 private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeWriter.class);
35 private final InstanceIdentifier<D> instanceIdentifier;
37 AbstractCompositeWriter(final InstanceIdentifier<D> type) {
38 this.instanceIdentifier = type;
41 protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx)
42 throws WriteFailedException {
43 LOG.debug("{}: Writing current: {} data: {}", this, id, data);
44 writeCurrentAttributes(id, data, ctx);
45 LOG.debug("{}: Current node written successfully", this);
48 protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter,
49 final WriteContext ctx) throws WriteFailedException {
50 LOG.debug("{}: Updating current: {} dataBefore: {}, datAfter: {}", this, id, dataBefore, dataAfter);
52 if (dataBefore.equals(dataAfter)) {
53 LOG.debug("{}: Skipping current(no update): {}", this, id);
57 updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
58 LOG.debug("{}: Current node updated successfully", this);
61 protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx)
62 throws WriteFailedException {
63 LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore);
64 deleteCurrentAttributes(id, dataBefore, ctx);
67 @SuppressWarnings("unchecked")
69 public void update(@Nonnull final InstanceIdentifier<? extends DataObject> id,
70 @Nullable final DataObject dataBefore,
71 @Nullable final DataObject dataAfter,
72 @Nonnull final WriteContext ctx) throws WriteFailedException {
73 LOG.debug("{}: Updating : {}", this, id);
74 LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter);
76 checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
77 id, getManagedDataObjectType(), this);
79 if (isWrite(dataBefore, dataAfter)) {
80 writeCurrent((InstanceIdentifier<D>) id, castToManaged(dataAfter), ctx);
81 } else if (isDelete(dataBefore, dataAfter)) {
82 deleteCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), ctx);
84 checkArgument(dataBefore != null && dataAfter != null, "No data to process");
85 updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
89 private void checkDataType(@Nonnull final DataObject dataAfter) {
90 checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
93 private D castToManaged(final DataObject data) {
95 return getManagedDataObjectType().getTargetType().cast(data);
98 private static boolean isWrite(final DataObject dataBefore,
99 final DataObject dataAfter) {
100 return dataBefore == null && dataAfter != null;
103 private static boolean isDelete(final DataObject dataBefore,
104 final DataObject dataAfter) {
105 return dataAfter == null && dataBefore != null;
108 private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
109 return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
112 protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
113 @Nonnull final D data,
114 @Nonnull final WriteContext ctx) throws WriteFailedException;
116 protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
117 @Nonnull final D dataBefore,
118 @Nonnull final WriteContext ctx) throws WriteFailedException;
120 protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
121 @Nonnull final D dataBefore,
122 @Nonnull final D dataAfter,
123 @Nonnull final WriteContext ctx) throws WriteFailedException;
127 public InstanceIdentifier<D> getManagedDataObjectType() {
128 return instanceIdentifier;
133 public String toString() {
134 return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());