5f1391c7404891db3de0b8f38c3b0490b667f854
[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.impl.write;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
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;
30
31 abstract class AbstractCompositeWriter<D extends DataObject> implements Writer<D> {
32
33     private static final Logger LOG = LoggerFactory.getLogger(AbstractCompositeWriter.class);
34
35     private final InstanceIdentifier<D> instanceIdentifier;
36
37     AbstractCompositeWriter(final InstanceIdentifier<D> type) {
38         this.instanceIdentifier = type;
39     }
40
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);
46     }
47
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);
51
52         if (dataBefore.equals(dataAfter)) {
53             LOG.debug("{}: Skipping current(no update): {}", this, id);
54             // No change, ignore
55             return;
56         }
57         updateCurrentAttributes(id, dataBefore, dataAfter, ctx);
58         LOG.debug("{}: Current node updated successfully", this);
59     }
60
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);
65     }
66
67     @SuppressWarnings("unchecked")
68     @Override
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);
75
76         checkArgument(idPointsToCurrent(id), "Cannot handle data: %s. Only: %s can be handled by writer: %s",
77                 id, getManagedDataObjectType(), this);
78
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);
83         } else {
84             checkArgument(dataBefore != null && dataAfter != null, "No data to process");
85             updateCurrent((InstanceIdentifier<D>) id, castToManaged(dataBefore), castToManaged(dataAfter), ctx);
86         }
87     }
88
89     private void checkDataType(@Nonnull final DataObject dataAfter) {
90         checkArgument(getManagedDataObjectType().getTargetType().isAssignableFrom(dataAfter.getClass()));
91     }
92
93     private D castToManaged(final DataObject data) {
94         checkDataType(data);
95         return getManagedDataObjectType().getTargetType().cast(data);
96     }
97
98     private static boolean isWrite(final DataObject dataBefore,
99                                     final DataObject dataAfter) {
100         return dataBefore == null && dataAfter != null;
101     }
102
103     private static boolean isDelete(final DataObject dataBefore,
104                                     final DataObject dataAfter) {
105         return dataAfter == null && dataBefore != null;
106     }
107
108     private boolean idPointsToCurrent(final @Nonnull InstanceIdentifier<? extends DataObject> id) {
109         return id.getTargetType().equals(getManagedDataObjectType().getTargetType());
110     }
111
112     protected abstract void writeCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
113                                                    @Nonnull final D data,
114                                                    @Nonnull final WriteContext ctx) throws WriteFailedException;
115
116     protected abstract void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<D> id,
117                                                     @Nonnull final D dataBefore,
118                                                     @Nonnull final WriteContext ctx) throws WriteFailedException;
119
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;
124
125     @Nonnull
126     @Override
127     public InstanceIdentifier<D> getManagedDataObjectType() {
128         return instanceIdentifier;
129     }
130
131
132     @Override
133     public String toString() {
134         return String.format("Writer[%s]", getManagedDataObjectType().getTargetType().getSimpleName());
135     }
136 }