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.read;
19 import static com.google.common.base.Preconditions.checkArgument;
21 import com.google.common.base.Optional;
22 import com.google.common.collect.Lists;
23 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
24 import io.fd.honeycomb.v3po.translate.util.ReflectionUtils;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import org.opendaylight.yangtools.concepts.Builder;
28 import org.opendaylight.yangtools.yang.binding.Augmentation;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
34 public class ReflexiveAugmentReaderCustomizer<C extends DataObject, B extends Builder<C>>
35 extends ReflexiveRootReaderCustomizer<C, B>
36 implements ChildReaderCustomizer<C,B> {
38 private final Class<C> augType;
40 public ReflexiveAugmentReaderCustomizer(final Class<B> builderClass, final Class<C> augType) {
42 this.augType = augType;
46 public void merge(final Builder<? extends DataObject> parentBuilder, final C readValue) {
47 final Optional<Method> method =
48 ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "addAugmentation",
49 Lists.newArrayList(Class.class, Augmentation.class), parentBuilder.getClass());
51 checkArgument(method.isPresent(), "Not possible to add augmentations to builder: %s", parentBuilder);
53 method.get().invoke(parentBuilder, augType, readValue);
54 } catch (IllegalAccessException | InvocationTargetException e) {
55 throw new IllegalArgumentException("Unable to set " + readValue + " to " + parentBuilder, e);