Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-model / src / main / java / org / onap / dcae / analytics / model / util / json / BaseObjectMapperSupplier.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.model.util.json;
21
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;
31
32 import java.util.EnumSet;
33 import java.util.Set;
34 import java.util.function.Supplier;
35
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;
39
40 /**
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
44  *
45  * @author Rajiv Singla
46  */
47 public abstract class BaseObjectMapperSupplier implements Supplier<ObjectMapper> {
48
49     /**
50      * Class that can used to configure Json Path configuration
51      */
52     public static class JsonPathConfiguration implements Configuration.Defaults {
53
54         private final JsonProvider jsonProvider;
55         private final MappingProvider mappingProvider;
56         private final Set<Option> options;
57
58         private JsonPathConfiguration(final ObjectMapper objectMapper, final Set<Option> options) {
59             jsonProvider = new JacksonJsonProvider(objectMapper);
60             mappingProvider = new JacksonMappingProvider(objectMapper);
61             this.options = options;
62         }
63
64
65         @Override
66         public JsonProvider jsonProvider() {
67             return jsonProvider;
68         }
69
70         @Override
71         public Set<Option> options() {
72             return options;
73         }
74
75         @Override
76         public MappingProvider mappingProvider() {
77             return mappingProvider;
78         }
79     }
80
81     public abstract void registerCustomModules(final ObjectMapper objectMapper);
82
83     @Override
84     public ObjectMapper get() {
85
86         final ObjectMapper objectMapper = new ObjectMapper();
87
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);
92
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());
99
100         // register custom modules
101         registerCustomModules(objectMapper);
102
103         // Setup JsonPath default config
104         setupJsonPathDefaultConfig(objectMapper);
105
106         return objectMapper;
107     }
108
109     /**
110      * Setups up default Config for Json Path
111      *
112      * @param objectMapper Jackson object mapper
113      */
114     private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
115
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
120                 ))
121         );
122
123     }
124
125 }