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