TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / main / java / org / onap / 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.onap.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         // Register TCA Controller App Config Module
71         objectMapper.registerModule(new TCAControllerConfigModule());
72
73
74         // Setup JsonPath default config
75         setupJsonPathDefaultConfig(objectMapper);
76
77         return objectMapper;
78     }
79
80
81     /**
82      * Setups up default Config for {@link JsonPath}
83      *
84      * @param objectMapper Jackson object mapper
85      */
86     private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
87
88         Configuration.setDefaults(new Configuration.Defaults() {
89
90             private final JsonProvider jsonProvider = new JacksonJsonProvider(objectMapper);
91             private final MappingProvider mappingProvider = new JacksonMappingProvider(objectMapper);
92
93             @Override
94             public JsonProvider jsonProvider() {
95                 return jsonProvider;
96             }
97
98             @Override
99             public MappingProvider mappingProvider() {
100                 return mappingProvider;
101             }
102
103             @Override
104             public Set<Option> options() {
105
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);
109             }
110         });
111
112
113     }
114
115 }