aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/yangtools/YangToolsCloner.java
blob: a9a658387f1aef41ac80e1fcbbe058a607cf0030 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class YangToolsCloner {

    private static YangToolsMapper yangtoolsMapper = new YangToolsMapper();
    private static final Logger LOG = LoggerFactory.getLogger(YangToolsCloner.class);
    public static final int ACCESSOR_FIELD = 0;
    public static final int ACCESSOR_METHOD = 1;


    private final int accessor;

    private YangToolsCloner(int ac) {
        this.accessor = ac;
    }

    public static YangToolsCloner instance() {
        return instance(ACCESSOR_METHOD);
    }

    public static YangToolsCloner instance(int ac) {
        return new YangToolsCloner(ac);
    }

    /**
     *
     * @param source source object
     * @param clazz Class of return object
     * @return list of cloned object
     * @return
     */
    public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz) {
        return cloneList(source, clazz, null);
    }

    /**
     *
     * @param source source object
     * @param clazz Class of return object
     * @attrList filter for attribute Names to clone
     * @return list of cloned object
     */
    public <S extends DataObject, T extends DataObject> List<T> cloneList(List<S> source, Class<T> clazz,
            @Nullable List<String> attrList) {
        if (source == null) {
            return null;
        }
        List<T> list = new ArrayList<>();
        for (S s : source) {
            list.add(clone(s, clazz, attrList));
        }
        return list;
    }

    /**
     *
     * @param source source object
     * @param clazz Class of return object
     * @return cloned object
     */
    public <S, T extends DataObject> T clone(S source, Class<T> clazz) {
        return clone(source, clazz, null);
    }

    /**
     *
     * @param source source object
     * @param clazz Class of return object
     * @attrList if empty copy all else list of attribute Names to clone
     * @return cloned object
     */
    public <S, T extends DataObject> T clone(S source, Class<T> clazz, @Nullable List<String> attrList) {
        if (source == null) {
            return (T) null;
        }
        Field[] attributeFields;
        Field sourceField;
        Method m;
        Builder<T> builder = yangtoolsMapper.getBuilder(clazz);
        T object = builder.build();
        attributeFields = object.getClass().getDeclaredFields();
        for (Field attributeField : attributeFields) {
            // check if attr is in inclusion list
            if (attrList != null && !attrList.contains(attributeField.getName())) {
                continue;
            }
            // ignore QNAME
            if (attributeField.getName().equals("QNAME")) {
                continue;
            }

            attributeField.setAccessible(true);
            try {
                if (accessor == ACCESSOR_FIELD) {
                    sourceField = source.getClass().getDeclaredField(attributeField.getName());
                    sourceField.setAccessible(true);
                    if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
                        attributeField.set(object, String.valueOf(sourceField.get(source)));
                    } else {
                        attributeField.set(object, sourceField.get(source));
                    }
                } else if (accessor == ACCESSOR_METHOD) {
                    String getter = getter(attributeField.getName());
                    System.out.println("getter=" + getter);
                    m = source.getClass().getDeclaredMethod(getter);
                    m.setAccessible(true);
                    if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
                        attributeField.set(object, String.valueOf(m.invoke(source)));
                    } else {
                        attributeField.set(object, m.invoke(source));
                    }
                }

            } catch (NoSuchMethodException | NoSuchFieldException e) {
                // Convert to run-time exception
                String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
                LOG.debug(msg);
                // throw new IllegalArgumentException(msg);
            } catch (IllegalAccessException | SecurityException e) {
                LOG.debug("Access problem " + attributeField.getName(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug("argument problem " + attributeField.getName(), e);
            } catch (InvocationTargetException e) {
                LOG.debug("invocation problem " + attributeField.getName(), e);
            }
        }

        return object;
    }

    private static String getter(String name) {
        return String.format("%s%s%s", "get", name.substring(1, 2).toUpperCase(), name.substring(2));
    }

    public <S extends DataObject, T extends DataObject, B extends Builder<T>> B cloneToBuilder(S source, B builder) {
        return cloneToBuilder(source, builder, null);
    }

    public <S extends DataObject, T extends DataObject, B extends Builder<T>> B cloneToBuilder(S source, B builder,
            @Nullable List<String> attrList) {
        Field[] attributeFields;
        Field sourceField;
        Method m;
        attributeFields = builder.getClass().getDeclaredFields();
        for (Field attributeField : attributeFields) {
            // check if attr is in inclusion list
            if (attrList != null && !attrList.contains(attributeField.getName())) {
                continue;
            }
            // ignore QNAME
            if (attributeField.getName().equals("QNAME")) {
                continue;
            }

            attributeField.setAccessible(true);
            try {
                if (accessor == ACCESSOR_FIELD) {
                    sourceField = source.getClass().getDeclaredField(attributeField.getName());
                    sourceField.setAccessible(true);
                    if (attributeField.getType().equals(String.class) && !sourceField.getType().equals(String.class)) {
                        attributeField.set(builder, String.valueOf(sourceField.get(source)));
                    } else {
                        attributeField.set(builder, sourceField.get(source));
                    }
                } else if (accessor == ACCESSOR_METHOD) {
                    m = source.getClass().getDeclaredMethod(getter(attributeField.getName()));
                    m.setAccessible(true);
                    if (attributeField.getType().equals(String.class) && !m.getReturnType().equals(String.class)) {
                        attributeField.set(builder, String.valueOf(m.invoke(source)));
                    } else {
                        attributeField.set(builder, m.invoke(source));
                    }
                }

            } catch (NoSuchMethodException | NoSuchFieldException e) {
                // Convert to run-time exception
                String msg = "no such field " + attributeField.getName() + " in class " + source.getClass().getName();
                LOG.debug(msg);
                // throw new IllegalArgumentException(msg);
            } catch (IllegalAccessException | SecurityException e) {
                LOG.debug("Access problem " + attributeField.getName(), e);
            } catch (IllegalArgumentException e) {
                LOG.debug("argument problem " + attributeField.getName(), e);
            } catch (InvocationTargetException e) {
                LOG.debug("invocation problem " + attributeField.getName(), e);
            }
        }
        return builder;
    }
}