Search in sources :

Example 6 with Empty

use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.

the class EntityDataServiceImpl method delete.

/**
 * Deletes an Entity by the EntityId and EntityType
 *
 * @param request ID of the entity to be deleted
 * @param responseObserver Observer to be notified on about the Entity delete request
 */
@Override
public void delete(ByIdRequest request, StreamObserver<Empty> responseObserver) {
    if (StringUtils.isEmpty(request.getEntityId())) {
        LOG.info("{}. Invalid delete request:{}", request, ErrorMessages.ENTITY_ID_EMPTY);
        responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
        return;
    }
    Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    Optional<Entity> existingEntity = getExistingEntity(tenantId.get(), request.getEntityType(), request.getEntityId());
    Key key = this.entityNormalizer.getEntityDocKey(tenantId.get(), request.getEntityType(), request.getEntityId());
    if (entitiesCollection.delete(key)) {
        responseObserver.onNext(Empty.newBuilder().build());
        responseObserver.onCompleted();
        existingEntity.ifPresent(entity -> entityChangeEventGenerator.sendDeleteNotification(RequestContext.CURRENT.get(), List.of(entity)));
    } else {
        responseObserver.onError(new RuntimeException("Could not delete the entity."));
    }
}
Also used : EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) ServiceException(com.google.protobuf.ServiceException) Key(org.hypertrace.core.documentstore.Key)

Example 7 with Empty

use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.

the class EntityDataServiceImpl method upsertEntities.

@Override
public void upsertEntities(Entities request, StreamObserver<Empty> responseObserver) {
    String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
    if (tenantId == null) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    try {
        Map<Key, Entity> entities = request.getEntityList().stream().map(entity -> this.entityNormalizer.normalize(tenantId, entity)).collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity), Function.identity()));
        java.util.Collection<Entity> existingEntities = getExistingEntities(entities.values());
        upsertEntities(entities, entitiesCollection, responseObserver);
        entityChangeEventGenerator.sendChangeNotification(RequestContext.CURRENT.get(), existingEntities, entities.values());
    } catch (Throwable throwable) {
        LOG.warn("Failed to upsert: {}", request, throwable);
        responseObserver.onError(throwable);
    }
}
Also used : ByIdRequest(org.hypertrace.entity.data.service.v1.ByIdRequest) ServiceException(com.google.protobuf.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Channel(io.grpc.Channel) EntityTypeClient(org.hypertrace.entity.type.service.rxclient.EntityTypeClient) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ENRICHED_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENRICHED_ENTITIES_COLLECTION) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) InvalidRequestException(org.hypertrace.entity.service.exception.InvalidRequestException) StringUtils(org.hypertrace.entity.service.util.StringUtils) Streams(com.google.common.collect.Streams) Empty(org.hypertrace.entity.data.service.v1.Empty) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) EntityRelationships(org.hypertrace.entity.data.service.v1.EntityRelationships) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) EntityChangeEventGenerator(org.hypertrace.entity.service.change.event.api.EntityChangeEventGenerator) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ByTypeAndIdentifyingAttributes(org.hypertrace.entity.data.service.v1.ByTypeAndIdentifyingAttributes) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) Descriptors(com.google.protobuf.Descriptors) Filter(org.hypertrace.core.documentstore.Filter) HashMap(java.util.HashMap) Function(java.util.function.Function) Collection(org.hypertrace.core.documentstore.Collection) Entities(org.hypertrace.entity.data.service.v1.Entities) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Key(org.hypertrace.core.documentstore.Key) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) ENTITY_RELATIONSHIPS_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_RELATIONSHIPS_COLLECTION) EnrichedEntities(org.hypertrace.entity.data.service.v1.EnrichedEntities) Query(org.hypertrace.entity.data.service.v1.Query) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) Message(com.google.protobuf.Message) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Entity(org.hypertrace.entity.data.service.v1.Entity) ServiceException(com.google.protobuf.ServiceException) Key(org.hypertrace.core.documentstore.Key)

Example 8 with Empty

use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.

the class EntityDataServiceImpl method upsertEnrichedEntities.

