HONEYCOMB-130: Separate v3po plugin from HC infra
[honeycomb.git] / infra / translate-utils / src / main / java / io / fd / honeycomb / v3po / translate / util / read / ReflexiveListReaderCustomizer.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.read;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.v3po.translate.spi.read.ListReaderCustomizer;
23 import io.fd.honeycomb.v3po.translate.util.ReflectionUtils;
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26 import java.util.Collections;
27 import java.util.List;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yangtools.concepts.Builder;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.Identifiable;
32 import org.opendaylight.yangtools.yang.binding.Identifier;
33
34 /**
35  * Might be slow !
36  */
37 public abstract class ReflexiveListReaderCustomizer<C extends DataObject & Identifiable<K>, K extends Identifier<C>, B extends Builder<C>>
38         extends ReflexiveReaderCustomizer<C, B>
39         implements ListReaderCustomizer<C, K, B> {
40
41
42     public ReflexiveListReaderCustomizer(final Class<C> typeClass, final Class<B> builderClass) {
43         super(typeClass, builderClass);
44     }
45
46     @Override
47     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final C readValue) {
48         merge(parentBuilder, Collections.singletonList(readValue));
49     }
50
51     @Override
52     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final List<C> readData) {
53         final Optional<Method> method =
54                 ReflectionUtils.findMethodReflex(parentBuilder.getClass(), "set" + getTypeClass().getSimpleName(),
55                         Collections.singletonList(List.class), parentBuilder.getClass());
56
57         checkArgument(method.isPresent(), "Unable to set %s to %s", readData, parentBuilder);
58
59         try {
60             method.get().invoke(parentBuilder, readData);
61         } catch (IllegalAccessException | InvocationTargetException e) {
62             throw new IllegalArgumentException("Unable to set " + readData + " to " + parentBuilder, e);
63         }
64     }
65 }