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.v3po.translate.util.write;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkState;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.yangtools.yang.binding.Augmentable;
26 import org.opendaylight.yangtools.yang.binding.Augmentation;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 public class ReflexiveAugmentWriterCustomizer<C extends DataObject> extends NoopWriterCustomizer<C> implements
34 ChildWriterCustomizer<C> {
38 @SuppressWarnings("unchecked")
39 public Optional<C> extract(@Nonnull final InstanceIdentifier<C> currentId, @Nonnull final DataObject parentData) {
40 checkArgument(parentData instanceof Augmentable<?>, "Not augmnatable parent object: %s", parentData);
41 final Class<C> currentType = currentId.getTargetType();
42 final Augmentation<?> augmentation = ((Augmentable) parentData).getAugmentation(currentType);
43 if(augmentation == null) {
44 return Optional.absent();
46 checkState(currentType.isAssignableFrom(augmentation.getClass()));
47 return Optional.of(currentType.cast(augmentation));