Enhancements for the aai-common library
[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-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
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
21 package org.onap.aai.dbmap;
22
23 import static org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.*;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import com.google.common.base.Preconditions;
28 import com.google.common.base.Predicate;
29 import com.google.common.collect.Iterators;
30
31 import java.io.File;
32 import java.io.FileNotFoundException;
33 import java.lang.management.ManagementFactory;
34 import java.util.Iterator;
35 import java.util.Objects;
36 import java.util.regex.Pattern;
37
38 import org.apache.commons.configuration.ConfigurationException;
39 import org.apache.commons.configuration.PropertiesConfiguration;
40 import org.apache.commons.lang.StringUtils;
41 import org.janusgraph.diskstorage.configuration.ConfigElement;
42 import org.janusgraph.diskstorage.configuration.backend.CommonsConfiguration;
43
44 /**
45  * For building a config that JanusGraphFactory.open can use with an identifiable graph.unique-instance-id
46  */
47 public class AAIGraphConfig {
48
49     private static final Logger logger = LoggerFactory.getLogger(AAIGraphConfig.class);
50
51     private AAIGraphConfig() {
52     };
53
54     public PropertiesConfiguration getCc(String configPath, String graphType, String service)
55             throws ConfigurationException, FileNotFoundException {
56
57         PropertiesConfiguration cc = this.loadJanusGraphPropFile(configPath);
58
59         String uid = ManagementFactory.getRuntimeMXBean().getName() + "_" + service + "_" + graphType + "_"
60                 + System.currentTimeMillis();
61         for (char c : ConfigElement.ILLEGAL_CHARS) {
62             uid = StringUtils.replaceChars(uid, c, '_');
63         }
64
65         cc.addProperty("graph.unique-instance-id", uid);
66
67         return cc;
68     }
69
70     private PropertiesConfiguration loadJanusGraphPropFile(String shortcutOrFile)
71             throws ConfigurationException, FileNotFoundException {
72         File file = new File(shortcutOrFile);
73         if (file.exists()) {
74             PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
75             propertiesConfiguration.setAutoSave(false);
76             propertiesConfiguration.load(shortcutOrFile);
77             return propertiesConfiguration;
78         } else {
79             throw new FileNotFoundException(shortcutOrFile);
80         }
81     }
82
83     public static class Builder {
84         private String configPath;
85         private String graphType;
86         private String service;
87
88         public Builder(String configPath) {
89             this.configPath = configPath;
90         }
91
92         public Builder withGraphType(String graphType) {
93             this.graphType = Objects.toString(graphType, "NA");
94             return this;
95         }
96
97         public Builder forService(String service) {
98             this.service = Objects.toString(service, "NA");
99             return this;
100         }
101
102         public PropertiesConfiguration buildConfiguration() throws ConfigurationException, FileNotFoundException {
103             return new AAIGraphConfig().getCc(this.configPath, this.graphType, this.service);
104         }
105
106     }
107
108 }