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<>();
40 private boolean isWildcarded = false;
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))));
57 private SubtreeWriter(final Writer<D> delegate) {
58 this.delegate = delegate;
59 this.isWildcarded = true;
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.
66 Set<InstanceIdentifier<?>> getHandledChildTypes() {
67 return handledChildTypes;
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);
79 public boolean supportsDirectUpdate() {
80 return delegate.supportsDirectUpdate();
84 public boolean canProcess(@Nonnull InstanceIdentifier<?> instanceIdentifier) {
86 final Class<D> parent = delegate.getManagedDataObjectType().getTargetType();
87 for (InstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()) {
88 if (pathArgument.getType().equals(parent)) {
94 return handledChildTypes.contains(instanceIdentifier);
99 public InstanceIdentifier<D> getManagedDataObjectType() {
100 return delegate.getManagedDataObjectType();
104 * Wrap a writer as a subtree writer.
106 static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
107 @Nonnull final Writer<? extends DataObject> writer) {
108 return new SubtreeWriter<>(writer, handledChildren);
112 * Wrap a writer as a subtree writer.
114 static Writer<?> createWildcardedForWriter(@Nonnull final Writer<? extends DataObject> writer) {
115 return new SubtreeWriter<>(writer);