added azure related code
[it/otf.git] / otf-camunda / src / main / java / org / oran / otf / spring / configuration / OTFMongoConfiguration.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.spring.configuration;\r
18 \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
30 \r
31 @Configuration\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
36 \r
37   @Value("${otf.mongo.username}")\r
38   private String username;\r
39 \r
40   @Value("${otf.mongo.password}")\r
41   private String password;\r
42 \r
43   @Value("${otf.mongo.replica-set}")\r
44   private String replicaSet;\r
45 \r
46   @Value("${otf.mongo.database}")\r
47   private String database;\r
48 \r
49   @Override\r
50   protected String getDatabaseName() {\r
51     return database;\r
52   }\r
53 \r
54   @Override\r
55   public MongoClient mongoClient() {\r
56     MongoCredential credential =\r
57         MongoCredential.createScramSha1Credential(username, database, password.toCharArray());\r
58 \r
59     MongoClientOptions options =\r
60        // MongoClientOptions.builder().sslEnabled(false).requiredReplicaSetName(replicaSet).build();\r
61     MongoClientOptions.builder().sslEnabled(true).build();\r
62 \r
63     String[] hostArray = hosts.split(",");\r
64     ArrayList<ServerAddress> hosts = new ArrayList<>();\r
65 \r
66     for (String host : hostArray) {\r
67       String[] hostSplit = host.split(":");\r
68       hosts.add(new ServerAddress(hostSplit[0], Integer.parseInt(hostSplit[1])));\r
69     }\r
70 \r
71     return new MongoClient(hosts, credential, options);\r
72   }\r
73 \r
74   @Override\r
75   public @Bean\r
76   MongoTemplate mongoTemplate() {\r
77     return new MongoTemplate(mongoClient(), database);\r
78   }\r
79 }\r