@Override
public void upsertEnrichedEntities(EnrichedEntities request, StreamObserver<Empty> responseObserver) {
    String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
    if (isNull(tenantId)) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    for (EnrichedEntity entity : request.getEntitiesList()) {
        if (StringUtils.isEmpty(entity.getEntityType())) {
            LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_TYPE_EMPTY);
            responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_TYPE_EMPTY));
            return;
        }
        if (StringUtils.isEmpty(entity.getEntityId())) {
            LOG.info("{}. Invalid upsertEnrichedEntities request:{}", entity, ErrorMessages.ENTITY_ID_EMPTY);
            responseObserver.onError(new RuntimeException(ErrorMessages.ENTITY_ID_EMPTY));
            return;
        }
    }
    Map<Key, EnrichedEntity> entities = request.getEntitiesList().stream().collect(Collectors.toUnmodifiableMap(entity -> this.entityNormalizer.getEntityDocKey(tenantId, entity.getEntityType(), entity.getEntityId()), Function.identity()));
    upsertEntities(entities, enrichedEntitiesCollection, responseObserver);
}
Also used : ByIdRequest(org.hypertrace.entity.data.service.v1.ByIdRequest) ServiceException(com.google.protobuf.ServiceException) LoggerFactory(org.slf4j.LoggerFactory) Channel(io.grpc.Channel) EntityTypeClient(org.hypertrace.entity.type.service.rxclient.EntityTypeClient) StreamObserver(io.grpc.stub.StreamObserver) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) ENRICHED_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENRICHED_ENTITIES_COLLECTION) DocStoreConverter(org.hypertrace.entity.service.util.DocStoreConverter) InvalidRequestException(org.hypertrace.entity.service.exception.InvalidRequestException) StringUtils(org.hypertrace.entity.service.util.StringUtils) Streams(com.google.common.collect.Streams) Empty(org.hypertrace.entity.data.service.v1.Empty) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) EntityRelationships(org.hypertrace.entity.data.service.v1.EntityRelationships) MergeAndUpsertEntityRequest(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityRequest) Optional(java.util.Optional) EntityChangeEventGenerator(org.hypertrace.entity.service.change.event.api.EntityChangeEventGenerator) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Builder(org.hypertrace.entity.data.service.v1.Entity.Builder) Document(org.hypertrace.core.documentstore.Document) RequestContext(org.hypertrace.core.grpcutils.context.RequestContext) ByTypeAndIdentifyingAttributes(org.hypertrace.entity.data.service.v1.ByTypeAndIdentifyingAttributes) DocStoreJsonFormat(org.hypertrace.entity.service.util.DocStoreJsonFormat) Descriptors(com.google.protobuf.Descriptors) Filter(org.hypertrace.core.documentstore.Filter) HashMap(java.util.HashMap) Function(java.util.function.Function) Collection(org.hypertrace.core.documentstore.Collection) Entities(org.hypertrace.entity.data.service.v1.Entities) ArrayList(java.util.ArrayList) Datastore(org.hypertrace.core.documentstore.Datastore) Key(org.hypertrace.core.documentstore.Key) EntityServiceConstants(org.hypertrace.entity.service.constants.EntityServiceConstants) Entity(org.hypertrace.entity.data.service.v1.Entity) EntityDataServiceImplBase(org.hypertrace.entity.data.service.v1.EntityDataServiceGrpc.EntityDataServiceImplBase) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) ENTITY_RELATIONSHIPS_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.ENTITY_RELATIONSHIPS_COLLECTION) EnrichedEntities(org.hypertrace.entity.data.service.v1.EnrichedEntities) Query(org.hypertrace.entity.data.service.v1.Query) RAW_ENTITIES_COLLECTION(org.hypertrace.entity.service.constants.EntityCollectionConstants.RAW_ENTITIES_COLLECTION) Message(com.google.protobuf.Message) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) MergeAndUpsertEntityResponse(org.hypertrace.entity.data.service.v1.MergeAndUpsertEntityResponse) Collections(java.util.Collections) RelationshipsQuery(org.hypertrace.entity.data.service.v1.RelationshipsQuery) ServiceException(com.google.protobuf.ServiceException) EnrichedEntity(org.hypertrace.entity.data.service.v1.EnrichedEntity) Key(org.hypertrace.core.documentstore.Key)

Example 9 with Empty

use of org.hypertrace.entity.data.service.v1.Empty in project hypertrace-ingester by hypertrace.

the class ApiEntityDao method upsertApiEntity.

