2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.spi;
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;
36 public abstract class MapperConfigProvider<T> {
38 private static final Logger LOG = LoggerFactory.getLogger(MapperConfigProvider.class);
40 private static final ObjectMapper YAML_READER = new ObjectMapper(new YAMLFactory());
41 private ReloadingFileBasedConfigurationBuilder<YAMLConfiguration> builder;
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);
55 //Test initial configuration
56 builder.getConfiguration();
59 public abstract String getMappingFilePath();
62 * Provide VES Mapping configuration.
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());
71 public abstract Class<T> getClazz();