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