2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
30 import javax.annotation.Nullable;
31 import org.opendaylight.yangtools.concepts.Builder;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class YangToolsCloner {
38 private static YangToolsMapper yangtoolsMapper = new YangToolsMapper();
39 private static final Logger LOG = LoggerFactory.getLogger(YangToolsCloner.class);
40 public static final int ACCESSOR_FIELD = 0;
41 public static final int ACCESSOR_METHOD = 1;
44 private final int accessor;
46 private YangToolsCloner(int ac) {
50 public static YangToolsCloner instance() {
51 return instance(ACCESSOR_METHOD);
54 public static YangToolsCloner instance(int ac) {
55 return new YangToolsCloner(ac);
60 * @param source source object
61 * @param clazz Class of return object
62 * @return list of cloned object
65 public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz) {
66 return cloneList(source, clazz, null);
71 * @param source source object
72 * @param clazz Class of return object
73 * @attrList filter for attribute Names to clone
74 * @return list of cloned object
76 public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz,
77 @Nullable List<String> attrList) {
81 List<T> list = new ArrayList<>();
83 list.add(clone(s, clazz, attrList));
90 * @param source source object
91 * @param clazz Class of return object
92 * @return cloned object
94 public <S, T extends DataObject> T clone(S source, Class<T> clazz) {
95 return clone(source, clazz, null);
100 * @param source source object
101 * @param clazz Class of return object
102 * @attrList if empty copy all else list of attribute Names to clone
103 * @return cloned object
105 public <S, T extends DataObject> T clone(S source, Class<T> clazz, @Nullable List<String> attrList) {
106 if (source == null) {
109 Field[] attributeFields;
112 Builder<T> builder = yangtoolsMapper.getBuilder(clazz);
113 T object = builder.build();
114 attributeFields = object.getClass().getDeclaredFields();
115 for (Field attributeField : attributeFields) {
116 // check if attr is in inclusion list
117 if (attrList != null && !attrList.contains(attributeField.getName())) {
121 if (attributeField.getName().equals("QNAME")) {
125 attributeField.setAccessible(true);
127 if (accessor == ACCESSOR_FIELD) {
128 sourceField = source.getClass().getDeclaredField(attributeField.getName());
129 sourceField.setAccessible(true);
130 if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
131 attributeField.set(object, String.valueOf(sourceField.get(source)));
133 attributeField.set(object, sourceField.get(source));
135 } else if (accessor == ACCESSOR_METHOD) {
136 String getter = getter(attributeField.getName());
137 System.out.println("getter=" + getter);
138 m = source.getClass().getDeclaredMethod(getter);
139 m.setAccessible(true);
140 if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
141 attributeField.set(object, String.valueOf(m.invoke(source)));
143 attributeField.set(object, m.invoke(source));
147 } catch (NoSuchMethodException | NoSuchFieldException e) {
148 // Convert to run-time exception
149 String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
151 // throw new IllegalArgumentException(msg);
152 } catch (IllegalAccessException | SecurityException e) {
153 LOG.debug("Access problem " + attributeField.getName(), e);
154 } catch (IllegalArgumentException e) {
155 LOG.debug("argument problem " + attributeField.getName(), e);
156 } catch (InvocationTargetException e) {
157 LOG.debug("invocation problem " + attributeField.getName(), e);
164 private static String getter(String name) {
165 return String.format("%s%s%s", "get", name.substring(1, 2).toUpperCase(), name.substring(2));
168 public <S extends DataObject, T extends DataObject, B extends Builder<T>> B cloneToBuilder(S source, B builder) {
169 return cloneToBuilder(source, builder, null);
172 public <S extends DataObject, T extends DataObject, B extends Builder<T>> B cloneToBuilder(S source, B builder,
173 @Nullable List<String> attrList) {
174 Field[] attributeFields;
177 attributeFields = builder.getClass().getDeclaredFields();
178 for (Field attributeField : attributeFields) {
179 // check if attr is in inclusion list
180 if (attrList != null && !attrList.contains(attributeField.getName())) {
184 if (attributeField.getName().equals("QNAME")) {
188 attributeField.setAccessible(true);
190 if (accessor == ACCESSOR_FIELD) {
191 sourceField = source.getClass().getDeclaredField(attributeField.getName());
192 sourceField.setAccessible(true);
193 if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
194 attributeField.set(builder, String.valueOf(sourceField.get(source)));
196 attributeField.set(builder, sourceField.get(source));
198 } else if (accessor == ACCESSOR_METHOD) {
199 m = source.getClass().getDeclaredMethod(getter(attributeField.getName()));
200 m.setAccessible(true);
201 if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
202 attributeField.set(builder, String.valueOf(m.invoke(source)));
204 attributeField.set(builder, m.invoke(source));
208 } catch (NoSuchMethodException | NoSuchFieldException e) {
209 // Convert to run-time exception
210 String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
212 // throw new IllegalArgumentException(msg);
213 } catch (IllegalAccessException | SecurityException e) {
214 LOG.debug("Access problem " + attributeField.getName(), e);
215 } catch (IllegalArgumentException e) {
216 LOG.debug("argument problem " + attributeField.getName(), e);
217 } catch (InvocationTargetException e) {
218 LOG.debug("invocation problem " + attributeField.getName(), e);