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.vpp.facade.impl.write.util;
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.vpp.facade.impl.util.ReflectionUtils;
23 import io.fd.honeycomb.v3po.vpp.facade.spi.write.ChildVppWriterCustomizer;
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;
34 public class ReflexiveChildWriterCustomizer<C extends DataObject> extends NoopWriterCustomizer<C> implements
35 ChildVppWriterCustomizer<C> {
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);
45 Preconditions.checkArgument(method.isPresent(), "Unable to get %s from %s", currentType, parentData);
48 return method.isPresent()
49 ? Optional.of((C) method.get().invoke(parentData))
50 : Optional.<C>absent();
51 } catch (IllegalAccessException | InvocationTargetException e) {
52 throw new IllegalArgumentException("Unable to get " + currentType + " from " + parentData, e);
56 private Class<? extends DataObject> getParentType(final @Nonnull InstanceIdentifier<C> currentId) {
57 return Iterables.get(currentId.getPathArguments(), Iterables.size(currentId.getPathArguments()) - 2).getType();