423ebab0bba23f6f8e2d9617f7fc83f50e9e15e8
[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.yangtools.yang.binding.Identifiable;
33 import org.opendaylight.yangtools.yang.binding.Identifier;
34 import org.opendaylight.yangtools.yang.common.Uint16;
35 import org.opendaylight.yangtools.yang.common.Uint32;
36 import org.opendaylight.yangtools.yang.common.Uint64;
37
38 public class YangHelper2 {
39
40     private YangHelper2() {
41
42     }
43
44     public static @NonNull Uint64 getUint64(@NonNull BigInteger val) {
45         return Uint64.valueOf(val);
46     }
47
48     public static @NonNull Uint64 getUint64(@NonNull Uint64 val) {
49         return val;
50     }
51
52     public static @NonNull Uint32 getUint32(@NonNull Long val) {
53         return Uint32.valueOf(val);
54     }
55
56     public static @NonNull Uint32 getUint32(@NonNull Uint32 val) {
57         return val;
58     }
59
60     public static @NonNull Uint16 getUint16(@NonNull Integer val) {
61         return Uint16.valueOf(val);
62     }
63
64     public static @NonNull Uint16 getUint16(@NonNull Uint16 val) {
65         return val;
66     }
67
68     public static @NonNull Integer getInteger(@NonNull Integer val) {
69         return val;
70     }
71
72     public static @NonNull Integer getInteger(@NonNull Uint16 val) {
73         return val.intValue();
74     }
75
76     public static @NonNull Long getInteger(@NonNull Long val) {
77         return val;
78     }
79
80     public static @NonNull Long getInteger(@NonNull Uint32 val) {
81         return val.longValue();
82     }
83     /**
84      * Aluminium version
85      */
86     public static <K extends Identifier<T>,T extends Identifiable<K>> Map<K, T> getListOrMap(Class<K> clazz, List<T> list) {
87         Map<K,T> map = new HashMap<>();
88         for(T listelement:list) {
89             Constructor<K> constructor;
90             try {
91                 constructor = clazz.getConstructor(clazz);
92             clazz.getConstructors();
93             map.put(constructor.newInstance(listelement.key()), listelement);
94             } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
95                 throw new IllegalArgumentException("Can not create map element.", e);
96             }
97         }
98         return map;
99     }
100     public static <K extends Identifier<T>,T extends Identifiable<K>> Map<K, T> getListOrMap(Class<K> clazz, T listElement) {
101         return getListOrMap(clazz, Arrays.asList(listElement) );
102     }
103     public static Uint32 getLongOrUint32(long longVal) {
104         return Uint32.valueOf(longVal);
105     }
106     public static Uint32 getLongOrUint32(Long longVal) {
107         return Uint32.valueOf(longVal);
108     }
109     public static Uint64 getBigIntegerOrUint64(BigInteger value) {
110         return Uint64.valueOf(value);
111     }
112     public static Class<?> getScalarTypeObjectClass() {
113         return org.opendaylight.yangtools.yang.binding.ScalarTypeObject.class;
114     }
115
116 }