1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.api.config;
\r
19 import com.mongodb.MongoClient;
\r
20 import com.mongodb.MongoClientOptions;
\r
21 import com.mongodb.MongoCredential;
\r
22 import com.mongodb.ServerAddress;
\r
23 import java.util.ArrayList;
\r
24 import org.springframework.beans.factory.annotation.Value;
\r
25 import org.springframework.context.annotation.Bean;
\r
26 import org.springframework.context.annotation.Configuration;
\r
27 import org.springframework.context.annotation.Profile;
\r
28 import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
\r
29 import org.springframework.data.mongodb.core.MongoTemplate;
\r
30 import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
\r
33 @EnableMongoRepositories(basePackages = "org.oran.otf.common.repository")
\r
35 public class DataConfig extends AbstractMongoConfiguration {
\r
37 @Value("${otf.mongo.hosts}")
\r
38 private String hosts;
\r
40 @Value("${otf.mongo.username}")
\r
41 private String username;
\r
43 @Value("${otf.mongo.password}")
\r
44 private String password;
\r
46 @Value("${otf.mongo.replicaSet}")
\r
47 private String replicaSet;
\r
49 @Value("${otf.mongo.database}")
\r
50 private String database;
\r
52 public DataConfig() {}
\r
55 protected String getDatabaseName() {
\r
60 public MongoClient mongoClient() {
\r
61 MongoCredential credential =
\r
62 MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
\r
64 MongoClientOptions options =
\r
65 MongoClientOptions.builder().sslEnabled(true).build();
\r
67 String[] hostArray = hosts.split(",");
\r
68 ArrayList<ServerAddress> hosts = new ArrayList<>();
\r
70 for (String host : hostArray) {
\r
71 String[] hostSplit = host.split(":");
\r
72 hosts.add(new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1])));
\r
75 return new MongoClient(hosts, credential, options);
\r
80 public MongoTemplate mongoTemplate() {
\r
81 return new MongoTemplate(mongoClient(), database);
\r