0837b8a19993d97fccc4c381a6e0700b00a429d9
[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.yangtools.serialize;
23
24 import com.fasterxml.jackson.databind.DeserializationContext;
25 import com.fasterxml.jackson.databind.KeyDeserializer;
26 import java.io.IOException;
27 import java.lang.reflect.InvocationTargetException;
28 import java.util.List;
29 import org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools.YangToolsMapperHelper;
30 import org.opendaylight.yangtools.yang.common.Uint16;
31 import org.opendaylight.yangtools.yang.common.Uint32;
32 import org.opendaylight.yangtools.yang.common.Uint64;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class IdentifierDeserializer extends KeyDeserializer {
37
38     private static final Logger LOG = LoggerFactory.getLogger(IdentifierDeserializer.class);
39
40     public IdentifierDeserializer() {}
41
42     @Override
43     public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException {
44         Class<?> clazz = ctxt.getClass();
45         final String arg = key;
46         LOG.debug("Deserialization for key:{}",key);
47         // find constructor argument types
48         List<Class<?>> ctypes = YangToolsMapperHelper.getConstructorParameterTypes(clazz, String.class);
49         for (Class<?> ctype : ctypes) {
50             try {
51                 if (ctype.equals(String.class)) {
52                     return clazz.getConstructor(ctype).newInstance(arg);
53                 } else if (ctype.equals(Uint16.class)) {
54                     return clazz.getConstructor(ctype).newInstance(Uint16.valueOf(arg));
55
56                 } else if (ctype.equals(Uint32.class)) {
57                     return clazz.getConstructor(ctype).newInstance(Uint32.valueOf(arg));
58                 } else if (ctype.equals(Uint64.class)) {
59                     return clazz.getConstructor(ctype).newInstance(Uint64.valueOf(arg));
60                 }
61             } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
62                     | InvocationTargetException | NoSuchMethodException | SecurityException e) {
63                 LOG.warn("unable to instantiate class {} with arg {}: ", clazz, arg, e);
64                 throw new IllegalArgumentException(
65                         "unable to instantiate class " + clazz.getName() + " with arg '" + arg + "' ", e);
66             }
67         }
68         return null;
69     }
70
71 }