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