Sync the latest code changes
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbmap / AAIGraphConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 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  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.dbmap;
24
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import com.google.common.base.Preconditions;
28 import com.google.common.base.Predicate;
29 import com.google.common.collect.Iterators;
30 import com.thinkaurelius.titan.diskstorage.configuration.ConfigElement;
31 import com.thinkaurelius.titan.diskstorage.configuration.backend.CommonsConfiguration;
32 import org.apache.commons.configuration.ConfigurationException;
33 import org.apache.commons.configuration.PropertiesConfiguration;
34 import org.apache.commons.lang.StringUtils;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.lang.management.ManagementFactory;
39 import java.util.Iterator;
40 import java.util.Objects;
41 import java.util.regex.Pattern;
42
43 import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.*;
44
45 /**
46  * For building a config that TitanFactory.open can use with an identifiable graph.unique-instance-id
47  */
48 public class AAIGraphConfig {
49
50         private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraphConfig.class);
51
52         private AAIGraphConfig(){};
53
54         public PropertiesConfiguration getCc(String configPath, String graphType, String service) throws ConfigurationException, FileNotFoundException {
55
56                 PropertiesConfiguration cc = this.loadTitanPropFile(configPath);
57
58                 String uid = ManagementFactory.getRuntimeMXBean().getName() + "_" + service  + "_" + graphType + "_" + System.currentTimeMillis();
59                 for (char c : ConfigElement.ILLEGAL_CHARS) {
60                         uid = StringUtils.replaceChars(uid, c,'_');
61                 }
62
63                 cc.addProperty("graph.unique-instance-id", uid);
64
65                 return cc;
66         }
67
68
69         private PropertiesConfiguration loadTitanPropFile(String shortcutOrFile) throws ConfigurationException, FileNotFoundException {
70                 File file = new File(shortcutOrFile);
71                 if (file.exists()) {
72                         PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
73                         propertiesConfiguration.setAutoSave(false);
74                         propertiesConfiguration.load(shortcutOrFile);
75                         return propertiesConfiguration;
76                 } else {
77                         throw new FileNotFoundException(shortcutOrFile);
78                 }
79         }
80
81         public static class Builder {
82                 private String configPath;
83                 private String graphType;
84                 private String service;
85
86                 public Builder(String configPath) {
87                         this.configPath = configPath;
88                 }
89
90                 public Builder withGraphType(String graphType) {
91                         this.graphType = Objects.toString(graphType, "NA");
92                         return this;
93                 }
94
95                 public Builder forService(String service) {
96                         this.service = Objects.toString(service, "NA");
97                         return this;
98                 }
99
100                 public PropertiesConfiguration buildConfiguration() throws ConfigurationException, FileNotFoundException {
101                         return new AAIGraphConfig().getCc(this.configPath, this.graphType, this.service);
102                 }
103
104         }
105
106
107 }