Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #179 from cmgrote/master
Browse files Browse the repository at this point in the history
Supertype mapping
  • Loading branch information
cmgrote authored Aug 13, 2020
2 parents 5f53bc7 + f6027b4 commit 64c4cc6
Show file tree
Hide file tree
Showing 14 changed files with 1,003 additions and 607 deletions.
2 changes: 1 addition & 1 deletion apache-atlas-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>egeria-connector-hadoop-ecosystem</artifactId>
<groupId>org.odpi.egeria</groupId>
<version>2.0-SNAPSHOT</version>
<version>2.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.instances.*;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.AttributeTypeDef;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.TypeDef;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.TypeDefPatch;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.repositoryeventmapper.OMRSRepositoryEventMapperBase;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.RepositoryErrorException;
import org.slf4j.Logger;
Expand Down Expand Up @@ -441,6 +442,21 @@ public void sendNewAttributeTypeDefEvent(AttributeTypeDef newAttributeTypeDef) {
newAttributeTypeDef);
}

/**
* Sends an updated TypeDef event.
*
* @param typeDefPatch the patch that was applied to achieve the update
*/
public void sendUpdatedTypeDefEvent(TypeDefPatch typeDefPatch) {
repositoryEventProcessor.processUpdatedTypeDefEvent(
sourceName,
metadataCollectionId,
localServerName,
localServerType,
localOrganizationName,
typeDefPatch);
}

/**
* Sends a refresh entity request event.
*
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.atlas.model.discovery.SearchParameters;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasRelationship;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.odpi.egeria.connectors.apache.atlas.auditlog.ApacheAtlasOMRSAuditCode;
import org.odpi.egeria.connectors.apache.atlas.auditlog.ApacheAtlasOMRSErrorCode;
Expand All @@ -21,6 +22,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ApacheAtlasOMRSRepositoryConnector extends OMRSRepositoryConnector {

private static final Logger log = LoggerFactory.getLogger(ApacheAtlasOMRSRepositoryConnector.class);
Expand All @@ -29,13 +35,15 @@ public class ApacheAtlasOMRSRepositoryConnector extends OMRSRepositoryConnector

private String url;
private AtlasClientV2 atlasClient;
private Map<String, AtlasEntityDef> atlasEntityTypesByName;

private boolean successfulInit = false;

/**
* Default constructor used by the OCF Connector Provider.
*/
public ApacheAtlasOMRSRepositoryConnector() {
// Nothing to do...
atlasEntityTypesByName = new HashMap<>();
}

/**
Expand Down Expand Up @@ -190,12 +198,23 @@ public AtlasRelationship.AtlasRelationshipWithExtInfo getRelationshipByGUID(Stri
*
* @param typeDefs the TypeDefs to add to Apache Atlas
* @return AtlasTypesDef
* @throws AtlasServiceException if there is any error retrieving the relationship
* @throws AtlasServiceException if there is any error creating the type definition
*/
public AtlasTypesDef createTypeDef(AtlasTypesDef typeDefs) throws AtlasServiceException {
return atlasClient.createAtlasTypeDefs(typeDefs);
}

/**
* Updates the list of TypeDefs provided in Apache Atlas.
*
* @param typeDefs the TypeDefs to update in Apache Atlas
* @return AtlasTypesDef
* @throws AtlasServiceException if there is any error applying the update
*/
public AtlasTypesDef updateTypeDef(AtlasTypesDef typeDefs) throws AtlasServiceException {
return atlasClient.updateAtlasTypeDefs(typeDefs);
}

/**
* Search for entities based on the provided parameters.
*
Expand All @@ -220,6 +239,23 @@ public AtlasSearchResult searchWithDSL(String dslQuery) throws AtlasServiceExcep
return atlasClient.dslSearch(dslQuery);
}

/**
* Resolve the parent type for the provided Apache Atlas entity type.
*
* @param type the Apache Atlas entity type
* @return String the Apache Atlas parent entity type
*/
public String getParentTypeForAtlasEntityType(String type) {
AtlasEntityDef atlasEntityDef = atlasEntityTypesByName.getOrDefault(type, null);
if (atlasEntityDef != null) {
Set<String> superTypes = atlasEntityDef.getSuperTypes();
if (superTypes != null && !superTypes.isEmpty()) {
return superTypes.iterator().next();
}
}
return null;
}

