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.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;
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;
34 * Simple writer delegate for subtree writers (writers handling also children nodes) providing a list of all the
35 * children nodes being handled.
37 final class SubtreeWriter<D extends DataObject> implements Writer<D> {
39 private final Writer<D> delegate;
40 private final Set<InstanceIdentifier<?>> handledChildTypes = new HashSet<>();
41 private boolean isWildcarded = false;
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))));
58 private SubtreeWriter(final Writer<D> delegate) {
59 this.delegate = delegate;
60 this.isWildcarded = true;
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.
67 Set<InstanceIdentifier<?>> getHandledChildTypes() {
68 return handledChildTypes;
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);
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);
87 public boolean supportsDirectUpdate() {
88 return delegate.supportsDirectUpdate();
92 public boolean canProcess(@Nonnull InstanceIdentifier<?> instanceIdentifier) {
94 final Class<D> parent = delegate.getManagedDataObjectType().getTargetType();
95 for (InstanceIdentifier.PathArgument pathArgument : instanceIdentifier.getPathArguments()) {
96 if (pathArgument.getType().equals(parent)) {
102 return handledChildTypes.contains(instanceIdentifier);
107 public InstanceIdentifier<D> getManagedDataObjectType() {
108 return delegate.getManagedDataObjectType();
112 * Wrap a writer as a subtree writer.
114 static Writer<?> createForWriter(@Nonnull final Set<InstanceIdentifier<?>> handledChildren,
115 @Nonnull final Writer<? extends DataObject> writer) {
116 return new SubtreeWriter<>(writer, handledChildren);
120 * Wrap a writer as a subtree writer.
122 static Writer<?> createWildcardedForWriter(@Nonnull final Writer<? extends DataObject> writer) {
123 return new SubtreeWriter<>(writer);