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;
39 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
40 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer;
41 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer;
42 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
43 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.TypeObjectDeserializer;
44 import org.opendaylight.yangtools.concepts.Identifier;
45 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
46 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
47 import org.opendaylight.yangtools.yang.binding.TypeObject;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class YangToolsDeserializerModifier extends BeanDeserializerModifier {
53 private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class);
54 private static final String getEnumMethodName = "valueOf";
55 private static final String getEnumMethodName2 = "forName";
57 @SuppressWarnings("unchecked")
58 public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException,
59 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
61 Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class);
62 Enum<?> result = (Enum<?>) method.invoke(null, value);
63 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
65 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
66 | NoSuchElementException | SecurityException e) {
67 Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class);
68 Optional<Enum<?>> result = Optional.ofNullable((Enum<?>) method.invoke(null, value));
69 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
70 return result.orElseThrow();
75 public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
76 BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
77 return new JsonDeserializer<Enum<?>>() {
80 public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
81 Class<?> clazz = type.getRawClass();
84 return parseEnum(jp.getValueAsString(), clazz);
85 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
86 | NoSuchMethodException | NoSuchElementException | SecurityException e) {
87 LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(),
88 jp.getValueAsString(), e);
90 throw new IOException(
91 "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString());
97 public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
98 JsonDeserializer<?> deserializer) {
99 final JavaType type = beanDesc.getType();
100 final Class<?> rawClass = type.getRawClass();
102 JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer);
104 if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) {
105 deser = new TypeObjectDeserializer<TypeObject>(type, deser);
106 } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) {
107 deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser);
108 } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) {
109 deser = new BaseIdentityDeserializer<BaseIdentity>(deser);
110 } else if (YangToolsMapperHelper.implementsInterface(rawClass, Identifier.class)) {
111 deser = new BaseIdentityDeserializer<Identifier>(deser);
112 } else if (rawClass.equals(Class.class)) {
113 deser = new ClassDeserializer(rawClass);
116 LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName());
121 public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type,
122 BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
123 final Class<?> rawClass = type.getBindings().getBoundType(1).getRawClass();
124 return new YangtoolsMapDesirializer(rawClass);
128 public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) {
130 if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) {
131 res = new IdentifierDeserializer();
133 res = super.modifyKeyDeserializer(config, type, deser);
135 LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName());