2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============================LICENSE_END===========================================
21 package org.onap.dcae.apod.analytics.model.util.json;
23 import com.fasterxml.jackson.annotation.JsonInclude;
24 import com.fasterxml.jackson.databind.DeserializationFeature;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.base.Supplier;
27 import com.jayway.jsonpath.Configuration;
28 import com.jayway.jsonpath.JsonPath;
29 import com.jayway.jsonpath.Option;
30 import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
31 import com.jayway.jsonpath.spi.json.JsonProvider;
32 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
33 import com.jayway.jsonpath.spi.mapper.MappingProvider;
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36 import java.util.EnumSet;
41 * {@link Supplier} that can be used by clients to get Object Mapper which specializes
42 * in serialize and deserialize - DCAE Analytics Model JSON Objects. Clients can
43 * choose to memoize this Supplier for performance enhancements
45 * NOTE: This supplier also setups up {@link JsonPath} default
46 * config to make use of this Supplier object mapper
48 * @author Rajiv Singla . Creation Date: 11/10/2016.
50 @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
51 public class AnalyticsModelObjectMapperSupplier implements Supplier<ObjectMapper> {
54 public ObjectMapper get() {
56 final ObjectMapper objectMapper = new ObjectMapper();
58 // Serialize null values
59 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
61 // Don't fail on unknown properties
62 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
64 // Register Common Event Format Module
65 objectMapper.registerModule(new CommonEventFormatModule());
66 // Register TCA Policy Module
67 objectMapper.registerModule(new TCAPolicyModule());
68 // Register TCA Facade Module
69 objectMapper.registerModule(new TCAFacadeModelModule());
70 // Register TCA Controller App Config Module
71 objectMapper.registerModule(new TCAControllerConfigModule());
74 // Setup JsonPath default config
75 setupJsonPathDefaultConfig(objectMapper);
82 * Setups up default Config for {@link JsonPath}
84 * @param objectMapper Jackson object mapper
86 private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
88 Configuration.setDefaults(new Configuration.Defaults() {
90 private final JsonProvider jsonProvider = new JacksonJsonProvider(objectMapper);
91 private final MappingProvider mappingProvider = new JacksonMappingProvider(objectMapper);
94 public JsonProvider jsonProvider() {
99 public MappingProvider mappingProvider() {
100 return mappingProvider;
104 public Set<Option> options() {
106 // Json Path exceptions are suppressed, also missing properties are tolerated
107 return EnumSet.of(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS,
108 Option.ALWAYS_RETURN_LIST);