HONEYCOMB-431: add validation support to Writers
[honeycomb.git] / infra / translate-impl / src / main / java / io / fd / honeycomb / translate / impl / write / registry / SubtreeWriter.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.impl.write.registry;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.collect.Iterables;
22 import io.fd.honeycomb.translate.write.DataValidationFailedException;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import io.fd.honeycomb.translate.write.Writer;
26 import java.util.HashSet;
27 import java.util.Set;
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32
33 /**
34  * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the
35  * children nodes being handled.
36  */
37 final class SubtreeWriter<D extends DataObject> implements Writer<D> {
38
39     private final Writer<D> delegate;
40     private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>();
41     private boolean isWildcarded = false;
42
43     private SubtreeWriter(final Writer<D> delegate, final Set<InstanceIdentifier<?>> handledTypes) {
44         this.delegate = delegate;
45         for (InstanceIdentifier<?> handledType : handledTypes) {
46             // Iid has to start with writer's handled root type
47             checkArgument(delegate.getManagedDataObjectType().getTargetType().equals(
48                     handledType.getPathArguments().iterator().next().getType()),
49                     "Handled node from subtree has to be identified by an instance identifier starting from: %s."
50                             + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType);
51             checkArgument(Iterables.size(handledType.getPathArguments()) > 1,
52                     "Handled node from subtree identifier too short: %s", handledType);
53             handledChildTypes.add(InstanceIdentifier.create(Iterables.concat(
54                     getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1))));
55         }
56     }
57
58     private SubtreeWriter(final Writer<D> delegate) {
59         this.delegate = delegate;
60         this.isWildcarded = true;
61     }
62
63     /**
64      * Return set of types also handled by this writer. All of the types are children of the type managed by this
65      * writer excluding the type of this writer.
66      */
67     Set<InstanceIdentifier<?>> getHandledChildTypes() {
68         return handledChildTypes;
69     }
70
71     @Override
72     public void validate(@Nonnull final InstanceIdentifier<? extends DataObject> id,
73                          @Nullable final DataObject dataBefore, @Nullable final DataObject dataAfter,
74                          @Nonnull final WriteContext ctx) throws DataValidationFailedException {
75         delegate.validate(id, dataBefore, dataAfter, ctx);
76     }
77
78     @Override
79     public void processModification(
80             @Nonnull final InstanceIdentifier<? extends DataObject> id,
81             @Nullable final DataObject dataBefore,
82             @Nullable final DataObject dataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException {
83         delegate.processModification(id, dataBefore, dataAfter, ctx);
84     }
85
86     @Override
87     public boolean supportsDirectUpdate() {
88         return delegate.supportsDirectUpdate();
89     }
90
91     @Override
92     public boolean canProcess(@Nonnull InstanceIdentifier<?> instanceIdentifier) {
93         if (isWildcarded) {
94             final Class<D> parent = delegate.getManagedDataObjectType().getTargetType();
95             for (InstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()) {
96                 if (pathArgument.getType().equals(parent)) {
97                     return true;
98                 }
99             }
100             return false;
101         }
102         return handledChildTypes.contains(instanceIdentifier);
103     }
104
105     @Override
106     @Nonnull
107     public InstanceIdentifier<D> getManagedDataObjectType() {
108         return delegate.getManagedDataObjectType();
109     }
110
111     /**
112      * Wrap a writer as a subtree writer.
113      */
114     static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
115                                      @Nonnull final Writer<? extends DataObject> writer) {
116         return new SubtreeWriter<>(writer, handledChildren);
117     }
118
119     /**
120      * Wrap a writer as a subtree writer.
121      */
122     static Writer<?> createWildcardedForWriter(@Nonnull final Writer<? extends DataObject> writer) {
123         return new SubtreeWriter<>(writer);
124     }
125 }