2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.impl.write.registry;
19 import static com.google.common.base.Preconditions.checkArgument;
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;
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;
33 * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the
34 * children nodes being handled.
36 final class SubtreeWriter<D extends DataObject> implements Writer<D> {
38 private final Writer<D> delegate;
39 private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>();
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))));
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.
60 Set<InstanceIdentifier<?>> getHandledChildTypes() {
61 return handledChildTypes;
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);
73 public boolean supportsDirectUpdate() {
74 return delegate.supportsDirectUpdate();
79 public InstanceIdentifier<D> getManagedDataObjectType() {
80 return delegate.getManagedDataObjectType();
84 * Wrap a writer as a subtree writer.
86 static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
87 @Nonnull final Writer<? extends DataObject> writer) {
88 return new SubtreeWriter<>(writer, handledChildren);