added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / config / DataConfig.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.api.config;\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.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
31 \r
32 @Configuration\r
33 @EnableMongoRepositories(basePackages = "org.oran.otf.common.repository")\r
34 @Profile("!test")\r
35 public class DataConfig extends AbstractMongoConfiguration {\r
36 \r
37   @Value("${otf.mongo.hosts}")\r
38   private String hosts;\r
39 \r
40   @Value("${otf.mongo.username}")\r
41   private String username;\r
42 \r
43   @Value("${otf.mongo.password}")\r
44   private String password;\r
45 \r
46   @Value("${otf.mongo.replicaSet}")\r
47   private String replicaSet;\r
48 \r
49   @Value("${otf.mongo.database}")\r
50   private String database;\r
51 \r
52   public DataConfig() {}\r
53 \r
54   @Override\r
55   protected String getDatabaseName() {\r
56     return database;\r
57   }\r
58 \r
59   @Override\r
60   public MongoClient mongoClient() {\r
61     MongoCredential credential =\r
62         MongoCredential.createScramSha1Credential(username, database, password.toCharArray());\r
63 \r
64     MongoClientOptions options =\r
65         MongoClientOptions.builder().sslEnabled(false).requiredReplicaSetName(replicaSet).build();\r
66 \r
67     String[] hostArray = hosts.split(",");\r
68     ArrayList<ServerAddress> hosts = new ArrayList<>();\r
69 \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
73     }\r
74 \r
75     return new MongoClient(hosts, credential, options);\r
76   }\r
77 \r
78   @Override\r
79   @Bean\r
80   public MongoTemplate mongoTemplate() {\r
81     return new MongoTemplate(mongoClient(), database);\r
82   }\r
83 }\r