44b36edae075a1bee74d8949e9923afe018a6ab2
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.v3po.translate.util.write;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
31
32 public abstract class AbstractGenericWriter<D extends DataObject> implements Writer<D> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(AbstractGenericWriter.class);
35
36     private final InstanceIdentifier<D> instanceIdentifier;
37
38     protected AbstractGenericWriter(final InstanceIdentifier<D> type) {
39         this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
40     }
41
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);
47     }
48
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);
52
53         if (dataBefore.equals(dataAfter)) {
54             LOG.debug("{}: Skipping current(no update): {}", this, id);
55             // No change, ignore
56             return;
57         }
58         updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
59         LOG.debug("{}: Current node updated successfully", this);
60     }
61
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);
66     }
67
68     @SuppressWarnings("unchecked")
69     @Override
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);
76
77         checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
78                 id, getManagedDataObjectType(), this);
79
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);
84         } else {
85             checkArgument(dataBefore != null && dataAfter != null, "No data to process");
86             updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
87         }
88     }
89
90     private void checkDataType(@Nonnull final DataObject dataAfter) {
91         checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
92     }
93
94     private D castToManaged(final DataObject data) {
95         checkDataType(data);
96         return getManagedDataObjectType().getTargetType().cast(data);
97     }
98
99     private static boolean isWrite(final DataObject dataBefore,
100                                     final DataObject dataAfter) {
101         return dataBefore == null && dataAfter != null;
102     }
103
104     private static boolean isDelete(final DataObject dataBefore,
105                                     final DataObject dataAfter) {
106         return dataAfter == null && dataBefore != null;
107     }
108
109     private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
110         return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
111     }
112
113     protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
114                                                    @Nonnull final D data,
115                                                    @Nonnull final WriteContext ctx) throws WriteFailedException;
116
117     protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
118                                                     @Nonnull final D dataBefore,
119                                                     @Nonnull final WriteContext ctx) throws WriteFailedException;
120
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;
125
126     @Nonnull
127     @Override
128     public InstanceIdentifier<D> getManagedDataObjectType() {
129         return instanceIdentifier;
130     }
131
132
133     @Override
134     public String toString() {
135         return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
136     }
137 }