OAM NF Adopter REST PM
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-pm-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / pm / rest / manager / PerformanceManagementMapperConfigProvider.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.pm.rest.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.net.URI;
29 import java.nio.file.Paths;
30 import javax.annotation.PostConstruct;
31 import org.apache.commons.configuration2.YAMLConfiguration;
32 import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
33 import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
34 import org.apache.commons.configuration2.builder.fluent.Parameters;
35 import org.apache.commons.configuration2.event.EventListener;
36 import org.apache.commons.configuration2.ex.ConfigurationException;
37 import org.o.ran.oam.nf.oam.adopter.pm.rest.manager.pojos.VesMappingConfiguration;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.stereotype.Service;
43
44 @Service
45 public class PerformanceManagementMapperConfigProvider  {
46
47     private static final Logger LOG = LoggerFactory.getLogger(PerformanceManagementMapperConfigProvider.class);
48
49     private static final ObjectMapper YAML_READER = new ObjectMapper(new YAMLFactory());
50     @Value("${pm-rest-manager.mapping-config-path:#{null}}")
51     private String mappingFilePath;
52     private ReloadingFileBasedConfigurationBuilder<YAMLConfiguration> builder;
53
54     @Autowired
55     public PerformanceManagementMapperConfigProvider() {
56
57     }
58
59     /**
60      * Initialize Service.
61      */
62     @PostConstruct
63     public void init() throws IOException, ConfigurationException {
64         requireNonNull(mappingFilePath);
65         final URI filePath = Paths.get(mappingFilePath).toUri();
66         builder = new ReloadingFileBasedConfigurationBuilder<>(YAMLConfiguration.class)
67                           .configure(new Parameters().hierarchical().setURL(filePath.toURL()));
68         builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST, (EventListener) event -> {
69             builder.getReloadingController().checkForReloading(null);
70             LOG.debug("Reloading {}", filePath.toString());
71         });
72         //Test initial configuration
73         builder.getConfiguration();
74     }
75
76     /**
77      * Provide VES Mapping configuration.
78      */
79     public VesMappingConfiguration getVesMappingConfiguration() throws ConfigurationException, IOException {
80         final YAMLConfiguration configuration = builder.getConfiguration();
81         final StringWriter output = new StringWriter();
82         configuration.write(output);
83         return YAML_READER.readValue(output.toString(), VesMappingConfiguration.class);
84     }
85 }