Java Code Examples for org.apache.atlas.model.instance.AtlasObjectId#getGuid()

The following examples show how to use org.apache.atlas.model.instance.AtlasObjectId#getGuid() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AtlasStructFormatConverter.java    From atlas with Apache License 2.0 6 votes vote down vote up
private String getGuid(Object obj) {
    final String ret;

    if (obj instanceof AtlasObjectId) {
        AtlasObjectId objId = (AtlasObjectId) obj;

        ret = objId.getGuid();
    } else if (obj instanceof Map) {
        Map v2Map = (Map) obj;

        ret = (String)v2Map.get(AtlasObjectId.KEY_GUID);
    } else {
        ret = null;
    }

    return ret;
}
 
Example 2
Source File: EntityGraphMapper.java    From atlas with Apache License 2.0 6 votes vote down vote up
private String mapSoftRefValue(AttributeMutationContext ctx, EntityMutationContext context) {
    String ret = null;

    if (ctx.getValue() instanceof AtlasObjectId) {
        AtlasObjectId objectId = (AtlasObjectId) ctx.getValue();
        String        typeName = objectId.getTypeName();
        String        guid     = AtlasTypeUtil.isUnAssignedGuid(objectId.getGuid()) ? context.getGuidAssignments().get(objectId.getGuid()) : objectId.getGuid();

        ret = AtlasEntityUtil.formatSoftRefValue(typeName, guid);
    } else {
        if (ctx.getValue() != null) {
            LOG.warn("mapSoftRefValue: Was expecting AtlasObjectId, but found: {}", ctx.getValue().getClass());
        }
    }

    setAssignedGuid(ctx.getValue(), context);

    return ret;
}
 
Example 3
Source File: AtlasRelationshipStoreV1.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
private void validateEnd(AtlasObjectId end) throws AtlasBaseException {
    String              guid             = end.getGuid();
    String              typeName         = end.getTypeName();
    Map<String, Object> uniqueAttributes = end.getUniqueAttributes();
    AtlasVertex         endVertex        = AtlasGraphUtilsV1.findByGuid(guid);

    if (!AtlasTypeUtil.isValidGuid(guid) || endVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    } else if (MapUtils.isNotEmpty(uniqueAttributes))  {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);

        if (AtlasGraphUtilsV1.findByUniqueAttributes(entityType, uniqueAttributes) == null) {
            throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, typeName, uniqueAttributes.toString());
        }
    }
}
 
Example 4
Source File: EntityGraphDiscoveryContext.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
public AtlasVertex getResolvedEntityVertex(AtlasObjectId objId) {
    AtlasVertex vertex = resolvedIdsByUniqAttribs.get(objId);

    // check also for sub-types; ref={typeName=Asset; guid=abcd} should match {typeName=hive_table; guid=abcd}
    if (vertex == null) {
        final AtlasEntityType entityType  = typeRegistry.getEntityTypeByName(objId.getTypeName());
        final Set<String>     allSubTypes = entityType.getAllSubTypes();

        for (String subType : allSubTypes) {
            AtlasObjectId subTypeObjId = new AtlasObjectId(objId.getGuid(), subType, objId.getUniqueAttributes());

            vertex = resolvedIdsByUniqAttribs.get(subTypeObjId);

            if (vertex != null) {
                resolvedIdsByUniqAttribs.put(objId, vertex);
                break;
            }
        }
    }

    return vertex;
}
 
Example 5
Source File: NiFiFlow.java    From nifi with Apache License 2.0 6 votes vote down vote up
private EntityChangeType getFlowPathIOChangeType(AtlasObjectId id) {
    final String guid = id.getGuid();
    if (!isGuidAssigned(guid)) {
        return EntityChangeType.CREATED;
    } else {
        if (TYPE_NIFI_QUEUE.equals(id.getTypeName()) && queues.containsKey(id)) {
            // If an input/output is a queue, and it is owned by this NiFiFlow, then check if it's still needed. NiFiFlow knows active queues.
            if (stillExistingEntityGuids.contains(guid)) {
                return EntityChangeType.AS_IS;
            } else {
                return EntityChangeType.DELETED;
            }
        } else {
            // Otherwise, do not need to delete.
            return EntityChangeType.AS_IS;
        }
    }
}
 
Example 6
Source File: NotificationHookConsumer.java    From atlas with Apache License 2.0 5 votes vote down vote up
private void updateProcessedEntityReferences(AtlasObjectId objId, Map<String, String> guidAssignments) {
    String guid = objId.getGuid();

    if (guid != null && guidAssignments.containsKey(guid)) {
        String assignedGuid = guidAssignments.get(guid);

        if (LOG.isDebugEnabled()) {
            LOG.debug("{}(guid={}) is already processed; updating its reference to use assigned-guid={}", objId.getTypeName(), guid, assignedGuid);
        }

        objId.setGuid(assignedGuid);
        objId.setTypeName(null);
        objId.setUniqueAttributes(null);
    }
}
 
Example 7
Source File: AtlasEntityStoreV2.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Override
@GraphTransaction
public EntityMutationResponse updateEntity(AtlasObjectId objectId, AtlasEntityWithExtInfo updatedEntityInfo, boolean isPartialUpdate) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> updateEntity({}, {}, {})", objectId, updatedEntityInfo, isPartialUpdate);
    }

    if (objectId == null || updatedEntityInfo == null || updatedEntityInfo.getEntity() == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "null entity-id/entity");
    }

    final String guid;

    if (AtlasTypeUtil.isAssignedGuid(objectId.getGuid())) {
        guid = objectId.getGuid();
    } else {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objectId.getTypeName());

        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_TYPENAME, objectId.getTypeName());
        }

        guid = AtlasGraphUtilsV2.getGuidByUniqueAttributes(graph, typeRegistry.getEntityTypeByName(objectId.getTypeName()), objectId.getUniqueAttributes());
    }

    AtlasEntity entity = updatedEntityInfo.getEntity();

    entity.setGuid(guid);

    return createOrUpdate(new AtlasEntityStream(updatedEntityInfo), isPartialUpdate, false, false);
}
 
Example 8
Source File: AtlasDeleteHandlerV1Test.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteEntitiesWithCompositeMapReference() throws Exception {
    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map with one entry that references MapValue instance.
    AtlasEntity.AtlasEntityWithExtInfo entityDefinition = createMapOwnerAndValueEntities();
    String mapOwnerGuid = entityDefinition.getEntity().getGuid();

    // Verify MapOwner.map attribute has expected value.
    AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance = entityStore.getById(mapOwnerGuid);
    Object object = mapOwnerInstance.getEntity().getAttribute("map");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof Map);
    Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>)object;
    Assert.assertEquals(map.size(), 1);
    AtlasObjectId mapValueInstance = map.get("value1");
    Assert.assertNotNull(mapValueInstance);
    String mapValueGuid = mapValueInstance.getGuid();
    String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(compositeMapOwnerType, "map");
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
    Assert.assertNotNull(object);

    RequestContextV1.clear();
    List<AtlasEntityHeader> deletedEntities = entityStore.deleteById(mapOwnerGuid).getDeletedEntities();
    Assert.assertEquals(deletedEntities.size(), 2);
    Assert.assertTrue(extractGuids(deletedEntities).contains(mapOwnerGuid));
    Assert.assertTrue(extractGuids(deletedEntities).contains(mapValueGuid));

    assertEntityDeleted(mapOwnerGuid);
    assertEntityDeleted(mapValueGuid);
}
 
Example 9
Source File: NiFiAtlasClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
public AtlasEntity.AtlasEntityWithExtInfo searchEntityDef(AtlasObjectId id) throws AtlasServiceException {
    final String guid = id.getGuid();
    if (!StringUtils.isEmpty(guid)) {
        return atlasClient.getEntityByGuid(guid, true, false);
    }
    final Map<String, String> attributes = new HashMap<>();
    id.getUniqueAttributes().entrySet().stream().filter(entry -> entry.getValue() != null)
            .forEach(entry -> attributes.put(entry.getKey(), entry.getValue().toString()));
    return atlasClient.getEntityByAttribute(id.getTypeName(), attributes, true, false);
}
 
Example 10
Source File: AtlasRelationshipStoreV2.java    From atlas with Apache License 2.0 4 votes vote down vote up
/**
 * Validate the ends of the passed relationship
 * @param relationship
 * @throws AtlasBaseException
 */
private void validateEnds(AtlasRelationship relationship) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("validateEnds entry relationship:" + relationship);
    }
    List<AtlasObjectId>           ends                 = new ArrayList<>();
    List<AtlasRelationshipEndDef> endDefs              = new ArrayList<>();
    String                        relationshipTypeName = relationship.getTypeName();
    AtlasRelationshipDef          relationshipDef      = typeRegistry.getRelationshipDefByName(relationshipTypeName);

    ends.add(relationship.getEnd1());
    ends.add(relationship.getEnd2());
    endDefs.add(relationshipDef.getEndDef1());
    endDefs.add(relationshipDef.getEndDef2());

    for (int i = 0; i < ends.size(); i++) {
        AtlasObjectId       end              = ends.get(i);
        String              guid             = end.getGuid();
        String              typeName         = end.getTypeName();
        Map<String, Object> uniqueAttributes = end.getUniqueAttributes();
        AtlasVertex         endVertex        = AtlasGraphUtilsV2.findByGuid(this.graph, guid);

        if (!AtlasTypeUtil.isValidGuid(guid) || endVertex == null) {
            throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);

        } else if (MapUtils.isNotEmpty(uniqueAttributes)) {
            AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);

            if (AtlasGraphUtilsV2.findByUniqueAttributes(this.graph, entityType, uniqueAttributes) == null) {
                throw new AtlasBaseException(AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, typeName, uniqueAttributes.toString());
            }
        } else {
            // check whether the guid is the correct type
            String vertexTypeName = endVertex.getProperty(Constants.TYPE_NAME_PROPERTY_KEY, String.class);

            if (!Objects.equals(vertexTypeName, typeName)) {
                String attrName = endDefs.get(i).getName();

                throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_INVALID_ENDTYPE, attrName, guid, vertexTypeName, typeName);
            }
        }
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("validateEnds exit successfully validated relationship:" + relationship);
    }
}
 
Example 11
Source File: EntityGraphMapper.java    From atlas with Apache License 2.0 4 votes vote down vote up
private AtlasEntityType getInstanceType(Object val, EntityMutationContext context) throws AtlasBaseException {
    AtlasEntityType ret = null;

    if (val != null) {
        String typeName = null;
        String guid     = null;

        if (val instanceof AtlasObjectId) {
            AtlasObjectId objId = (AtlasObjectId) val;

            typeName = objId.getTypeName();
            guid     = objId.getGuid();
        } else if (val instanceof Map) {
            Map map = (Map) val;

            Object typeNameVal = map.get(AtlasObjectId.KEY_TYPENAME);
            Object guidVal     = map.get(AtlasObjectId.KEY_GUID);

            if (typeNameVal != null) {
                typeName = typeNameVal.toString();
            }

            if (guidVal != null) {
                guid = guidVal.toString();
            }
        }

        if (typeName == null) {
            if (guid != null) {
                ret = context.getType(guid);

                if (ret == null) {
                    AtlasVertex vertex = context.getDiscoveryContext().getResolvedEntityVertex(guid);

                    if (vertex != null) {
                        typeName = AtlasGraphUtilsV2.getTypeName(vertex);
                    }
                }
            }
        }

        if (ret == null && typeName != null) {
            ret = typeRegistry.getEntityTypeByName(typeName);
        }

        if (ret == null) {
            throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, val.toString());
        }
    }

    return ret;
}
 
Example 12
Source File: AtlasBuiltInTypes.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Override
public boolean areEqualValues(Object val1, Object val2, Map<String, String> guidAssignments) {
    boolean ret = true;

    if (val1 == null) {
        ret = val2 == null;
    } else if (val2 == null) {
        ret = false;
    } else {
        AtlasObjectId v1 = getNormalizedValue(val1);
        AtlasObjectId v2 = getNormalizedValue(val2);

        if (v1 == null || v2 == null) {
            ret = false;
        } else {
            String guid1 = v1.getGuid();
            String guid2 = v2.getGuid();

            if (guidAssignments != null ) {
                if (guidAssignments.containsKey(guid1)) {
                    guid1 = guidAssignments.get(guid1);
                }

                if (guidAssignments.containsKey(guid2)) {
                    guid2 = guidAssignments.get(guid2);
                }
            }

            boolean isV1AssignedGuid = AtlasTypeUtil.isAssignedGuid(guid1);
            boolean isV2AssignedGuid = AtlasTypeUtil.isAssignedGuid(guid2);

            if (isV1AssignedGuid == isV2AssignedGuid) { // if both have assigned/unassigned guids, compare guids
                ret = Objects.equals(guid1, guid2);
            } else { // if one has assigned and other unassigned guid, compare typeName and unique-attribute
                ret = Objects.equals(v1.getTypeName(), v2.getTypeName()) && Objects.equals(v1.getUniqueAttributes(), v2.getUniqueAttributes());
            }
        }
    }

    return ret;
}
 
Example 13
Source File: EntityGraphMapper.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
public static AtlasEntityHeader constructHeader(AtlasObjectId id) {
    return new AtlasEntityHeader(id.getTypeName(), id.getGuid(), id.getUniqueAttributes());
}
 
Example 14
Source File: AtlasDeleteHandlerV1Test.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
/**
 * Verify deleting entities that are the target of class map references.
 */
@Test
public void testDisconnectMapReferenceFromClassType() throws Exception {
    // Define type for map value.
    AtlasStructDef.AtlasAttributeDef[] mapValueAttributes = new AtlasStructDef.AtlasAttributeDef[]{
        new AtlasStructDef.AtlasAttributeDef("biMapOwner", "MapOwner",
            true,
            AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
            false, false,
            new ArrayList<AtlasStructDef.AtlasConstraintDef>() {{
                add(new AtlasStructDef.AtlasConstraintDef(
                    AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {{
                    put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "biMap");
                }}));
            }})};

    AtlasEntityDef mapValueContainerDef =
        new AtlasEntityDef("MapValue", "MapValue_desc", "1.0",
            Arrays.asList(mapValueAttributes), Collections.<String>emptySet());

    // Define type with unidirectional and bidirectional map references,
    // where the map value is a class reference to MapValue.

    AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[]{
        new AtlasStructDef.AtlasAttributeDef("map", "map<string,MapValue>",
            true,
            AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
            false, false,
            Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()),
        new AtlasStructDef.AtlasAttributeDef("biMap", "map<string,MapValue>",
            true,
            AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1,
            false, false,
            new ArrayList<AtlasStructDef.AtlasConstraintDef>() {{
                add(new AtlasStructDef.AtlasConstraintDef(
                    AtlasStructDef.AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {{
                    put(AtlasStructDef.AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, "biMapOwner");
                }}));
            }})};

    AtlasEntityDef mapOwnerContainerDef =
        new AtlasEntityDef("MapOwner", "MapOwner_desc", "1.0",
            Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());

    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(),
        ImmutableList.<AtlasStructDef>of(),
        ImmutableList.<AtlasClassificationDef>of(),
        ImmutableList.<AtlasEntityDef>of(mapValueContainerDef, mapOwnerContainerDef));

    typeDefStore.createTypesDef(typesDef);

    // Create instances of MapOwner and MapValue.
    // Set MapOwner.map and MapOwner.biMap with one entry that references MapValue instance.
    AtlasEntity mapOwnerInstance = new AtlasEntity("MapOwner");
    AtlasEntity mapValueInstance = new AtlasEntity("MapValue");

    mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
    mapOwnerInstance.setAttribute("biMap", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
    // Set biMapOwner reverse reference on MapValue.
    mapValueInstance.setAttribute("biMapOwner", AtlasTypeUtil.getAtlasObjectId(mapOwnerInstance));

    AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    entities.addReferredEntity(mapValueInstance);
    entities.addEntity(mapOwnerInstance);

    final EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entities), false);
    Assert.assertEquals(response.getCreatedEntities().size(), 2);
    final List<AtlasEntityHeader> mapOwnerCreated = response.getCreatedEntitiesByTypeName("MapOwner");
    AtlasEntity.AtlasEntityWithExtInfo mapOwnerEntity = entityStore.getById(mapOwnerCreated.get(0).getGuid());

    String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(typeRegistry.getEntityTypeByName("MapOwner"), "map");
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);

    // Verify MapOwner.map attribute has expected value.
    String mapValueGuid = null;
    AtlasVertex mapOwnerVertex = null;
    for (String mapAttrName : Arrays.asList("map", "biMap")) {
        Object object = mapOwnerEntity.getEntity().getAttribute(mapAttrName);
        Assert.assertNotNull(object);
        Assert.assertTrue(object instanceof Map);
        Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>)object;
        Assert.assertEquals(map.size(), 1);
        AtlasObjectId value1Id = map.get("value1");
        Assert.assertNotNull(value1Id);
        mapValueGuid = value1Id.getGuid();
        mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerEntity.getEntity().getGuid());
        object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
        Assert.assertNotNull(object);
    }

    // Delete the map value instance.
    // This should disconnect the references from the map owner instance.
    entityStore.deleteById(mapValueGuid);
    assertEntityDeleted(mapValueGuid);
    assertTestDisconnectMapReferenceFromClassType(mapOwnerEntity.getEntity().getGuid());
}
 
Example 15
Source File: AbstractLineageStrategy.java    From nifi with Apache License 2.0 4 votes vote down vote up
private Referenceable toReferenceable(AtlasObjectId id) {
    return StringUtils.isEmpty(id.getGuid())
            ? new Referenceable(id.getTypeName(), id.getUniqueAttributes())
            : new Referenceable(id.getGuid(), id.getTypeName(), id.getUniqueAttributes());
}