Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-api / src / main / java / org / o / ran / oam / nf / oam / adopter / spi / MapperConfigProvider.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 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  *        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 package org.o.ran.oam.nf.oam.adopter.spi;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
24 import java.io.IOException;
25 import java.io.StringWriter;
26 import java.nio.file.Paths;
27 import javax.annotation.PostConstruct;
28 import org.apache.commons.configuration2.YAMLConfiguration;
29 import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
30 import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
31 import org.apache.commons.configuration2.builder.fluent.Parameters;
32 import org.apache.commons.configuration2.ex.ConfigurationException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public abstract class MapperConfigProvider<T> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(MapperConfigProvider.class);
39
40     private static final ObjectMapper YAML_READER = new ObjectMapper(new YAMLFactory());
41     private ReloadingFileBasedConfigurationBuilder<YAMLConfiguration> builder;
42
43     /**
44      * Initialize Service.
45      */
46     @PostConstruct
47     public final void init() throws IOException, ConfigurationException {
48         final var filePath = Paths.get(getMappingFilePath()).toUri();
49         builder = new ReloadingFileBasedConfigurationBuilder<>(YAMLConfiguration.class)
50                           .configure(new Parameters().hierarchical().setURL(filePath.toURL()));
51         builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, event -> {
52             builder.getReloadingController().checkForReloading(null);
53             LOG.debug("Reloading {}", filePath);
54         });
55         //Test initial configuration
56         builder.getConfiguration();
57     }
58
59     public abstract String getMappingFilePath();
60
61     /**
62      * Provide VES Mapping configuration.
63      */
64     public final T getVesMappingConfiguration() throws ConfigurationException, IOException {
65         final YAMLConfiguration configuration = builder.getConfiguration();
66         final var output = new StringWriter();
67         configuration.write(output);
68         return YAML_READER.readValue(output.toString(), getClazz());
69     }
70
71     public abstract Class<T> getClazz();
72 }