2  * ============LICENSE_START=======================================================
 
   3  * ONAP : ccsdk features
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
 
   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
 
  12  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.onap.ccsdk.features.sdnr.wt.yang.mapper.serialize;
 
  24 import com.fasterxml.jackson.core.JsonParser;
 
  25 import com.fasterxml.jackson.core.JsonProcessingException;
 
  26 import com.fasterxml.jackson.databind.DeserializationContext;
 
  27 import com.fasterxml.jackson.databind.JavaType;
 
  28 import com.fasterxml.jackson.databind.JsonDeserializer;
 
  29 import java.io.IOException;
 
  30 import java.lang.reflect.InvocationTargetException;
 
  31 import java.util.NoSuchElementException;
 
  32 import java.util.Optional;
 
  33 import org.onap.ccsdk.features.sdnr.wt.yang.mapper.YangToolsMapperHelper;
 
  34 import org.opendaylight.yangtools.concepts.Builder;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  38 public class TypeObjectDeserializer<T> extends JsonDeserializer<T> {
 
  40     private static final Logger LOG = LoggerFactory.getLogger(TypeObjectDeserializer.class);
 
  41     private final JavaType type;
 
  42     private final JsonDeserializer<?> deser;
 
  45     public TypeObjectDeserializer(final JavaType type, final JsonDeserializer<?> deser) {
 
  50     @SuppressWarnings("unchecked")
 
  52     public T deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
 
  54         Class<T> clazz = (Class<T>) type.getRawClass();
 
  55         final String arg = parser.getValueAsString();
 
  56         LOG.debug("Try to build arg:'{}' with class {}",arg, clazz);
 
  57         Optional<T> oRes = Optional.empty();
 
  59             //try get method for default instance
 
  60             if ((oRes = YangToolsMapperHelper.getDefaultInstance(clazz, arg)).isEmpty()) {
 
  61                 //try to find builder with getDefaultInstance method
 
  62                 Optional<Class<Builder<?>>> oBuilderClazz = YangToolsMapperHelper.findBuilderClassOptional(ctxt, clazz);
 
  63                 LOG.debug("Try builder class present:{}",oBuilderClazz.isPresent());
 
  64                 if (oBuilderClazz.isEmpty()
 
  65                         || ((oRes = YangToolsMapperHelper.getDefaultInstance(oBuilderClazz.get(), arg)).isEmpty())) {
 
  66                     //try to find constructor with string
 
  67                     LOG.debug("Try constructor");
 
  68                     if ((oRes = YangToolsMapperHelper.getInstanceByConstructor(clazz, arg)).isEmpty()) {
 
  69                         //forward to standard deserializer or throw if not available
 
  70                         LOG.debug("Try default deserializer");
 
  71                         oRes = Optional.of((T) deser.deserialize(parser, ctxt));
 
  75             LOG.debug("Deserialize string value:{} for class:{} success:{}", arg, clazz, oRes.isPresent());
 
  76         } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
 
  77                 | NoSuchElementException | SecurityException | InstantiationException e) {
 
  78             LOG.warn("problem deserializing {} with value {}: {}", clazz.getName(), arg, e);
 
  80         if (oRes.isPresent()) {
 
  83             throw new IllegalArgumentException("Could not find constructor for arg:'" + arg + "' and class: " + clazz);