e2ea115355e58b4b700498b64138c7b0c16a5508
[honeycomb.git] / infra / translate-utils / src / main / java / io / fd / honeycomb / translate / util / write / AbstractGenericWriter.java
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.translate.util.write;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
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     private final boolean supportsUpdate;
38
39     protected AbstractGenericWriter(final InstanceIdentifier<D> type, final boolean supportsUpdate) {
40         this.instanceIdentifier = RWUtils.makeIidWildcarded(type);
41         this.supportsUpdate = supportsUpdate;
42     }
43
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);
49     }
50
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);
54
55         if (dataBefore.equals(dataAfter)) {
56             LOG.debug("{}: Skipping current(no update): {}", this, id);
57             // No change, ignore
58             return;
59         }
60         updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
61         LOG.debug("{}: Current node updated successfully", this);
62     }
63
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);
68     }
69
70     @SuppressWarnings("unchecked")
71     @Override
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);
78
79         checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
80                 id, getManagedDataObjectType(), this);
81
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);
86         } else {
87             checkArgument(dataBefore != null && dataAfter != null, "No data to process");
88             updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
89         }
90     }
91
92     private void checkDataType(@Nonnull final DataObject dataAfter) {
93         checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
94     }
95
96     private D castToManaged(final DataObject data) {
97         checkDataType(data);
98         return getManagedDataObjectType().getTargetType().cast(data);
99     }
100
101     private static boolean isWrite(final DataObject dataBefore,
102                                     final DataObject dataAfter) {
103         return dataBefore == null && dataAfter != null;
104     }
105
106     private static boolean isDelete(final DataObject dataBefore,
107                                     final DataObject dataAfter) {
108         return dataAfter == null && dataBefore != null;
109     }
110
111     private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
112         return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
113     }
114
115     protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
116                                                    @Nonnull final D data,
117                                                    @Nonnull final WriteContext ctx) throws WriteFailedException;
118
119     protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
120                                                     @Nonnull final D dataBefore,
121                                                     @Nonnull final WriteContext ctx) throws WriteFailedException;
122
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;
127
128     @Nonnull
129     @Override
130     public InstanceIdentifier<D> getManagedDataObjectType() {
131         return instanceIdentifier;
132     }
133
134
135     @Override
136     public String toString() {
137         return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
138     }
139
140     @Override
141     public boolean supportsDirectUpdate() {
142         return supportsUpdate;
143     }
144 }