diff options
author | Michael DÜrre <michael.duerre@highstreet-technologies.com> | 2022-03-10 10:39:16 +0100 |
---|---|---|
committer | Dan Timoney <dtimoney@att.com> | 2022-03-23 15:08:48 -0400 |
commit | eb2a7c97c0007b013bd1784ac17d57be02b63d03 (patch) | |
tree | b0861438532727219216cfd426e21ae053abb31a /sdnr/wt/common-yang/utils/src/main | |
parent | 3c74361514bb920ed441d105f66ca978bd074ced (diff) |
migrate sdnr features to phosphorus
fix mapper and switch to dom api
Updated to use phosphorus version of parent poms
Issue-ID: CCSDK-3566
Signed-off-by: Michael DÜrre <michael.duerre@highstreet-technologies.com>
Change-Id: I98c5bef9286622e0d66b53db687557d798cd53f5
Signed-off-by: Michael DÜrre <michael.duerre@highstreet-technologies.com>
Diffstat (limited to 'sdnr/wt/common-yang/utils/src/main')
6 files changed, 323 insertions, 101 deletions
diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapper.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapper.java index f406ea97d..7d73afae0 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapper.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapper.java @@ -22,11 +22,13 @@ package org.onap.ccsdk.features.sdnr.wt.yang.mapper; import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.KeyDeserializer; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsBuilderAnnotationIntrospector; import org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions.YangToolsModule; import org.slf4j.Logger; @@ -44,7 +46,7 @@ public class YangToolsMapper extends ObjectMapper { private final YangToolsBuilderAnnotationIntrospector annotationIntrospector; private final YangToolsModule module; private static final long serialVersionUID = 1L; - + private boolean isModuleRegistered=false; public YangToolsMapper() { this(new YangToolsBuilderAnnotationIntrospector()); } @@ -54,13 +56,12 @@ public class YangToolsMapper extends ObjectMapper { this.annotationIntrospector = yangToolsBuilderAnnotationIntrospector; this.module = new YangToolsModule(); + this.registerModule(this.module); configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE); + setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE); setSerializationInclusion(Include.NON_NULL); enable(MapperFeature.USE_GETTERS_AS_SETTERS); setAnnotationIntrospector(yangToolsBuilderAnnotationIntrospector); - registerModule(this.module); - } public void addDeserializer(Class<?> clsToDeserialize, String builderClassName) { this.annotationIntrospector.addDeserializer(clsToDeserialize, builderClassName); @@ -69,5 +70,21 @@ public class YangToolsMapper extends ObjectMapper { public void addKeyDeserializer(Class<?> type, KeyDeserializer deserializer) { this.module.addKeyDeserializer(type, deserializer); } + @Override + public <T> T readValue(String content, Class<T> valueType) throws JsonProcessingException, JsonMappingException { + if(!this.isModuleRegistered) { + this.registerModule(this.module); + this.isModuleRegistered=true; + } + return super.readValue(content, valueType); + } + @Override + public String writeValueAsString(Object value) throws JsonProcessingException { + if(!this.isModuleRegistered) { + this.registerModule(this.module); + this.isModuleRegistered=true; + } + return super.writeValueAsString(value); + } } diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java index d468e075c..fe7631a6d 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/YangToolsMapperHelper.java @@ -22,6 +22,8 @@ package org.onap.ccsdk.features.sdnr.wt.yang.mapper; import com.fasterxml.jackson.databind.DeserializationContext; +import com.google.common.collect.Maps; + import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -32,6 +34,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import javax.annotation.Nullable; @@ -40,6 +43,8 @@ import org.opendaylight.mdsal.dom.api.DOMNotification; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime; import org.opendaylight.yangtools.concepts.Builder; import org.opendaylight.yangtools.yang.binding.EventInstantAware; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; import org.opendaylight.yangtools.yang.binding.Notification; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -278,12 +283,6 @@ public class YangToolsMapperHelper { return notification instanceof DOMEvent; } - /** - * Get time instant from notification if available or default - * @param notification - * @param defaultValue - * @return DateAndTime - */ public static DateAndTime getTime(Notification notification, Instant defaultValue) { Instant time; if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware @@ -296,21 +295,6 @@ public class YangToolsMapperHelper { return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); } - /** - * Get time instant from notification if available or actual time - * @param notification - * @return DateAndTime - */ - public static DateAndTime getTime(Notification notification) { - return getTime(notification, Instant.now()); - } - - /** - * Get time instant from DOM notification if available or default - * @param DOM notification - * @param defaultValue - * @return DateAndTime - */ public static DateAndTime getTime(DOMNotification notification, Instant defaultValue) { Instant time; if (hasTime(notification)) { // If notification class extends/implements the EventInstantAware @@ -322,13 +306,9 @@ public class YangToolsMapperHelper { } return DateAndTime.getDefaultInstance(ZonedDateTime.ofInstant(time, ZoneOffset.UTC).format(formatterOutput)); } - - /** - * Get time instant from notification if available or actual time - * @param DOM notification - * @return DateAndTime - */ - public static DateAndTime getTime(DOMNotification notification) { - return getTime(notification, Instant.now()); + + + public static <K extends Identifier<V>, V extends Identifiable<K>> Map<K,V> toMap(List<V> list) { + return list == null || list.isEmpty() ? null : Maps.uniqueIndex(list, Identifiable::key); } } diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java index 58e75bde5..0fe8ab9d7 100644 --- a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier.java @@ -29,6 +29,9 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.KeyDeserializer; import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; +import com.fasterxml.jackson.databind.type.ArrayType; +import com.fasterxml.jackson.databind.type.MapType; + import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -48,80 +51,87 @@ import org.slf4j.LoggerFactory; public class YangToolsDeserializerModifier extends BeanDeserializerModifier { - private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class); - private static final String getEnumMethodName = "valueOf"; - private static final String getEnumMethodName2 = "forName"; + private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier.class); + private static final String getEnumMethodName = "valueOf"; + private static final String getEnumMethodName2 = "forName"; + + @SuppressWarnings("unchecked") + public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + try { + Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class); + Enum<?> result = (Enum<?>) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | NoSuchElementException | SecurityException e) { + Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class); + Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result.orElseThrow(); + } + } - @SuppressWarnings("unchecked") - public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException, - IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { - try { - Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class); - Enum<?> result = (Enum<?>) method.invoke(null, value); - LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); - return result; - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | NoSuchElementException | SecurityException e) { - Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class); - Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value); - LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); - return result.orElseThrow(); - } - } + @Override + public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, + BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { + return new JsonDeserializer<Enum<?>>() { - @Override - public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, - BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { - return new JsonDeserializer<Enum<?>>() { + @Override + public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + Class<?> clazz = type.getRawClass(); - @Override - public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { - Class<?> clazz = type.getRawClass(); + try { + return parseEnum(jp.getValueAsString(), clazz); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | NoSuchElementException | SecurityException e) { + LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(), + jp.getValueAsString(), e); + } + throw new IOException( + "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString()); + } + }; + } - try { - return parseEnum(jp.getValueAsString(), clazz); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | NoSuchElementException | SecurityException e) { - LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(), - jp.getValueAsString(), e); - } - throw new IOException( - "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString()); - } - }; - } + @Override + public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, + JsonDeserializer<?> deserializer) { + final JavaType type = beanDesc.getType(); + final Class<?> rawClass = type.getRawClass(); - @Override - public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, - JsonDeserializer<?> deserializer) { - final JavaType type = beanDesc.getType(); - final Class<?> rawClass = type.getRawClass(); + JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer); - JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer); + if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) { + deser = new TypeObjectDeserializer<TypeObject>(type, deser); + } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) { + deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser); + } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) { + deser = new BaseIdentityDeserializer<BaseIdentity>(deser); + } else if (rawClass.equals(Class.class)) { + deser = new ClassDeserializer(rawClass); + } - if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) { - deser = new TypeObjectDeserializer<TypeObject>(type, deser); - } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) { - deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser); - } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) { - deser = new BaseIdentityDeserializer<BaseIdentity>(deser); - } else if (rawClass.equals(Class.class)) { - deser = new ClassDeserializer(rawClass); - } + LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName()); + return deser; + } - LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName()); - return deser; - } + @Override + public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type, + BeanDescription beanDesc, JsonDeserializer<?> deserializer) { + final Class<?> rawClass = type.getBindings().getBoundType(1).getRawClass(); + return new YangtoolsMapDesirializer(rawClass); + } - @Override - public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) { - KeyDeserializer res; - if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) { - res = new IdentifierDeserializer(); - } else { - res = super.modifyKeyDeserializer(config, type, deser); - } - LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName()); - return res; - } + @Override + public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) { + KeyDeserializer res; + if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) { + res = new IdentifierDeserializer(); + } else { + res = super.modifyKeyDeserializer(config, type, deser); + } + LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName()); + return res; + } } diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier2.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier2.java new file mode 100644 index 000000000..0697f5f2e --- /dev/null +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangToolsDeserializerModifier2.java @@ -0,0 +1,135 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.DeserializationConfig; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.KeyDeserializer; +import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; +import com.fasterxml.jackson.databind.type.MapType; +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.NoSuchElementException; +import java.util.Optional; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.BaseIdentityDeserializer; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.ClassDeserializer; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.IdentifierDeserializer; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize.TypeObjectDeserializer; +import org.opendaylight.yangtools.yang.binding.BaseIdentity; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.ScalarTypeObject; +import org.opendaylight.yangtools.yang.binding.TypeObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class YangToolsDeserializerModifier2 extends BeanDeserializerModifier { + + private static final Logger LOG = LoggerFactory.getLogger(YangToolsDeserializerModifier2.class); + private static final String getEnumMethodName = "valueOf"; + private static final String getEnumMethodName2 = "forName"; + + @SuppressWarnings("unchecked") + public static Enum<?> parseEnum(String value, Class<?> clazz) throws IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + try { + Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class); + Enum<?> result = (Enum<?>) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result; + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException + | NoSuchElementException | SecurityException e) { + Method method = clazz.getDeclaredMethod(getEnumMethodName2, String.class); + Optional<Enum<?>> result = (Optional<Enum<?>>) method.invoke(null, value); + LOG.debug("Deserialize '{}' with class '{}' to '{}'", value, clazz.getName(), result); + return result.orElseThrow(); + } + } + + @Override + public JsonDeserializer<Enum<?>> modifyEnumDeserializer(DeserializationConfig config, final JavaType type, + BeanDescription beanDesc, final JsonDeserializer<?> deserializer) { + return new JsonDeserializer<Enum<?>>() { + + @Override + public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + Class<?> clazz = type.getRawClass(); + + try { + return parseEnum(jp.getValueAsString(), clazz); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | NoSuchElementException | SecurityException e) { + LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(), + jp.getValueAsString(), e); + } + throw new IOException( + "unable to parse enum (" + type.getRawClass() + ")for value " + jp.getValueAsString()); + } + }; + } + + @Override + public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, + JsonDeserializer<?> deserializer) { + final JavaType type = beanDesc.getType(); + final Class<?> rawClass = type.getRawClass(); + + JsonDeserializer<?> deser = super.modifyDeserializer(config, beanDesc, deserializer); + + if (YangToolsMapperHelper.implementsInterface(rawClass, TypeObject.class)) { + deser = new TypeObjectDeserializer<TypeObject>(type, deser); + } else if (YangToolsMapperHelper.implementsInterface(rawClass, ScalarTypeObject.class)) { + deser = new TypeObjectDeserializer<ScalarTypeObject<?>>(type, deser); + } else if (YangToolsMapperHelper.implementsInterface(rawClass, BaseIdentity.class)) { + deser = new BaseIdentityDeserializer<BaseIdentity>(deser); + } else if (rawClass.equals(Class.class)) { + deser = new ClassDeserializer(rawClass); + } + + LOG.debug("Deserialize '{}' with deserializer '{}'", rawClass.getName(), deser.getClass().getName()); + return deser; + } + + @Override + public JsonDeserializer<?> modifyMapDeserializer(DeserializationConfig config, MapType type, + BeanDescription beanDesc, JsonDeserializer<?> deserializer) { + final Class<?> rawClass = type.getBindings().getBoundType(1).getRawClass(); + return new YangtoolsMapDesirializer(rawClass); + } + + @Override + public KeyDeserializer modifyKeyDeserializer(DeserializationConfig config, JavaType type, KeyDeserializer deser) { + KeyDeserializer res; + if (YangToolsMapperHelper.implementsInterface(type.getRawClass(), Identifier.class)) { + res = new IdentifierDeserializer(); + } else { + res = super.modifyKeyDeserializer(config, type, deser); + } + LOG.debug("Keydeserialize '{}' with deserializer '{}'", type.getRawClass().getName(), res.getClass().getName()); + return res; + } +} diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer.java new file mode 100644 index 000000000..2fe7fa11f --- /dev/null +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer.java @@ -0,0 +1,44 @@ +package org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.core.TreeNode; +import com.fasterxml.jackson.databind.BeanDescription; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.type.CollectionLikeType; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.google.common.collect.Maps; + +public class YangtoolsMapDesirializer<K extends Identifier<V>, V extends Identifiable<K>> + extends JsonDeserializer<Map<K, V>> { + + private final Class<V> clazz; + private final YangToolsMapper mapper; + + public YangtoolsMapDesirializer(Class<V> clazz) { + super(); + this.clazz = clazz; + this.mapper = new YangToolsMapper(); + } + + @Override + public Map<K, V> deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException, JsonProcessingException { + CollectionLikeType type = ctxt.getTypeFactory().constructCollectionType(List.class, clazz); + List<V> list = mapper.readValue(p,type); + return YangToolsMapperHelper.toMap(list); + } + +} diff --git a/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer2.java b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer2.java new file mode 100644 index 000000000..6a419eee1 --- /dev/null +++ b/sdnr/wt/common-yang/utils/src/main/java/org/onap/ccsdk/features/sdnr/wt/yang/mapper/mapperextensions/YangtoolsMapDesirializer2.java @@ -0,0 +1,36 @@ +package org.onap.ccsdk.features.sdnr.wt.yang.mapper.mapperextensions; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.type.CollectionLikeType; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapper; +import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; + +public class YangtoolsMapDesirializer2<K extends Identifier<V>, V extends Identifiable<K>> + extends JsonDeserializer<Map<K, V>> { + + private final Class<V> clazz; + private final YangToolsMapper mapper; + + public YangtoolsMapDesirializer2(Class<V> clazz) { + super(); + this.clazz = clazz; + this.mapper = new YangToolsMapper(); + } + + @Override + public Map<K, V> deserialize(JsonParser p, DeserializationContext ctxt) + throws IOException, JsonProcessingException { + CollectionLikeType type = ctxt.getTypeFactory().constructCollectionType(List.class, clazz); + List<V> list = mapper.readValue(p,type); + return YangToolsMapperHelper.toMap(list); + } + +} |