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