1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.yangtools;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.annotation.Nullable;
28 import org.opendaylight.yangtools.concepts.Builder;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class YangToolsCloner {
35 private static YangToolsMapper yangtoolsMapper = new YangToolsMapper();
36 private static final Logger LOG = LoggerFactory.getLogger(YangToolsCloner.class);
37 public static final int ACCESSOR_FIELD = 0;
38 public static final int ACCESSOR_METHOD = 1;
41 private final int accessor;
43 private YangToolsCloner(int ac) {
46 public static YangToolsCloner instance() {
47 return instance(ACCESSOR_METHOD);
49 public static YangToolsCloner instance(int ac) {
50 return new YangToolsCloner(ac);
54 * @param source source object
55 * @param clazz Class of return object
56 * @return list of cloned object
59 public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz) {
60 return cloneList(source, clazz, null);
65 * @param source source object
66 * @param clazz Class of return object
67 * @attrList filter for attribute Names to clone
68 * @return list of cloned object
70 public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz,
71 @Nullable List<String> attrList) {
75 List<T> list = new ArrayList<T>();
77 list.add(clone(s, clazz, attrList));
84 * @param source source object
85 * @param clazz Class of return object
86 * @return cloned object
88 public <S , T extends DataObject> T clone(S source, Class<T> clazz) {
89 return clone(source, clazz, null);
93 * @param source source object
94 * @param clazz Class of return object
95 * @attrList if empty copy all else list of attribute Names to clone
96 * @return cloned object
98 public <S, T extends DataObject> T clone(S source, Class<T> clazz,
99 @Nullable List<String> attrList) {
100 if (source == null) {
103 Field[] attributeFields;
106 Builder<T> builder = yangtoolsMapper.getBuilder(clazz);
107 T object = builder.build();
108 attributeFields = object.getClass().getDeclaredFields();
109 for (Field attributeField : attributeFields) {
110 // check if attr is in inclusion list
111 if (attrList != null && !attrList.contains(attributeField.getName())) {
115 if (attributeField.getName().equals("QNAME")) {
119 attributeField.setAccessible(true);
121 if(accessor==ACCESSOR_FIELD) {
122 sourceField = source.getClass().getDeclaredField(attributeField.getName());
123 sourceField.setAccessible(true);
124 if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
125 attributeField.set(object, String.valueOf(sourceField.get(source)));
127 attributeField.set(object, sourceField.get(source));
130 else if(accessor==ACCESSOR_METHOD) {
131 String getter = getter(attributeField.getName());
132 System.out.println("getter="+getter);
133 m = source.getClass().getDeclaredMethod(getter);
134 m.setAccessible(true);
135 if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
136 attributeField.set(object, String.valueOf(m.invoke(source)));
138 attributeField.set(object, m.invoke(source));
142 } catch (NoSuchMethodException | NoSuchFieldException e) {
143 // Convert to run-time exception
144 String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
146 // throw new IllegalArgumentException(msg);
147 } catch (IllegalAccessException|SecurityException e) {
148 LOG.debug("Access problem " + attributeField.getName(), e);
149 } catch (IllegalArgumentException e) {
150 LOG.debug("argument problem " + attributeField.getName(), e);
151 } catch (InvocationTargetException e) {
152 LOG.debug("invocation problem " + attributeField.getName(), e);
159 private static String getter(String name) {
160 return String.format("%s%s%s","get",name.substring(1, 2).toUpperCase(),name.substring(2));
162 public <S extends DataObject, T extends DataObject,B extends Builder<T>> B cloneToBuilder(S source, B builder){
163 return cloneToBuilder(source, builder,null);
165 public <S extends DataObject, T extends DataObject,B extends Builder<T>> B cloneToBuilder(S source, B builder,
166 @Nullable List<String> attrList) {
167 Field[] attributeFields;
170 attributeFields = builder.getClass().getDeclaredFields();
171 for (Field attributeField : attributeFields) {
172 // check if attr is in inclusion list
173 if (attrList != null && !attrList.contains(attributeField.getName())) {
177 if (attributeField.getName().equals("QNAME")) {
181 attributeField.setAccessible(true);
183 if(accessor==ACCESSOR_FIELD) {
184 sourceField = source.getClass().getDeclaredField(attributeField.getName());
185 sourceField.setAccessible(true);
186 if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
187 attributeField.set(builder, String.valueOf(sourceField.get(source)));
189 attributeField.set(builder, sourceField.get(source));
192 else if(accessor==ACCESSOR_METHOD) {
193 m = source.getClass().getDeclaredMethod(getter(attributeField.getName()));
194 m.setAccessible(true);
195 if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
196 attributeField.set(builder, String.valueOf(m.invoke(source)));
198 attributeField.set(builder, m.invoke(source));
202 } catch (NoSuchMethodException | NoSuchFieldException e) {
203 // Convert to run-time exception
204 String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
206 // throw new IllegalArgumentException(msg);
207 } catch (IllegalAccessException|SecurityException e) {
208 LOG.debug("Access problem " + attributeField.getName(), e);
209 } catch (IllegalArgumentException e) {
210 LOG.debug("argument problem " + attributeField.getName(), e);
211 } catch (InvocationTargetException e) {
212 LOG.debug("invocation problem " + attributeField.getName(), e);