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 static com.google.common.base.Preconditions.checkArgument;
21 import io.fd.honeycomb.translate.util.RWUtils;
22 import io.fd.honeycomb.translate.write.WriteContext;
23 import io.fd.honeycomb.translate.write.WriteFailedException;
24 import io.fd.honeycomb.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;
37 private final boolean supportsUpdate;
39 protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate) {
40 this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
41 this.supportsUpdate = supportsUpdate;
44 protected void writeCurrent(final InstanceIdentifier<D> id, final D data, final WriteContext ctx)
45 throws WriteFailedException {
46 LOG.debug("{}: Writing current: {} data: {}", this, id, data);
47 writeCurrentAttributes(id, data, ctx);
48 LOG.debug("{}: Current node written successfully", this);
51 protected void updateCurrent(final InstanceIdentifier<D> id, final D dataBefore, final D dataAfter,
52 final WriteContext ctx) throws WriteFailedException {
53 LOG.debug("{}: Updating current: {} dataBefore: {}, datAfter: {}", this, id, dataBefore, dataAfter);
55 if (dataBefore.equals(dataAfter)) {
56 LOG.debug("{}: Skipping current(no update): {}", this, id);
60 updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
61 LOG.debug("{}: Current node updated successfully", this);
64 protected void deleteCurrent(final InstanceIdentifier<D> id, final D dataBefore, final WriteContext ctx)
65 throws WriteFailedException {
66 LOG.debug("{}: Deleting current: {} dataBefore: {}", this, id, dataBefore);
67 deleteCurrentAttributes(id, dataBefore, ctx);
70 @SuppressWarnings("unchecked")
72 public void processModification(@Nonnull final InstanceIdentifier<? extends DataObject> id,
73 @Nullable final DataObject dataBefore,
74 @Nullable final DataObject dataAfter,
75 @Nonnull final WriteContext ctx) throws WriteFailedException {
76 LOG.debug("{}: Updating : {}", this, id);
77 LOG.trace("{}: Updating : {}, from: {} to: {}", this, id, dataBefore, dataAfter);
79 checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
80 id, getManagedDataObjectType(), this);
82 if (isWrite(dataBefore, dataAfter)) {
83 writeCurrent((InstanceIdentifier<D>) id, castToManaged(dataAfter), ctx);
84 } else if (isDelete(dataBefore, dataAfter)) {
85 deleteCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), ctx);
87 checkArgument(dataBefore != null && dataAfter != null, "No data to process");
88 updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
92 private void checkDataType(@Nonnull final DataObject dataAfter) {
93 checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
96 private D castToManaged(final DataObject data) {
98 return getManagedDataObjectType().getTargetType().cast(data);
101 private static boolean isWrite(final DataObject dataBefore,
102 final DataObject dataAfter) {
103 return dataBefore == null && dataAfter != null;
106 private static boolean isDelete(final DataObject dataBefore,
107 final DataObject dataAfter) {
108 return dataAfter == null && dataBefore != null;
111 private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
112 return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
115 protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
116 @Nonnull final D data,
117 @Nonnull final WriteContext ctx) throws WriteFailedException;
119 protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
120 @Nonnull final D dataBefore,
121 @Nonnull final WriteContext ctx) throws WriteFailedException;
123 protected abstract void updateCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
124 @Nonnull final D dataBefore,
125 @Nonnull final D dataAfter,
126 @Nonnull final WriteContext ctx) throws WriteFailedException;
130 public InstanceIdentifier<D> getManagedDataObjectType() {
131 return instanceIdentifier;
136 public String toString() {
137 return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
141 public boolean supportsDirectUpdate() {
142 return supportsUpdate;