Expect null in ReflexChildWriterCustomizer
[honeycomb.git] / v3po / translate-utils / src / main / java / io / fd / honeycomb / v3po / translate / util / write / ReflexiveChildWriterCustomizer.java
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 com.google.common.base.Optional;
20 import com.google.common.base.Preconditions;
21 import com.google.common.collect.Iterables;
22 import io.fd.honeycomb.v3po.translate.util.ReflectionUtils;
23 import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.Collections;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 /**
32  * Might be slow !
33  */
34 public class ReflexiveChildWriterCustomizer<C extends DataObject> extends NoopWriterCustomizer<C> implements
35     ChildWriterCustomizer<C> {
36
37     @Nonnull
38     @Override
39     @SuppressWarnings("unchecked")
40     public Optional<C> extract(@Nonnull final InstanceIdentifier<C> currentId, @Nonnull final DataObject parentData) {
41         final Class<C> currentType = currentId.getTargetType();
42         final Optional<Method> method = ReflectionUtils.findMethodReflex(getParentType(currentId),
43             "get" + currentType.getSimpleName(), Collections.<Class<?>>emptyList(), currentType);
44
45         Preconditions.checkArgument(method.isPresent(), "Unable to get %s from %s", currentType, parentData);
46
47         try {
48             return method.isPresent()
49                 ? Optional.fromNullable((C) method.get().invoke(parentData))
50                 : Optional.absent();
51         } catch (IllegalAccessException | InvocationTargetException e) {
52             throw new IllegalArgumentException("Unable to get " + currentType + " from " + parentData, e);
53         }
54     }
55
56     private Class<? extends DataObject> getParentType(final @Nonnull InstanceIdentifier<C> currentId) {
57         return Iterables.get(currentId.getPathArguments(), Iterables.size(currentId.getPathArguments()) - 2).getType();
58     }
59 }