Commit c3ad227a authored by neelesh thakur's avatar neelesh thakur Committed by ethiraj krishnamanaidu
Browse files

remove mongo implementation from core

parent f58500d0
......@@ -18,15 +18,9 @@ import org.opengroup.osdu.azure.dependencies.AzureOSDUConfig;
import org.opengroup.osdu.register.provider.azure.di.AzureBootstrapConfig;
import org.opengroup.osdu.register.provider.azure.di.CosmosContainerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
@ComponentScan(value = {
"org.opengroup.osdu.register",
"org.opengroup.osdu.core",
......
package org.opengroup.osdu.register.provider.gcp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import javax.annotation.PostConstruct;
@SpringBootApplication(exclude = {
MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class
})
@ComponentScan({"org.opengroup.osdu"})
public class RegisterApplication {
@PostConstruct
......
......@@ -177,11 +177,6 @@
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
......
/*
* Copyright 2017-2020, Schlumberger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.register.ddms.persistence.mongo;
import com.mongodb.client.MongoCollection;
import org.opengroup.osdu.register.utils.AppServiceConfig;
import org.opengroup.osdu.register.utils.MongoClientHandler;
import org.bson.Document;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "PERSISTENCE", havingValue = "MONGODB")
public class MongoDdmsClient {
@Autowired
private AppServiceConfig config;
@Autowired
private MongoClientHandler mongoClientHandler;
@Autowired
private TenantInfo tenantInfo;
public MongoCollection<Document> getTenantRegisterMongoCollection() {
return mongoClientHandler.getMongoClient().getDatabase(config.getMongoDatabaseName()).getCollection(getMongoCollectionName("register"));
}
private String getMongoCollectionName(String serviceId) {
return String.format("collection-%s-%s", tenantInfo.getName(), serviceId).toLowerCase();
}
}
/*
* Copyright 2017-2020, Schlumberger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.register.ddms.persistence.mongo;
import com.google.gson.Gson;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.register.ddms.model.Ddms;
import org.opengroup.osdu.register.ddms.model.RegisteredInterface;
import org.opengroup.osdu.register.provider.interfaces.ddms.IDdmsRepository;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static com.mongodb.client.model.Filters.eq;
@Repository
@ConditionalOnProperty(name="PERSISTENCE", havingValue = "MONGODB")
public class MongoDdmsRepository implements IDdmsRepository {
private static final String ID = "id";
private static final String NAME = "name";
private static final String CREATED_ON_SECONDS = "createdDateTimeEpoch.seconds";
private static final String CREATED_ON_NANOS = "createdDateTimeEpoch.nanos";
private static final String CONTACT_EMAIL = "contactEmail";
private static final String DESCRIPTION = "description";
private static final String INTERFACES = "interfaces";
private static final String TYPE = "entityType";
private static final String SCHEMA = "schema";
@Autowired
private MongoDdmsClient mongoClient;
@Autowired
private JaxRsDpsLog log;
@Override
public Ddms create(Ddms spec) {
MongoCollection collection = this.mongoClient.getTenantRegisterMongoCollection();
FindIterable<Document> results = collection.find(eq("id", spec.getId()));
if (results != null && results.first() != null) {
throw new AppException(409, "Conflict", "A DDMS already exists with the same id");
}
collection.insertOne(Document.parse(new Gson().toJson(spec)));
return spec;
}
@Override
public Ddms get(String id) {
MongoCollection collection = this.mongoClient.getTenantRegisterMongoCollection();
Document record = (Document) collection.find(eq("id", id)).first();
if (record == null) {
throw new AppException(404, "Not found", String.format("DDMS with id %s does not exist.", id));
}
return new Gson().fromJson(record.toJson(), Ddms.class);
}
@Override
public List<Ddms> query(String type) {
List<Ddms> output = new ArrayList<>();
MongoCollection collection = this.mongoClient.getTenantRegisterMongoCollection();
FindIterable<Document> queryResults = collection.find(eq("interfaces.entityType", type), Document.class);
for (Document doc : queryResults) {
output.add(convertEntityToDocument(doc));
}
return output;
}
@Override
public boolean delete(String id) {
MongoCollection collection = this.mongoClient.getTenantRegisterMongoCollection();
DeleteResult deleteResult = collection.deleteOne(eq("id", id));
return deleteResult.wasAcknowledged() && deleteResult.getDeletedCount() == 1;
}
private Ddms convertEntityToDocument(Document doc) {
Ddms ddms = new Ddms();
try {
ddms.setId(doc.getString(ID));
ddms.setName(doc.getString(NAME));
ddms.setDescription(doc.getString(DESCRIPTION));
ddms.setContactEmail(doc.getString(CONTACT_EMAIL));
List<Document> dsInterfaces = doc.getList(INTERFACES, Document.class);
for (Document typeInterface : dsInterfaces) {
RegisteredInterface ri = new RegisteredInterface();
ri.setEntityType(typeInterface.getString(TYPE));
Map<String, Object> map = typeInterface.get((SCHEMA), Map.class);
ri.setSchema(map);
ddms.getInterfaces().add(ri);
}
} catch (ClassCastException e) {
this.log.error(String.format("error casting property, message: %s", e.getMessage()));
}
return ddms;
}
}
/*
* Copyright 2017-2020, Schlumberger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.opengroup.osdu.register.utils;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(name = "PERSISTENCE", havingValue = "MONGODB")
public class MongoClientHandler {
private static final String MONGO_PREFIX = "mongodb+srv://";
private static final String MONGO_OPTIONS = "retryWrites=true&w=majority";
private com.mongodb.client.MongoClient mongoClient = null;
@Autowired
private JaxRsDpsLog log;
@Value("${MONGO_SERVER}")
private String MONGO_SERVER;
@Value("${MONGO_USERNAME}")
private String MONGO_USERNAME;
@Value("${MONGO_PASSWORD}")
private String MONGO_PASSWORD;
private MongoClient getOrInitMongoClient() throws RuntimeException {
if (mongoClient != null) {
return mongoClient;
}
final String connectionString = String.format("%s%s:%s@%s?%s",
MONGO_PREFIX,
MONGO_USERNAME,
MONGO_PASSWORD,
MONGO_SERVER,
MONGO_OPTIONS);
ConnectionString connString = new ConnectionString(connectionString);
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connString)
.retryWrites(true)
.build();
try {
mongoClient = MongoClients.create(settings);
} catch (Exception ex) {
this.log.error("Error connecting MongoDB", ex);
throw new AppException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Error connecting MongoDB", ex.getMessage(), ex);
}
return mongoClient;
}
public MongoClient getMongoClient() {
if (mongoClient == null) {
getOrInitMongoClient();
}
return mongoClient;
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment