2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.dbmap;
24 import java.io.FileNotFoundException;
25 import java.lang.management.ManagementFactory;
26 import java.util.Objects;
28 import org.apache.commons.configuration.ConfigurationException;
29 import org.apache.commons.configuration.PropertiesConfiguration;
30 import org.apache.commons.lang3.StringUtils;
31 import org.janusgraph.diskstorage.configuration.ConfigElement;
34 * For building a config that JanusGraphFactory.open can use with an identifiable graph.unique-instance-id
36 public class AAIGraphConfig {
38 private AAIGraphConfig() {
42 public PropertiesConfiguration getCc(String configPath, String graphType, String service)
43 throws ConfigurationException, FileNotFoundException {
45 PropertiesConfiguration cc = this.loadJanusGraphPropFile(configPath);
47 String uid = ManagementFactory.getRuntimeMXBean().getName() + "_" + service + "_" + graphType + "_"
48 + System.currentTimeMillis();
49 for (char c : ConfigElement.ILLEGAL_CHARS) {
50 uid = StringUtils.replaceChars(uid, c, '_');
53 cc.addProperty("graph.unique-instance-id", uid);
58 private PropertiesConfiguration loadJanusGraphPropFile(String shortcutOrFile)
59 throws ConfigurationException, FileNotFoundException {
60 File file = new File(shortcutOrFile);
62 PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
63 propertiesConfiguration.setAutoSave(false);
64 propertiesConfiguration.load(shortcutOrFile);
65 return propertiesConfiguration;
67 throw new FileNotFoundException(shortcutOrFile);
71 public static class Builder {
72 private String configPath;
73 private String graphType;
74 private String service;
76 public Builder(String configPath) {
77 this.configPath = configPath;
80 public Builder withGraphType(String graphType) {
81 this.graphType = Objects.toString(graphType, "NA");
85 public Builder forService(String service) {
86 this.service = Objects.toString(service, "NA");
90 public PropertiesConfiguration buildConfiguration() throws ConfigurationException, FileNotFoundException {
91 return new AAIGraphConfig().getCc(this.configPath, this.graphType, this.service);