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.util.write;
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;
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
31 import static com.google.common.base.Preconditions.checkArgument;
33 public abstract class AbstractGenericWriter<D extends DataObject> implements Writer<D> {
35 private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericWriter.class);
37 private final InstanceIdentifier<D> instanceIdentifier;
38 private final boolean supportsUpdate;
40 protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate) {
41 this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
42 this.supportsUpdate = supportsUpdate;
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);
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);
56 if (dataBefore.equals(dataAfter)) {
57 LOG.debug("{}: Skipping current(no update): {}", this, id);
61 updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
62 LOG.debug("{}: Current node updated successfully", this);
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);
71 @SuppressWarnings("unchecked")
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);
80 checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
81 id, getManagedDataObjectType(), this);
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);
88 checkArgument(dataBefore != null && dataAfter != null, "No data to process");
89 updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
93 private void checkDataType(@Nonnull final DataObject dataAfter) {
94 checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
97 private D castToManaged(final DataObject data) {
99 return getManagedDataObjectType().getTargetType().cast(data);
102 private static boolean isWrite(final DataObject dataBefore,
103 final DataObject dataAfter) {
104 return dataBefore == null && dataAfter != null;
107 private static boolean isDelete(final DataObject dataBefore,
108 final DataObject dataAfter) {
109 return dataAfter == null && dataBefore != null;
112 private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
113 return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
116 protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
117 @Nonnull final D data,
118 @Nonnull final WriteContext ctx) throws WriteFailedException;
120 protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
121 @Nonnull final D dataBefore,
122 @Nonnull final WriteContext ctx) throws WriteFailedException;
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;
131 public InstanceIdentifier<D> getManagedDataObjectType() {
132 return instanceIdentifier;
137 public String toString() {
138 return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
142 public boolean supportsDirectUpdate() {
143 return supportsUpdate;