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.spring.configuration;
\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.data.mongodb.config.AbstractMongoConfiguration;
\r
28 import org.springframework.data.mongodb.core.MongoTemplate;
\r
29 import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
\r
32 @EnableMongoRepositories(basePackages = "org.oran.otf.common.repository")
\r
33 public class OTFMongoConfiguration extends AbstractMongoConfiguration {
\r
34 @Value("${otf.mongo.hosts}")
\r
35 private String hosts;
\r
37 @Value("${otf.mongo.username}")
\r
38 private String username;
\r
40 @Value("${otf.mongo.password}")
\r
41 private String password;
\r
43 @Value("${otf.mongo.replica-set}")
\r
44 private String replicaSet;
\r
46 @Value("${otf.mongo.database}")
\r
47 private String database;
\r
50 protected String getDatabaseName() {
\r
55 public MongoClient mongoClient() {
\r
56 MongoCredential credential =
\r
57 MongoCredential.createScramSha1Credential(username, database, password.toCharArray());
\r
59 MongoClientOptions options =
\r
60 MongoClientOptions.builder().sslEnabled(false).requiredReplicaSetName(replicaSet).build();
\r
62 String[] hostArray = hosts.split(",");
\r
63 ArrayList<ServerAddress> hosts = new ArrayList<>();
\r
65 for (String host : hostArray) {
\r
66 String[] hostSplit = host.split(":");
\r
67 hosts.add(new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1])));
\r
70 return new MongoClient(hosts, credential, options);
\r
75 MongoTemplate mongoTemplate() {
\r
76 return new MongoTemplate(mongoClient(), database);
\r