Commit aaf2971a by Hemanth Yamijala

ATLAS-491 Business Catalog / Taxonomy (jspeidel via yhemanth)

parent b65dd91c
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.atlas</groupId>
<artifactId>apache-atlas</artifactId>
<version>0.7-incubating-SNAPSHOT</version>
</parent>
<artifactId>atlas-catalog</artifactId>
<description>Apache Atlas Business Catalog Module</description>
<name>Apache Atlas Business Catalog</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.apache.atlas</groupId>
<artifactId>atlas-repository</artifactId>
</dependency>
<dependency>
<groupId>org.apache.atlas</groupId>
<artifactId>atlas-typesystem</artifactId>
</dependency>
<dependency>
<groupId>org.apache.atlas</groupId>
<artifactId>atlas-server-api</artifactId>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-throwingproviders</artifactId>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-multibindings</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
</dependency>
<dependency>
<groupId>com.tinkerpop.gremlin</groupId>
<artifactId>gremlin-java</artifactId>
</dependency>
<!-- testing -->
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/log4j.xml</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.exception.ResourceAlreadyExistsException;
import java.util.Map;
/**
* Abstraction for Atlas Type System.
*/
public interface AtlasTypeSystem {
/**
* Create a Type in the Atlas Type System if it doesn't already exist.
* If the type already exists, this method has no affect.
*
* @param resourceDefinition resource definition for type being created
* @param name type name
* @param description description of the type being created
*
* @throws ResourceAlreadyExistsException if entity already exists
*/
void createClassType(ResourceDefinition resourceDefinition, String name, String description)
throws ResourceAlreadyExistsException;
/**
* Create an entity in the Atlas Type System for the provided request and resource definition.
* If Type associated with the entity doesn't already exist, it is created.
*
* @param definition the definition of the resource for which we are creating the entity
* @param request the user request
*
* @throws ResourceAlreadyExistsException if type already exists
*/
void createEntity(ResourceDefinition definition, Request request)
throws ResourceAlreadyExistsException;
/**
* Create a trait instance instance in the Atlas Type System.
*
* @param resourceDefinition resource definition for trait type being created
* @param name type name
* @param description description of the type being created
*
* @throws ResourceAlreadyExistsException if type already exists
*/
void createTraitType(ResourceDefinition resourceDefinition, String name, String description)
throws ResourceAlreadyExistsException;
/**
* Create a trait instance in the Atlas Type System and associate it with the entity identified by the provided guid.
*
* @param guid id of the entity which will be associated with the trait instance
* @param typeName type name of the trait
* @param properties property map used to populate the trait instance
*
* @throws ResourceAlreadyExistsException if trait instance is already associated with the entity
*/
void createTraitInstance(String guid, String typeName, Map<String, Object> properties)
throws ResourceAlreadyExistsException;
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
/**
* Base user API request.
*/
public abstract class BaseRequest implements Request {
private final Map<String, Object> properties = new HashMap<>();
private final String queryString;
private final Collection<String> additionalSelectProperties = new HashSet<>();
protected BaseRequest(Map<String, Object> properties, String queryString) {
if (properties != null) {
this.properties.putAll((properties));
}
this.queryString = queryString;
}
public Map<String, Object> getProperties() {
return properties;
}
public <T> T getProperty(String name) {
return (T)properties.get(name);
}
public String getQueryString() {
return queryString;
}
@Override
public void addAdditionalSelectProperties(Collection<String> resultProperties) {
additionalSelectProperties.addAll(resultProperties);
}
@Override
public Collection<String> getAdditionalSelectProperties() {
return additionalSelectProperties;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.query.QueryFactory;
import java.util.*;
/**
* Base class for resource providers.
*/
public abstract class BaseResourceProvider implements ResourceProvider {
protected AtlasTypeSystem typeSystem;
protected QueryFactory queryFactory = new QueryFactory();
protected BaseResourceProvider(AtlasTypeSystem typeSystem) {
this.typeSystem = typeSystem;
}
protected void setQueryFactory(QueryFactory factory) {
queryFactory = factory;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.Map;
/**
* A request for a collection resource.
*/
public class CollectionRequest extends BaseRequest {
public CollectionRequest(Map<String, Object> properties, String queryString) {
super(properties, queryString);
}
@Override
public Cardinality getCardinality() {
return Cardinality.COLLECTION;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* Format a date field which is represented as a long.
*/
public class DefaultDateFormatter implements PropertyValueFormatter<Long, String> {
//todo: obtain format from atlas proper
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
@Override
public String format(Long l) {
Calendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(l);
return DATE_FORMAT.format(calendar.getTime());
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.AtlasException;
import org.apache.atlas.catalog.exception.CatalogRuntimeException;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.typesystem.types.FieldMapping;
import org.apache.atlas.typesystem.types.HierarchicalType;
import org.apache.atlas.typesystem.types.TypeSystem;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Default property mapper which translates property names to/from name exposed in API to internal fully qualified name.
*/
public class DefaultPropertyMapper implements PropertyMapper {
//todo: abstract HierarchicalType
private Map<String, HierarchicalType> typeInstances = new HashMap<>();
private final Map<String, String> m_qualifiedToCleanMap = new HashMap<>();
private final Map<String, String> m_cleanToQualifiedMap = new HashMap<>();
public DefaultPropertyMapper() {
this(Collections.<String, String>emptyMap(), Collections.<String, String>emptyMap());
}
public DefaultPropertyMapper(Map<String, String> qualifiedToCleanMap,
Map<String, String> cleanToQualifiedMap) {
setDefaultMappings();
m_qualifiedToCleanMap.putAll(qualifiedToCleanMap);
m_cleanToQualifiedMap.putAll(cleanToQualifiedMap);
}
@Override
public String toCleanName(String propName, String type) {
HierarchicalType dataType = getDataType(type);
String replacement = m_qualifiedToCleanMap.get(propName);
if (replacement == null && dataType != null) {
FieldMapping fieldMap = dataType.fieldMapping();
if (! fieldMap.fields.containsKey(propName) && propName.contains(".")) {
String cleanName = propName.substring(propName.lastIndexOf('.') + 1);
if (fieldMap.fields.containsKey(cleanName)) {
replacement = cleanName;
}
}
}
if (replacement == null) {
replacement = propName;
}
return replacement;
}
@Override
public String toFullyQualifiedName(String propName, String type) {
HierarchicalType dataType = getDataType(type);
String replacement = m_cleanToQualifiedMap.get(propName);
if (replacement == null && dataType != null) {
FieldMapping fieldMap = dataType.fieldMapping();
if (fieldMap.fields.containsKey(propName)) {
try {
replacement = dataType.getQualifiedName(propName);
} catch (AtlasException e) {
throw new CatalogRuntimeException(String.format(
"Unable to resolve fully qualified property name for type '%s': %s", type, e), e);
}
}
}
if (replacement == null) {
replacement = propName;
}
return replacement;
}
//todo: abstract this via AtlasTypeSystem
protected synchronized HierarchicalType getDataType(String type) {
HierarchicalType dataType = typeInstances.get(type);
//todo: are there still cases where type can be null?
if (dataType == null) {
dataType = createDataType(type);
typeInstances.put(type, dataType);
}
return dataType;
}
protected HierarchicalType createDataType(String type) {
try {
return TypeSystem.getInstance().getDataType(HierarchicalType.class, type);
} catch (AtlasException e) {
throw new CatalogRuntimeException("Unable to get type instance from type system for type: " + type, e);
}
}
private void setDefaultMappings() {
//todo: these are all internal "__*" properties
//todo: should be able to ask type system for the "clean" name for these
m_qualifiedToCleanMap.put(Constants.GUID_PROPERTY_KEY, "id");
m_cleanToQualifiedMap.put("id", Constants.GUID_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.TIMESTAMP_PROPERTY_KEY, "creation_time");
m_cleanToQualifiedMap.put("creation_time", Constants.TIMESTAMP_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, "modified_time");
m_cleanToQualifiedMap.put("modified_time", Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.ENTITY_TYPE_PROPERTY_KEY, "type");
m_cleanToQualifiedMap.put("type", Constants.ENTITY_TYPE_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.VERSION_PROPERTY_KEY, "version");
m_cleanToQualifiedMap.put("version", Constants.VERSION_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.TRAIT_NAMES_PROPERTY_KEY, "trait_names");
m_cleanToQualifiedMap.put("trait_names", Constants.TRAIT_NAMES_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.SUPER_TYPES_PROPERTY_KEY, "super_types");
m_cleanToQualifiedMap.put("super_types", Constants.SUPER_TYPES_PROPERTY_KEY);
m_qualifiedToCleanMap.put(Constants.STATE_PROPERTY_KEY, "state");
m_cleanToQualifiedMap.put("state", Constants.STATE_PROPERTY_KEY);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.AtlasException;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.exception.CatalogRuntimeException;
import org.apache.atlas.catalog.exception.ResourceAlreadyExistsException;
import org.apache.atlas.services.MetadataService;
import org.apache.atlas.typesystem.ITypedReferenceableInstance;
import org.apache.atlas.typesystem.Referenceable;
import org.apache.atlas.typesystem.Struct;
import org.apache.atlas.typesystem.exception.EntityExistsException;
import org.apache.atlas.typesystem.exception.TypeExistsException;
import org.apache.atlas.typesystem.json.TypesSerialization;
import org.apache.atlas.typesystem.types.*;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* Default implementation.
*/
public class DefaultTypeSystem implements AtlasTypeSystem {
private final MetadataService metadataService;
/**
* Constructor.
*
* @param metadataService atlas metadata service
*/
public DefaultTypeSystem(MetadataService metadataService) {
this.metadataService = metadataService;
}
@Override
public void createEntity(ResourceDefinition definition, Request request) throws ResourceAlreadyExistsException {
String typeName = definition.getTypeName();
try {
createClassType(definition, typeName, typeName + " Definition");
} catch (ResourceAlreadyExistsException e) {
// ok if type already exists
}
try {
Referenceable entity = new Referenceable(typeName, request.getProperties());
ITypedReferenceableInstance typedInstance = metadataService.getTypedReferenceableInstance(entity);
metadataService.createEntities(Collections.singletonList(typedInstance).toArray(new ITypedReferenceableInstance[1]));
} catch (EntityExistsException e) {
throw new ResourceAlreadyExistsException(
"Attempted to create an entity which already exists: " + request.getProperties());
} catch (AtlasException e) {
throw new CatalogRuntimeException("An expected exception occurred creating an entity: " + e, e);
}
}
@Override
public void createClassType(ResourceDefinition resourceDefinition, String name, String description)
throws ResourceAlreadyExistsException {
createType(resourceDefinition.getPropertyDefinitions(), ClassType.class, name, description, false);
}
@Override
public void createTraitType(ResourceDefinition resourceDefinition, String name, String description)
throws ResourceAlreadyExistsException {
createType(resourceDefinition.getPropertyDefinitions(), TraitType.class, name, description, true);
}
public void createTraitInstance(String guid, String typeName, Map<String, Object> properties)
throws ResourceAlreadyExistsException {
try {
// not using the constructor with properties argument because it is marked 'InterfaceAudience.Private'
Struct struct = new Struct(typeName);
for (Map.Entry<String, Object> propEntry : properties.entrySet()) {
struct.set(propEntry.getKey(), propEntry.getValue());
}
metadataService.addTrait(guid, metadataService.createTraitInstance(struct));
} catch (IllegalArgumentException e) {
//todo: unfortunately, IllegalArgumentException can be thrown for other reasons
if (e.getMessage().contains("is already defined for entity")) {
throw new ResourceAlreadyExistsException(
String.format("Tag '%s' already associated with the entity", typeName));
} else {
throw e;
}
} catch (AtlasException e) {
throw new CatalogRuntimeException(String.format(
"Unable to create trait instance '%s' in type system: %s", typeName, e), e);
}
}
private <T extends HierarchicalType> void createType(Collection<AttributeDefinition> attributes,
Class<T> type,
String name,
String description,
boolean isTrait)
throws ResourceAlreadyExistsException {
try {
HierarchicalTypeDefinition<T> definition = new HierarchicalTypeDefinition<>(type, name, description, null,
attributes.toArray(new AttributeDefinition[attributes.size()]));
metadataService.createType(TypesSerialization.toJson(definition, isTrait));
} catch (TypeExistsException e) {
throw new ResourceAlreadyExistsException(String.format("Type '%s' already exists", name));
} catch (AtlasException e) {
throw new CatalogRuntimeException(String.format(
"Unable to create type '%s' in type system: %s", name, e), e);
}
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.EntityResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import java.util.*;
/**
* Provider for entity resources.
*/
public class EntityResourceProvider extends BaseResourceProvider implements ResourceProvider {
private final static ResourceDefinition resourceDefinition = new EntityResourceDefinition();
public EntityResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem);
}
@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
AtlasQuery atlasQuery;
try {
atlasQuery = queryFactory.createEntityQuery(request);
} catch (InvalidQueryException e) {
throw new CatalogRuntimeException("Unable to compile internal Entity query: " + e, e);
}
Collection<Map<String, Object>> results = atlasQuery.execute();
if (results.isEmpty()) {
throw new ResourceNotFoundException(String.format("Entity '%s' not found.",
request.getProperty(resourceDefinition.getIdPropertyName())));
}
return new Result(results);
}
@Override
public Result getResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
AtlasQuery atlasQuery = queryFactory.createEntityQuery(request);
return new Result(atlasQuery.execute());
}
@Override
public void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException {
// creation of entities is currently unsupported
throw new UnsupportedOperationException("Creation of entities is not currently supported");
}
@Override
public Collection<String> createResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
throw new UnsupportedOperationException("Creation of entities is not currently supported");
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import java.util.*;
/**
* Provider for entity tag resources.
*/
public class EntityTagResourceProvider extends BaseResourceProvider implements ResourceProvider {
private final static ResourceDefinition resourceDefinition = new EntityTagResourceDefinition();
private TermResourceProvider termResourceProvider;
public EntityTagResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem);
}
@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
AtlasQuery atlasQuery;
try {
atlasQuery = queryFactory.createEntityTagQuery(request);
} catch (InvalidQueryException e) {
throw new CatalogRuntimeException("Unable to compile internal Entity Tag query: " + e, e);
}
Collection<Map<String, Object>> results = atlasQuery.execute();
if (results.isEmpty()) {
throw new ResourceNotFoundException(String.format("Tag '%s' not found.",
request.getProperty(resourceDefinition.getIdPropertyName())));
}
return new Result(results);
}
@Override
public Result getResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
AtlasQuery atlasQuery = queryFactory.createEntityTagQuery(request);
return new Result(atlasQuery.execute());
}
@Override
public void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException {
String entityId = String.valueOf(request.getProperties().remove("id"));
resourceDefinition.validate(request);
Result termResult = getTermQueryResult(request.<String>getProperty("name"));
Map<String, Object> termProperties = termResult.getPropertyMaps().iterator().next();
//todo: use constant for property name
if (String.valueOf(termProperties.get("available_as_tag")).equals("false")) {
throw new InvalidPayloadException(
"Attempted to tag an entity with a term which is not available to be tagged");
}
tagEntities(Collections.singleton(entityId), termProperties);
}
//todo: response for case mixed case where some subset of creations fail
@Override
public Collection<String> createResources(Request request)
throws InvalidQueryException, ResourceNotFoundException, ResourceAlreadyExistsException {
Collection<String> relativeUrls = new ArrayList<>();
AtlasQuery atlasQuery = queryFactory.createEntityQuery(request);
Collection<String> guids = new ArrayList<>();
for (Map<String, Object> entityMap: atlasQuery.execute()) {
guids.add(String.valueOf(entityMap.get("id")));
}
Collection<Map<String, String>> tagMaps = request.getProperty("tags");
for (Map<String, String> tagMap : tagMaps) {
Result termResult = getTermQueryResult(tagMap.get("name"));
relativeUrls.addAll(tagEntities(guids, termResult.getPropertyMaps().iterator().next()));
}
return relativeUrls;
}
private Result getTermQueryResult(String termName) throws ResourceNotFoundException {
Request tagRequest = new InstanceRequest(
Collections.<String, Object>singletonMap("termPath", new TermPath(termName)));
tagRequest.addAdditionalSelectProperties(Collections.singleton("type"));
return getTermResourceProvider().getResourceById(tagRequest);
}
private Collection<String> tagEntities(Collection<String> entityGuids, Map<String, Object> termProperties)
throws ResourceAlreadyExistsException {
Collection<String> relativeUrls = new ArrayList<>();
for (String guid : entityGuids) {
//createTermEdge(entity, Collections.singleton(termVertex));
// copy term properties from trait associated with taxonomy to be set
// on trait associated with new entity (basically clone at time of tag event)
//todo: any changes to 'singleton' trait won't be reflected in new trait
//todo: iterate over properties in term definition instead of hard coding here
Map<String, Object> properties = new HashMap<>();
String termName = String.valueOf(termProperties.get("name"));
properties.put("name", termName);
properties.put("description", termProperties.get("description"));
typeSystem.createTraitInstance(guid, termName, properties);
//todo: *** shouldn't know anything about href structure in this class ***
relativeUrls.add(String.format("v1/entities/%s/tags/%s", guid, termName));
}
return relativeUrls;
}
protected synchronized ResourceProvider getTermResourceProvider() {
if (termResourceProvider == null) {
termResourceProvider = new TermResourceProvider(typeSystem);
}
return termResourceProvider;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.Map;
/**
* A request for an instance resource.
*/
public class InstanceRequest extends BaseRequest {
public InstanceRequest(Map<String, Object> properties) {
super(properties, null);
}
@Override
public Cardinality getCardinality() {
return Cardinality.INSTANCE;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import com.google.gson.stream.JsonWriter;
import org.apache.atlas.catalog.exception.CatalogRuntimeException;
import javax.ws.rs.core.UriInfo;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* JSON serializer.
*/
public class JsonSerializer {
public String serialize(Result result, UriInfo ui) {
Writer json = new StringWriter();
JsonWriter writer = new JsonWriter(json);
writer.setIndent(" ");
try {
writeValue(writer, result.getPropertyMaps(), ui.getBaseUri().toASCIIString());
} catch (IOException e) {
throw new CatalogRuntimeException("Unable to write JSON response.", e);
}
return json.toString();
}
private void writeValue(JsonWriter writer, Object value, String baseUrl) throws IOException {
if (value == null) {
writer.nullValue();
} else if (value instanceof Map) {
writer.beginObject();
LinkedHashMap<String, Object> nonScalarMap = new LinkedHashMap<>();
for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet()) {
String key = entry.getKey();
Object val = entry.getValue();
if (val == null || ! (val instanceof Collection || val instanceof Map)) {
//todo: use a token in value instead of prop name
if (key.equals("href")) {
val = baseUrl + String.valueOf(val);
}
writer.name(key);
writeValue(writer, val, baseUrl);
} else {
nonScalarMap.put(key, val);
}
}
for (Map.Entry<String, Object> entry : nonScalarMap.entrySet()) {
writer.name(entry.getKey());
writeValue(writer, entry.getValue(), baseUrl);
}
writer.endObject();
} else if (value instanceof Collection) {
writer.beginArray();
for (Object o : (Collection) value) {
writeValue(writer, o, baseUrl);
}
writer.endArray();
} else if (value instanceof Number) {
writer.value((Number) value);
} else if (value instanceof Boolean) {
writer.value((Boolean) value);
} else {
// everything else is String
writer.value(String.valueOf(value));
}
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
/**
* Translates property names to/from name exposed in API to internal fully qualified name.
*/
public interface PropertyMapper {
/**
* Translate a qualified name to a clean name.
*
* @param propName property name to translate
* @param type resource type
*
* @return clean property name
*/
String toCleanName(String propName, String type);
/**
* Translate a clean name to a fully qualified name.
*
* @param propName property name to translate
* @param type resource type
*
* @return fully qualified property name
*/
String toFullyQualifiedName(String propName, String type);
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
/**
* A rule for translating a property value.
*/
public interface PropertyValueFormatter <T,V> {
/**
* Format a property value.
*
* @param value property value to format
*
* @return formatted property value
*/
V format(T value);
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.Collection;
import java.util.Map;
/**
* Represents a user request.
*/
public interface Request {
/**
* Request cardinality enum.
*/
enum Cardinality {INSTANCE, COLLECTION}
/**
* Get request properties.
*
* @return request property map
*/
Map<String, Object> getProperties();
/**
* Get the value of a specified property.
*
* @param name property name
* @param <T> value type
*
* @return value for the requested property or null if property not in map
*/
<T> T getProperty(String name);
/**
* Get the query string.
*
* @return the user specified query string or null
*/
String getQueryString();
/**
* Get the cardinality of the request.
*
* @return the request cardinality
*/
Cardinality getCardinality();
/**
* Add additional property names which should be returned in the result.
*
* @param resultProperties collection of property names
*/
void addAdditionalSelectProperties(Collection<String> resultProperties);
/**
* Get any additional property names which should be included in the result.
*
* @return collection of added property names or an empty collection
*/
Collection<String> getAdditionalSelectProperties();
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* Provides key ordering for resource property maps.
* Ordering can be defined explicitly for specific properties,
* otherwise natural ordering is used.
*/
public class ResourceComparator implements Comparator<String> {
private static List<String> ordering = new ArrayList<>();
@Override
public int compare(String s1, String s2) {
if (s1.equals(s2)) {
return 0;
}
int s1Order = ordering.indexOf(s1);
int s2Order = ordering.indexOf(s2);
if (s1Order == -1 && s2Order == -1) {
return s1.compareTo(s2);
}
if (s1Order != -1 && s2Order != -1) {
return s1Order - s2Order;
}
return s1Order == -1 ? 1 : -1;
}
//todo: each resource definition can provide its own ordering list
static {
ordering.add("href");
ordering.add("name");
ordering.add("id");
ordering.add("description");
ordering.add("type");
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.exception.*;
import java.util.Collection;
/**
* Provider for a resource type.
*/
public interface ResourceProvider {
/**
* Get a resource by primary key.
*
* @param request request instance which contains the required id properties and no query string
* @return result containing the requested resource; never null
*
* @throws ResourceNotFoundException if the requested resource isn't found
*/
Result getResourceById(Request request) throws ResourceNotFoundException;
/**
* Get all resources which match the provider query.
*
* @param request request instance which will include a query string and possibly properties
* @return result containing collection of matching resources. If no resources match
* a result is returned with no resources
*
* @throws InvalidQueryException if the user query contains invalid syntax
* @throws ResourceNotFoundException if a parent resource of the requested resource doesn't exist
*/
Result getResources(Request request) throws InvalidQueryException, ResourceNotFoundException;
/**
* Create a single resource.
*
* @param request request instance containing the contents of the resource to create
*
* @throws InvalidPayloadException if the payload or any other part of the user request is invalid
* @throws ResourceAlreadyExistsException if the resource already exists
* @throws ResourceNotFoundException if a parent of the resource to create doesn't exist
*/
void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException;
//todo: define the behavior for partial success
/**
* Create multiple resources.
*
* @param request request instance containing the contents of 1..n resources
* @return collection of relative urls for the created resources
*
* @throws InvalidPayloadException if the payload or any other part of the user request is invalid
* @throws ResourceAlreadyExistsException if the resource already exists
* @throws ResourceNotFoundException if a parent of the resource to create doesn't exist
*/
Collection<String> createResources(Request request) throws CatalogException;
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import java.util.Collection;
import java.util.Map;
/**
* Resource provider result.
*/
public class Result {
/**
* collection of property maps
*/
private Collection<Map<String, Object>> propertyMaps;
/**
* Constructor.
*
* @param propertyMaps collection of property maps
*/
public Result(Collection<Map<String, Object>> propertyMaps) {
this.propertyMaps = propertyMaps;
}
/**
* Obtain the result property maps.
*
* @return result property maps
*/
public Collection<Map<String, Object>> getPropertyMaps() {
return propertyMaps;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.definition.TaxonomyResourceDefinition;
import org.apache.atlas.catalog.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import java.util.*;
/**
* Provider for taxonomy resources.
*/
public class TaxonomyResourceProvider extends BaseResourceProvider implements ResourceProvider {
private static final ResourceDefinition resourceDefinition = new TaxonomyResourceDefinition();
public TaxonomyResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem);
}
@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
AtlasQuery atlasQuery;
try {
atlasQuery = queryFactory.createTaxonomyQuery(request);
} catch (InvalidQueryException e) {
throw new CatalogRuntimeException("Unable to compile internal Taxonomy query: " + e, e);
}
Collection<Map<String, Object>> results = atlasQuery.execute();
if (results.isEmpty()) {
throw new ResourceNotFoundException(String.format("Taxonomy '%s' not found.",
request.getProperty(resourceDefinition.getIdPropertyName())));
}
return new Result(results);
}
public Result getResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
AtlasQuery atlasQuery = queryFactory.createTaxonomyQuery(request);
return new Result(atlasQuery.execute());
}
public synchronized void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException {
resourceDefinition.validate(request);
ensureTaxonomyDoesntExist(request);
typeSystem.createEntity(resourceDefinition, request);
}
@Override
public Collection<String> createResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
throw new UnsupportedOperationException("Creating multiple Taxonomies in a request is not currently supported");
}
private void ensureTaxonomyDoesntExist(Request request) throws ResourceAlreadyExistsException {
try {
getResourceById(request);
throw new ResourceAlreadyExistsException(String.format("Taxonomy '%s' already exists.",
request.getProperty("name")));
} catch (ResourceNotFoundException e) {
// expected case
}
}
}
\ No newline at end of file
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
/**
* Term path information.
*/
//todo: split between Term and TermPath
public class TermPath {
private final String m_taxonomy;
private final String m_fqn;
private final String m_name;
private final String[] m_paths;
public TermPath(String fullyQualifiedName) {
m_fqn = fullyQualifiedName;
//todo: validation
int idx = fullyQualifiedName.indexOf('.');
if (idx != -1) {
m_taxonomy = fullyQualifiedName.substring(0, idx);
m_name = fullyQualifiedName.substring(idx + 1);
m_paths = m_name.split("\\.");
} else {
m_taxonomy = fullyQualifiedName;
m_name = null;
m_paths = new String[0];
}
}
public TermPath(String taxonomyName, String termName) {
m_taxonomy = taxonomyName;
m_name = termName != null && termName.isEmpty() ? null : termName;
if (m_name != null) {
m_fqn = String.format("%s.%s", taxonomyName, termName);
m_paths = termName.split("\\.");
} else {
m_fqn = taxonomyName;
m_paths = new String[0];
}
}
/**
* Get the absolute term name which is in the form of TAXONOMY_NAME.TERM_NAME
*
* @return absolute term name which includes the taxonomy name
*/
public String getFullyQualifiedName() {
return m_fqn;
}
/**
* Get the term name. This differs from the absolute name in that it doesn't
* include the taxonomy name.
*
* @return the term name
*/
public String getName() {
return m_name;
}
/**
* Get the short name for the term which doesn't include any taxonomy or parent information.
* @return term short name
*/
public String getShortName() {
return m_paths[m_paths.length - 1];
}
public String getPath() {
if (m_name == null) {
return "/";
} else {
int idx = m_fqn.indexOf('.');
int lastIdx = m_fqn.lastIndexOf('.');
return idx == lastIdx ? "/" :
m_fqn.substring(idx, lastIdx).replaceAll("\\.", "/");
}
}
public TermPath getParent() {
//todo: if this is the root path, throw exception
return new TermPath(m_taxonomy, m_name.substring(0, m_name.lastIndexOf('.')));
}
public String getTaxonomyName() {
return m_taxonomy;
}
public String[] getPathSegments() {
return m_paths;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.definition.TermResourceDefinition;
import org.apache.atlas.catalog.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import java.util.*;
/**
* Provider for Term resources.
*/
public class TermResourceProvider extends BaseResourceProvider implements ResourceProvider {
private final static ResourceDefinition resourceDefinition = new TermResourceDefinition();
private TaxonomyResourceProvider taxonomyResourceProvider;
public TermResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem);
}
@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
//todo: shouldn't need to add this here
request.getProperties().put("name", request.<TermPath>getProperty("termPath").getFullyQualifiedName());
AtlasQuery atlasQuery;
try {
atlasQuery = queryFactory.createTermQuery(request);
} catch (InvalidQueryException e) {
throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e);
}
Collection<Map<String, Object>> results = atlasQuery.execute();
if (results.isEmpty()) {
throw new ResourceNotFoundException(String.format("Term '%s' not found.",
request.<TermPath>getProperty("termPath").getFullyQualifiedName()));
}
return new Result(results);
}
public Result getResources(Request request)
throws InvalidQueryException, ResourceNotFoundException {
TermPath termPath = request.getProperty("termPath");
String queryString = doQueryStringConversions(termPath, request.getQueryString());
Request queryRequest = new CollectionRequest(request.getProperties(), queryString);
AtlasQuery atlasQuery = queryFactory.createTermQuery(queryRequest);
Collection<Map<String, Object>> result = atlasQuery.execute();
return new Result(result);
}
public void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException, ResourceNotFoundException {
TermPath termPath = (TermPath) request.getProperties().remove("termPath");
String qualifiedTermName = termPath.getFullyQualifiedName();
request.getProperties().put("name", qualifiedTermName);
resourceDefinition.validate(request);
// get taxonomy
Request taxonomyRequest = new InstanceRequest(
Collections.<String, Object>singletonMap("name", termPath.getTaxonomyName()));
taxonomyRequest.addAdditionalSelectProperties(Collections.singleton("id"));
Result taxonomyResult = getTaxonomyResourceProvider().getResourceById(taxonomyRequest);
Map<String, Object> taxonomyPropertyMap = taxonomyResult.getPropertyMaps().iterator().next();
// ensure that parent exists if not a root level term
if (! termPath.getPath().equals("/")) {
Map<String, Object> parentProperties = new HashMap<>(request.getProperties());
parentProperties.put("termPath", termPath.getParent());
getResourceById(new InstanceRequest(parentProperties));
}
typeSystem.createTraitType(resourceDefinition, qualifiedTermName,
request.<String>getProperty("description"));
typeSystem.createTraitInstance(String.valueOf(taxonomyPropertyMap.get("id")),
qualifiedTermName, request.getProperties());
}
@Override
public Collection<String> createResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
throw new UnsupportedOperationException("Creating multiple Terms in a request is not currently supported");
}
//todo: add generic support for pre-query modification of expected value
//todo: similar path parsing code is used in several places in this class
private String doQueryStringConversions(TermPath termPath, String queryStr) throws InvalidQueryException {
String hierarchyPathProp = "hierarchy/path";
// replace "."
if (queryStr != null && queryStr.contains(String.format("%s:.", hierarchyPathProp))) {
//todo: regular expression replacement
queryStr = queryStr.replaceAll(String.format("%s:.", hierarchyPathProp),
String.format("%s:%s", hierarchyPathProp, termPath.getPath()));
}
return queryStr;
}
protected synchronized ResourceProvider getTaxonomyResourceProvider() {
if (taxonomyResourceProvider == null) {
taxonomyResourceProvider = new TaxonomyResourceProvider(typeSystem);
}
return taxonomyResourceProvider;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import com.tinkerpop.blueprints.Vertex;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.Collections;
/**
* Wrapper for term vertices.
*/
public class TermVertexWrapper extends VertexWrapper {
public TermVertexWrapper(Vertex v) {
super(v, new EntityTagResourceDefinition());
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import com.tinkerpop.blueprints.Vertex;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.*;
/**
* Wrapper for vertices which provides additional information.
*/
public class VertexWrapper {
private final Vertex vertex;
private final String vertexType;
private final Set<String> removedProperties = new HashSet<>();
private final PropertyMapper propertyMapper;
private final Map<String, PropertyValueFormatter> propertyValueFormatters;
protected ResourceComparator resourceComparator = new ResourceComparator();
public VertexWrapper(Vertex v, ResourceDefinition resourceDefinition) {
this(v, resourceDefinition.getPropertyMapper(), resourceDefinition.getPropertyValueFormatters());
}
public VertexWrapper(Vertex v,
PropertyMapper mapper,
Map<String, PropertyValueFormatter> formatters) {
vertex = v;
vertexType = getVertexType(v);
propertyMapper = mapper;
propertyValueFormatters = formatters;
}
public Vertex getVertex() {
return vertex;
}
public <T> T getProperty(String name) {
T val;
if (removedProperties.contains(name)) {
val = null;
} else {
val = vertex.getProperty(propertyMapper.toFullyQualifiedName(name, vertexType));
if (propertyValueFormatters.containsKey(name)) {
//todo: fix typing of property mapper
val = (T) propertyValueFormatters.get(name).format(val);
}
}
return val;
}
public Collection<String> getPropertyKeys() {
Collection<String> propertyKeys = new TreeSet<>(resourceComparator);
for (String p : vertex.getPropertyKeys()) {
String cleanName = propertyMapper.toCleanName(p, vertexType);
if (! removedProperties.contains(cleanName)) {
propertyKeys.add(cleanName);
}
}
return propertyKeys;
}
public Map<String, Object> getPropertyMap() {
Map<String, Object> props = new TreeMap<>(resourceComparator);
for (String p : vertex.getPropertyKeys()) {
String cleanName = propertyMapper.toCleanName(p, vertexType);
if (! removedProperties.contains(cleanName)) {
Object val = vertex.getProperty(p);
if (propertyValueFormatters.containsKey(cleanName)) {
val = propertyValueFormatters.get(cleanName).format(val);
}
props.put(cleanName, val);
}
}
return props;
}
public void removeProperty(String name) {
removedProperties.add(name);
}
public boolean isPropertyRemoved(String name) {
return removedProperties.contains(name);
}
public String toString() {
return String.format("VertexWrapper[name=%s]", getProperty("name"));
}
private String getVertexType(Vertex v) {
return v.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import org.apache.atlas.AtlasException;
import org.apache.atlas.catalog.*;
import org.apache.atlas.catalog.exception.CatalogRuntimeException;
import org.apache.atlas.catalog.exception.InvalidPayloadException;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.Relation;
import org.apache.atlas.typesystem.types.AttributeDefinition;
import org.apache.atlas.typesystem.types.AttributeInfo;
import org.apache.atlas.typesystem.types.Multiplicity;
import org.apache.atlas.typesystem.types.TypeSystem;
import java.util.*;
/**
* Base class for resource definitions.
*/
public abstract class BaseResourceDefinition implements ResourceDefinition {
protected static final TypeSystem typeSystem = TypeSystem.getInstance();
protected final Set<String> instanceProperties = new HashSet<>();
protected final Set<String> collectionProperties = new HashSet<>();
protected Map<String, AttributeDefinition> propertyDefs = new HashMap<>();
protected Map<String, AttributeInfo> properties = new HashMap<>();
protected final Map<String, Projection> projections = new HashMap<>();
protected final Map<String, Relation> relations = new HashMap<>();
protected final PropertyMapper propertyMapper;
protected final Map<String, PropertyValueFormatter> propertyValueFormatters = new HashMap<>();
public BaseResourceDefinition() {
DefaultDateFormatter defaultDateFormatter = new DefaultDateFormatter();
registerPropertyValueFormatter("creation_time", defaultDateFormatter);
registerPropertyValueFormatter("modified_time", defaultDateFormatter);
this.propertyMapper = createPropertyMapper();
}
@Override
public void validate(Request request) throws InvalidPayloadException {
Collection<String> propKeys = new HashSet<>(request.getProperties().keySet());
Collection<String> missingProperties = new HashSet<>();
for (AttributeInfo property : properties.values()) {
String name = property.name;
if (property.multiplicity == Multiplicity.REQUIRED) {
if (request.getProperty(name) == null) {
missingProperties.add(name);
}
}
propKeys.remove(name);
}
if (! missingProperties.isEmpty() || ! propKeys.isEmpty()) {
throw new InvalidPayloadException(missingProperties, propKeys);
}
//todo: property type validation
}
@Override
public Collection<AttributeDefinition> getPropertyDefinitions() {
return propertyDefs.values();
}
@Override
public Map<String, Object> filterProperties(Request request, Map<String, Object> propertyMap) {
Request.Cardinality cardinality = request.getCardinality();
Collection<String> requestProperties = request.getAdditionalSelectProperties();
Iterator<Map.Entry<String, Object>> propIter = propertyMap.entrySet().iterator();
while(propIter.hasNext()) {
Map.Entry<String, Object> propEntry = propIter.next();
String prop = propEntry.getKey();
if (! requestProperties.contains(prop)) {
if (cardinality == Request.Cardinality.COLLECTION) {
if (! collectionProperties.contains(prop)) {
propIter.remove();
}
} else {
if (! instanceProperties.isEmpty() && ! instanceProperties.contains(prop)) {
propIter.remove();
}
}
}
}
return propertyMap;
}
@Override
public Map<String, Projection> getProjections() {
return projections;
}
@Override
public Map<String, Relation> getRelations() {
return relations;
}
@Override
public synchronized PropertyMapper getPropertyMapper() {
return propertyMapper;
}
@Override
public Map<String, PropertyValueFormatter> getPropertyValueFormatters() {
return propertyValueFormatters;
}
protected void registerProperty(AttributeDefinition propertyDefinition) {
try {
propertyDefs.put(propertyDefinition.name, propertyDefinition);
properties.put(propertyDefinition.name, new AttributeInfo(typeSystem, propertyDefinition, null));
} catch (AtlasException e) {
throw new CatalogRuntimeException("Unable to create attribute: " + propertyDefinition.name, e);
}
}
protected void registerPropertyValueFormatter(String property, PropertyValueFormatter valueFormatter) {
propertyValueFormatters.put(property, valueFormatter);
}
/**
* Create a new property mapper instance.
* Should be overridden in children where the default implementation isn't sufficient.
*
* @return a new property mapper instance
*/
protected PropertyMapper createPropertyMapper() {
return new DefaultPropertyMapper();
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.TransformFunctionPipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.exception.InvalidPayloadException;
import org.apache.atlas.catalog.projection.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* Entity resource definition.
*/
public class EntityResourceDefinition extends BaseResourceDefinition {
public EntityResourceDefinition() {
collectionProperties.add("name");
collectionProperties.add("id");
collectionProperties.add("type");
RelationProjection tagProjection = getTagProjection();
projections.put("tags", tagProjection);
RelationProjection traitProjection = getTraitProjection();
projections.put("traits", traitProjection);
projections.put("default", getDefaultRelationProjection());
relations.put(tagProjection.getName(), tagProjection.getRelation());
relations.put(traitProjection.getName(), traitProjection.getRelation());
}
@Override
public String getIdPropertyName() {
return "id";
}
// not meaningful for entities
@Override
public String getTypeName() {
return null;
}
@Override
public void validate(Request request) throws InvalidPayloadException {
// no op for entities as we don't currently create entities and
// each entity type is different
}
@Override
public String resolveHref(Map<String, Object> properties) {
Object id = properties.get("id");
return id == null ? null : String.format("v1/entities/%s", id);
}
private RelationProjection getTagProjection() {
Relation traitRelation = new TagRelation();
RelationProjection tagProjection = new RelationProjection("tags", Collections.singleton("name"),
traitRelation, Projection.Cardinality.MULTIPLE);
tagProjection.addPipe(new TransformFunctionPipe<>(
new PipeFunction<Collection<ProjectionResult>, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(Collection<ProjectionResult> results) {
for (ProjectionResult result : results) {
for (Map<String, Object> properties : result.getPropertyMaps()) {
properties.put("href", String.format("v1/entities/%s/tags/%s",
result.getStartingVertex().getProperty("id"), properties.get("name")));
}
}
return results;
}
}));
return tagProjection;
}
private RelationProjection getTraitProjection() {
return new RelationProjection("traits", Collections.<String>emptySet(),
new TraitRelation(), Projection.Cardinality.MULTIPLE);
}
private RelationProjection getDefaultRelationProjection() {
Relation genericRelation = new GenericRelation(this);
RelationProjection relationProjection = new RelationProjection(
"relations",
Arrays.asList("type", "id", "name"),
genericRelation, Projection.Cardinality.MULTIPLE);
relationProjection.addPipe(new TransformFunctionPipe<>(
new PipeFunction<Collection<ProjectionResult>, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(Collection<ProjectionResult> results) {
for (ProjectionResult result : results) {
for (Map<String, Object> properties : result.getPropertyMaps()) {
properties.put("href", String.format("v1/entities/%s", properties.get("id")));
}
}
return results;
}
}));
return relationProjection;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.TransformFunctionPipe;
import org.apache.atlas.catalog.DefaultPropertyMapper;
import org.apache.atlas.catalog.PropertyMapper;
import org.apache.atlas.catalog.ResourceComparator;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.typesystem.types.DataTypes;
import org.apache.atlas.typesystem.types.utils.TypesUtil;
import java.util.*;
/**
* Entity Tag resource definition.
*/
public class EntityTagResourceDefinition extends BaseResourceDefinition {
public static final String ENTITY_GUID_PROPERTY = "entity-guid";
public EntityTagResourceDefinition() {
registerProperty(TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE));
instanceProperties.add("name");
instanceProperties.add("description");
instanceProperties.add("creation_time");
collectionProperties.add("name");
collectionProperties.add("description");
projections.put("terms", getTermProjection());
}
@Override
public String getIdPropertyName() {
return "name";
}
//not meaningful for entity tags
@Override
public String getTypeName() {
return null;
}
@Override
public String resolveHref(Map<String, Object> properties) {
return String.format("v1/entities/%s/tags/%s", properties.get(ENTITY_GUID_PROPERTY), properties.get("name"));
}
private Projection getTermProjection() {
return new Projection("term", Projection.Cardinality.SINGLE,
new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(VertexWrapper start) {
Map<String, Object> map = new TreeMap<>(new ResourceComparator());
StringBuilder sb = new StringBuilder();
sb.append("v1/taxonomies/");
String fullyQualifiedName = start.getVertex().getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
String[] paths = fullyQualifiedName.split("\\.");
// first path segment is the taxonomy
sb.append(paths[0]);
for (int i = 1; i < paths.length; ++i) {
String path = paths[i];
if (path != null && !path.isEmpty()) {
sb.append("/terms/");
sb.append(path);
}
}
map.put("href", sb.toString());
return Collections.singleton(new ProjectionResult("term", start,
Collections.singleton(map)));
}
}));
}
@Override
protected PropertyMapper createPropertyMapper() {
return new DefaultPropertyMapper(Collections.singletonMap(Constants.ENTITY_TYPE_PROPERTY_KEY, "name"),
Collections.singletonMap("name", Constants.ENTITY_TYPE_PROPERTY_KEY));
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import org.apache.atlas.catalog.PropertyMapper;
import org.apache.atlas.catalog.PropertyValueFormatter;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.exception.InvalidPayloadException;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.Relation;
import org.apache.atlas.typesystem.types.AttributeDefinition;
import java.util.Collection;
import java.util.Map;
/**
* Resource definition.
*/
public interface ResourceDefinition {
/**
* The type name of the resource.
*
* @return the resources type name
*/
String getTypeName();
/**
* Validate a user request.
*
* @param request user request
*
* @throws InvalidPayloadException if the request payload is invalid in any way
*/
void validate(Request request) throws InvalidPayloadException;
/**
* Get the name of the resources id property.
*
* @return the id property name
*/
String getIdPropertyName();
/**
* Get the property definitions for the resource.
*
* @return resource property definitions
*/
//todo: abstract usage of AttributeDefinition
Collection<AttributeDefinition> getPropertyDefinitions();
/**
* Filter out properties which shouldn't be returned in the result.
* The passed in map is directly modified as well as returned.
*
* @param request user request
* @param propertyMap property map to filter
*
* @return the filtered property map
*/
Map<String, Object> filterProperties(Request request, Map<String, Object> propertyMap);
/**
* Generate an href for the resource from the provided resource property map.
*
* @param properties resource property map
*
* @return a URL to be used as an href property value for the resource
*/
String resolveHref(Map<String, Object> properties);
/**
* Get map of resource projections.
*
* @return map of resource projections
*/
Map<String, Projection> getProjections();
/**
* Get map of resource relations.
*
* @return map of resource relations
*/
Map<String, Relation> getRelations();
/**
* Get the property mapper associated with the resource.
*
* @return associated property mapper
*/
PropertyMapper getPropertyMapper();
/**
* Get the registered property value formatters.
* @return map of property name to property value formatter
*/
Map<String, PropertyValueFormatter> getPropertyValueFormatters();
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.TransformFunctionPipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.exception.InvalidPayloadException;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.typesystem.types.DataTypes;
import org.apache.atlas.typesystem.types.utils.TypesUtil;
import java.util.*;
/**
* Taxonomy resource definition.
*/
public class TaxonomyResourceDefinition extends BaseResourceDefinition {
public TaxonomyResourceDefinition() {
registerProperty(TypesUtil.createUniqueRequiredAttrDef("name", DataTypes.STRING_TYPE));
registerProperty(TypesUtil.createOptionalAttrDef("description", DataTypes.STRING_TYPE));
//todo: combine with above registrations
instanceProperties.add("name");
instanceProperties.add("description");
instanceProperties.add("creation_time");
collectionProperties.add("name");
collectionProperties.add("description");
projections.put("terms", getTermsProjection());
}
@Override
public void validate(Request request) throws InvalidPayloadException {
super.validate(request);
if (String.valueOf(request.getProperties().get("name")).contains(".")) {
throw new InvalidPayloadException("The \"name\" property may not contain the character '.'");
}
}
@Override
public String getTypeName() {
return "Taxonomy";
}
@Override
public String getIdPropertyName() {
return "name";
}
@Override
public String resolveHref(Map<String, Object> properties) {
return String.format("v1/taxonomies/%s", properties.get("name"));
}
private Projection getTermsProjection() {
final String termsProjectionName = "terms";
return new Projection(termsProjectionName, Projection.Cardinality.SINGLE,
new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {
private String baseHref = "v1/taxonomies/";
@Override
public Collection<ProjectionResult> compute(VertexWrapper v) {
Map<String, Object> map = new HashMap<>();
map.put("href", baseHref + v.getProperty("name") + "/terms");
return Collections.singleton(new ProjectionResult(termsProjectionName, v,
Collections.singleton(map)));
}
}));
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.definition;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.TransformFunctionPipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.ResourceComparator;
import org.apache.atlas.catalog.TermPath;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.exception.InvalidPayloadException;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.typesystem.types.*;
import org.apache.atlas.typesystem.types.utils.TypesUtil;
import java.util.*;
/**
* Term resource definition.
*/
public class TermResourceDefinition extends BaseResourceDefinition {
public TermResourceDefinition() {
registerProperty(TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE));
registerProperty(TypesUtil.createOptionalAttrDef("description", DataTypes.STRING_TYPE));
registerProperty(TypesUtil.createOptionalAttrDef("available_as_tag", DataTypes.BOOLEAN_TYPE));
registerProperty(TypesUtil.createOptionalAttrDef("acceptable_use", DataTypes.STRING_TYPE));
instanceProperties.add("name");
instanceProperties.add("description");
instanceProperties.add("creation_time");
instanceProperties.add("available_as_tag");
instanceProperties.add("acceptable_use");
collectionProperties.add("name");
collectionProperties.add("description");
projections.put("terms", getSubTermProjection());
projections.put("hierarchy", getHierarchyProjection());
}
@Override
public void validate(Request request) throws InvalidPayloadException {
super.validate(request);
String name = request.getProperty("name");
// name will be in the fully qualified form: taxonomyName.termName
if (! name.contains(".")) {
throw new InvalidPayloadException("Term name must be in the form 'taxonomyName.termName.subTermName'");
}
if (! request.getProperties().containsKey("available_as_tag")) {
request.getProperties().put("available_as_tag", true);
}
}
@Override
public String getTypeName() {
return "Term";
}
@Override
public String getIdPropertyName() {
return "name";
}
//todo
@Override
public String resolveHref(Map<String, Object> properties) {
StringBuilder sb = new StringBuilder();
sb.append("v1/taxonomies/");
TermPath termPath = new TermPath(String.valueOf(properties.get("name")));
String[] paths = termPath.getPathSegments();
sb.append(termPath.getTaxonomyName());
for (String path : paths) {
//todo: shouldn't need to check for null or empty after TermPath addition
if (path != null && !path.isEmpty()) {
sb.append("/terms/");
sb.append(path);
}
}
return sb.toString();
}
private Projection getHierarchyProjection() {
final String projectionName = "hierarchy";
return new Projection(projectionName, Projection.Cardinality.SINGLE,
new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(VertexWrapper start) {
Map<String, Object> map = new TreeMap<>(new ResourceComparator());
TermPath termPath = new TermPath(start.getVertex().<String>getProperty(
Constants.ENTITY_TYPE_PROPERTY_KEY));
map.put("path", termPath.getPath());
map.put("short_name", termPath.getShortName());
map.put("taxonomy", termPath.getTaxonomyName());
return Collections.singleton(new ProjectionResult(projectionName, start,
Collections.singleton(map)));
}
}));
}
private Projection getSubTermProjection() {
//todo: combine with other term projections
final String termsProjectionName = "terms";
return new Projection(termsProjectionName, Projection.Cardinality.SINGLE,
new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(VertexWrapper start) {
Map<String, Object> map = new TreeMap<>(new ResourceComparator());
StringBuilder sb = new StringBuilder();
sb.append("v1/taxonomies/");
TermPath termPath = new TermPath(start.getVertex().<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY));
String[] paths = termPath.getPathSegments();
sb.append(termPath.getTaxonomyName());
for (String path : paths) {
//todo: shouldn't need to check for null or empty after TermPath addition
if (path != null && !path.isEmpty()) {
sb.append("/terms/");
sb.append(path);
}
}
sb.append("/terms");
map.put("href", sb.toString());
return Collections.singleton(new ProjectionResult(termsProjectionName, start,
Collections.singleton(map)));
}
}));
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
/**
* Base checked catalog exception.
*/
public class CatalogException extends Exception {
private int status;
public CatalogException(String message, int status) {
super(message);
this.status = status;
}
public int getStatus() {
return status;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
/**
* Base runtime catalog exception.
*/
public class CatalogRuntimeException extends RuntimeException {
int statusCode = 500;
public CatalogRuntimeException(Exception e) {
super("", e);
}
public CatalogRuntimeException(String message, Exception e) {
super(message, e);
}
public CatalogRuntimeException(String message, int statusCode) {
super(message);
this.statusCode = statusCode;
}
public int getStatusCode() {
return statusCode;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
import java.util.Collection;
/**
* Exception used for invalid API payloads.
*/
public class InvalidPayloadException extends CatalogException {
private final static String baseMsg = "Invalid Request.";
private final static String missingMsg = " The following required properties are missing: %s.";
private final static String unknownMsg = " The following properties are not supported: %s";
public InvalidPayloadException(Collection<String> missingProperties, Collection<String> unknownProperties) {
super(baseMsg + (!missingProperties.isEmpty() ? String.format(missingMsg, missingProperties): "") +
(!unknownProperties.isEmpty() ? String.format(unknownMsg, unknownProperties): ""), 400);
}
public InvalidPayloadException(String msg) {
super(msg, 400);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
/**
* Exception for invalid user query.
*/
public class InvalidQueryException extends CatalogException {
public InvalidQueryException(String message) {
super("Unable to parse query: " + message, 400);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
/**
* Exception used when an attempt is made to create a resource which already exists.
*/
public class ResourceAlreadyExistsException extends CatalogException {
public ResourceAlreadyExistsException(String message) {
super(message, 409);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.exception;
/**
* Exception used when an explicitly requested resource doesn't exist.
*/
public class ResourceNotFoundException extends CatalogException {
public ResourceNotFoundException(String message) {
super(message, 404);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Represents a generic relation
*/
public class GenericRelation implements Relation {
private final ResourceDefinition resourceDefinition;
public GenericRelation(ResourceDefinition resourceDefinition) {
this.resourceDefinition = resourceDefinition;
}
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
Collection<RelationSet> relations = new ArrayList<>();
Vertex v = vWrapper.getVertex();
String vertexType = v.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
Map<String, Collection<VertexWrapper>> vertexMap = new HashMap<>();
for (Edge e : v.getEdges(Direction.OUT)) {
String edgeLabel = e.getLabel();
String edgePrefix = String.format("%s%s.", Constants.INTERNAL_PROPERTY_KEY_PREFIX, vertexType);
if (edgeLabel.startsWith(edgePrefix)) {
Vertex adjacentVertex = e.getVertex(Direction.IN);
VertexWrapper relationVertex = new VertexWrapper(adjacentVertex, resourceDefinition);
String relationName = edgeLabel.substring(edgePrefix.length());
Collection<VertexWrapper> vertices = vertexMap.get(relationName);
if (vertices == null) {
vertices = new ArrayList<>();
vertexMap.put(relationName, vertices);
}
vertices.add(relationVertex);
}
}
for (Map.Entry<String, Collection<VertexWrapper>> entry : vertexMap.entrySet()) {
relations.add(new RelationSet(entry.getKey(), entry.getValue()));
}
return relations;
}
@Override
public Pipe asPipe() {
return null;
}
@Override
public ResourceDefinition getResourceDefinition() {
return resourceDefinition;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.util.Pipeline;
import org.apache.atlas.catalog.VertexWrapper;
import java.util.Collection;
import java.util.Collections;
/**
* Projection representation.
* Used to project properties onto a resource from another source.
*/
public class Projection {
public enum Cardinality {SINGLE, MULTIPLE}
private final String m_name;
private final Cardinality m_cardinality;
protected Pipeline<VertexWrapper, Collection<ProjectionResult>> m_pipeline = new Pipeline<>();
public Projection(String name, Cardinality cardinality) {
m_name = name;
m_cardinality = cardinality;
}
public Projection(String name, Cardinality cardinality, Pipe<VertexWrapper, Collection<ProjectionResult>> pipe) {
m_name = name;
m_cardinality = cardinality;
m_pipeline.addPipe(pipe);
}
public Collection<ProjectionResult> values(VertexWrapper start) {
m_pipeline.setStarts(Collections.singleton(start));
return m_pipeline.iterator().next();
}
public void addPipe(Pipe<Collection<ProjectionResult>, Collection<ProjectionResult>> p) {
m_pipeline.addPipe(p);
}
public String getName() {
return m_name;
}
public Cardinality getCardinality() {
return m_cardinality;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import org.apache.atlas.catalog.VertexWrapper;
import java.util.Collection;
import java.util.Map;
/**
* Result of a projection.
*/
public class ProjectionResult {
private final VertexWrapper m_startVertex;
private final String m_name;
private final Collection<Map<String, Object>> m_propertyMaps;
public ProjectionResult(String name, VertexWrapper startingVertex, Collection<Map<String, Object>> propertyMaps) {
m_name = name;
m_startVertex = startingVertex;
m_propertyMaps = propertyMaps;
}
public String getName() {
return m_name;
}
public VertexWrapper getStartingVertex() {
return m_startVertex;
}
public Collection<Map<String, Object>> getPropertyMaps() {
return m_propertyMaps;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import java.util.Collection;
/**
* Represents the relationship from one vertex to another via an edge.
*/
public interface Relation {
/**
* Traverse the relation.
*
* @param vWrapper vertex to start traversal from
*
* @return results of the traversal
*/
Collection<RelationSet> traverse(VertexWrapper vWrapper);
/**
* Get the pipe representation of the traversal.
*
* @return pipe representation
*/
Pipe asPipe();
/**
* Get the associated resource definition.
*
* @return associated resource definition
*/
ResourceDefinition getResourceDefinition();
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.transform.TransformFunctionPipe;
import org.apache.atlas.catalog.ResourceComparator;
import org.apache.atlas.catalog.VertexWrapper;
import java.util.*;
/**
* Projection based on a relation.
*/
public class RelationProjection extends Projection {
private Relation relation;
public RelationProjection(String name, final Collection<String> fields, final Relation relation, Cardinality cardinality) {
super(name, cardinality, new TransformFunctionPipe<>(
new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {
@Override
public Collection<ProjectionResult> compute(VertexWrapper start) {
Collection<ProjectionResult> projectionResults = new ArrayList<>();
for (RelationSet relationSet : relation.traverse(start)) {
Collection<Map<String, Object>> propertyMaps = new ArrayList<>();
for (VertexWrapper vWrapper : relationSet.getVertices()) {
Map<String, Object> propertyMap = new TreeMap<>(new ResourceComparator());
propertyMaps.add(propertyMap);
if (fields.isEmpty()) {
for (String property : vWrapper.getPropertyKeys()) {
propertyMap.put(property, vWrapper.<String>getProperty(property));
}
} else {
for (String property : fields) {
propertyMap.put(property, vWrapper.<String>getProperty(property));
}
}
}
projectionResults.add(new ProjectionResult(relationSet.getName(), start, propertyMaps));
}
return projectionResults;
}
}));
this.relation = relation;
}
public Relation getRelation() {
return relation;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import org.apache.atlas.catalog.VertexWrapper;
import java.util.*;
/**
* Encapsulates the response of a relation traversal.
*/
public class RelationSet {
private final String m_name;
private final Collection<VertexWrapper> m_vertices;
public RelationSet(String name, Collection<VertexWrapper> vertices) {
m_name = name;
m_vertices = vertices;
}
public String getName() {
return m_name;
}
public Collection<VertexWrapper> getVertices() {
return Collections.unmodifiableCollection(m_vertices);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.filter.FilterFunctionPipe;
import org.apache.atlas.catalog.TermVertexWrapper;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/**
* Relation for adjacent Tag vertices.
*/
public class TagRelation implements Relation {
private static ResourceDefinition resourceDefinition = new EntityTagResourceDefinition();
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
Vertex v = vWrapper.getVertex();
Collection<VertexWrapper> vertices = new ArrayList<>();
for (Edge e : v.getEdges(Direction.OUT)) {
if (e.getLabel().startsWith(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY))) {
VertexWrapper trait = new TermVertexWrapper(e.getVertex(Direction.IN));
if (trait.getPropertyKeys().contains("available_as_tag")) {
vertices.add(trait);
}
}
}
return Collections.singletonList(new RelationSet("tags", vertices));
}
@Override
public Pipe asPipe() {
return new FilterFunctionPipe<>(new PipeFunction<Edge, Boolean>() {
@Override
public Boolean compute(Edge edge) {
String name = edge.getVertex(Direction.OUT).getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
VertexWrapper v = new TermVertexWrapper(edge.getVertex(Direction.IN));
return edge.getLabel().startsWith(name) && v.getPropertyKeys().contains("available_as_tag");
}
});
}
@Override
public ResourceDefinition getResourceDefinition() {
return resourceDefinition;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.projection;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.filter.FilterFunctionPipe;
import org.apache.atlas.catalog.TermVertexWrapper;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
/**
* Trait specific relation.
*/
//todo: combine with TagRelation
public class TraitRelation implements Relation {
//todo: for now using entity tag resource definition
private static ResourceDefinition resourceDefinition = new EntityTagResourceDefinition();
@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
Vertex v = vWrapper.getVertex();
Collection<VertexWrapper> vertices = new ArrayList<>();
for (Edge e : v.getEdges(Direction.OUT)) {
if (e.getLabel().startsWith(v.<String>getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY))) {
VertexWrapper trait = new TermVertexWrapper(e.getVertex(Direction.IN));
if (! trait.getPropertyKeys().contains("available_as_tag")) {
vertices.add(trait);
}
}
}
return Collections.singletonList(new RelationSet("traits", vertices));
}
@Override
public Pipe asPipe() {
return new FilterFunctionPipe<>(new PipeFunction<Edge, Boolean>() {
@Override
public Boolean compute(Edge edge) {
String type = edge.getVertex(Direction.OUT).getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
VertexWrapper v = new TermVertexWrapper(edge.getVertex(Direction.IN));
return edge.getLabel().startsWith(type) && ! v.getPropertyKeys().contains("available_as_tag");
}
});
}
@Override
public ResourceDefinition getResourceDefinition() {
return resourceDefinition;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.VertexWrapper;
/**
* Query expression which always returns true.
*/
public class AlwaysQueryExpression extends BaseQueryExpression {
protected AlwaysQueryExpression() {
super(null, null, null);
}
@Override
public Pipe asPipe() {
return null;
}
@Override
public boolean evaluate(VertexWrapper vWrapper) {
return ! negate;
}
@Override
public boolean evaluate(Object value) {
return true;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
/**
* Entity resource query.
*/
public class AtlasEntityQuery extends BaseQuery {
public AtlasEntityQuery(QueryExpression queryExpression, ResourceDefinition resourceDefinition, Request request) {
super(queryExpression, resourceDefinition, request);
}
protected GremlinPipeline getInitialPipeline() {
//todo: the property 'entityText' isn't currently indexed
//todo: we could use Constants.ENTITY_TYPE_PROPERTY_KEY initially but trait instances also contain this property
return new GremlinPipeline(getGraph()).V().has(Constants.ENTITY_TEXT_PROPERTY_KEY).
hasNot(Constants.ENTITY_TYPE_PROPERTY_KEY, "Taxonomy");
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.blueprints.Direction;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.filter.FilterFunctionPipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.TermVertexWrapper;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
import java.util.HashMap;
import java.util.Map;
/**
* Entity Tag resource query.
*/
public class AtlasEntityTagQuery extends BaseQuery {
private final String guid;
public AtlasEntityTagQuery(QueryExpression queryExpression, ResourceDefinition resourceDefinition, String guid, Request request) {
super(queryExpression, resourceDefinition, request);
this.guid = guid;
}
@Override
protected GremlinPipeline getInitialPipeline() {
GremlinPipeline p = new GremlinPipeline(getGraph()).V().has(Constants.GUID_PROPERTY_KEY, guid).outE();
//todo: this is basically the same pipeline used in TagRelation.asPipe()
p.add(new FilterFunctionPipe<>(new PipeFunction<Edge, Boolean>() {
@Override
public Boolean compute(Edge edge) {
String type = edge.getVertex(Direction.OUT).getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
VertexWrapper v = new TermVertexWrapper(edge.getVertex(Direction.IN));
return edge.getLabel().startsWith(type) && v.getPropertyKeys().contains("available_as_tag");
}
}));
return p.inV();
}
//todo: duplication of effort with resource definition
@Override
protected void addHref(Map<String, Object> propertyMap) {
Map<String, Object> map = new HashMap<>(propertyMap);
map.put(EntityTagResourceDefinition.ENTITY_GUID_PROPERTY, guid);
String href = resourceDefinition.resolveHref(map);
if (href != null) {
propertyMap.put("href", href);
}
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.exception.ResourceNotFoundException;
import java.util.Collection;
import java.util.Map;
/**
* Query functionality.
*/
public interface AtlasQuery {
/**
* Execute the query.
*
* @return collection of property maps, one per matching resource
* @throws ResourceNotFoundException if an explicitly specified resource doesn't exist
*/
Collection<Map<String, Object>> execute() throws ResourceNotFoundException;
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.definition.ResourceDefinition;
/**
* Taxonomy resource query.
*/
public class AtlasTaxonomyQuery extends BaseQuery {
public AtlasTaxonomyQuery(QueryExpression queryExpression, ResourceDefinition resourceDefinition, Request request) {
super(queryExpression, resourceDefinition, request);
}
@Override
protected GremlinPipeline getInitialPipeline() {
return new GremlinPipeline(getGraph()).V().has("__typeName", "Taxonomy");
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.thinkaurelius.titan.core.attribute.Text;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.TermPath;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.repository.Constants;
/**
* Term resource query.
*/
public class AtlasTermQuery extends BaseQuery {
private final TermPath termPath;
public AtlasTermQuery(QueryExpression queryExpression, ResourceDefinition resourceDefinition, TermPath termPath, Request request) {
super(queryExpression, resourceDefinition, request);
this.termPath = termPath;
}
@Override
protected GremlinPipeline getInitialPipeline() {
return new GremlinPipeline(getGraph()).V().has("Taxonomy.name", termPath.getTaxonomyName()).out().
has(Constants.ENTITY_TYPE_PROPERTY_KEY, Text.PREFIX, termPath.getFullyQualifiedName());
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.thinkaurelius.titan.core.TitanGraph;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.exception.ResourceNotFoundException;
import org.apache.atlas.catalog.projection.Projection;
import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.repository.graph.TitanGraphProvider;
import java.util.*;
/**
* Base Query implementation.
*/
public abstract class BaseQuery implements AtlasQuery {
protected final QueryExpression queryExpression;
protected final ResourceDefinition resourceDefinition;
protected final Request request;
public BaseQuery(QueryExpression queryExpression, ResourceDefinition resourceDefinition, Request request) {
this.queryExpression = queryExpression;
this.resourceDefinition = resourceDefinition;
this.request = request;
}
public Collection<Map<String, Object>> execute() throws ResourceNotFoundException {
Collection<Map<String, Object>> resultMaps = new ArrayList<>();
for (Vertex vertex : executeQuery()) {
resultMaps.add(processPropertyMap(new VertexWrapper(vertex, resourceDefinition)));
}
return resultMaps;
}
private List<Vertex> executeQuery() {
GremlinPipeline pipeline = getInitialPipeline().as("root");
Pipe adapterPipe = queryExpression.asPipe();
//todo: AlwaysQueryAdapter returns null for pipe
//todo: Is there a no-op pipe that I could add that wouldn't negatively affect performance
return adapterPipe == null ?
pipeline.toList() :
pipeline.add(adapterPipe).back("root").toList();
}
protected abstract GremlinPipeline getInitialPipeline();
// todo: consider getting
protected Map<String, Object> processPropertyMap(VertexWrapper vertex) {
Map<String, Object> propertyMap = vertex.getPropertyMap();
resourceDefinition.filterProperties(request, propertyMap);
addHref(propertyMap);
return request.getCardinality() == Request.Cardinality.INSTANCE ?
applyProjections(vertex, propertyMap) :
propertyMap;
}
protected void addHref(Map<String, Object> propertyMap) {
String href = resourceDefinition.resolveHref(propertyMap);
if (href != null) {
propertyMap.put("href", href);
}
}
private Map<String, Object> applyProjections(VertexWrapper vertex, Map<String, Object> propertyMap) {
for (Projection p : resourceDefinition.getProjections().values()) {
for (ProjectionResult projectionResult : p.values(vertex)) {
if (p.getCardinality() == Projection.Cardinality.MULTIPLE) {
propertyMap.put(projectionResult.getName(), projectionResult.getPropertyMaps());
} else {
for (Map<String, Object> projectionMap : projectionResult.getPropertyMaps()) {
propertyMap.put(projectionResult.getName(), projectionMap);
}
}
}
}
return propertyMap;
}
protected QueryExpression getQueryExpression() {
return queryExpression;
}
protected ResourceDefinition getResourceDefinition() {
return resourceDefinition;
}
protected Request getRequest() {
return request;
}
//todo: abstract
// Underlying method is synchronized and caches the graph in a static field
protected TitanGraph getGraph() {
return TitanGraphProvider.getGraphInstance();
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.filter.FilterFunctionPipe;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import java.util.Collection;
import java.util.HashSet;
/**
* Base query expression class.
*/
public abstract class BaseQueryExpression implements QueryExpression {
protected String m_field;
protected final String m_expectedValue;
protected final ResourceDefinition resourceDefinition;
protected boolean negate = false;
protected Collection<String> properties = new HashSet<>();
protected BaseQueryExpression(String field, String expectedValue, ResourceDefinition resourceDefinition) {
m_field = field;
if (field != null) {
properties.add(field);
}
m_expectedValue = expectedValue;
this.resourceDefinition = resourceDefinition;
}
@Override
public boolean evaluate(VertexWrapper vWrapper) {
return negate ^ evaluate(vWrapper.getProperty(m_field));
}
@Override
public Collection<String> getProperties() {
return properties;
}
@Override
public boolean evaluate(Object value) {
// subclasses which don't override evaluate(VertexWrapper) should implement this
return false;
}
//todo: use 'has' instead of closure where possible for performance
public Pipe asPipe() {
return new FilterFunctionPipe(new PipeFunction<Vertex, Boolean>() {
@Override
public Boolean compute(Vertex vertex) {
return evaluate(new VertexWrapper(vertex, resourceDefinition));
}
});
}
@Override
public String getField() {
return m_field;
}
@Override
public String getExpectedValue() {
return m_expectedValue;
}
@Override
public void setField(String field) {
m_field = field;
}
@Override
public void setNegate() {
this.negate = true;
}
@Override
public boolean isNegate() {
return negate;
}
@Override
public boolean isProjectionExpression() {
return getField() != null && getField().contains(QueryFactory.PATH_SEP_TOKEN);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.filter.AndFilterPipe;
import com.tinkerpop.pipes.filter.OrFilterPipe;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import java.util.*;
/**
* Expression where operands are other expressions and operator is logical AND or OR
*/
public class BooleanQueryExpression extends BaseQueryExpression {
private final BooleanClause[] clauses;
private final QueryFactory queryFactory;
public BooleanQueryExpression(BooleanQuery query, ResourceDefinition resourceDefinition, QueryFactory queryFactory) {
super(null, null, resourceDefinition);
clauses = query.getClauses();
this.queryFactory = queryFactory;
}
@Override
public Pipe asPipe() {
Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses = groupClauses();
Pipe andPipe = null;
Collection<Pipe> andPipes = processAndClauses(groupedClauses);
andPipes.addAll(processNotClauses(groupedClauses));
if (! andPipes.isEmpty()) {
andPipe = new AndFilterPipe(andPipes.toArray(new Pipe[andPipes.size()]));
}
Collection<Pipe> orPipes = processOrClauses(groupedClauses);
if (! orPipes.isEmpty()) {
if (andPipe != null) {
orPipes.add(andPipe);
}
return new OrFilterPipe(orPipes.toArray(new Pipe[orPipes.size()]));
} else {
return andPipe;
}
}
private Map<BooleanClause.Occur, Collection<BooleanClause>> groupClauses() {
Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses = new HashMap<>();
for (BooleanClause clause : clauses) {
BooleanClause.Occur occur = resolveClauseOccur(clause);
Collection<BooleanClause> clauseGrouping = groupedClauses.get(occur);
if (clauseGrouping == null) {
clauseGrouping = new ArrayList<>();
groupedClauses.put(occur, clauseGrouping);
}
clauseGrouping.add(clause);
}
return groupedClauses;
}
private BooleanClause.Occur resolveClauseOccur(BooleanClause clause) {
BooleanClause.Occur occur = clause.getOccur();
if (negate) {
switch (occur) {
case SHOULD:
occur = BooleanClause.Occur.MUST_NOT;
break;
case MUST:
occur = BooleanClause.Occur.SHOULD;
break;
case MUST_NOT:
occur = BooleanClause.Occur.SHOULD;
break;
}
}
return occur;
}
private Collection<Pipe> processAndClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
Collection<BooleanClause> andClauses = groupedClauses.get(BooleanClause.Occur.MUST);
Collection<Pipe> andPipes = new ArrayList<>();
if (andClauses != null) {
for (BooleanClause andClause : andClauses) {
QueryExpression queryExpression = queryFactory.create(andClause.getQuery(), resourceDefinition);
properties.addAll(queryExpression.getProperties());
andPipes.add(queryExpression.asPipe());
}
}
return andPipes;
}
private Collection<Pipe> processOrClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
Collection<BooleanClause> shouldClauses = groupedClauses.get(BooleanClause.Occur.SHOULD);
Collection<Pipe> orPipes = new ArrayList<>();
if (shouldClauses != null) {
for (BooleanClause shouldClause : shouldClauses) {
QueryExpression queryExpression = queryFactory.create(shouldClause.getQuery(), resourceDefinition);
// don't negate expression if we negated MUST_NOT -> SHOULD
if (negate && shouldClause.getOccur() != BooleanClause.Occur.MUST_NOT) {
queryExpression.setNegate();
}
properties.addAll(queryExpression.getProperties());
orPipes.add(queryExpression.asPipe());
}
}
return orPipes;
}
private Collection<Pipe> processNotClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
Collection<BooleanClause> notClauses = groupedClauses.get(BooleanClause.Occur.MUST_NOT);
Collection<Pipe> notPipes = new ArrayList<>();
if (notClauses != null) {
for (BooleanClause notClause : notClauses) {
QueryExpression queryExpression = queryFactory.create(notClause.getQuery(), resourceDefinition);
queryExpression.setNegate();
properties.addAll(queryExpression.getProperties());
notPipes.add(queryExpression.asPipe());
}
}
return notPipes;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.search.PrefixQuery;
/**
* Expression that evaluates whether a property starts with a prefix.
*/
public class PrefixQueryExpression extends BaseQueryExpression {
// query 'f*' results in a PrefixQuery
public PrefixQueryExpression(PrefixQuery query, ResourceDefinition resourceDefinition) {
super(query.getPrefix().field(), query.getPrefix().text(), resourceDefinition);
}
@Override
public boolean evaluate(Object value) {
return value != null && String.valueOf(value).startsWith(getExpectedValue());
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.thinkaurelius.titan.core.attribute.Text;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.PipeFunction;
import com.tinkerpop.pipes.filter.FilterFunctionPipe;
import org.apache.atlas.catalog.VertexWrapper;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.atlas.catalog.projection.ProjectionResult;
import org.apache.atlas.catalog.projection.Relation;
import java.util.*;
/**
* Query expression wrapper which handles projection queries.
*/
public class ProjectionQueryExpression extends BaseQueryExpression {
private final QueryExpression underlyingExpression;
private final ResourceDefinition resourceDefinition;
private final String[] fieldSegments;
protected ProjectionQueryExpression(QueryExpression underlyingExpression, ResourceDefinition resourceDefinition) {
super(underlyingExpression.getField(), underlyingExpression.getExpectedValue(), resourceDefinition);
this.underlyingExpression = underlyingExpression;
this.resourceDefinition = resourceDefinition;
this.fieldSegments = getField().split(QueryFactory.PATH_SEP_TOKEN);
}
@Override
public Pipe asPipe() {
//todo: encapsulate all of this path logic including path sep escaping and normalizing
final int sepIdx = getField().indexOf(QueryFactory.PATH_SEP_TOKEN);
final String edgeToken = getField().substring(0, sepIdx);
GremlinPipeline pipeline = new GremlinPipeline();
Relation relation = resourceDefinition.getRelations().get(fieldSegments[0]);
if (relation != null) {
pipeline = pipeline.outE();
pipeline.add(relation.asPipe()).inV();
} else {
if (resourceDefinition.getProjections().get(fieldSegments[0]) != null) {
return super.asPipe();
} else {
//todo: default Relation implementation
pipeline = pipeline.outE().has("label", Text.REGEX, String.format(".*\\.%s", edgeToken)).inV();
}
}
//todo: set resource definition from relation on underlying expression where appropriate
String childFieldName = getField().substring(sepIdx + QueryFactory.PATH_SEP_TOKEN.length());
underlyingExpression.setField(childFieldName);
Pipe childPipe;
if (childFieldName.contains(QueryFactory.PATH_SEP_TOKEN)) {
childPipe = new ProjectionQueryExpression(underlyingExpression, resourceDefinition).asPipe();
} else {
childPipe = underlyingExpression.asPipe();
}
pipeline.add(childPipe);
return negate ? new FilterFunctionPipe(new ExcludePipeFunction(pipeline)) : pipeline;
}
@Override
public boolean evaluate(VertexWrapper vWrapper) {
boolean result = false;
Iterator<ProjectionResult> projectionIterator = resourceDefinition.getProjections().
get(fieldSegments[0]).values(vWrapper).iterator();
while (! result && projectionIterator.hasNext()) {
ProjectionResult projectionResult = projectionIterator.next();
for (Map<String, Object> propertyMap : projectionResult.getPropertyMaps()) {
Object val = propertyMap.get(fieldSegments[1]);
if (val != null && underlyingExpression.evaluate(QueryFactory.escape(val))) {
result = true;
break;
}
}
}
return negate ^ result;
}
private static class ExcludePipeFunction implements PipeFunction<Object, Boolean> {
private final GremlinPipeline excludePipeline;
public ExcludePipeFunction(GremlinPipeline excludePipeline) {
this.excludePipeline = excludePipeline;
}
@Override
public Boolean compute(Object vertices) {
GremlinPipeline p = new GremlinPipeline(Collections.singleton(vertices));
p.add(excludePipeline);
return p.gather().toList().isEmpty();
}
}
protected QueryExpression getUnderlyingExpression() {
return underlyingExpression;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import com.tinkerpop.pipes.Pipe;
import org.apache.atlas.catalog.VertexWrapper;
import java.util.Collection;
/**
* Represents a query expression.
*/
public interface QueryExpression {
/**
* Evaluate the expression based on properties of the provied vertex.
*
* @param vWrapper vertex wrapper that expression is applied to
* @return result of expression evaluation
*/
boolean evaluate(VertexWrapper vWrapper);
/**
* Evaluate the expression based on the provided value.
*
* @param value value used to evaluate expression
* @return
*/
boolean evaluate(Object value);
/**
* Get the complete set of properties which are contained in the expression.
*
* @return collection of expression properties
*/
Collection<String> getProperties();
/**
* Get the pipe representation of the expression.
*
* @return pipe representation
*/
Pipe asPipe();
/**
* Negate the expression.
*/
void setNegate();
/**
* Get the negate status of the expression.
*
* @return true if the expression is negated, false otherwise
*/
boolean isNegate();
/**
* Determine whether the expression is being applied to a projection.
*
* @return true if expression is being applied to a projection, false otherwise
*/
boolean isProjectionExpression();
/**
* Get the field name used in the expression.
*
* @return expression field name or null if there is no field name
*/
String getField();
/**
* Set the expressions field name.
*
* @param fieldName field name
*/
public void setField(String fieldName);
/**
* Get the expected value for the expression.
*
* @return expected value or null if there isn't a expected value
*/
String getExpectedValue();
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.Request;
import org.apache.atlas.catalog.TermPath;
import org.apache.atlas.catalog.definition.*;
import org.apache.atlas.catalog.exception.CatalogRuntimeException;
import org.apache.atlas.catalog.exception.InvalidQueryException;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.sandbox.queries.regex.RegexQuery;
import org.apache.lucene.search.*;
import org.apache.lucene.util.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
/**
* Factory used to create QueryAdapter instances.
*/
public class QueryFactory {
private static final Logger LOG = LoggerFactory.getLogger(QueryFactory.class);
public static final String PATH_SEP_TOKEN = "__slash__";
private final Map<Class<? extends Query>, ExpressionCreateFunction<? extends Query>>
expressionCreateFunctions = new HashMap<>();
public QueryFactory() {
registerExpressionCreateFunctions();
}
public AtlasQuery createTaxonomyQuery(Request request) throws InvalidQueryException {
ResourceDefinition taxonomyDefinition = new TaxonomyResourceDefinition();
QueryExpression queryExpression = create(request, taxonomyDefinition);
return new AtlasTaxonomyQuery(queryExpression, taxonomyDefinition, request);
}
public AtlasQuery createTermQuery(Request request) throws InvalidQueryException {
ResourceDefinition termDefinition = new TermResourceDefinition();
QueryExpression queryExpression = create(request, termDefinition);
TermPath termPath = request.getProperty("termPath");
return new AtlasTermQuery(queryExpression, termDefinition, termPath, request);
}
public AtlasQuery createEntityQuery(Request request) throws InvalidQueryException {
ResourceDefinition entityDefinition = new EntityResourceDefinition();
QueryExpression queryExpression = create(request, entityDefinition);
return new AtlasEntityQuery(queryExpression, entityDefinition, request);
}
public AtlasQuery createEntityTagQuery(Request request) throws InvalidQueryException {
ResourceDefinition entityTagDefinition = new EntityTagResourceDefinition();
QueryExpression queryExpression = create(request, entityTagDefinition);
String guid = request.getProperty("id");
return new AtlasEntityTagQuery(queryExpression, entityTagDefinition, guid, request);
}
private QueryExpression create(Request request, ResourceDefinition resourceDefinition) throws InvalidQueryException {
String queryString;
if (request.getCardinality() == Request.Cardinality.INSTANCE) {
String idPropertyName = resourceDefinition.getIdPropertyName();
queryString = String.format("%s:%s", idPropertyName, request.<String>getProperty(idPropertyName));
} else {
queryString = request.getQueryString();
}
QueryExpression queryExpression;
if (queryString != null && !queryString.isEmpty()) {
QueryParser queryParser = new QueryParser(Version.LUCENE_48, "name", new KeywordAnalyzer());
queryParser.setLowercaseExpandedTerms(false);
Query query;
try {
query = queryParser.parse((String) escape(queryString));
} catch (ParseException e) {
throw new InvalidQueryException(e.getMessage());
}
LOG.info("LuceneQuery: " + query);
queryExpression = create(query, resourceDefinition);
} else {
queryExpression = new AlwaysQueryExpression();
}
// add query properties to request so that they are returned
request.addAdditionalSelectProperties(queryExpression.getProperties());
return queryExpression;
}
@SuppressWarnings("unchecked")
protected <T extends Query> QueryExpression create(T query, ResourceDefinition resourceDefinition) {
if (! expressionCreateFunctions.containsKey(query.getClass())) {
throw new CatalogRuntimeException("Query type currently not supported: " + query.getClass(), 400);
}
//todo: fix generic typing
ExpressionCreateFunction expressionCreateFunction = expressionCreateFunctions.get(query.getClass());
return expressionCreateFunction.createExpression(query, resourceDefinition);
}
// "escapes" characters as necessary for lucene parser
//todo: currently '/' characters are blindly being replaced but this will not allow regex queries to be used
protected static Object escape(Object val) {
if (val instanceof String) {
return ((String)val).replaceAll("/", PATH_SEP_TOKEN);
} else {
return val;
}
}
private abstract static class ExpressionCreateFunction<T extends Query> {
QueryExpression createExpression(T query, ResourceDefinition resourceDefinition) {
QueryExpression expression = create(query, resourceDefinition);
return expression.isProjectionExpression() ?
new ProjectionQueryExpression(expression, resourceDefinition) :
expression;
}
protected abstract QueryExpression create(T query, ResourceDefinition resourceDefinition);
}
private void registerExpressionCreateFunctions() {
expressionCreateFunctions.put(WildcardQuery.class, new ExpressionCreateFunction<WildcardQuery>() {
@Override
public QueryExpression create(WildcardQuery query, ResourceDefinition definition) {
return new WildcardQueryExpression(query, definition);
}
});
expressionCreateFunctions.put(PrefixQuery.class, new ExpressionCreateFunction<PrefixQuery>() {
@Override
public QueryExpression create(PrefixQuery query, ResourceDefinition definition) {
return new PrefixQueryExpression(query, definition);
}
});
expressionCreateFunctions.put(TermQuery.class, new ExpressionCreateFunction<TermQuery>() {
@Override
public QueryExpression create(TermQuery query, ResourceDefinition definition) {
return new TermQueryExpression(query, definition);
}
});
expressionCreateFunctions.put(TermRangeQuery.class, new ExpressionCreateFunction<TermRangeQuery>() {
@Override
public QueryExpression create(TermRangeQuery query, ResourceDefinition definition) {
return new TermRangeQueryExpression(query, definition);
}
});
expressionCreateFunctions.put(RegexQuery.class, new ExpressionCreateFunction<RegexQuery>() {
@Override
public QueryExpression create(RegexQuery query, ResourceDefinition definition) {
return new RegexQueryExpression(query, definition);
}
});
expressionCreateFunctions.put(BooleanQuery.class, new ExpressionCreateFunction<BooleanQuery>() {
@Override
public QueryExpression create(BooleanQuery query, ResourceDefinition definition) {
return new BooleanQueryExpression(query, definition, QueryFactory.this);
}
});
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.sandbox.queries.regex.RegexQuery;
import java.util.regex.Pattern;
/**
* Query expression which evaluates a property against a regular expression.
*/
public class RegexQueryExpression extends BaseQueryExpression {
public RegexQueryExpression(RegexQuery query, ResourceDefinition resourceDefinition) {
super(query.getField(), query.getTerm().text(), resourceDefinition);
}
@Override
public boolean evaluate(Object value) {
Pattern p = Pattern.compile(getExpectedValue());
return value != null && p.matcher(String.valueOf(value)).matches();
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.search.TermQuery;
import java.util.Collection;
/**
* Query expression which evaluates whether a property equals a value.
*/
public class TermQueryExpression extends BaseQueryExpression {
public TermQueryExpression(TermQuery query, ResourceDefinition resourceDefinition) {
super(query.getTerm().field(), query.getTerm().text(), resourceDefinition);
}
@Override
public boolean evaluate(Object value) {
String expectedValue = getExpectedValue();
if (value == null) {
return expectedValue.equals("null");
//todo: refactor; we shouldn't need to use instanceof/cast here
} else if (value instanceof Collection) {
return ((Collection)value).contains(expectedValue);
} else {
return expectedValue.equals(QueryFactory.escape(String.valueOf(value)));
}
}
public String getExpectedValue() {
return m_expectedValue;
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.util.BytesRef;
/**
* Query expression which evaluates whether a property value is within a range.
*/
//todo: for month and year which are expressed via a single digit, must ensure that
//todo: a leading '0' is provided. For example, "2016-1-5" must be converted to "2016-01-05".
//todo: Month and day values aren't currently validated.
public class TermRangeQueryExpression extends BaseQueryExpression {
private final BytesRef m_lowerTerm;
private final BytesRef m_upperTerm;
private final boolean m_lowerInclusive;
private final boolean m_upperInclusive;
public TermRangeQueryExpression(TermRangeQuery query, ResourceDefinition resourceDefinition) {
super(query.getField(), null, resourceDefinition);
m_lowerTerm = query.getLowerTerm();
m_upperTerm = query.getUpperTerm();
m_lowerInclusive = query.includesLower();
m_upperInclusive = query.includesUpper();
}
@Override
public boolean evaluate(Object value) {
BytesRef valueBytes = new BytesRef(String.valueOf(value));
return compareLowerBound(valueBytes) && compareUpperBound(valueBytes);
}
private boolean compareLowerBound(BytesRef valueBytes) {
return m_lowerTerm == null || (m_lowerInclusive ? valueBytes.compareTo(m_lowerTerm) > 0 :
valueBytes.compareTo(m_lowerTerm) >= 0);
}
private boolean compareUpperBound(BytesRef valueBytes) {
return m_upperTerm == null || (m_upperInclusive ? valueBytes.compareTo(m_upperTerm) < 0 :
valueBytes.compareTo(m_upperTerm) <= 0);
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog.query;
import org.apache.atlas.catalog.definition.ResourceDefinition;
import org.apache.lucene.search.WildcardQuery;
import java.util.regex.Pattern;
/**
* Query expression which evaluates values with wildcards.
* This differs from PrefixQueryExpression which handles expressions which end with a wildcard.
*/
public class WildcardQueryExpression extends BaseQueryExpression {
public WildcardQueryExpression(WildcardQuery query, ResourceDefinition resourceDefinition) {
super(query.getTerm().field(), query.getTerm().text(), resourceDefinition);
}
@Override
public boolean evaluate(Object value) {
// replace '*' with ".*"
// replace '?' with '.'
String regex = getExpectedValue().replaceAll("\\*", ".*").replaceAll("\\?", ".");
return Pattern.compile(regex).matcher(String.valueOf(value)).matches();
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.testng.annotations.Test;
import java.util.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
/**
* Unit tests for CollectionRequest.
*/
public class CollectionRequestTest {
@Test
public void testNoProperties() {
String query = "name:foo*";
Request request = new CollectionRequest(null, query);
assertEquals(Request.Cardinality.COLLECTION, request.getCardinality());
assertTrue(request.getProperties().isEmpty());
assertNull(request.getProperty("foo"));
assertTrue(request.getAdditionalSelectProperties().isEmpty());
}
@Test
public void testWithProperties() {
String query = "name:foo*";
Map<String, Object> properties = new HashMap<>();
properties.put("foo", "fooValue");
properties.put("someBoolean", true);
Request request = new CollectionRequest(properties, query);
assertEquals(Request.Cardinality.COLLECTION, request.getCardinality());
assertEquals(properties, request.getProperties());
assertEquals("fooValue", request.getProperty("foo"));
assertTrue(request.<Boolean>getProperty("someBoolean"));
assertNull(request.getProperty("other"));
assertTrue(request.getAdditionalSelectProperties().isEmpty());
}
@Test
public void testSelectProperties() {
String query = "name:foo*";
Request request = new CollectionRequest(null, query);
Collection<String> additionalSelectProps = new ArrayList<>();
additionalSelectProps.add("foo");
additionalSelectProps.add("bar");
request.addAdditionalSelectProperties(additionalSelectProps);
Collection<String> requestAdditionalSelectProps = request.getAdditionalSelectProperties();
assertEquals(2, requestAdditionalSelectProps.size());
assertTrue(requestAdditionalSelectProps.contains("foo"));
assertTrue(requestAdditionalSelectProps.contains("bar"));
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.testng.annotations.Test;
import java.util.Calendar;
import java.util.GregorianCalendar;
import static org.testng.Assert.assertEquals;
/**
* Unit tests for DefaultDateFormatter.
*/
public class DefaultDateFormatterTest {
@Test
public void test() {
Calendar calendar = new GregorianCalendar(2016, 0, 20, 5, 10, 15);
long millis = calendar.getTimeInMillis();
DefaultDateFormatter dateFormatter = new DefaultDateFormatter();
// month starts at 0 so we need to add 1
assertEquals("2016-01-20:05:10:15", dateFormatter.format(millis));
}
}
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.atlas.catalog;
import org.apache.atlas.typesystem.types.AttributeInfo;
import org.apache.atlas.typesystem.types.FieldMapping;
import org.apache.atlas.typesystem.types.HierarchicalType;
import org.testng.annotations.Test;
import java.util.HashMap;
import java.util.Map;
import static org.easymock.EasyMock.*;
import static org.testng.Assert.assertEquals;
/**
* Unit tests for DefaultPropertyMapper.
*/
public class DefaultPropertyMapperTest {
@Test
public void testToCleanName_defaultMappings() {
String typeName = "testType";
HierarchicalType dataType = createNiceMock(HierarchicalType.class);
// currently only use key in map
Map<String, AttributeInfo> fields = new HashMap<>();
fields.put("foo", null);
fields.put("prop", null);
// can't mock FieldMapping due to direct access to final instance var 'fields'
FieldMapping fieldMapping = new FieldMapping(fields, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// mock expectations
expect(dataType.fieldMapping()).andReturn(fieldMapping).anyTimes();
replay(dataType);
PropertyMapper propertyMapper = new TestDefaultPropertyMapper(dataType);
assertEquals(propertyMapper.toCleanName("Prefix.prop", typeName), "prop");
assertEquals(propertyMapper.toCleanName("foo", typeName), "foo");
assertEquals(propertyMapper.toCleanName("other", typeName), "other");
assertEquals(propertyMapper.toCleanName("Prefix.other", typeName), "Prefix.other");
verify(dataType);
}
@Test
public void testToQualifiedName_defaultMappings() throws Exception {
String typeName = "testType";
HierarchicalType dataType = createNiceMock(HierarchicalType.class);
// currently only use key in map
Map<String, AttributeInfo> fields = new HashMap<>();
fields.put("foo", null);
fields.put("prop", null);
// can't mock FieldMapping due to direct access to final instance var 'fields'
FieldMapping fieldMapping = new FieldMapping(fields, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// mock expectations
expect(dataType.fieldMapping()).andReturn(fieldMapping).anyTimes();
expect(dataType.getQualifiedName("foo")).andReturn("foo");
expect(dataType.getQualifiedName("prop")).andReturn("Prefix.prop");
replay(dataType);
PropertyMapper propertyMapper = new TestDefaultPropertyMapper(dataType);
assertEquals(propertyMapper.toFullyQualifiedName("foo", typeName), "foo");
assertEquals(propertyMapper.toFullyQualifiedName("prop", typeName), "Prefix.prop");
assertEquals(propertyMapper.toFullyQualifiedName("other", typeName), "other");
assertEquals(propertyMapper.toFullyQualifiedName("Prefix.other", typeName), "Prefix.other");
verify(dataType);
}
@Test
public void testToCleanName_specifiedMappings() {
String typeName = "testType";
HierarchicalType dataType = createNiceMock(HierarchicalType.class);
// currently only use key in map
Map<String, AttributeInfo> fields = new HashMap<>();
fields.put("foo", null);
fields.put("prop", null);
// can't mock FieldMapping due to direct access to final instance var 'fields'
FieldMapping fieldMapping = new FieldMapping(fields, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// mock expectations
expect(dataType.fieldMapping()).andReturn(fieldMapping).anyTimes();
replay(dataType);
Map<String, String> cleanToQualifiedMap = new HashMap<>();
cleanToQualifiedMap.put("prop1", "property_1");
Map<String, String> qualifiedToCleanMap = new HashMap<>();
qualifiedToCleanMap.put("property_1", "prop1");
PropertyMapper propertyMapper = new TestDefaultPropertyMapper(
typeName, qualifiedToCleanMap, cleanToQualifiedMap, dataType);
assertEquals(propertyMapper.toCleanName("property_1", typeName), "prop1");
assertEquals(propertyMapper.toCleanName("Prefix.prop", typeName), "prop");
assertEquals(propertyMapper.toCleanName("foo", typeName), "foo");
assertEquals(propertyMapper.toCleanName("other", typeName), "other");
assertEquals(propertyMapper.toCleanName("Prefix.other", typeName), "Prefix.other");
verify(dataType);
}
@Test
public void testToQualifiedName_specifiedMappings() throws Exception {
String typeName = "testType";
HierarchicalType dataType = createNiceMock(HierarchicalType.class);
// currently only use key in map
Map<String, AttributeInfo> fields = new HashMap<>();
fields.put("foo", null);
fields.put("prop", null);
// can't mock FieldMapping due to direct access to final instance var 'fields'
FieldMapping fieldMapping = new FieldMapping(fields, null, null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// mock expectations
expect(dataType.fieldMapping()).andReturn(fieldMapping).anyTimes();
expect(dataType.getQualifiedName("foo")).andReturn("foo");
expect(dataType.getQualifiedName("prop")).andReturn("Prefix.prop");
replay(dataType);
Map<String, String> cleanToQualifiedMap = new HashMap<>();
cleanToQualifiedMap.put("prop1", "property_1");
Map<String, String> qualifiedToCleanMap = new HashMap<>();
qualifiedToCleanMap.put("property_1", "prop1");
PropertyMapper propertyMapper = new TestDefaultPropertyMapper(
typeName, qualifiedToCleanMap, cleanToQualifiedMap, dataType);
assertEquals(propertyMapper.toFullyQualifiedName("prop1", typeName), "property_1");
assertEquals(propertyMapper.toFullyQualifiedName("foo", typeName), "foo");
assertEquals(propertyMapper.toFullyQualifiedName("prop", typeName), "Prefix.prop");
assertEquals(propertyMapper.toFullyQualifiedName("other", typeName), "other");
assertEquals(propertyMapper.toFullyQualifiedName("Prefix.other", typeName), "Prefix.other");
verify(dataType);
}
private static class TestDefaultPropertyMapper extends DefaultPropertyMapper {
private HierarchicalType dataType;
public TestDefaultPropertyMapper(HierarchicalType dataType) {
super();
this.dataType = dataType;
}
public TestDefaultPropertyMapper(String type,
Map<String, String> qualifiedToCleanMap,
Map<String, String> cleanToQualifiedMap,
HierarchicalType dataType) {
super(qualifiedToCleanMap, cleanToQualifiedMap);
this.dataType = dataType;
}
@Override
protected HierarchicalType createDataType(String type) {
return dataType;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment