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