6d29214b3cde98965dac057fa56ba18d9fd9a5d8
[hc2vpp.git] /
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.v3po.translate.util.write;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkState;
21
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;
29
30 /**
31  * Might be slow !
32  */
33 public class ReflexiveAugmentWriterCustomizer<C extends DataObject> extends NoopWriterCustomizer<C> implements
34     ChildWriterCustomizer<C> {
35
36     @Nonnull
37     @Override
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();
45         } else {
46             checkState(currentType.isAssignableFrom(augmentation.getClass()));
47             return Optional.of(currentType.cast(augmentation));
48         }
49     }
50 }