aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/data-provider/dblib/src/main/java/org/onap/ccsdk/features/sdnr/wt/dataprovider/database/sqldb/database/SqlDBMapper.java
blob: 85d3cfe4b932cd0933f97e4c231a6bcde300aa79 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2021 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.database.sqldb.database;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.rowset.CachedRowSet;
import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query.filters.DBKeyValuePair;
import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper;
import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsBuilderAnnotationIntrospector;
import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsDeserializerModifier;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Entity;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.Enumeration;
import org.opendaylight.yangtools.yang.common.Uint16;
import org.opendaylight.yangtools.yang.common.Uint32;
import org.opendaylight.yangtools.yang.common.Uint64;
import org.opendaylight.yangtools.yang.common.Uint8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SqlDBMapper {

    private static final Logger LOG = LoggerFactory.getLogger(SqlDBMapper.class);

    private static final Map<Class<?>, String> mariaDBTypeMap = initTypeMap();
    private static final String ODLID_DBTYPE = "VARCHAR(40)";
    private static final String STRING_DBTYPE = "VARCHAR(255)";
    private static final String ENUM_DBTYPE = "VARCHAR(100)";
    public static final String ODLID_DBCOL = "controller-id";
    private static List<Class<?>> numericClasses = Arrays.asList(Byte.class, Integer.class, Long.class,
            BigInteger.class, Uint8.class, Uint16.class, Uint32.class, Uint64.class);
    private static final YangToolsMapper mapper = new YangToolsMapper();
    public static final String TABLENAME_CONTROLLER = "controller";
    private static final String DEFAULTID_DBTYPE = "int(11)";

    public static String createTableOdl() {
        return "CREATE TABLE IF NOT EXISTS " + TABLENAME_CONTROLLER + " (" + "`id` " + ODLID_DBTYPE + " "
                + getColumnOptions("id", ODLID_DBTYPE) + "," + "`desc` " + STRING_DBTYPE + " "
                + getColumnOptions("description", STRING_DBTYPE) + "," + "primary key(id))";
    }

    public static <T> String createTable(Class<T> clazz, Entity e) throws UnableToMapClassException {
        return createTable(clazz, e, "", false);
    }

    public static <T> String createTable(Class<T> clazz, Entity e, String suffix) throws UnableToMapClassException {
        return createTable(clazz, e, suffix, false);
    }

    public static <T> String createTable(Class<T> clazz, Entity e, boolean autoIndex) throws UnableToMapClassException {
        return createTable(clazz, e, "", false);
    }

    public static <T> String createTable(Class<T> clazz, Entity e, String suffix, boolean autoIndex)
            throws UnableToMapClassException {
        StringBuilder sb = new StringBuilder();
        sb.append("CREATE TABLE IF NOT EXISTS `" + e.getName() + suffix + "` (\n");
        if (autoIndex) {
            sb.append("`id` " + DEFAULTID_DBTYPE + " " + getColumnOptions("id", DEFAULTID_DBTYPE) + ",\n");
        } else {
            sb.append("`id` " + STRING_DBTYPE + " " + getColumnOptions("id", STRING_DBTYPE) + ",\n");
        }
        sb.append("`" + ODLID_DBCOL + "` " + ODLID_DBTYPE + " " + getColumnOptions(ODLID_DBCOL, ODLID_DBTYPE) + ",\n");
        for (Method method : getFilteredMethods(clazz, true)) {
            Class<?> valueType = method.getReturnType();
            String colName = getColumnName(method);
            if (colName.equals("id")) {
                continue;
            }
            String dbType = getDBType(valueType);
            String options = getColumnOptions(colName, dbType);
            sb.append("`" + colName + "` " + dbType + " " + options + ",\n");
        }
        sb.append("primary key(id),");
        sb.append("foreign key(`" + ODLID_DBCOL + "`) references " + TABLENAME_CONTROLLER + "(id)");

        sb.append(");");
        return sb.toString();
    }

    private static String getColumnOptions(String colName, String dbType) {
        StringBuilder options = new StringBuilder();
        if (dbType.contains("VARCHAR")) {
            options.append("CHARACTER SET utf8 ");
        }
        if (colName.equals("id") || colName.equals(ODLID_DBCOL)) {
            if (dbType.equals(DEFAULTID_DBTYPE)) {
                options.append("NOT NULL AUTO_INCREMENT");
            } else {
                options.append("NOT NULL");
            }
        }
        return options.toString();
    }

    public static List<Method> getFilteredMethods(Class<?> clazz, boolean getterOrSetter) {
        Method[] methods = clazz.getMethods();
        List<Method> list = new ArrayList<>();
        for (Method method : methods) {
            if (getterOrSetter) {
                if (!isGetter(method)) {
                    continue;
                }
            } else {
                if (!isSetter(method)) {
                    continue;
                }
            }
            if (ignoreMethod(method, methods, getterOrSetter)) {
                continue;
            }
            list.add(method);
        }
        return list;
    }


    private static Map<Class<?>, String> initTypeMap() {
        Map<Class<?>, String> map = new HashMap<>();
        map.put(String.class, STRING_DBTYPE);
        map.put(Boolean.class, "BOOLEAN");
        map.put(Byte.class, "TINYINT");
        map.put(Integer.class, "INTEGER");
        map.put(Long.class, "BIGINT");
        map.put(BigInteger.class, "BIGINT");
        map.put(Uint8.class, "SMALLINT");
        map.put(Uint16.class, "INTEGER");
        map.put(Uint32.class, "BIGINT");
        map.put(Uint64.class, "BIGINT"); //????
        map.put(DateAndTime.class, "DATETIME(3)");
        return map;
    }

    private static boolean ignoreMethod(Method method, Method[] classMehtods, boolean getterOrSetter) {
        final String name = method.getName();
        if (name.equals("getAugmentations") || name.equals("getImplementedInterface")
                || name.equals("implementedInterface") || name.equals("getClass")) {
            return true;
        }
        for (Method cm : classMehtods) {
            if (!cm.equals(method) && cm.getName().equals(name)) {
                //resolve conflict
                return !resolveConflict(method, cm, getterOrSetter);
            }
            //silicon fix
            if (method.getReturnType().equals(Boolean.class) && getterOrSetter) {
                if (name.startsWith("get") && cm.getName().startsWith("is")
                        && cm.getName().endsWith(name.substring(3))) {
                    return true;
                }
            }
        }
        return false;
    }

    private static boolean resolveConflict(Method m1, Method m2, boolean getterOrSetter) {
        Class<?> p1 = getterOrSetter ? m1.getReturnType() : m1.getParameterTypes()[0];
        Class<?> p2 = getterOrSetter ? m2.getReturnType() : m2.getParameterTypes()[0];
        if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Map.class, List.class)) {
            return p1.isAssignableFrom(List.class); //prefer List setter
        } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint64.class, BigInteger.class)) {
            return p1.isAssignableFrom(Uint64.class);
        } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint32.class, Long.class)) {
            return p1.isAssignableFrom(Uint32.class);
        } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint16.class, Integer.class)) {
            return p1.isAssignableFrom(Uint16.class);
        } else if (YangToolsBuilderAnnotationIntrospector.isAssignable(p1, p2, Uint8.class, Short.class)) {
            return p1.isAssignableFrom(Uint8.class);
        }
        return false;
    }

    public static String getColumnName(Method method) {
        String camelName = (method.getName().startsWith("get") || method.getName().startsWith("set"))
                ? method.getName().substring(3)
                : method.getName().substring(2);
        return convertCamelToKebabCase(camelName);
    }

    private static String getDBType(Class<?> valueType) throws UnableToMapClassException {
        String type = mariaDBTypeMap.getOrDefault(valueType, null);
        if (type == null) {
            if (implementsInterface(valueType, DataObject.class) || implementsInterface(valueType, List.class)
                    || implementsInterface(valueType, Map.class)) {
                return "JSON";
            }
            if (implementsInterface(valueType, Enumeration.class)) {
                return ENUM_DBTYPE;
            }
            throw new UnableToMapClassException("no mapping for " + valueType.getName() + " found");
        }
        return type;
    }

    private static boolean implementsInterface(Class<?> valueType, Class<?> iftoImpl) {
        return iftoImpl.isAssignableFrom(valueType);
    }

    private static boolean isGetter(Method method) {
        return method.getName().startsWith("get") || method.getName().startsWith("is")
                || method.getName().startsWith("do");
    }

    private static boolean isSetter(Method method) {
        return method.getName().startsWith("set");
    }

    /**
     * @param input string in Camel Case
     * @return String in Kebab case Inspiration from KebabCaseStrategy class of com.fasterxml.jackson.databind with an
     *         additional condition to handle numbers as well Using QNAME would have been a more fool proof solution,
     *         however it can lead to performance problems due to usage of Java reflection
     */
    private static String convertCamelToKebabCase(String input) {
        if (input == null)
            return input; // garbage in, garbage out
        int length = input.length();
        if (length == 0) {
            return input;
        }

        StringBuilder result = new StringBuilder(length + (length >> 1));

        int upperCount = 0;

        for (int i = 0; i < length; ++i) {
            char ch = input.charAt(i);
            char lc = Character.toLowerCase(ch);

            if (lc == ch) { // lower-case letter means we can get new word
                // but need to check for multi-letter upper-case (acronym), where assumption
                // is that the last upper-case char is start of a new word
                if ((upperCount > 1)) {
                    // so insert hyphen before the last character now
                    result.insert(result.length() - 1, '-');
                } else if ((upperCount == 1) && Character.isDigit(ch) && i != length - 1) {
                    result.append('-');
                }
                upperCount = 0;
            } else {
                // Otherwise starts new word, unless beginning of string
                if ((upperCount == 0) && (i > 0)) {
                    result.append('-');
                }
                ++upperCount;
            }
            result.append(lc);
        }
        return result.toString();
    }

    public static class UnableToMapClassException extends Exception {

        private static final long serialVersionUID = 1L;

        public UnableToMapClassException(String message) {
            super(message);
        }

    }

    public static String escape(Object o) {
        return escape(o.toString());
    }

    public static String escape(String o) {
        return o.replace("'", "\'");
    }

    public static boolean isComplex(Class<?> valueType) {
        return DataObject.class.isAssignableFrom(valueType) || List.class.isAssignableFrom(valueType);
    }

    public static Object getNumericValue(Object value, Class<?> valueType) {
        if (valueType.equals(Byte.class) || valueType.equals(Integer.class) || valueType.equals(Long.class)) {
            return value;
        }
        if (valueType.equals(Uint8.class) || valueType.equals(Uint16.class) || valueType.equals(Uint32.class)
                || valueType.equals(Uint64.class)) {
            return ((Number) value).longValue();
        }
        return value;
    }

    public static Object bool2int(Object invoke) {
        return Boolean.TRUE.equals(invoke) ? 1 : 0;
    }

    public static boolean isBoolean(Class<?> valueType) {
        return valueType.equals(Boolean.class);
    }

    public static boolean isNumeric(Class<?> valueType) {
        return numericClasses.contains(valueType);

    }

    private static boolean isDateTime(Class<?> valueType) {
        return valueType.equals(DateAndTime.class);
    }

    private static boolean isYangEnum(Class<?> valueType) {
        return YangToolsMapperHelper.implementsInterface(valueType, Enumeration.class);
    }

    public static <T extends DataObject> List<T> read(CachedRowSet data, Class<T> clazz)
            throws JsonMappingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
            InstantiationException, SecurityException, NoSuchMethodException, JsonProcessingException, SQLException {
        return read(data, clazz, null);
    }

    public static <T extends DataObject> List<T> read(ResultSet data, Class<T> clazz)
            throws JsonMappingException, IllegalAccessException, IllegalArgumentException, InvocationTargetException,
            InstantiationException, SecurityException, NoSuchMethodException, JsonProcessingException, SQLException {
        return read(data, clazz, null);
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> read(CachedRowSet data, Class<T> clazz, String column) throws IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException,
            SecurityException, NoSuchMethodException, JsonMappingException, JsonProcessingException {

        List<T> list = new ArrayList<>();
        while (data.next()) {
            if (column == null) {
                Builder<T> builder = findPOJOBuilder(clazz);
                Class<?> argType;
                String col;
                for (Method m : getFilteredMethods(builder.getClass(), false)) {
                    argType = m.getParameterTypes()[0];
                    col = getColumnName(m);
                    m.setAccessible(true);
                    m.invoke(builder, getValueOrDefault(data, col, argType, null));
                }
                list.add(builder.build());
            } else {
                Object value = getValueOrDefault(data, column, clazz, null);
                if (value != null) {
                    list.add((T) value);
                }
            }
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> read(ResultSet data, Class<T> clazz, String column) throws IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, SQLException, InstantiationException,
            SecurityException, NoSuchMethodException, JsonMappingException, JsonProcessingException {

        List<T> list = new ArrayList<>();
        while (data.next()) {
            if (column == null) {
                Builder<T> builder = findPOJOBuilder(clazz);
                Class<?> argType;
                String col;
                for (Method m : getFilteredMethods(builder.getClass(), false)) {
                    argType = m.getParameterTypes()[0];
                    col = getColumnName(m);
                    m.setAccessible(true);
                    m.invoke(builder, getValueOrDefault(data, col, argType, null));
                }
                list.add(builder.build());
            } else {
                Object value = getValueOrDefault(data, column, clazz, null);
                if (value != null) {
                    list.add((T) value);
                }
            }
        }
        return list;
    }

    @SuppressWarnings("unchecked")
    private static <T> Builder<T> findPOJOBuilder(Class<T> ac) throws InstantiationException, IllegalAccessException,
            IllegalArgumentException, InvocationTargetException, SecurityException, NoSuchMethodException {
        try {
            String builder = null;

            if (ac.isInterface()) {
                String clsName = ac.getName();
                if (clsName.endsWith("Entity")) {
                    clsName = clsName.substring(0, clsName.length() - 6);
                }
                builder = clsName + "Builder";
            }
            if (builder != null) {
                Class<?> innerBuilder = YangToolsMapperHelper.findClass(builder);
                Class<Builder<T>> builderClass = (Class<Builder<T>>) innerBuilder;
                return builderClass.getDeclaredConstructor().newInstance();
            }
        } catch (ClassNotFoundException e) {

        }
        return null;
    }

    private static Object getValueOrDefault(CachedRowSet data, String col, Class<?> dstType, Object defaultValue)
            throws SQLException, JsonMappingException, JsonProcessingException {
        if (isBoolean(dstType)) {
            return data.getBoolean(col);
        } else if (isNumeric(dstType)) {
            return getNumeric(dstType, data.getLong(col));
        } else if (String.class.equals(dstType)) {
            return data.getString(col);
        } else if (isYangEnum(dstType)) {
            return getYangEnum(data.getString(col), dstType);
        } else if (isDateTime(dstType)) {
            String v = data.getString(col);
            return v == null || v.equals("null") ? null : DateAndTime.getDefaultInstance(v.replace(" ", "T") + "Z");
        } else if (isComplex(dstType)) {
            String v = data.getString(col);

            return (v == null || v.toLowerCase().equals("null")) ? null : mapper.readValue(v, dstType);
        }
        return defaultValue;
    }

    private static Object getValueOrDefault(ResultSet data, String col, Class<?> dstType, Object defaultValue)
            throws SQLException, JsonMappingException, JsonProcessingException {
        if (isBoolean(dstType)) {
            return data.getBoolean(col);
        } else if (isNumeric(dstType)) {
            return getNumeric(dstType, data.getLong(col));
        } else if (String.class.equals(dstType)) {
            return data.getString(col);
        } else if (isYangEnum(dstType)) {
            return getYangEnum(data.getString(col), dstType);
        } else if (isDateTime(dstType)) {
            String v = data.getString(col);
            return v == null || v.equals("null") ? null : DateAndTime.getDefaultInstance(v.replace(" ", "T") + "Z");
        } else if (isComplex(dstType)) {
            String v = data.getString(col);

            return (v == null || v.toLowerCase().equals("null")) ? null : mapper.readValue(v, dstType);
        }
        return defaultValue;
    }



    private static Object getYangEnum(String value, Class<?> dstType) {
        if (value == null || value.equals("null")) {
            return null;
        }
        try {
            return YangToolsDeserializerModifier.parseEnum(value, dstType);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
                | SecurityException e) {
            LOG.warn("unable to parse enum value '{}' to class {}: ", value, dstType, e);
        }
        return null;
    }

    private static Object getNumeric(Class<?> dstType, long value) {
        if (dstType.equals(Uint64.class)) {
            return Uint64.valueOf(value);
        } else if (dstType.equals(Uint32.class)) {
            return Uint32.valueOf(value);
        } else if (dstType.equals(Uint16.class)) {
            return Uint16.valueOf(value);
        } else if (dstType.equals(Uint16.class)) {
            return Uint8.valueOf(value);
        } else if (dstType.equals(Long.class)) {
            return Long.valueOf(value);
        } else if (dstType.equals(Integer.class)) {
            return Long.valueOf(value).intValue();
        } else if (dstType.equals(Byte.class)) {
            return Long.valueOf(value).byteValue();
        }
        return null;
    }

    public static DBKeyValuePair<String> getEscapedKeyValue(Method m, String col, Object value)
            throws JsonProcessingException {
        Class<?> valueType = m.getReturnType();
        String svalue = null;
        if (isBoolean(valueType)) {
            svalue = String.valueOf(bool2int(value));
        } else if (isNumeric(valueType)) {
            svalue = String.valueOf(getNumericValue(value, valueType));
        } else if (isDateTime(valueType)) {
            svalue = "'" + getDateTimeValue((DateAndTime) value) + "'";
        } else if (isComplex(valueType)) {
            svalue = "'" + escape(mapper.writeValueAsString(value)) + "'";
        } else {
            svalue = "'" + escape(value) + "'";
        }
        return new DBKeyValuePair<String>("`" + col + "`", svalue);
    }

    private static String getDateTimeValue(DateAndTime value) {
        String s = value.getValue();
        if (s.endsWith("Z")) {
            s = s.substring(0, s.length() - 1).replace("T", " ");
        } else if (s.contains("+")) {
            s = s.substring(0, s.indexOf("+")).replace("T", " ");
        }
        return s;
    }


}