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