Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / snmp / manager / SnmpMappingConfigurationProvider.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.snmp.manager;
21
22 import static java.util.Objects.requireNonNull;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
26 import java.io.IOException;
27 import java.io.StringWriter;
28 import java.nio.file.Paths;
29 import javax.annotation.PostConstruct;
30 import org.apache.commons.configuration2.YAMLConfiguration;
31 import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
32 import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
33 import org.apache.commons.configuration2.builder.fluent.Parameters;
34 import org.apache.commons.configuration2.ex.ConfigurationException;
35 import org.o.ran.oam.nf.oam.adopter.snmp.manager.pojos.VesMappingConfiguration;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.stereotype.Service;
40
41 /**
42  * Reads and listens for changes on snmp mapping configuration.
43  */
44 @Service
45 public class SnmpMappingConfigurationProvider {
46     private static final Logger LOG = LoggerFactory.getLogger(SnmpMappingConfigurationProvider.class);
47
48     private static final ObjectMapper YAML_READER = new ObjectMapper(new YAMLFactory());
49     @Value("${snmp-manager.mapping-config-path:#{null}}")
50     private String mappingFilePath;
51     private ReloadingFileBasedConfigurationBuilder<YAMLConfiguration> builder;
52
53     /**
54      * Initialize service.
55      * @throws IOException on error
56      */
57     @PostConstruct
58     public void init() throws IOException, ConfigurationException {
59         requireNonNull(mappingFilePath);
60         final var filePath = Paths.get(mappingFilePath).toUri();
61         builder = new ReloadingFileBasedConfigurationBuilder<>(YAMLConfiguration.class)
62                 .configure(new Parameters().hierarchical().setURL(filePath.toURL()));
63         builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, event -> {
64             builder.getReloadingController().checkForReloading(null);
65             LOG.info("Reloading {}", filePath);
66         });
67         //Test initial configuration
68         builder.getConfiguration();
69     }
70
71     /**
72      * Reads VesMappingConfiguration from yaml file.
73      *
74      * @return Ves Mapping Configuration
75      */
76     public VesMappingConfiguration getVesMappingConfiguration() throws ConfigurationException, IOException {
77         final YAMLConfiguration configuration = builder.getConfiguration();
78         final var output = new StringWriter();
79         configuration.write(output);
80         return YAML_READER.readValue(output.toString(), VesMappingConfiguration.class);
81     }
82 }