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=========================================================
20 package org.onap.aai.dbmap;
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import com.google.common.base.Preconditions;
25 import com.google.common.base.Predicate;
26 import com.google.common.collect.Iterators;
27 import com.thinkaurelius.titan.diskstorage.configuration.ConfigElement;
28 import com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration;
29 import org.apache.commons.configuration.ConfigurationException;
30 import org.apache.commons.configuration.PropertiesConfiguration;
31 import org.apache.commons.lang.StringUtils;
34 import java.io.FileNotFoundException;
35 import java.lang.management.ManagementFactory;
36 import java.util.Iterator;
37 import java.util.Objects;
38 import java.util.regex.Pattern;
40 import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.*;
43 * For building a config that TitanFactory.open can use with an identifiable graph.unique-instance-id
45 public class AAIGraphConfig {
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraphConfig.class);
49 private AAIGraphConfig(){};
51 public PropertiesConfiguration getCc(String configPath, String graphType, String service) throws ConfigurationException, FileNotFoundException {
53 PropertiesConfiguration cc = this.loadTitanPropFile(configPath);
55 String uid = ManagementFactory.getRuntimeMXBean().getName() + "_" + service + "_" + graphType + "_" + System.currentTimeMillis();
56 for (char c : ConfigElement.ILLEGAL_CHARS) {
57 uid = StringUtils.replaceChars(uid, c,'_');
60 cc.addProperty("graph.unique-instance-id", uid);
66 private PropertiesConfiguration loadTitanPropFile(String shortcutOrFile) throws ConfigurationException, FileNotFoundException {
67 File file = new File(shortcutOrFile);
69 PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
70 propertiesConfiguration.setAutoSave(false);
71 propertiesConfiguration.load(shortcutOrFile);
72 return propertiesConfiguration;
74 throw new FileNotFoundException(shortcutOrFile);
78 public static class Builder {
79 private String configPath;
80 private String graphType;
81 private String service;
83 public Builder(String configPath) {
84 this.configPath = configPath;
87 public Builder withGraphType(String graphType) {
88 this.graphType = Objects.toString(graphType, "NA");
92 public Builder forService(String service) {
93 this.service = Objects.toString(service, "NA");
97 public PropertiesConfiguration buildConfiguration() throws ConfigurationException, FileNotFoundException {
98 return new AAIGraphConfig().getCc(this.configPath, this.graphType, this.service);