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.yang.mapper.mapperextensions;
24 import com.fasterxml.jackson.core.JsonParser;
25 import com.fasterxml.jackson.databind.BeanDescription;
26 import com.fasterxml.jackson.databind.DeserializationConfig;
27 import com.fasterxml.jackson.databind.DeserializationContext;
28 import com.fasterxml.jackson.databind.JavaType;
29 import com.fasterxml.jackson.databind.JsonDeserializer;
30 import com.fasterxml.jackson.databind.KeyDeserializer;
31 import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
32 import com.fasterxml.jackson.databind.type.MapType;
33 import java.io.IOException;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.util.NoSuchElementException;
37 import java.util.Optional;
38 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
39 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer;
40 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer;
41 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
42 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.TypeObjectDeserializer;
43 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
44 import org.opendaylight.yangtools.yang.binding.Identifier;
45 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
46 import org.opendaylight.yangtools.yang.binding.TypeObject;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class YangToolsDeserializerModifier2 extends BeanDeserializerModifier {
52 private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier2.class);
53 private static final String getEnumMethodName = "valueOf";
54 private static final String getEnumMethodName2 = "forName";
56 @SuppressWarnings("unchecked")
57 public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException,
58 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
60 Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class);
61 Enum<?> result = (Enum<?>) method.invoke(null, value);
62 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
64 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
65 | NoSuchElementException | SecurityException e) {
66 Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class);
67 Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value);
68 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
69 return result.orElseThrow();
74 public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
75 BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
76 return new JsonDeserializer<Enum<?>>() {
79 public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
80 Class<?> clazz = type.getRawClass();
83 return parseEnum(jp.getValueAsString(), clazz);
84 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
85 | NoSuchMethodException | NoSuchElementException | SecurityException e) {
86 LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(),
87 jp.getValueAsString(), e);
89 throw new IOException(
90 "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString());
96 public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
97 JsonDeserializer<?> deserializer) {
98 final JavaType type = beanDesc.getType();
99 final Class<?> rawClass = type.getRawClass();
101 JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer);
103 if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) {
104 deser = new TypeObjectDeserializer<TypeObject>(type, deser);
105 } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) {
106 deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser);
107 } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) {
108 deser = new BaseIdentityDeserializer<BaseIdentity>(deser);
109 } else if (rawClass.equals(Class.class)) {
110 deser = new ClassDeserializer(rawClass);
113 LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName());
118 public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type,
119 BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
120 final Class<?> rawClass = type.getBindings().getBoundType(1).getRawClass();
121 return new YangtoolsMapDesirializer(rawClass);
125 public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) {
127 if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) {
128 res = new IdentifierDeserializer();
130 res = super.modifyKeyDeserializer(config, type, deser);
132 LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName());