b2a571b40bbe42560d0c235130315ad709c62ff4
[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
41     private SubtreeWriter(final Writer<D> delegate, Set<InstanceIdentifier<?>> handledTypes) {
42         this.delegate = delegate;
43         for (InstanceIdentifier<?> handledType : handledTypes) {
44             // Iid has to start with writer's handled root type
45             checkArgument(delegate.getManagedDataObjectType().getTargetType().equals(
46                     handledType.getPathArguments().iterator().next().getType()),
47                     "Handled node from subtree has to be identified by an instance identifier starting from: %s."
48                     + "Instance identifier was: %s", getManagedDataObjectType().getTargetType(), handledType);
49             checkArgument(Iterables.size(handledType.getPathArguments()) > 1,
50                     "Handled node from subtree identifier too short: %s", handledType);
51             handledChildTypes.add(InstanceIdentifier.create(Iterables.concat(
52                     getManagedDataObjectType().getPathArguments(), Iterables.skip(handledType.getPathArguments(), 1))));
53         }
54     }
55
56     /**
57      * Return set of types also handled by this writer. All of the types are children of the type managed by this
58      * writer excluding the type of this writer.
59      */
60     Set<InstanceIdentifier<?>> getHandledChildTypes() {
61         return handledChildTypes;
62     }
63
64     @Override
65     public void processModification(
66             @Nonnull final InstanceIdentifier<? extends DataObject> id,
67             @Nullable final DataObject dataBefore,
68             @Nullable final DataObject dataAfter, @Nonnull final WriteContext ctx) throws WriteFailedException {
69         delegate.processModification(id, dataBefore, dataAfter, ctx);
70     }
71
72     @Override
73     public boolean supportsDirectUpdate() {
74         return delegate.supportsDirectUpdate();
75     }
76
77     @Override
78     @Nonnull
79     public InstanceIdentifier<D> getManagedDataObjectType() {
80         return delegate.getManagedDataObjectType();
81     }
82
83     /**
84      * Wrap a writer as a subtree writer.
85      */
86     static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
87                                      @Nonnull final Writer<? extends DataObject> writer) {
88         return new SubtreeWriter<>(writer, handledChildren);
89     }
90 }