/**
* Save the entity provided to Apache Atlas.
*
Expand Down Expand Up @@ -274,6 +310,14 @@ private void connectToAtlas(String methodName) throws ConnectorCheckedException
raiseConnectorCheckedException(ApacheAtlasOMRSErrorCode.REST_CLIENT_FAILURE, methodName, null, getBaseURL());
} else {

List<AtlasEntityDef> atlasEntityDefs = atlasTypes.getEntityDefs();
if (atlasEntityDefs != null) {
for (AtlasEntityDef atlasEntityDef : atlasEntityDefs) {
String atlasEntityTypeName = atlasEntityDef.getName();
atlasEntityTypesByName.put(atlasEntityTypeName, atlasEntityDef);
}
}

auditLog.logMessage(methodName, ApacheAtlasOMRSAuditCode.CONNECTED_TO_ATLAS.getMessageDefinition(getBaseURL()));

metadataCollection = new ApacheAtlasOMRSMetadataCollection(this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
package org.odpi.egeria.connectors.apache.atlas.repositoryconnector.mapping;

import org.apache.atlas.model.typedef.*;
import org.odpi.egeria.connectors.apache.atlas.auditlog.ApacheAtlasOMRSErrorCode;
import org.odpi.egeria.connectors.apache.atlas.repositoryconnector.stores.AttributeTypeDefStore;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.*;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.PatchErrorException;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.TypeDefNotSupportedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -183,4 +186,46 @@ private static String getAtlasPrimitiveNameFromOMRSPrimitiveName(String omrsPrim

}

/**
* Throws a TypeDefNotSupportedException using the provided parameters.
* @param errorCode the error code for the exception
* @param methodName the method throwing the exception
* @param cause the underlying cause of the exception (if any, otherwise null)
* @param params any parameters for formatting the error message
* @throws TypeDefNotSupportedException always
*/
protected static void raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode errorCode, String methodName, Throwable cause, String ...params) throws TypeDefNotSupportedException {
if (cause == null) {
throw new TypeDefNotSupportedException(errorCode.getMessageDefinition(params),
RelationshipDefMapping.class.getName(),
methodName);
} else {
throw new TypeDefNotSupportedException(errorCode.getMessageDefinition(params),
RelationshipDefMapping.class.getName(),
methodName,
cause);
}
}

/**
* Throws a PatchErrorException using the provided parameters.
* @param errorCode the error code for the exception
* @param methodName the method throwing the exception
* @param cause the underlying cause of the exception (if any, otherwise null)
* @param params any parameters for formatting the error message
* @throws PatchErrorException always
*/
protected static void raisePatchErrorException(ApacheAtlasOMRSErrorCode errorCode, String methodName, Throwable cause, String ...params) throws PatchErrorException {
if (cause == null) {
throw new PatchErrorException(errorCode.getMessageDefinition(params),
RelationshipDefMapping.class.getName(),
methodName);
} else {
throw new PatchErrorException(errorCode.getMessageDefinition(params),
RelationshipDefMapping.class.getName(),
methodName,
cause);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.odpi.egeria.connectors.apache.atlas.repositoryconnector.stores.AttributeTypeDefStore;
import org.odpi.egeria.connectors.apache.atlas.repositoryconnector.stores.TypeDefStore;
import org.odpi.openmetadata.repositoryservices.connectors.stores.metadatacollectionstore.properties.typedefs.*;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.PatchErrorException;
import org.odpi.openmetadata.repositoryservices.ffdc.exception.TypeDefNotSupportedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -47,6 +48,73 @@ public static void addClassificationTypeToAtlas(ClassificationDef omrsClassifica

final String methodName = "addClassificationTypeToAtlas";

String omrsTypeDefName = omrsClassificationDef.getName();
AtlasTypesDef atlasTypesDef = setupClassificationType(omrsClassificationDef, typeDefStore, attributeDefStore);

if (atlasTypesDef != null) {
try {
atlasRepositoryConnector.createTypeDef(atlasTypesDef);
typeDefStore.addTypeDef(omrsClassificationDef);
} catch (AtlasServiceException e) {
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, e, omrsTypeDefName, atlasRepositoryConnector.getServerName());
}
} else {
// Otherwise, we'll drop it as unimplemented
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, null, omrsTypeDefName, atlasRepositoryConnector.getServerName());
}

}

/**
* Updates the provided OMRS type definition in Apache Atlas (if possible), or throws a PatchErrorException
* if not possible.
*
* @param omrsClassificationDef the OMRS ClassificationDef to add to Apache Atlas
* @param typeDefStore the store of mapped / implemented TypeDefs in Apache Atlas
* @param attributeDefStore the store of mapped / implemented AttributeTypeDefs in Apache Atlas
* @param atlasRepositoryConnector connectivity to the Apache Atlas environment
* @throws PatchErrorException if the patched typedef cannot be fully represented in Atlas
*/
public static void updateClassificationTypeInAtlas(ClassificationDef omrsClassificationDef,
TypeDefStore typeDefStore,
AttributeTypeDefStore attributeDefStore,
ApacheAtlasOMRSRepositoryConnector atlasRepositoryConnector) throws PatchErrorException {

final String methodName = "updateClassificationTypeInAtlas";

String omrsTypeDefName = omrsClassificationDef.getName();
AtlasTypesDef atlasTypesDef = setupClassificationType(omrsClassificationDef, typeDefStore, attributeDefStore);

if (atlasTypesDef != null) {
try {
atlasRepositoryConnector.updateTypeDef(atlasTypesDef);
typeDefStore.addTypeDef(omrsClassificationDef);
} catch (AtlasServiceException e) {
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raisePatchErrorException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, e, omrsTypeDefName, atlasRepositoryConnector.getServerName());
}
} else {
// Otherwise, we'll drop it as unimplemented
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raisePatchErrorException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, null, omrsTypeDefName, atlasRepositoryConnector.getServerName());
}

}

/**
* Setup the provided OMRS type definition as an Apache Atlas type definition (if possible), or return null if
* not possible.
*
* @param omrsClassificationDef the OMRS ClassificationDef to add to Apache Atlas
* @param typeDefStore the store of mapped / implemented TypeDefs in Apache Atlas
* @param attributeDefStore the store of mapped / implemented AttributeTypeDefs in Apache Atlas
*/
private static AtlasTypesDef setupClassificationType(ClassificationDef omrsClassificationDef,
TypeDefStore typeDefStore,
AttributeTypeDefStore attributeDefStore) {

String omrsTypeDefName = omrsClassificationDef.getName();
boolean fullyCovered = true;

Expand All @@ -73,46 +141,16 @@ public static void addClassificationTypeToAtlas(ClassificationDef omrsClassifica

fullyCovered = fullyCovered && setupPropertyMappings(omrsClassificationDef, classificationTypeDef, attributeDefStore);

AtlasTypesDef atlasTypesDef = null;
if (fullyCovered) {
// Only create the classification if we can fully model it
AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
atlasTypesDef = new AtlasTypesDef();
List<AtlasClassificationDef> classificationList = new ArrayList<>();
classificationList.add(classificationTypeDef);
atlasTypesDef.setClassificationDefs(classificationList);
try {
atlasRepositoryConnector.createTypeDef(atlasTypesDef);
typeDefStore.addTypeDef(omrsClassificationDef);
} catch (AtlasServiceException e) {
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, e, omrsClassificationDef.getName(), atlasRepositoryConnector.getServerName());
}
} else {
// Otherwise, we'll drop it as unimplemented
typeDefStore.addUnimplementedTypeDef(omrsClassificationDef);
raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode.TYPEDEF_NOT_SUPPORTED, methodName, null, omrsClassificationDef.getName(), atlasRepositoryConnector.getServerName());
}
return atlasTypesDef;

}

/**
* Throws a TypeDefNotSupportedException using the provided parameters.
* @param errorCode the error code for the exception
* @param methodName the method throwing the exception
* @param cause the underlying cause of the exception (if any, otherwise null)
* @param params any parameters for formatting the error message
* @throws TypeDefNotSupportedException always
*/
private static void raiseTypeDefNotSupportedException(ApacheAtlasOMRSErrorCode errorCode, String methodName, Throwable cause, String ...params) throws TypeDefNotSupportedException {
if (cause == null) {
throw new TypeDefNotSupportedException(errorCode.getMessageDefinition(params),
ClassificationDefMapping.class.getName(),
methodName);
} else {
throw new TypeDefNotSupportedException(errorCode.getMessageDefinition(params),
ClassificationDefMapping.class.getName(),
methodName,
cause);
}
}

}
Loading

0 comments on commit 64c4cc6

Please sign in to comment.