2  * ================================================================================
 
   3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  16  * ============LICENSE_END=========================================================
 
  20 package org.onap.dcae.analytics.model.util.json;
 
  22 import com.fasterxml.jackson.annotation.JsonInclude;
 
  23 import com.fasterxml.jackson.databind.DeserializationFeature;
 
  24 import com.fasterxml.jackson.databind.ObjectMapper;
 
  25 import com.jayway.jsonpath.Configuration;
 
  26 import com.jayway.jsonpath.Option;
 
  27 import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
 
  28 import com.jayway.jsonpath.spi.json.JsonProvider;
 
  29 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
 
  30 import com.jayway.jsonpath.spi.mapper.MappingProvider;
 
  32 import java.util.EnumSet;
 
  34 import java.util.function.Supplier;
 
  36 import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModule;
 
  37 import org.onap.dcae.analytics.model.util.json.module.ConfigBindingServiceModule;
 
  38 import org.onap.dcae.analytics.model.util.json.module.DynamicPropertiesModule;
 
  41  * Base Object mapper supplies Jackson {@link ObjectMapper} that specializes in serialize and deserialize -
 
  42  * Analytics model objects. Various analytics components should inherit from this supplier and register
 
  43  * their custom modules
 
  45  * @author Rajiv Singla
 
  47 public abstract class BaseObjectMapperSupplier implements Supplier<ObjectMapper> {
 
  50      * Class that can used to configure Json Path configuration
 
  52     public static class JsonPathConfiguration implements Configuration.Defaults {
 
  54         private final JsonProvider jsonProvider;
 
  55         private final MappingProvider mappingProvider;
 
  56         private final Set<Option> options;
 
  58         private JsonPathConfiguration(final ObjectMapper objectMapper, final Set<Option> options) {
 
  59             jsonProvider = new JacksonJsonProvider(objectMapper);
 
  60             mappingProvider = new JacksonMappingProvider(objectMapper);
 
  61             this.options = options;
 
  66         public JsonProvider jsonProvider() {
 
  71         public Set<Option> options() {
 
  76         public MappingProvider mappingProvider() {
 
  77             return mappingProvider;
 
  81     public abstract void registerCustomModules(final ObjectMapper objectMapper);
 
  84     public ObjectMapper get() {
 
  86         final ObjectMapper objectMapper = new ObjectMapper();
 
  88         //  Ignore null values during serialization. Null values will not be included in serialized JSON object
 
  89         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
 
  90         // Don't fail on unknown properties
 
  91         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
 
  93         // register dynamic properties module
 
  94         objectMapper.registerModule(new DynamicPropertiesModule());
 
  95         // register config binding service module
 
  96         objectMapper.registerModule(new ConfigBindingServiceModule());
 
  97         // register common event format module
 
  98         objectMapper.registerModule(new CommonEventFormatModule());
 
 100         // register custom modules
 
 101         registerCustomModules(objectMapper);
 
 103         // Setup JsonPath default config
 
 104         setupJsonPathDefaultConfig(objectMapper);
 
 110      * Setups up default Config for Json Path
 
 112      * @param objectMapper Jackson object mapper
 
 114     private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
 
 116         Configuration.setDefaults(new JsonPathConfiguration(objectMapper, EnumSet.of(
 
 117                 Option.DEFAULT_PATH_LEAF_TO_NULL,  // missing properties are tolerated
 
 118                 Option.SUPPRESS_EXCEPTIONS, // Json Path exceptions are suppressed
 
 119                 Option.ALWAYS_RETURN_LIST // always return results as list