@VisibleForTesting List<AtlasStructDef.AtlasAttributeDef> getElementsAbsentInExisting(AtlasStructDef existing, AtlasStructDef incoming) throws AtlasBaseException { List<AtlasStructDef.AtlasAttributeDef> difference = new ArrayList<>(); for (AtlasStructDef.AtlasAttributeDef attr : incoming.getAttributeDefs()) { updateCollectionWithDifferingAttributes(difference, existing, attr); } return difference; }
public String getRelationshipLabel() { return "r:" + super.getName(); }
public static AtlasStructDef createStructTypeDef(String name, String description, AtlasAttributeDef... attrDefs) { return new AtlasStructDef(name, description, "1.0", Arrays.asList(attrDefs)); }
public AtlasStructDef(AtlasStructDef other) { super(other); setAttributeDefs(other != null ? other.getAttributeDefs() : null); }
private void addOrUpdateAttributes(AtlasStructDef structDef, List<AtlasAttributeDef> attributesToUpdate) { for (AtlasAttributeDef attributeToUpdate : attributesToUpdate) { String attrName = attributeToUpdate.getName(); if (structDef.hasAttribute(attrName)) { structDef.removeAttribute(attrName); } structDef.addAttribute(attributeToUpdate); } } }
private static boolean updateTypeAttributes(AtlasStructDef oldStructDef, AtlasStructDef newStructDef, boolean checkTypeVersion) { boolean ret = isTypeUpdateApplicable(oldStructDef, newStructDef, checkTypeVersion); if (ret) { // make sure that all attributes in oldDef are in newDef as well if (CollectionUtils.isNotEmpty(oldStructDef.getAttributeDefs())){ for (AtlasAttributeDef oldAttrDef : oldStructDef.getAttributeDefs()) { if (!newStructDef.hasAttribute(oldAttrDef.getName())) { newStructDef.addAttribute(oldAttrDef); } } } } return ret; }
if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) { for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { attrNames.add(attributeDef.getName()); if (!attrNames.contains(currAttrName)) { throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_DELETION_NOT_SUPPORTED, structDef.getName(), currAttrName); AtlasStructDef currentStructDef = toStructDef(vertex, new AtlasStructDef(), typeDefStore); if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) { for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { if (CollectionUtils.isEmpty(currAttrNames) || !currAttrNames.contains(attributeDef.getName())) { throw new AtlasBaseException(AtlasErrorCode.CANNOT_ADD_MANDATORY_ATTRIBUTE, structDef.getName(), attributeDef.getName()); throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, structDef.getName(), "name"); throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, structDef.getName(), "typeName"); AtlasAttributeDef existingAttribute = currentStructDef.getAttribute(attributeDef.getName()); if (null != existingAttribute && !attributeDef.getTypeName().equals(existingAttribute.getTypeName())) { throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Data type update for attribute is not supported");
@Override public AtlasStructDef create(AtlasStructDef structDef, AtlasVertex preCreateResult) throws AtlasBaseException { if (LOG.isDebugEnabled()) { LOG.debug("==> AtlasStructDefStoreV1.create({}, {})", structDef, preCreateResult); } AtlasAuthorizationUtils.verifyAccess(new AtlasTypeAccessRequest(AtlasPrivilege.TYPE_CREATE, structDef), "create struct-def ", structDef.getName()); if (CollectionUtils.isEmpty(structDef.getAttributeDefs())) { throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Missing attributes for structdef"); } AtlasVertex vertex = (preCreateResult == null) ? preCreate(structDef) : preCreateResult; AtlasStructDefStoreV2.updateVertexAddReferences(structDef, vertex, typeDefStore); AtlasStructDef ret = toStructDef(vertex); if (LOG.isDebugEnabled()) { LOG.debug("<== AtlasStructDefStoreV1.create({}, {}): {}", structDef, preCreateResult, ret); } return ret; }
public static AtlasStructDef newStructDef(AtlasTypeRegistry typesRegistry) { int structDefIdx = IDX_STRUCT_DEF.getAndIncrement(); AtlasStructDef ret = new AtlasStructDef(); ret.setName(PREFIX_STRUCT_DEF + structDefIdx); ret.setDescription(ret.getName()); ret.setAttributeDefs(newAttributeDefsWithAllBuiltInTypes(PREFIX_ATTRIBUTE_NAME)); AtlasTransientTypeRegistry ttr = null; boolean commit = false; try { ttr = typesRegistry.lockTypeRegistryForUpdate(); ttr.addType(ret); commit = true; } catch (AtlasBaseException excp) { LOG.error("failed to create struct-def", excp); ret = null; } finally { typesRegistry.releaseTypeRegistryForUpdate(ttr, commit); } return ret; }
@Override public AtlasStructDef update(AtlasStructDef structDef) throws AtlasBaseException { if (LOG.isDebugEnabled()) { LOG.debug("==> AtlasStructDefStoreV1.update({})", structDef); } validateType(structDef); AtlasStructDef ret = StringUtils.isNotBlank(structDef.getGuid()) ? updateByGuid(structDef.getGuid(), structDef) : updateByName(structDef.getName(), structDef); if (LOG.isDebugEnabled()) { LOG.debug("<== AtlasStructDefStoreV1.update({}): {}", structDef, ret); } return ret; }
@Test public void testStructDefSetAttributeDefs() { AtlasStructDef structDef = ModelTestUtil.newStructDef(); List<AtlasAttributeDef> oldAttributes = structDef.getAttributeDefs(); List<AtlasAttributeDef> newttributes = ModelTestUtil.newAttributeDefsWithAllBuiltInTypes("newAttributes"); structDef.setAttributeDefs(newttributes); for (AtlasAttributeDef attributeDef : oldAttributes) { assertFalse(structDef.hasAttribute(attributeDef.getName())); } for (AtlasAttributeDef attributeDef : newttributes) { assertTrue(structDef.hasAttribute(attributeDef.getName())); } } }
@Test public void testStructDefRemoveAttribute() { AtlasStructDef structDef = ModelTestUtil.newStructDef(); String attrName = structDef.getAttributeDefs().get(0).getName(); assertTrue(structDef.hasAttribute(attrName)); structDef.removeAttribute(attrName); assertFalse(structDef.hasAttribute(attrName)); }
@Test public void testStructDefHasAttribute() { AtlasStructDef structDef = ModelTestUtil.getStructDef(); for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) { assertTrue(structDef.hasAttribute(attributeDef.getName())); } assertFalse(structDef.hasAttribute("01234-xyzabc-;''-)(")); }
for (AtlasStructDef.AtlasAttributeDef attr : incoming.getAttributeDefs()) { if(existing.getAttribute(attr.getName()) == null) { difference.add(attr); def.addAttribute(ad);
private static TypesDef enumToTypesDef(AtlasEnumType enumType) { TypesDef ret = null; AtlasEnumDef enumDef = enumType.getEnumDef(); String enumName = enumDef.getName(); String enumDesc = enumDef.getDescription(); String enumVersion = enumDef.getTypeVersion(); EnumValue[] enumValues = getEnumValues(enumDef.getElementDefs()); if (enumName != null && enumValues != null && enumValues.length > 0) { EnumTypeDefinition enumTypeDef = new EnumTypeDefinition(enumName, enumDesc, enumVersion, enumValues); ret = TypesUtil.getTypesDef(ImmutableList.of(enumTypeDef), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.<HierarchicalTypeDefinition<ClassType>>of()); } return ret; }
public static AtlasStructDef toStructDef(AtlasVertex vertex, AtlasStructDef structDef, AtlasTypeDefGraphStoreV2 typeDefStore) throws AtlasBaseException { AtlasStructDef ret = (structDef != null) ? structDef : new AtlasStructDef(); typeDefStore.vertexToTypeDef(vertex, ret); List<AtlasAttributeDef> attributeDefs = new ArrayList<>(); String typePropertyKey = AtlasGraphUtilsV2.getTypeDefPropertyKey(ret); String encodedTypePropertyKey = AtlasGraphUtilsV2.encodePropertyKey(typePropertyKey); List<String> attrNames = vertex.getProperty(encodedTypePropertyKey, List.class); if (CollectionUtils.isNotEmpty(attrNames)) { for (String attrName : attrNames) { String attrPropertyKey = AtlasGraphUtilsV2.getTypeDefPropertyKey(ret, attrName); String encodedAttrPropertyKey = AtlasGraphUtilsV2.encodePropertyKey(attrPropertyKey); String attrJson = vertex.getProperty(encodedAttrPropertyKey, String.class); attributeDefs.add(toAttributeDefFromJson(structDef, AtlasType.fromJson(attrJson, Map.class), typeDefStore)); } } ret.setAttributeDefs(attributeDefs); return ret; }
private void resolveConstraints(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { for (AtlasAttributeDef attributeDef : getStructDef().getAttributeDefs()) { if (CollectionUtils.isEmpty(attributeDef.getConstraints())) { continue; AtlasAttributeDef inverseRefAttrDef = attrType.getStructDef().getAttribute(inverseRefAttrName);
private static boolean updateTypeAttributes(AtlasStructDef oldStructDef, AtlasStructDef newStructDef) { boolean ret = isTypeUpdateApplicable(oldStructDef, newStructDef); if (ret) { // make sure that all attributes in oldDef are in newDef as well if (CollectionUtils.isNotEmpty(oldStructDef.getAttributeDefs())){ for (AtlasAttributeDef oldAttrDef : oldStructDef.getAttributeDefs()) { if (!newStructDef.hasAttribute(oldAttrDef.getName())) { newStructDef.addAttribute(oldAttrDef); } } } } return ret; }
private void updateCollectionWithDifferingAttributes(List<AtlasStructDef.AtlasAttributeDef> difference, AtlasStructDef existing, AtlasStructDef.AtlasAttributeDef incoming) throws AtlasBaseException { AtlasStructDef.AtlasAttributeDef existingAttribute = existing.getAttribute(incoming.getName()); if (existingAttribute == null) { difference.add(incoming); } else { if (!existingAttribute.getTypeName().equals(incoming.getTypeName())) { LOG.error("Attribute definition difference found: {}, {}", existingAttribute, incoming); throw new AtlasBaseException(AtlasErrorCode.INVALID_IMPORT_ATTRIBUTE_TYPE_CHANGED, existing.getName(), existingAttribute.getName(), existingAttribute.getTypeName(), incoming.getTypeName()); } } }
AtlasStructDef updatedDef = new AtlasStructDef((AtlasStructDef)typeDef); updatedDef.addAttribute(attributeDef); updatedDef.setTypeVersion(patch.getUpdateToVersion());