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