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.ArrayType;
33 import com.fasterxml.jackson.databind.type.MapType;
35 import java.io.IOException;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38 import java.util.NoSuchElementException;
39 import java.util.Optional;
40 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
41 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer;
42 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer;
43 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
44 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.TypeObjectDeserializer;
45 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
46 import org.opendaylight.yangtools.yang.binding.Identifier;
47 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
48 import org.opendaylight.yangtools.yang.binding.TypeObject;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class YangToolsDeserializerModifier extends BeanDeserializerModifier {
54 private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class);
55 private static final String getEnumMethodName = "valueOf";
56 private static final String getEnumMethodName2 = "forName";
58 @SuppressWarnings("unchecked")
59 public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException,
60 IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
62 Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class);
63 Enum<?> result = (Enum<?>) method.invoke(null, value);
64 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
66 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
67 | NoSuchElementException | SecurityException e) {
68 Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class);
69 Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value);
70 LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
71 return result.orElseThrow();
76 public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
77 BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
78 return new JsonDeserializer<Enum<?>>() {
81 public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
82 Class<?> clazz = type.getRawClass();
85 return parseEnum(jp.getValueAsString(), clazz);
86 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
87 | NoSuchMethodException | NoSuchElementException | SecurityException e) {
88 LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(),
89 jp.getValueAsString(), e);
91 throw new IOException(
92 "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString());
98 public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
99 JsonDeserializer<?> deserializer) {
100 final JavaType type = beanDesc.getType();
101 final Class<?> rawClass = type.getRawClass();
103 JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer);
105 if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) {
106 deser = new TypeObjectDeserializer<TypeObject>(type, deser);
107 } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) {
108 deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser);
109 } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) {
110 deser = new BaseIdentityDeserializer<BaseIdentity>(deser);
111 } else if (rawClass.equals(Class.class)) {
112 deser = new ClassDeserializer(rawClass);
115 LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName());
120 public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type,
121 BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
122 final Class<?> rawClass = type.getBindings().getBoundType(1).getRawClass();
123 return new YangtoolsMapDesirializer(rawClass);
127 public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) {
129 if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) {
130 res = new IdentifierDeserializer();
132 res = super.modifyKeyDeserializer(config, type, deser);
134 LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName());