Support 7.2.1 VES in TCAGEN2
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-model / src / main / java / org / onap / dcae / analytics / model / util / json / BaseObjectMapperSupplier.java
1 /*
2  * =============LICENSE_START===================================================================
3  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4  * Copyright (c) 2022 Wipro Limited Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  * ============LICENSE_END=========================================================
18  *
19  */
20
21 package org.onap.dcae.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.jayway.jsonpath.Configuration;
27 import com.jayway.jsonpath.Option;
28 import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
29 import com.jayway.jsonpath.spi.json.JsonProvider;
30 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
31 import com.jayway.jsonpath.spi.mapper.MappingProvider;
32
33 import java.util.EnumSet;
34 import java.util.Set;
35 import java.util.function.Supplier;
36
37 import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModule;
38 import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModuleV7;
39 import org.onap.dcae.analytics.model.util.json.module.ConfigBindingServiceModule;
40 import org.onap.dcae.analytics.model.util.json.module.DynamicPropertiesModule;
41
42 /**
43  * Base Object mapper supplies Jackson {@link ObjectMapper} that specializes in serialize and deserialize -
44  * Analytics model objects. Various analytics components should inherit from this supplier and register
45  * their custom modules
46  *
47  * @author Rajiv Singla
48  */
49 public abstract class BaseObjectMapperSupplier implements Supplier<ObjectMapper> {
50
51     /**
52      * Class that can used to configure Json Path configuration
53      */
54     public static class JsonPathConfiguration implements Configuration.Defaults {
55
56         private final JsonProvider jsonProvider;
57         private final MappingProvider mappingProvider;
58         private final Set<Option> options;
59
60         private JsonPathConfiguration(final ObjectMapper objectMapper, final Set<Option> options) {
61             jsonProvider = new JacksonJsonProvider(objectMapper);
62             mappingProvider = new JacksonMappingProvider(objectMapper);
63             this.options = options;
64         }
65
66
67         @Override
68         public JsonProvider jsonProvider() {
69             return jsonProvider;
70         }
71
72         @Override
73         public Set<Option> options() {
74             return options;
75         }
76
77         @Override
78         public MappingProvider mappingProvider() {
79             return mappingProvider;
80         }
81     }
82
83     public abstract void registerCustomModules(final ObjectMapper objectMapper);
84
85     @Override
86     public ObjectMapper get() {
87
88         final ObjectMapper objectMapper = new ObjectMapper();
89
90         //  Ignore null values during serialization. Null values will not be included in serialized JSON object
91         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
92         // Don't fail on unknown properties
93         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
94
95         // register dynamic properties module
96         objectMapper.registerModule(new DynamicPropertiesModule());
97         // register config binding service module
98         objectMapper.registerModule(new ConfigBindingServiceModule());
99         // register common event format module
100         objectMapper.registerModule(new CommonEventFormatModule());
101         // register common event format module for v7
102         objectMapper.registerModule(new CommonEventFormatModuleV7());
103
104         // register custom modules
105         registerCustomModules(objectMapper);
106
107         // Setup JsonPath default config
108         setupJsonPathDefaultConfig(objectMapper);
109
110         return objectMapper;
111     }
112
113     /**
114      * Setups up default Config for Json Path
115      *
116      * @param objectMapper Jackson object mapper
117      */
118     private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {
119
120         Configuration.setDefaults(new JsonPathConfiguration(objectMapper, EnumSet.of(
121                 Option.DEFAULT_PATH_LEAF_TO_NULL,  // missing properties are tolerated
122                 Option.SUPPRESS_EXCEPTIONS, // Json Path exceptions are suppressed
123                 Option.ALWAYS_RETURN_LIST // always return results as list
124                 ))
125         );
126
127     }
128
129 }