Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / main / java / org / openecomp / dcae / apod / analytics / model / util / json / AnalyticsModelObjectMapperSupplier.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.model.util.json;
22
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;
35
36 import java.util.EnumSet;
37 import java.util.Set;
38
39 /**
40  *<p>
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
44  *     <br>
45  *     NOTE: This supplier also setups up {@link JsonPath} default
46  *     config to make use of this Supplier object mapper
47  *</p>
48  * @author Rajiv Singla . Creation Date: 11/10/2016.
49  */
50 @SuppressFBWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
51 public class AnalyticsModelObjectMapperSupplier implements Supplier<ObjectMapper> {
52
53     @Override
54     public ObjectMapper get() {
55
56         final ObjectMapper objectMapper = new ObjectMapper();
57
58         // Serialize null values
59         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
60
61         // Don't fail on unknown properties
62         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
63
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
71
72         // Setup JsonPath default config
73         setupJsonPathDefaultConfig(objectMapper);
74
75         return objectMapper;
76     }
77
78
79     /**
80      * Setups up default Config for {@link JsonPath}
81      *
82      * @param objectMapper Jackson object mapper
83      */
84     private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
85
86         Configuration.setDefaults(new Configuration.Defaults() {
87
88             private final JsonProvider jsonProvider = new JacksonJsonProvider(objectMapper);
89             private final MappingProvider mappingProvider = new JacksonMappingProvider(objectMapper);
90
91             @Override
92             public JsonProvider jsonProvider() {
93                 return jsonProvider;
94             }
95
96             @Override
97             public MappingProvider mappingProvider() {
98                 return mappingProvider;
99             }
100
101             @Override
102             public Set<Option> options() {
103
104                 // Json Path exceptions are suppressed, also missing properties are tolerated
105                 return EnumSet.of(Option.DEFAULT_PATH_LEAF_TO_NULL, Option.SUPPRESS_EXCEPTIONS);
106             }
107         });
108
109
110     }
111
112 }