public Entity upsertApiEntity(String tenantId, String serviceId, String serviceName, String apiType, String apiName) {
    Preconditions.checkNotNull(tenantId, "tenantId can't be empty");
    Preconditions.checkNotNull(serviceId, "serviceId can't be empty");
    Preconditions.checkNotNull(serviceName, "serviceName can't be empty");
    Preconditions.checkNotNull(apiType, "apiType can't be empty");
    Preconditions.checkNotNull(apiName, "apiName can't be empty");
    String pattern = apiName;
    Entity.Builder entityBuilder = Entity.newBuilder().setTenantId(tenantId).setEntityType(EntityType.API.name()).setEntityName(pattern).putIdentifyingAttributes(API_URL_PATTERN_ATTR, createAttributeValue(pattern)).putIdentifyingAttributes(SERVICE_ID_ATTR, createAttributeValue(serviceId)).putIdentifyingAttributes(API_TYPE_ATTR, createAttributeValue(apiType)).putAttributes(SERVICE_NAME_ATTR, createAttributeValue(serviceName)).putAttributes(API_DISCOVERY_FROM_ATTR, createAttributeValue(DISCOVERED_FROM)).putAttributes(API_DISCOVERY_STATE_ATTR, createAttributeValue(DISCOVERED_STATE)).putAttributes(API_NAME_ATTR, createAttributeValue(apiName));
    Entity entity = entityBuilder.build();
    LOGGER.info("Upserting Api entity: [{}]", TextFormat.shortDebugString(entity));
    return edsClient.upsert(entity);
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity)

Example 10 with Empty

use of org.hypertrace.entity.data.service.v1.Empty in project entity-service by hypertrace.

the class EntityDataServiceImpl method upsertRelationships.

@Override
public void upsertRelationships(EntityRelationships request, StreamObserver<Empty> responseObserver) {
    String tenantId = RequestContext.CURRENT.get().getTenantId().orElse(null);
    if (tenantId == null) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    Map<Key, Document> entityRelations = new HashMap<>();
    try {
        for (EntityRelationship relationship : request.getRelationshipList()) {
            if (StringUtils.isEmpty(relationship.getFromEntityId()) || StringUtils.isEmpty(relationship.getToEntityId()) || StringUtils.isEmpty(relationship.getEntityRelationshipType())) {
                LOG.warn("Invalid relationship upsert request:{}", relationship);
                continue;
            }
            Document document = convertEntityRelationshipToDocument(tenantId, relationship);
            Key key = new EntityRelationshipDocKey(tenantId, relationship.getEntityRelationshipType(), relationship.getFromEntityId(), relationship.getToEntityId());
            entityRelations.put(key, document);
        }
        boolean status = relationshipsCollection.bulkUpsert(entityRelations);
        if (status) {
            responseObserver.onNext(Empty.newBuilder().build());
            responseObserver.onCompleted();
        } else {
            responseObserver.onError(new RuntimeException("Could not bulk upsert relationships."));
        }
    } catch (IOException e) {
        LOG.error("Failed to bulk upsert relationships.", e);
        responseObserver.onError(e);
    }
}
Also used : ServiceException(com.google.protobuf.ServiceException) HashMap(java.util.HashMap) EntityRelationship(org.hypertrace.entity.data.service.v1.EntityRelationship) IOException(java.io.IOException) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Key(org.hypertrace.core.documentstore.Key)

Aggregations

Filter (org.hypertrace.core.documentstore.Filter)7 Entity (org.hypertrace.entity.data.service.v1.Entity)6 AttributeFilter (org.hypertrace.entity.data.service.v1.AttributeFilter)5 ServiceException (com.google.protobuf.ServiceException)4 IOException (java.io.IOException)4 Key (org.hypertrace.core.documentstore.Key)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 List (java.util.List)3 Document (org.hypertrace.core.documentstore.Document)3 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)3 EnrichedEntity (org.hypertrace.entity.data.service.v1.EnrichedEntity)3 EntityRelationship (org.hypertrace.entity.data.service.v1.EntityRelationship)3 Query (org.hypertrace.entity.data.service.v1.Query)3 Test (org.junit.jupiter.api.Test)3 Streams (com.google.common.collect.Streams)2 Descriptors (com.google.protobuf.Descriptors)2 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)2 Message (com.google.protobuf.Message)2