Commit df255c6a authored by Nikita Ruzhevsky's avatar Nikita Ruzhevsky
Browse files

starting to split groups and users to different docs

parent 63b584e6
Pipeline #62866 failed with stages
in 1 minute and 38 seconds
......@@ -15,19 +15,19 @@ List of services supported:
<dependency>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>storage</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</dependency>
<!-- or -->
<dependency>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>legal</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</dependency>
<!-- or -->
<dependency>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>entitlements</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</dependency>
```
- Add following to your `application.properties` file:
......
......@@ -7,7 +7,7 @@
<parent>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>os-core-lib-mongodb</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</parent>
<artifactId>entitlements</artifactId>
......
package org.opengroup.osdu.core.mongodb.entitlements.converter;
import org.opengroup.osdu.core.mongodb.entitlements.entity.GroupDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.UserDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.NodeIdDoc;
import org.opengroup.osdu.core.mongodb.entitlements.model.EntityNode;
import org.springframework.stereotype.Component;
/**
* This class doesn't use traditional converters from spring core, it is used for manual translation of classes with
* no automatic
*/
@Component
public class NodeConverter {
public GroupDoc toGroupDoc(EntityNode node) {
GroupDoc result = new GroupDoc();
NodeIdDoc id = new NodeIdDoc(node.getNodeId(), node.getDataPartitionId());
result.setId(id);
result.setAppIds(node.getAppIds());
result.setDescription(node.getDescription());
result.setName(node.getName());
return result;
}
public UserDoc toUserDoc(EntityNode node) {
UserDoc result = new UserDoc();
NodeIdDoc id = new NodeIdDoc(node.getNodeId(), node.getDataPartitionId());
result.setId(id);
result.setDescription(node.getDescription());
result.setName(node.getName());
return result;
}
}
package org.opengroup.osdu.core.mongodb.entitlements.entity;
import lombok.Data;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.NodeIdDoc;
import org.springframework.data.annotation.Id;
import java.util.HashSet;
import java.util.Set;
@Data
public class BaseDoc {
@Id
private NodeIdDoc id;
private String name;
private String description = "";
private Set<String> parents = new HashSet<>();
}
package org.opengroup.osdu.core.mongodb.entitlements.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.RelationDoc;
import java.util.HashSet;
import java.util.Set;
@EqualsAndHashCode(callSuper = true)
@Data
public class GroupDoc extends BaseDoc {
private Set<String> appIds = new HashSet<>();
private Set<RelationDoc> children = new HashSet<>();
}
package org.opengroup.osdu.core.mongodb.entitlements.entity;
import lombok.Data;
import lombok.EqualsAndHashCode;
@EqualsAndHashCode(callSuper = true)
@Data
public class UserDoc extends BaseDoc {
}
package org.opengroup.osdu.core.mongodb.entitlements.helper;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeConverter;
import org.opengroup.osdu.core.mongodb.entitlements.entity.NodeDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.NodeIdDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.RelationDoc;
......@@ -12,6 +13,7 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Collection;
......@@ -26,20 +28,24 @@ import static org.opengroup.osdu.core.mongodb.entitlements.repo.BasicEntitlement
/**
* Common-used operations with nodes
*/
@Service
public class EntitlementsNodeHelper {
protected static final Class<NodeDoc> _CLASS = BasicEntitlementsHelper.CLASS;
private final BasicMongoDBHelper helper;
private final ConversionService conversionService;
private final NodeConverter nodeConverter;
public EntitlementsNodeHelper(BasicMongoDBHelper helper, ConversionService conversionService) {
public EntitlementsNodeHelper(BasicMongoDBHelper helper, ConversionService conversionService, NodeConverter nodeConverter) {
this.helper = helper;
this.conversionService = conversionService;
this.nodeConverter = nodeConverter;
}
/**
* Get a corresponding to EntityNode document for the database
*
* @param groupNode corresponding node
* @return node document is it is present in the database; or null.
*/
......@@ -49,7 +55,8 @@ public class EntitlementsNodeHelper {
/**
* Get a NodeDocument
* @param id nodeId
*
* @param id nodeId
* @param partitionId tenant
* @return node document or null
*/
......@@ -59,6 +66,7 @@ public class EntitlementsNodeHelper {
/**
* Get a NodeDocument
*
* @param id composite identifier entity of document
* @return node document or null
*/
......@@ -68,7 +76,8 @@ public class EntitlementsNodeHelper {
/**
* Get list of the nodes. If the node is not present in DB, nothing is put in the response.
* @param nodeIds id's of nodes
*
* @param nodeIds id's of nodes
* @param partitionId tenant
* @return list of nodes. empty list if all the id's are invalid
*/
......@@ -84,6 +93,7 @@ public class EntitlementsNodeHelper {
/**
* Get all the nodes with nodeId match, ignoring tenant
*
* @param nodeIds nodeId's
* @return list of documents
*/
......@@ -96,7 +106,8 @@ public class EntitlementsNodeHelper {
/**
* Get a map of id's and corresponding node documents
* @param nodeIds nodeId's
*
* @param nodeIds nodeId's
* @param partitionId tenant
* @return map of id to document. if all the id's are invalid - map will be empty.
*/
......@@ -107,9 +118,10 @@ public class EntitlementsNodeHelper {
/**
* Create a relation between the parent and a child with a role property
*
* @param parent parent node
* @param child child node
* @param role relation role
* @param child child node
* @param role relation role
*/
public void createRelation(NodeDoc parent, NodeDoc child, Role role) {
parent = getOrInsertNode(parent);
......@@ -137,12 +149,13 @@ public class EntitlementsNodeHelper {
/**
* Get a node or insert one to database
*
* @param userNode node to get result for or to insert. not null.
* @return nodeDocument
*/
public NodeDoc getOrInsertNode(EntityNode userNode) {
if (userNode == null) {
throw ExceptionGenerator.groupIsNull();
throw ExceptionGenerator.nodeIsNull();
}
NodeDoc document = conversionService.convert(userNode, NodeDoc.class);
if (document == null) {
......@@ -153,6 +166,7 @@ public class EntitlementsNodeHelper {
/**
* Get a node or insert one to database
*
* @param document node to get result for or to insert. not null.
* @return nodeDocument
*/
......
package org.opengroup.osdu.core.mongodb.entitlements.helper;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeConverter;
import org.opengroup.osdu.core.mongodb.entitlements.entity.BaseDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.GroupDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.UserDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.NodeIdDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.RelationDoc;
import org.opengroup.osdu.core.mongodb.entitlements.model.EntityNode;
import org.opengroup.osdu.core.mongodb.entitlements.model.Role;
import org.opengroup.osdu.core.mongodb.entitlements.util.ExceptionGenerator;
import org.opengroup.osdu.core.mongodb.helper.BasicMongoDBHelper;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.opengroup.osdu.core.mongodb.entitlements.repo.BasicEntitlementsHelper.*;
@Service
public class NextGenNodeHelper {
private final BasicMongoDBHelper helper;
private final NodeConverter nodeConverter;
public NextGenNodeHelper(BasicMongoDBHelper helper, NodeConverter nodeConverter) {
this.helper = helper;
this.nodeConverter = nodeConverter;
}
public GroupDoc getGroup(NodeIdDoc id) {
return helper.findOne(Query.query(Criteria.where(ID).is(id)), GroupDoc.class);
}
public UserDoc getUser(NodeIdDoc id) {
return helper.findOne(Query.query(Criteria.where(ID).is(id)), UserDoc.class);
}
public BaseDoc getGeneralNode(NodeIdDoc id) {
CompletableFuture<GroupDoc> group = CompletableFuture.supplyAsync(() -> helper.get(ID, id, GroupDoc.class));
CompletableFuture<UserDoc> user = CompletableFuture.supplyAsync(() -> helper.get(ID, id, UserDoc.class));
try {
BaseDoc result = group.get();
if (result != null) {
return result;
}
return user.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return null;
}
}
public void createRelation(BaseDoc parent, BaseDoc child, Role role) {
if (parent instanceof UserDoc) {
throw new IllegalArgumentException("users cant be parents");
}
if (child.getParents().contains(parent.getId().getNodeId())) {
return;
}
RelationDoc parentToChildRelation = new RelationDoc(child.getId().getNodeId(), role);
helper.update(
Query.query(Criteria.where(ID).is(parent.getId())),
new Update().addToSet(CHILDREN, parentToChildRelation),
GroupDoc.class).getModifiedCount();
helper.update(
Query.query(Criteria.where(ID).is(child.getId())),
new Update().addToSet(PARENTS, parent.getId().getNodeId()),
child.getClass()).getModifiedCount();
}
public GroupDoc getOrInsertGroup(EntityNode node) {
if (node == null) {
throw ExceptionGenerator.nodeIsNull();
}
GroupDoc doc = nodeConverter.toGroupDoc(node);
return Optional.ofNullable(helper.get(ID, doc.getId(), GroupDoc.class))
.orElseGet(() -> helper.save(doc));
}
public UserDoc getOrInsertUser(EntityNode node) {
if (node == null) {
throw ExceptionGenerator.nodeIsNull();
}
UserDoc doc = nodeConverter.toUserDoc(node);
return Optional.ofNullable(helper.get(ID, doc.getId(), UserDoc.class))
.orElseGet(() -> helper.save(doc));
}
}
......@@ -3,6 +3,7 @@ package org.opengroup.osdu.core.mongodb.entitlements.repo;
import org.opengroup.osdu.core.mongodb.entitlements.entity.NodeDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.NodeIdDoc;
import org.opengroup.osdu.core.mongodb.entitlements.helper.EntitlementsNodeHelper;
import org.opengroup.osdu.core.mongodb.entitlements.helper.NextGenNodeHelper;
import org.opengroup.osdu.core.mongodb.helper.BasicMongoDBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
......@@ -23,6 +24,8 @@ abstract public class BasicEntitlementsHelper {
@Autowired
protected EntitlementsNodeHelper nodeHelper;
@Autowired
protected NextGenNodeHelper newNodeHelper;
@Autowired
protected BasicMongoDBHelper mongoHelper;
public Query queryById(String id, String tenant) {
......
package org.opengroup.osdu.core.mongodb.entitlements.repo;
import org.opengroup.osdu.core.mongodb.entitlements.entity.NodeDoc;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeConverter;
import org.opengroup.osdu.core.mongodb.entitlements.entity.GroupDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.UserDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.internal.RelationDoc;
import org.opengroup.osdu.core.mongodb.entitlements.model.CreateGroupRepoDto;
import org.opengroup.osdu.core.mongodb.entitlements.model.EntityNode;
......@@ -15,23 +17,24 @@ import java.util.Set;
public class CreateGroupHelper extends BasicEntitlementsHelper {
private final ConversionService conversionService;
private final NodeConverter nodeConverter;
public CreateGroupHelper(ConversionService conversionService) {
public CreateGroupHelper(ConversionService conversionService, NodeConverter nodeConverter) {
this.conversionService = conversionService;
this.nodeConverter = nodeConverter;
}
public Set<String> createGroup(EntityNode groupNode, CreateGroupRepoDto createGroupRequest) {
NodeDoc group = conversionService.convert(groupNode, NodeDoc.class);
NodeDoc user = nodeHelper.getOrInsertNode(createGroupRequest.getRequesterNode());
GroupDoc group = newNodeHelper.getOrInsertGroup(groupNode);
UserDoc user = newNodeHelper.getOrInsertUser(createGroupRequest.getRequesterNode());
Set<String> affectedNodes = new HashSet<>();
nodeHelper.createRelation(group, user, Role.OWNER);
newNodeHelper.createRelation(group, user, Role.OWNER);
if (createGroupRequest.isAddDataRootGroup()) {
NodeDoc rootGroup = nodeHelper.getOrInsertNode(createGroupRequest.getDataRootGroupNode());
nodeHelper.createRelation(rootGroup, group, Role.MEMBER);
nodeHelper.createRelation(group, user, Role.MEMBER);
GroupDoc rootGroup = newNodeHelper.getOrInsertGroup(createGroupRequest.getDataRootGroupNode());
newNodeHelper.createRelation(rootGroup, group, Role.MEMBER);
newNodeHelper.createRelation(group, user, Role.MEMBER);
affectedNodes.addAll(rootGroup.getParents());
rootGroup.getChildren().stream()
......
......@@ -20,7 +20,7 @@ public class DeleteGroupHelper extends BasicEntitlementsHelper {
NodeDoc node = nodeHelper.getNode(groupNode);
if (node == null) {
throw ExceptionGenerator.groupIsNull();
throw ExceptionGenerator.nodeIsNull();
}
long updatedParentRefs = mongoHelper.update(NodeDoc.class)
......
......@@ -14,8 +14,8 @@ public class ExceptionGenerator {
return new IllegalStateException(String.format("Group with id %s was not deleted.", id));
}
public static IllegalArgumentException groupIsNull() {
return new IllegalArgumentException("Group passed as arg is null.");
public static IllegalArgumentException nodeIsNull() {
return new IllegalArgumentException("Node passed as arg is null.");
}
public static IllegalStateException deleteCountMismatch(String id, long remoteRefs, long docRefs) {
......
package org.opengroup.osdu.core.mongodb.entitlements.config;
import org.opengroup.osdu.core.mongodb.entitlements.converter.EntityNodeToNodeDocConverter;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeConverter;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeDocToEntityNodeConverter;
import org.opengroup.osdu.core.mongodb.entitlements.converter.NodeDocToParentRefConverter;
import org.opengroup.osdu.core.mongodb.entitlements.helper.EntitlementsNodeHelper;
......@@ -27,8 +28,9 @@ public class EntitlementsContext {
@Autowired
@Bean
public EntitlementsNodeHelper entitlementsNodeHelper(BasicMongoDBHelper mongoDBHelper, ConversionService conversionService) {
return new EntitlementsNodeHelper(mongoDBHelper, conversionService);
public EntitlementsNodeHelper entitlementsNodeHelper(BasicMongoDBHelper mongoDBHelper, ConversionService conversionService,
NodeConverter nodeConverter) {
return new EntitlementsNodeHelper(mongoDBHelper, conversionService, nodeConverter);
}
// region repository implementations
......@@ -39,8 +41,8 @@ public class EntitlementsContext {
@Autowired
@Bean
public CreateGroupHelper createGroupRepo(ConversionService conversionService) {
return new CreateGroupHelper(conversionService);
public CreateGroupHelper createGroupRepo(ConversionService conversionService, NodeConverter nodeConverter) {
return new CreateGroupHelper(conversionService, nodeConverter);
}
@Bean
......@@ -94,6 +96,12 @@ public class EntitlementsContext {
return new NodeDocToParentRefConverter(other);
}
@Autowired
@Bean
public NodeConverter nodeConverter() {
return new NodeConverter();
}
// endregion
}
......@@ -6,7 +6,9 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.opengroup.osdu.core.mongodb.entitlements.config.EntitlementsTestConfig;
import org.opengroup.osdu.core.mongodb.entitlements.config.MongoEmbeddedConfig;
import org.opengroup.osdu.core.mongodb.entitlements.entity.GroupDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.NodeDoc;
import org.opengroup.osdu.core.mongodb.entitlements.entity.UserDoc;
import org.opengroup.osdu.core.mongodb.entitlements.model.CreateGroupRepoDto;
import org.opengroup.osdu.core.mongodb.entitlements.model.EntityNode;
import org.opengroup.osdu.core.mongodb.entitlements.repo.CreateGroupHelper;
......@@ -47,13 +49,13 @@ public class CreateGroupRepoImplTest extends RepoImplTestParent {
createGroupRepo.createGroup(groupNode, request);
NodeDoc user = mongoTemplate.findById(createIdFor(userNode), CLASS);
NodeDoc group = mongoTemplate.findById(createIdFor(groupNode), CLASS);
UserDoc user = mongoTemplate.findById(createIdFor(userNode), UserDoc.class);
GroupDoc group = mongoTemplate.findById(createIdFor(groupNode), GroupDoc.class);
assertThat(user).isNotNull();
assertThat(group).isNotNull();
assertThat(user.getParents()).contains(group.getNodeId());
assertThat(group.getChildren()).anyMatch(relation -> relation.getNodeId().equals(user.getNodeId()));
assertThat(user.getParents()).contains(group.getId().getNodeId());
assertThat(group.getChildren()).anyMatch(relation -> relation.getNodeId().equals(user.getId().getNodeId()));
}
@Test
......
......@@ -104,15 +104,15 @@ public abstract class RepoImplTestParent {
group4.getChildren().add(getRelationTo(user4, Role.OWNER));
group4.getChildren().add(getRelationTo(user4, Role.MEMBER));
user1.getParents().add(group2.getNodeId());
// user1.getParents().add(group2.getNodeId());
user2.getParents().add(group1.getNodeId());
user3.getParents().add(group4.getNodeId());
user2.getParents().add(group3.getNodeId());
user4.getParents().add(group3.getNodeId());
group1.getChildren().add(getRelationTo(user2, Role.MEMBER));
group2.getChildren().add(getRelationTo(user1, Role.MEMBER));
group3.getChildren().add(getRelationTo(user2, Role.MEMBER));
group3.getChildren().add(getRelationTo(user4, Role.MEMBER));
group4.getChildren().add(getRelationTo(user3, Role.MEMBER));
// group4.getChildren().add(getRelationTo(user3, Role.MEMBER));
mongoTemplate.insertAll(Arrays.asList(rootGroup, superGroup1, superGroup2, group1, group2, group3, group4,
user1, user2, user3, user4));
......
......@@ -7,7 +7,7 @@
<parent>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>os-core-lib-mongodb</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</parent>
<artifactId>legal</artifactId>
......
......@@ -7,7 +7,7 @@
<parent>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>os-core-lib-mongodb</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
</parent>
<artifactId>mongo-core</artifactId>
......
......@@ -60,8 +60,8 @@ public class BasicMongoDBHelper {
*
* @param documentToSave document for saving.
*/
public <T> void save(T documentToSave) {
mongoOperations.save(documentToSave);
public <T> T save(T documentToSave) {
return mongoOperations.save(documentToSave);
}
/**
......
......@@ -21,7 +21,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.opengroup.osdu.core.mongodb</groupId>
<artifactId>os-core-lib-mongodb</artifactId>
<version>0.1.9</version>
<version>0.1.10</version>
<packaging>pom</packaging>
<name>os-core-lib-mongodb</name>
......@@ -34,7 +34,7 @@
</licenses>
<properties>
<mongo.lib.version>0.1.9</mongo.lib.version>
<mongo.lib.version>0.1.10</mongo.lib.version>