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