Update the license for 2017-2018 license
[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 package org.onap.aai.dbmap;
21
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;
32
33 import java.io.File;
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;
39
40 import static com.thinkaurelius.titan.graphdb.configuration.GraphDatabaseConfiguration.*;
41
42 /**
43  * For building a config that TitanFactory.open can use with an identifiable graph.unique-instance-id
44  */
45 public class AAIGraphConfig {
46
47         private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIGraphConfig.class);
48
49         private AAIGraphConfig(){};
50
51         public PropertiesConfiguration getCc(String configPath, String graphType, String service) throws ConfigurationException, FileNotFoundException {
52
53                 PropertiesConfiguration cc = this.loadTitanPropFile(configPath);
54
55                 String uid = ManagementFactory.getRuntimeMXBean().getName() + "_" + service  + "_" + graphType + "_" + System.currentTimeMillis();
56                 for (char c : ConfigElement.ILLEGAL_CHARS) {
57                         uid = StringUtils.replaceChars(uid, c,'_');
58                 }
59
60                 cc.addProperty("graph.unique-instance-id", uid);
61
62                 return cc;
63         }
64
65
66         private PropertiesConfiguration loadTitanPropFile(String shortcutOrFile) throws ConfigurationException, FileNotFoundException {
67                 File file = new File(shortcutOrFile);
68                 if (file.exists()) {
69                         PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
70                         propertiesConfiguration.setAutoSave(false);
71                         propertiesConfiguration.load(shortcutOrFile);
72                         return propertiesConfiguration;
73                 } else {
74                         throw new FileNotFoundException(shortcutOrFile);
75                 }
76         }
77
78         public static class Builder {
79                 private String configPath;
80                 private String graphType;
81                 private String service;
82
83                 public Builder(String configPath) {
84                         this.configPath = configPath;
85                 }
86
87                 public Builder withGraphType(String graphType) {
88                         this.graphType = Objects.toString(graphType, "NA");
89                         return this;
90                 }
91
92                 public Builder forService(String service) {
93                         this.service = Objects.toString(service, "NA");
94                         return this;
95                 }
96
97                 public PropertiesConfiguration buildConfiguration() throws ConfigurationException, FileNotFoundException {
98                         return new AAIGraphConfig().getCc(this.configPath, this.graphType, this.service);
99                 }
100
101         }
102
103
104 }