migrate sdnr features to sulfur
[ccsdk/features.git] / sdnr / wt / common-yang / utils / src / main / java / org / onap / ccsdk / features / sdnr / wt / yang / mapper / mapperextensions / YangToolsDeserializerModifier.java
index c12d17e..b7f1782 100644 (file)
@@ -29,14 +29,19 @@ 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 java.util.Set;
+
 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.SetDeserializer;
 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;
@@ -48,7 +53,25 @@ 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 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,
@@ -60,10 +83,7 @@ public class YangToolsDeserializerModifier extends BeanDeserializerModifier {
                 Class<?> clazz = type.getRawClass();
 
                 try {
-                    Method method = clazz.getDeclaredMethod(getEnumMethodName, String.class);
-                    Enum<?> result = (Enum<?>) method.invoke(null, jp.getValueAsString());
-                    LOG.debug("Deserialize '{}' with class '{}' to '{}'", jp.getValueAsString(), clazz.getName(), result);
-                    return result;
+                    return parseEnum(jp.getValueAsString(), clazz);
                 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
                         | NoSuchMethodException | NoSuchElementException | SecurityException e) {
                     LOG.warn("problem deserializing enum for {} with value {}: {}", clazz.getName(),
@@ -91,12 +111,19 @@ public class YangToolsDeserializerModifier extends BeanDeserializerModifier {
             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;