summaryrefslogtreecommitdiffstats
path: root/dcae-analytics/dcae-analytics-model/src/main/java/org/onap/dcae/analytics/model/util/json/BaseObjectMapperSupplier.java
blob: 18e82621cd536db1109ab16d7fda9cc451ee3f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * ================================================================================
 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */

package org.onap.dcae.analytics.model.util.json;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;

import java.util.EnumSet;
import java.util.Set;
import java.util.function.Supplier;

import org.onap.dcae.analytics.model.util.json.module.CommonEventFormatModule;
import org.onap.dcae.analytics.model.util.json.module.ConfigBindingServiceModule;
import org.onap.dcae.analytics.model.util.json.module.DynamicPropertiesModule;

/**
 * Base Object mapper supplies Jackson {@link ObjectMapper} that specializes in serialize and deserialize -
 * Analytics model objects. Various analytics components should inherit from this supplier and register
 * their custom modules
 *
 * @author Rajiv Singla
 */
public abstract class BaseObjectMapperSupplier implements Supplier<ObjectMapper> {

    /**
     * Class that can used to configure Json Path configuration
     */
    public static class JsonPathConfiguration implements Configuration.Defaults {

        private final JsonProvider jsonProvider;
        private final MappingProvider mappingProvider;
        private final Set<Option> options;

        private JsonPathConfiguration(final ObjectMapper objectMapper, final Set<Option> options) {
            jsonProvider = new JacksonJsonProvider(objectMapper);
            mappingProvider = new JacksonMappingProvider(objectMapper);
            this.options = options;
        }


        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public Set<Option> options() {
            return options;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }
    }

    public abstract void registerCustomModules(final ObjectMapper objectMapper);

    @Override
    public ObjectMapper get() {

        final ObjectMapper objectMapper = new ObjectMapper();

        //  Ignore null values during serialization. Null values will not be included in serialized JSON object
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // Don't fail on unknown properties
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        // register dynamic properties module
        objectMapper.registerModule(new DynamicPropertiesModule());
        // register config binding service module
        objectMapper.registerModule(new ConfigBindingServiceModule());
        // register common event format module
        objectMapper.registerModule(new CommonEventFormatModule());

        // register custom modules
        registerCustomModules(objectMapper);

        // Setup JsonPath default config
        setupJsonPathDefaultConfig(objectMapper);

        return objectMapper;
    }

    /**
     * Setups up default Config for Json Path
     *
     * @param objectMapper Jackson object mapper
     */
    private void setupJsonPathDefaultConfig(final ObjectMapper objectMapper) {

        Configuration.setDefaults(new JsonPathConfiguration(objectMapper, EnumSet.of(
                Option.DEFAULT_PATH_LEAF_TO_NULL,  // missing properties are tolerated
                Option.SUPPRESS_EXCEPTIONS, // Json Path exceptions are suppressed
                Option.ALWAYS_RETURN_LIST // always return results as list
                ))
        );

    }

}