Commit 96da2306 by Sarath Subramanian Committed by Madhan Neethiraj

ATLAS-2216: Remove Catalog/Taxonomy feature from Atlas

parent a9928f90
{
"enumDefs": [],
"structDefs": [],
"classificationDefs": [
{
"name": "TaxonomyTerm",
"superTypes": [],
"typeVersion": "1.0",
"attributeDefs": [
{
"name": "atlas.taxonomy",
"typeName": "string",
"cardinality": "SINGLE",
"isIndexable": false,
"isOptional": true,
"isUnique": false
}
]
}
],
"classificationDefs": [],
"entityDefs": [
{
"name": "Referenceable",
......
......@@ -19,5 +19,5 @@
package org.apache.atlas.authorize;
public enum AtlasResourceTypes {
UNKNOWN, ENTITY, TYPE, OPERATION, TAXONOMY, TERM, RELATIONSHIP
UNKNOWN, ENTITY, TYPE, OPERATION, RELATIONSHIP
}
......@@ -111,9 +111,6 @@ public class AtlasAuthorizationUtils {
* entities,lineage and discovery apis are mapped with AtlasResourceTypes.ENTITY eg :- /api/atlas/lineage/hive/table/*
* /api/atlas/entities/{guid}* /api/atlas/discovery/*
*
* taxonomy API are also mapped to AtlasResourceTypes.TAXONOMY & AtlasResourceTypes.ENTITY and its terms APIs have
* added AtlasResourceTypes.TERM associations.
*
* unprotected types are mapped with AtlasResourceTypes.UNKNOWN, access to these are allowed.
*/
public static Set<AtlasResourceTypes> getAtlasResourceType(String contextPath) {
......@@ -132,13 +129,6 @@ public class AtlasAuthorizationUtils {
} else if (api.startsWith("entities") || api.startsWith("lineage") ||
api.startsWith("discovery") || api.startsWith("entity") || api.startsWith("search")) {
resourceTypes.add(AtlasResourceTypes.ENTITY);
} else if (api.startsWith("taxonomies")) {
resourceTypes.add(AtlasResourceTypes.TAXONOMY);
// taxonomies are modeled as entities
resourceTypes.add(AtlasResourceTypes.ENTITY);
if (contextPath.contains("/terms")) {
resourceTypes.add(AtlasResourceTypes.TERM);
}
} else if (api.startsWith("relationship")) {
resourceTypes.add(AtlasResourceTypes.RELATIONSHIP);
} else {
......
......@@ -226,10 +226,6 @@ public class PolicyParser {
resourceType = AtlasResourceTypes.OPERATION;
} else if (type.equalsIgnoreCase("TYPE")) {
resourceType = AtlasResourceTypes.TYPE;
} else if (type.equalsIgnoreCase("TAXONOMY")) {
resourceType = AtlasResourceTypes.TAXONOMY;
} else if (type.equalsIgnoreCase("TERM")) {
resourceType = AtlasResourceTypes.TERM;
} else if (type.equalsIgnoreCase("RELATIONSHIP")) {
resourceType = AtlasResourceTypes.RELATIONSHIP;
} else {
......
......@@ -92,19 +92,6 @@ public class AtlasAuthorizationUtilsTest {
assertEquals(resourceTypes.size(), 1);
assertTrue(resourceTypes.contains(AtlasResourceTypes.ENTITY));
contextPath = "/api/atlas/v1/taxonomies";
resourceTypes = AtlasAuthorizationUtils.getAtlasResourceType(contextPath);
assertEquals(resourceTypes.size(), 2);
assertTrue(resourceTypes.contains(AtlasResourceTypes.TAXONOMY));
assertTrue(resourceTypes.contains(AtlasResourceTypes.ENTITY));
contextPath = "/api/atlas/v1/taxonomies/taxonomy1/terms";
resourceTypes = AtlasAuthorizationUtils.getAtlasResourceType(contextPath);
assertEquals(resourceTypes.size(), 3);
assertTrue(resourceTypes.contains(AtlasResourceTypes.TAXONOMY));
assertTrue(resourceTypes.contains(AtlasResourceTypes.ENTITY));
assertTrue(resourceTypes.contains(AtlasResourceTypes.TERM));
contextPath = "/api/atlas/v1/entities/111";
resourceTypes = AtlasAuthorizationUtils.getAtlasResourceType(contextPath);
assertEquals(resourceTypes.size(), 1);
......
<?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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>atlas-catalog</artifactId>
<description>Apache Atlas Business Catalog Module</description>
<name>Apache Atlas Business Catalog</name>
<packaging>jar</packaging>
<properties>
<tinkerpop.version>2.6.0</tinkerpop.version>
<titan.version>0.5.4</titan.version>
<lucene.version>4.8.1</lucene.version>
</properties>
<profiles>
<profile>
<id>titan1</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<lucene.version>4.10.4</lucene.version>
</properties>
</profile>
</profiles>
<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>org.apache.atlas</groupId>
<artifactId>atlas-graphdb-api</artifactId>
</dependency>
<!-- for now, keep the titan 0.5.4 / TP 2 dependencies. This will need to be changed to use a
more generic query framework that supports both TP2/TP3. Maybe the DSL translation could be changed to use
that as well...
-->
<dependency>
<groupId>com.tinkerpop.blueprints</groupId>
<artifactId>blueprints-core</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>com.tinkerpop.gremlin</groupId>
<artifactId>gremlin-java</artifactId>
<version>${tinkerpop.version}</version>
</dependency>
<dependency>
<groupId>com.thinkaurelius.titan</groupId>
<artifactId>titan-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>${lucene.version}</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>${lucene.version}</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>${lucene.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</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>
<dependency>
<groupId>org.apache.atlas</groupId>
<artifactId>atlas-typesystem</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</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 org.apache.atlas.catalog.exception.ResourceNotFoundException;
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
*/
String createEntity(ResourceDefinition definition, Request request)
throws ResourceAlreadyExistsException;
/**
* Delete an entity from the Atlas type system.
*
* @param definition definition of the resource being deleted
* @param request user request
*
* @throws ResourceNotFoundException if the resource to delete doesn't exist
*/
void deleteEntity(ResourceDefinition definition, Request request) throws ResourceNotFoundException;
/**
* 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;
/**
* Delete a tag instance.
*
* @param guid associated entity guid
* @param traitName name of the trait to delete
*
* @throws ResourceNotFoundException if the specified trait doesn't exist for the specified entity
*/
void deleteTag(String guid, String traitName) 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;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
/**
* Base user API request.
*/
public abstract class BaseRequest implements Request {
private final Map<String, Object> queryProperties = new HashMap<>();
private final Map<String, Object> updateProperties = new HashMap<>();
private final String queryString;
private final Collection<String> additionalSelectProperties = new HashSet<>();
protected BaseRequest(Map<String, Object> queryProperties, String queryString) {
this(queryProperties, queryString, null);
}
protected BaseRequest(Map<String, Object> queryProperties, String queryString, Map<String, Object> updateProperties) {
if (queryProperties != null) {
this.queryProperties.putAll(queryProperties);
}
if (updateProperties != null) {
this.updateProperties.putAll(updateProperties);
}
this.queryString = queryString;
}
public Map<String, Object> getQueryProperties() {
return queryProperties;
}
public Map<String, Object> getUpdateProperties() {
return updateProperties;
}
public <T> T getProperty(String name) {
return (T) queryProperties.get(name);
}
public String getQueryString() {
return queryString;
}
@Override
public void addAdditionalSelectProperties(Collection<String> resultProperties) {
additionalSelectProperties.addAll(resultProperties);
}
@Override
public Collection<String> getAdditionalSelectProperties() {
return additionalSelectProperties;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseRequest that = (BaseRequest) o;
return Objects.equals(queryProperties, that.queryProperties) &&
Objects.equals(updateProperties, that.updateProperties) &&
Objects.equals(queryString, that.queryString) &&
Objects.equals(additionalSelectProperties, that.additionalSelectProperties);
}
@Override
public int hashCode() {
return Objects.hash(queryProperties, updateProperties, queryString, 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.exception.InvalidPayloadException;
import org.apache.atlas.catalog.exception.ResourceNotFoundException;
import org.apache.atlas.catalog.query.QueryFactory;
/**
* Base class for resource providers.
*/
public abstract class BaseResourceProvider implements ResourceProvider {
protected final AtlasTypeSystem typeSystem;
protected QueryFactory queryFactory = new QueryFactory();
protected final ResourceDefinition resourceDefinition;
protected BaseResourceProvider(AtlasTypeSystem typeSystem, ResourceDefinition resourceDefinition) {
this.typeSystem = typeSystem;
this.resourceDefinition = resourceDefinition;
}
protected void setQueryFactory(QueryFactory factory) {
queryFactory = factory;
}
@Override
public void deleteResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException {
throw new InvalidPayloadException("Delete is not supported for this resource type");
}
@Override
public void updateResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException {
throw new InvalidPayloadException("Update is not supported for this resource 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 java.util.Map;
/**
* A request for a collection resource.
*/
public class CollectionRequest extends BaseRequest {
public CollectionRequest(Map<String, Object> queryProperties, String queryString) {
super(queryProperties, queryString);
}
public CollectionRequest(Map<String, Object> queryProperties, String queryString, Map<String, Object> updateProperties) {
super(queryProperties, queryString, updateProperties);
}
@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);
m_qualifiedToCleanMap.put(Constants.CREATED_BY_KEY, "created_by");
m_cleanToQualifiedMap.put("created_by", Constants.CREATED_BY_KEY);
m_qualifiedToCleanMap.put(Constants.MODIFIED_BY_KEY, "modified_by");
m_cleanToQualifiedMap.put("modified_by", Constants.MODIFIED_BY_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.catalog.definition.EntityResourceDefinition;
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 {
public EntityResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem, new EntityResourceDefinition());
}
@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.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import java.util.*;
/**
* Provider for entity tag resources.
*/
public class EntityTagResourceProvider extends BaseResourceProvider implements ResourceProvider {
private TermResourceProvider termResourceProvider;
public EntityTagResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem, new EntityTagResourceDefinition());
}
@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.getQueryProperties().remove("id"));
resourceDefinition.validateCreatePayload(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;
}
@Override
public void deleteResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException {
typeSystem.deleteTag(request.<String>getProperty("id"), request.<String>getProperty("name"));
}
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> queryProperties) {
super(queryProperties, null);
}
public InstanceRequest(Map<String, Object> queryProperties, Map<String, Object> updateProperties) {
super(queryProperties, null, updateProperties);
}
@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 query properties of request.
* These are the properties which are used to build the query.
*
* @return query property map
*/
Map<String, Object> getQueryProperties();
/**
* Get update properties of request.
* These properties are updated on all resources which are returned from the query.
*
* @return update property map
*/
Map<String, Object> getUpdateProperties();
/**
* 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;
/**
* Delete a single resource.
*
* @param request request instance containing the id of the resource to delete.
*
* @throws ResourceNotFoundException if the resource doesn't exist
* @throws InvalidPayloadException if the request is invalid
*/
void deleteResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException;
//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;
/**
* Update a single resource.
*
* @param request request instance containing the contents of the resource to update
*
* @throws ResourceNotFoundException if the resource doesn't exist
* @throws InvalidPayloadException if the request payload is invalid
*/
void updateResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException;
}
/**
* 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.ApplicationProperties;
import org.apache.atlas.AtlasException;
import org.apache.atlas.catalog.definition.TaxonomyResourceDefinition;
import org.apache.atlas.catalog.exception.*;
import org.apache.atlas.catalog.query.AtlasQuery;
import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* Provider for taxonomy resources.
*/
public class TaxonomyResourceProvider extends BaseResourceProvider implements ResourceProvider {
private static final Logger LOG = LoggerFactory.getLogger(TaxonomyResourceProvider.class);
public static final String DEFAULT_TAXONOMY_NAME = "Catalog";
public static final String DEFAULT_TAXONOMY_DESCRIPTION = "Business Catalog";
public static final String NAMESPACE_ATTRIBUTE_NAME = "taxonomy.namespace";
// Taxonomy Term type
public static final String TAXONOMY_TERM_TYPE = "TaxonomyTerm";
// Taxonomy Namespace
public static final String TAXONOMY_NS = "atlas.taxonomy";
private final TermResourceProvider termResourceProvider;
// This is a cached value to prevent checking for taxonomy objects in every API call.
// It is updated once per lifetime of the application.
// TODO: If a taxonomy is deleted outside of this application, this value is not updated
// TODO: and there is no way in which a taxonomy will be auto-created.
// TODO: Assumption is that if a taxonomy is deleted externally, it will be created externally as well.
private static boolean taxonomyAutoInitializationChecked = false;
public TaxonomyResourceProvider(AtlasTypeSystem typeSystem) {
super(typeSystem, new TaxonomyResourceDefinition());
termResourceProvider = new TermResourceProvider(typeSystem);
}
@Override
public Result getResourceById(Request request) throws ResourceNotFoundException {
synchronized (TaxonomyResourceProvider.class) {
createDefaultTaxonomyIfNeeded();
}
return doGetResourceById(request);
}
@Override
public Result getResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
synchronized (TaxonomyResourceProvider.class) {
createDefaultTaxonomyIfNeeded();
}
return doGetResources(request);
}
@Override
public void createResource(Request request)
throws InvalidPayloadException, ResourceAlreadyExistsException {
// not checking for default taxonomy in create per requirements
resourceDefinition.validateCreatePayload(request);
synchronized (TaxonomyResourceProvider.class) {
ensureTaxonomyDoesntExist(request);
doCreateResource(request);
}
}
@Override
public Collection<String> createResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
throw new UnsupportedOperationException(
"Creating multiple Taxonomies in a request is not currently supported");
}
@Override
public void deleteResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException {
String taxonomyId = getResourceId(request);
getTermResourceProvider().deleteChildren(taxonomyId, new TermPath(request.<String>getProperty("name")));
typeSystem.deleteEntity(resourceDefinition, request);
}
@Override
public void updateResourceById(Request request) throws ResourceNotFoundException, InvalidPayloadException {
resourceDefinition.validateUpdatePayload(request);
AtlasQuery atlasQuery;
try {
atlasQuery = queryFactory.createTaxonomyQuery(request);
} catch (InvalidQueryException e) {
throw new CatalogRuntimeException("Unable to compile internal Term query: " + e, e);
}
synchronized (TaxonomyResourceProvider.class) {
createDefaultTaxonomyIfNeeded();
if (atlasQuery.execute(request.getUpdateProperties()).isEmpty()) {
throw new ResourceNotFoundException(String.format("Taxonomy '%s' not found.",
request.getQueryProperties().get("name")));
}
}
}
private String getResourceId(Request request) throws ResourceNotFoundException {
request.addAdditionalSelectProperties(Collections.singleton("id"));
// will result in expected ResourceNotFoundException if taxonomy doesn't exist
Result result = getResourceById(request);
return String.valueOf(result.getPropertyMaps().iterator().next().get("id"));
}
//todo: this is currently required because the expected exception isn't thrown by the Atlas repository
//todo: when an attempt is made to create an entity that already exists
// must be called from within class monitor
private void ensureTaxonomyDoesntExist(Request request) throws ResourceAlreadyExistsException {
try {
doGetResourceById(request);
throw new ResourceAlreadyExistsException(String.format("Taxonomy '%s' already exists.",
request.getProperty("name")));
} catch (ResourceNotFoundException e) {
// expected case
}
}
// must be called from within class monitor
private Result doGetResourceById(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>> resultSet = atlasQuery.execute();
if (resultSet.isEmpty()) {
throw new ResourceNotFoundException(String.format("Taxonomy '%s' not found.",
request.getProperty(resourceDefinition.getIdPropertyName())));
}
return new Result(resultSet);
}
// must be called from within class monitor
private Result doGetResources(Request request) throws InvalidQueryException, ResourceNotFoundException {
AtlasQuery atlasQuery = queryFactory.createTaxonomyQuery(request);
return new Result(atlasQuery.execute());
}
// must be called from within class monitor
private void doCreateResource(Request request) throws ResourceAlreadyExistsException {
typeSystem.createEntity(resourceDefinition, request);
taxonomyAutoInitializationChecked = true;
}
// must be called from within class monitor
private void createDefaultTaxonomyIfNeeded() {
if (! autoInitializationChecked()) {
try {
LOG.info("Checking if default taxonomy needs to be created.");
// if any business taxonomy has been created, don't create one more - hence searching to
// see if any taxonomy exists.
if (doGetResources(new CollectionRequest(null, null)).getPropertyMaps().isEmpty()) {
LOG.info("No taxonomies found - going to create default taxonomy.");
Map<String, Object> requestProperties = new HashMap<>();
String defaultTaxonomyName = DEFAULT_TAXONOMY_NAME;
try {
Configuration configuration = ApplicationProperties.get();
defaultTaxonomyName = configuration.getString("atlas.taxonomy.default.name",
defaultTaxonomyName);
} catch (AtlasException e) {
LOG.warn("Unable to read Atlas configuration, will use {} as default taxonomy name",
defaultTaxonomyName, e);
}
requestProperties.put("name", defaultTaxonomyName);
requestProperties.put("description", DEFAULT_TAXONOMY_DESCRIPTION);
doCreateResource(new InstanceRequest(requestProperties));
LOG.info("Successfully created default taxonomy {}.", defaultTaxonomyName);
} else {
taxonomyAutoInitializationChecked = true;
LOG.info("Some taxonomy exists, not creating default taxonomy");
}
} catch (InvalidQueryException | ResourceNotFoundException e) {
LOG.error("Unable to query for existing taxonomies due to internal error.", e);
} catch (ResourceAlreadyExistsException e) {
LOG.info("Attempted to create default taxonomy and it already exists.");
}
}
}
protected boolean autoInitializationChecked() {
return taxonomyAutoInitializationChecked;
}
protected TermResourceProvider getTermResourceProvider() {
return termResourceProvider;
}
}
\ 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 com.tinkerpop.blueprints.Vertex;
import org.apache.atlas.catalog.definition.EntityTagResourceDefinition;
/**
* 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 void setProperty(String name, Object value) {
vertex.setProperty(propertyMapper.toFullyQualifiedName(name, vertexType), value);
}
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 validateCreatePayload(Request request) throws InvalidPayloadException {
Collection<String> propKeys = new HashSet<>(request.getQueryProperties().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 void validateUpdatePayload(Request request) throws InvalidPayloadException {
Collection<String> updateKeys = new HashSet<>(request.getUpdateProperties().keySet());
Collection<String> validProperties = new HashSet<>(properties.keySet());
// currently updating 'name' property for any resource is unsupported
validProperties.remove("name");
updateKeys.removeAll(validProperties);
if (! updateKeys.isEmpty()) {
throw new InvalidPayloadException(Collections.<String>emptySet(), updateKeys);
}
}
@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 validateCreatePayload(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 create request payload.
*
* @param request user create request
*
* @throws InvalidPayloadException if the request payload is invalid in any way
*/
void validateCreatePayload(Request request) throws InvalidPayloadException;
/**
* Validate a user update request payload.
*
* @param request user update request
*
* @throws InvalidPayloadException if the request payload is invalid in any way
*/
void validateUpdatePayload(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.TaxonomyResourceProvider;
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));
registerProperty(TypesUtil.createOptionalAttrDef(TaxonomyResourceProvider.NAMESPACE_ATTRIBUTE_NAME, 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 validateCreatePayload(Request request) throws InvalidPayloadException {
super.validateCreatePayload(request);
if (String.valueOf(request.getQueryProperties().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 validateCreatePayload(Request request) throws InvalidPayloadException {
super.validateCreatePayload(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.getQueryProperties().containsKey("available_as_tag")) {
request.getQueryProperties().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.Vertex;
import org.apache.atlas.repository.Constants;
import org.apache.atlas.typesystem.persistence.Id;
/**
* Provides functionality common across implementations.
*/
public abstract class BaseRelation implements Relation {
protected boolean isDeleted(Vertex v) {
return ! Id.EntityState.ACTIVE.name().equals(
v.<String>getProperty(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.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 extends BaseRelation {
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);
if (! isDeleted(adjacentVertex)) {
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 extends BaseRelation {
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") && ! isDeleted(trait.getVertex())) {
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);
if (edge.getLabel().startsWith(name)) {
VertexWrapper v = new TermVertexWrapper(edge.getVertex(Direction.IN));
return v.getPropertyKeys().contains("available_as_tag") && ! isDeleted(v.getVertex());
} else {
return false;
}
}
});
}
@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 extends BaseRelation {
//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") && ! isDeleted(trait.getVertex())) {
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 name = edge.getVertex(Direction.OUT).getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
if (edge.getLabel().startsWith(name)) {
VertexWrapper v = new TermVertexWrapper(edge.getVertex(Direction.IN));
return ! v.getPropertyKeys().contains("available_as_tag") && ! isDeleted(v.getVertex());
} else {
return false;
}
}
});
}
@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 com.tinkerpop.pipes.Pipe;
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 Pipe getQueryPipe() {
return new GremlinPipeline().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.Pipe;
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 Pipe getQueryPipe() {
GremlinPipeline p;
if (guid.equals("*")) {
p = new GremlinPipeline().has(Constants.ENTITY_TEXT_PROPERTY_KEY).
hasNot(Constants.ENTITY_TYPE_PROPERTY_KEY, "Taxonomy").outE();
} else {
p = new GremlinPipeline().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(VertexWrapper vWrapper, Map<String, Object> filteredPropertyMap) {
Map<String, Object> map = new HashMap<>(filteredPropertyMap);
if (guid.equals("*")) {
map.put(EntityTagResourceDefinition.ENTITY_GUID_PROPERTY, vWrapper.getVertex().getEdges(Direction.IN).
iterator().next().getVertex(Direction.OUT).getProperty(Constants.GUID_PROPERTY_KEY));
} else {
map.put(EntityTagResourceDefinition.ENTITY_GUID_PROPERTY, guid);
}
String href = resourceDefinition.resolveHref(map);
if (href != null) {
filteredPropertyMap.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;
/**
* Execute the query and update the results with the provided properties.
*
* @param updateProperties properties name/values to update on query results
*
* @return collection of property maps, one per matching resource
* @throws ResourceNotFoundException if an explicitly specified resource doesn't exist
*/
Collection<Map<String, Object>> execute(Map<String, Object> updateProperties) 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 com.tinkerpop.pipes.Pipe;
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 Pipe getQueryPipe() {
return new GremlinPipeline().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 com.tinkerpop.pipes.Pipe;
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 Pipe getQueryPipe() {
GremlinPipeline p;
if (termPath.getTaxonomyName().equals("*")) {
p = new GremlinPipeline().has("Taxonomy.name").out();
} else {
p = new GremlinPipeline().has("Taxonomy.name", termPath.getTaxonomyName()).out().
has(Constants.ENTITY_TYPE_PROPERTY_KEY, Text.PREFIX, termPath.getFullyQualifiedName());
}
return p;
}
}
/**
* 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 java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
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.Constants;
import org.apache.atlas.repository.graph.AtlasGraphProvider;
import org.apache.atlas.repository.graphdb.AtlasElement;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.typesystem.persistence.Id;
import com.tinkerpop.blueprints.Compare;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.gremlin.java.GremlinPipeline;
import com.tinkerpop.pipes.Pipe;
import com.tinkerpop.pipes.filter.PropertyFilterPipe;
/**
* 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<>();
try {
for (Vertex vertex : executeQuery()) {
resultMaps.add(processPropertyMap(wrapVertex(vertex)));
}
getGraph().commit();
} catch (Throwable t) {
getGraph().rollback();
throw t;
}
return resultMaps;
}
@Override
public Collection<Map<String, Object>> execute(Map<String, Object> updateProperties)
throws ResourceNotFoundException {
Collection<Map<String, Object>> resultMaps = new ArrayList<>();
try {
for (Vertex vertex : executeQuery()) {
VertexWrapper vWrapper = wrapVertex(vertex);
for (Map.Entry<String, Object> property : updateProperties.entrySet()) {
vWrapper.setProperty(property.getKey(), property.getValue());
vWrapper.setProperty(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, System.currentTimeMillis());
}
resultMaps.add(processPropertyMap(vWrapper));
}
getGraph().commit();
} catch (Throwable e) {
getGraph().rollback();
throw e;
}
return resultMaps;
}
private List<Vertex> executeQuery() {
GremlinPipeline pipeline = buildPipeline().as("root");
Pipe expressionPipe = queryExpression.asPipe();
// AlwaysQuery returns null for pipe
return expressionPipe == null ? pipeline.toList() :
pipeline.add(expressionPipe).back("root").toList();
}
protected GremlinPipeline buildPipeline() {
GremlinPipeline pipeline = getRootVertexPipeline();
Pipe queryPipe = getQueryPipe();
if (queryPipe != null) {
pipeline.add(queryPipe);
}
pipeline.add(getNotDeletedPipe());
return pipeline;
}
protected abstract Pipe getQueryPipe();
protected GremlinPipeline getRootVertexPipeline() {
return new GremlinPipeline(unWrapVertices());
}
protected Iterable<Object> unWrapVertices() {
final Iterable<AtlasVertex> vertices = getGraph().getVertices();
Iterable<Object> vertexIterable = new Iterable<Object>() {
Iterator<Object> iterator = new Iterator<Object>() {
Iterator<AtlasVertex> wrapperIterator = vertices.iterator();
@Override
public boolean hasNext() {
return wrapperIterator.hasNext();
}
@Override
public Object next() {
if (hasNext()) {
return ((AtlasElement) wrapperIterator.next().getV()).getWrappedElement();
} else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported");
}
};
@Override
public Iterator<Object> iterator() {
return iterator;
}
};
return vertexIterable;
}
protected Pipe getNotDeletedPipe() {
return new PropertyFilterPipe(Constants.STATE_PROPERTY_KEY, Compare.EQUAL,
Id.EntityState.ACTIVE.name());
}
protected Map<String, Object> processPropertyMap(VertexWrapper vertex) {
Map<String, Object> propertyMap = resourceDefinition.filterProperties(
request, vertex.getPropertyMap());
addHref(vertex, propertyMap);
return request.getCardinality() == Request.Cardinality.INSTANCE ?
applyProjections(vertex, propertyMap) :
propertyMap;
}
protected void addHref(VertexWrapper vWrapper, Map<String, Object> filteredPropertyMap) {
String href = resourceDefinition.resolveHref(filteredPropertyMap);
if (href != null) {
filteredPropertyMap.put("href", href);
}
}
protected 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;
}
// Underlying method is synchronized and caches the graph in a static field
protected AtlasGraph getGraph() {
return AtlasGraphProvider.getGraphInstance();
}
protected VertexWrapper wrapVertex(Vertex v) {
return new VertexWrapper(v, 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.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
*/
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);
queryParser.setAllowLeadingWildcard(true);
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);
}
});
}
}
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