5c06130dc203465f9651cfdf3e44f37f7e45c1ff
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types;
23
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.InvocationTargetException;
26 import java.math.BigInteger;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SortOrder;
33
34 import org.opendaylight.yangtools.yang.binding.Key;
35 import org.opendaylight.yangtools.yang.binding.KeyAware;
36 import org.opendaylight.yangtools.yang.common.Uint16;
37 import org.opendaylight.yangtools.yang.common.Uint32;
38 import org.opendaylight.yangtools.yang.common.Uint64;
39
40 public class YangHelper2 {
41
42     private YangHelper2() {
43
44     }
45
46     public static @NonNull Uint64 getUint64(@NonNull BigInteger val) {
47         return Uint64.valueOf(val);
48     }
49
50     public static @NonNull Uint64 getUint64(@NonNull Uint64 val) {
51         return val;
52     }
53
54     public static @NonNull Uint32 getUint32(@NonNull Long val) {
55         return Uint32.valueOf(val);
56     }
57
58     public static @NonNull Uint32 getUint32(@NonNull Uint32 val) {
59         return val;
60     }
61
62     public static @NonNull Uint16 getUint16(@NonNull Integer val) {
63         return Uint16.valueOf(val);
64     }
65
66     public static @NonNull Uint16 getUint16(@NonNull Uint16 val) {
67         return val;
68     }
69
70     public static @NonNull Integer getInteger(@NonNull Integer val) {
71         return val;
72     }
73
74     public static @NonNull Integer getInteger(@NonNull Uint16 val) {
75         return val.intValue();
76     }
77
78     public static @NonNull Long getInteger(@NonNull Long val) {
79         return val;
80     }
81
82     public static @NonNull Long getInteger(@NonNull Uint32 val) {
83         return val.longValue();
84     }
85     /**
86      * Aluminium version
87      */
88     public static <K extends Key<T>,T extends KeyAware<K>> Map<K, T> getListOrMap(Class<K> clazz, List<T> list) {
89         Map<K,T> map = new HashMap<>();
90         for(T listelement:list) {
91             Constructor<K> constructor;
92             try {
93                 constructor = clazz.getConstructor(clazz);
94             clazz.getConstructors();
95             map.put(constructor.newInstance(listelement.key()), listelement);
96             } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
97                 throw new IllegalArgumentException("Can not create map element.", e);
98             }
99         }
100         return map;
101     }
102     public static <K extends Key<T>,T extends KeyAware<K>> Map<K, T> getListOrMap(Class<K> clazz, T listElement) {
103         return getListOrMap(clazz, Arrays.asList(listElement) );
104     }
105     public static Uint32 getLongOrUint32(long longVal) {
106         return Uint32.valueOf(longVal);
107     }
108     public static Uint32 getLongOrUint32(Long longVal) {
109         return Uint32.valueOf(longVal);
110     }
111     public static Uint64 getBigIntegerOrUint64(BigInteger value) {
112         return Uint64.valueOf(value);
113     }
114     public static Class<?> getScalarTypeObjectClass() {
115         return org.opendaylight.yangtools.yang.binding.ScalarTypeObject.class;
116     }
117
118     public static SortOrder getSortOrder(org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder sortOrder){
119         return sortOrder== org.onap.ccsdk.features.sdnr.wt.common.database.queries.SortOrder.ASCENDING?
120                 SortOrder.Ascending:SortOrder.Descending;
121     }
122
123 }