2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.yangtools.serialize;
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;
36 public class IdentifierDeserializer extends KeyDeserializer {
38 private static final Logger LOG = LoggerFactory.getLogger(IdentifierDeserializer.class);
40 public IdentifierDeserializer() {}
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) {
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));
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));
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);