Support for MariaDB
[ccsdk/features.git] / sdnr / wt / common-yang / utils / src / main / java / org / onap / ccsdk / features / sdnr / wt / yang / mapper / mapperextensions / YangToolsDeserializerModifier.java
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.yang.mapper.mapperextensions;
23
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 java.io.IOException;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.util.NoSuchElementException;
36 import java.util.Optional;
37 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
38 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer;
39 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer;
40 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer;
41 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.TypeObjectDeserializer;
42 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
43 import org.opendaylight.yangtools.yang.binding.Identifier;
44 import org.opendaylight.yangtools.yang.binding.ScalarTypeObject;
45 import org.opendaylight.yangtools.yang.binding.TypeObject;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class YangToolsDeserializerModifier extends BeanDeserializerModifier {
50
51     private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class);
52     private static final String getEnumMethodName = "valueOf";
53     private static final String getEnumMethodName2 = "forName";
54
55     @SuppressWarnings("unchecked")
56     public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException,
57             IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
58         try {
59             Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class);
60             Enum<?> result = (Enum<?>) method.invoke(null, value);
61             LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
62             return result;
63         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
64                 | NoSuchElementException | SecurityException e) {
65             Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class);
66             Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value);
67             LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result);
68             return result.orElseThrow();
69         }
70     }
71
72     @Override
73     public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type,
74             BeanDescription beanDesc, final JsonDeserializer<?> deserializer) {
75         return new JsonDeserializer<Enum<?>>() {
76
77             @Override
78             public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
79                 Class<?> clazz = type.getRawClass();
80
81                 try {
82                     return parseEnum(jp.getValueAsString(), clazz);
83                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
84                         | NoSuchMethodException | NoSuchElementException | SecurityException e) {
85                     LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(),
86                             jp.getValueAsString(), e);
87                 }
88                 throw new IOException(
89                         "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString());
90             }
91         };
92     }
93
94     @Override
95     public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
96             JsonDeserializer<?> deserializer) {
97         final JavaType type = beanDesc.getType();
98         final Class<?> rawClass = type.getRawClass();
99
100         JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer);
101
102         if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) {
103             deser = new TypeObjectDeserializer<TypeObject>(type, deser);
104         } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) {
105             deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser);
106         } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) {
107             deser = new BaseIdentityDeserializer<BaseIdentity>(deser);
108         } else if (rawClass.equals(Class.class)) {
109             deser = new ClassDeserializer(rawClass);
110         }
111
112         LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName());
113         return deser;
114     }
115
116     @Override
117     public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) {
118         KeyDeserializer res;
119         if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) {
120             res = new IdentifierDeserializer();
121         } else {
122             res = super.modifyKeyDeserializer(config, type, deser);
123         }
124         LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName());
125         return res;
126     }
127 }