Search in sources :

Example 36 with DataSource

use of io.atlasmap.v2.DataSource in project syndesis-qe by syndesisio.

the class AtlasMapperGenerator method getAtlasMappingStep.

/**
 * This method is used to generate the "AtlasMapping" - the atlasMapping contains list of specifications of
 * dataSources and a list of specifications of dataMappings. Both these a must have for a complete and working
 * AtlasMapping.
 *
 * @return step with the mapping defined
 */
public Step getAtlasMappingStep() {
    processPrecedingSteps();
    processFollowingStep();
    AtlasMapping atlasMapping = new AtlasMapping();
    atlasMapping.setMappings(new Mappings());
    for (DataSource s : processSources()) {
        atlasMapping.getDataSource().add(s);
    }
    atlasMapping.setName("REST." + UUID.randomUUID().toString().replaceAll("-", ""));
    atlasMapping.setLookupTables(new LookupTables());
    atlasMapping.setProperties(new Properties());
    atlasMapping.getDataSource().add(processTarget());
    atlasMapping.getMappings().getMapping().addAll(generateBaseMappings());
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
    String mapperString = null;
    try {
        mapperString = mapper.writeValueAsString(atlasMapping);
        log.debug(mapperString);
    } catch (JsonProcessingException e) {
        log.error("Unable to write mapper json as string", e);
    }
    return new Step.Builder().stepKind(StepKind.mapper).name(mapping.getStep().getName()).configuredProperties(TestUtils.map("atlasmapping", mapperString)).action(getMapperStepAction(followingStep.getStep().getAction().get().getInputDataShape().get())).id(UUID.randomUUID().toString()).build();
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) Mappings(io.atlasmap.v2.Mappings) LookupTables(io.atlasmap.v2.LookupTables) Properties(io.atlasmap.v2.Properties) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(io.atlasmap.v2.DataSource) JsonDataSource(io.atlasmap.json.v2.JsonDataSource) XmlDataSource(io.atlasmap.xml.v2.XmlDataSource)

Example 37 with DataSource

use of io.atlasmap.v2.DataSource in project syndesis-qe by syndesisio.

the class AtlasMapperGenerator method createDataSource.

/**
 * Used to generate data source elements for atlasMapping. There are three types of them: Java, Json, XML.
 *
 * @param dataShape datashape of the processed step
 * @param step step definition
 * @param dataSourceType datasource type
 * @return datasource
 */
private DataSource createDataSource(DataShape dataShape, StepDefinition step, DataSourceType dataSourceType) {
    DataShapeKinds dataShapeKind = dataShape.getKind();
    DataSource source = null;
    if (dataShapeKind.toString().contains("json")) {
        source = new JsonDataSource();
        source.setUri("atlas:" + "json:" + step.getStep().getId().get());
    } else if (dataShapeKind.toString().contains("java")) {
        source = new DataSource();
        source.setUri("atlas:" + "java:" + step.getStep().getId().get() + "?className=" + dataShape.getType());
    } else if (dataShapeKind.toString().contains("xml")) {
        source = new XmlDataSource();
        source.setUri("atlas:xml:" + step.getStep().getId().get());
        XmlNamespaces xmlNamespaces = new XmlNamespaces();
        // Init the array, so that we don't have the null value
        xmlNamespaces.getXmlNamespace();
        ((XmlDataSource) source).setXmlNamespaces(xmlNamespaces);
    } else {
        fail("Unknown datashape kind " + dataShapeKind.toString());
    }
    source.setId(step.getStep().getId().get());
    source.setDataSourceType(dataSourceType);
    return source;
}
Also used : JsonDataSource(io.atlasmap.json.v2.JsonDataSource) DataShapeKinds(io.syndesis.common.model.DataShapeKinds) XmlNamespaces(io.atlasmap.xml.v2.XmlNamespaces) DataSource(io.atlasmap.v2.DataSource) JsonDataSource(io.atlasmap.json.v2.JsonDataSource) XmlDataSource(io.atlasmap.xml.v2.XmlDataSource) XmlDataSource(io.atlasmap.xml.v2.XmlDataSource)

Example 38 with DataSource

use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.

the class BaseMappingTest method getAtlasMappingFullValid.

protected AtlasMapping getAtlasMappingFullValid() {
    AtlasMapping mapping = AtlasModelFactory.createAtlasMapping();
    mapping.setName("thisis_a_valid.name");
    DataSource src = new DataSource();
    src.setDataSourceType(DataSourceType.SOURCE);
    src.setUri("atlas:java?2");
    DataSource tgt = new DataSource();
    tgt.setDataSourceType(DataSourceType.TARGET);
    tgt.setUri("atlas:java?3");
    mapping.getDataSource().add(src);
    mapping.getDataSource().add(tgt);
    Mapping mapFieldMapping = AtlasModelFactory.createMapping(MappingType.MAP);
    MockField inputField = AtlasModelFactory.createMockField();
    inputField.setFieldType(FieldType.STRING);
    inputField.setCustom("java.lang.String");
    inputField.setName("inputName");
    mapFieldMapping.getInputField().add(inputField);
    MockField outputField = AtlasModelFactory.createMockField();
    outputField.setFieldType(FieldType.STRING);
    outputField.setCustom("java.lang.String");
    outputField.setName("outputName");
    mapFieldMapping.getOutputField().add(outputField);
    Mapping separateMapping = AtlasModelFactory.createMapping(MappingType.SEPARATE);
    MockField sIJavaField = AtlasModelFactory.createMockField();
    sIJavaField.setFieldType(FieldType.STRING);
    sIJavaField.setCustom("java.lang.String");
    sIJavaField.setName("inputName");
    separateMapping.getInputField().add(sIJavaField);
    MockField sOJavaField = AtlasModelFactory.createMockField();
    sOJavaField.setFieldType(FieldType.STRING);
    sOJavaField.setCustom("java.lang.String");
    sOJavaField.setName("outputName");
    sOJavaField.setIndex(0);
    separateMapping.getOutputField().add(sOJavaField);
    mapping.getMappings().getMapping().add(mapFieldMapping);
    mapping.getMappings().getMapping().add(separateMapping);
    return mapping;
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) MockField(io.atlasmap.v2.MockField) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) DataSource(io.atlasmap.v2.DataSource)

Example 39 with DataSource

use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.

the class AtlasEndpoint method populateTargetDocuments.

private void populateTargetDocuments(AtlasSession session, Exchange exchange) {
    Message outMessage = exchange.getOut();
    outMessage.setHeaders(exchange.getIn().getHeaders());
    outMessage.setAttachments(exchange.getIn().getAttachments());
    if (session.getMapping().getDataSource() == null) {
        return;
    }
    DataSource[] targetDataSources = session.getMapping().getDataSource().stream().filter(ds -> ds.getDataSourceType() == DataSourceType.TARGET).toArray(DataSource[]::new);
    if (targetDataSources.length == 0) {
        Object newBody = session.getDefaultTargetDocument();
        outMessage.setBody(newBody);
        return;
    }
    if (targetDataSources.length == 1) {
        String docId = targetDataSources[0].getId();
        if (docId == null || docId.isEmpty()) {
            Object newBody = session.getDefaultTargetDocument();
            outMessage.setBody(newBody);
        } else {
            Object newBody = session.getTargetDocument(docId);
            outMessage.setBody(newBody);
        }
        setContentType(targetDataSources[0], outMessage);
        return;
    }
    Map<String, Object> targetDocuments = new HashMap<>();
    for (DataSource ds : targetDataSources) {
        String docId = ds.getId();
        if (docId == null || docId.isEmpty()) {
            targetDocuments.put(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID, session.getDefaultTargetDocument());
            Object newBody = session.getDefaultTargetDocument();
            outMessage.setBody(newBody);
            setContentType(ds, outMessage);
        } else {
            targetDocuments.put(docId, session.getTargetDocument(docId));
        }
    }
    switch(targetMapMode) {
        case MAP:
            if (targetMapName != null) {
                exchange.setProperty(targetMapName, targetDocuments);
            } else {
                outMessage.setBody(targetDocuments);
            }
            break;
        case MESSAGE_HEADER:
            targetDocuments.remove(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID);
            outMessage.getHeaders().putAll(targetDocuments);
            break;
        case EXCHANGE_PROPERTY:
            targetDocuments.remove(io.atlasmap.api.AtlasConstants.DEFAULT_TARGET_DOCUMENT_ID);
            exchange.getProperties().putAll(targetDocuments);
            break;
    }
}
Also used : Message(org.apache.camel.Message) LoggerFactory(org.slf4j.LoggerFactory) DataSource(io.atlasmap.v2.DataSource) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) ArrayList(java.util.ArrayList) UriEndpoint(org.apache.camel.spi.UriEndpoint) UriParam(org.apache.camel.spi.UriParam) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ResourceEndpoint(org.apache.camel.component.ResourceEndpoint) AtlasContextFactory(io.atlasmap.api.AtlasContextFactory) ExchangePattern(org.apache.camel.ExchangePattern) Logger(org.slf4j.Logger) AtlasException(io.atlasmap.api.AtlasException) ADM(io.atlasmap.api.AtlasContextFactory.Format.ADM) MessageHelper(org.apache.camel.util.MessageHelper) DataSourceType(io.atlasmap.v2.DataSourceType) List(java.util.List) Audit(io.atlasmap.v2.Audit) JSON(io.atlasmap.api.AtlasContextFactory.Format.JSON) AtlasSession(io.atlasmap.api.AtlasSession) ObjectHelper(org.apache.camel.util.ObjectHelper) AtlasContext(io.atlasmap.api.AtlasContext) InputStream(java.io.InputStream) Message(org.apache.camel.Message) HashMap(java.util.HashMap) DataSource(io.atlasmap.v2.DataSource)

Example 40 with DataSource

use of io.atlasmap.v2.DataSource in project atlasmap by atlasmap.

the class AtlasEndpoint method populateSourceDocuments.

private void populateSourceDocuments(Exchange exchange, AtlasSession session) {
    if (session.getMapping().getDataSource() == null) {
        return;
    }
    Message inMessage = exchange.getIn();
    CamelAtlasPropertyStrategy propertyStrategy = new CamelAtlasPropertyStrategy();
    propertyStrategy.setCurrentSourceMessage(inMessage);
    propertyStrategy.setTargetMessage(exchange.getMessage());
    propertyStrategy.setExchange(exchange);
    session.setAtlasPropertyStrategy(propertyStrategy);
    DataSource[] sourceDataSources = session.getMapping().getDataSource().stream().filter(ds -> ds.getDataSourceType() == DataSourceType.SOURCE).toArray(DataSource[]::new);
    if (sourceDataSources.length == 0) {
        session.setDefaultSourceDocument(exchange.getIn().getBody());
        return;
    }
    if (sourceDataSources.length == 1) {
        String docId = sourceDataSources[0].getId();
        Object payload = extractPayload(sourceDataSources[0], inMessage);
        if (docId == null || docId.isEmpty()) {
            session.setDefaultSourceDocument(payload);
        } else {
            session.setSourceDocument(docId, payload);
            propertyStrategy.setSourceMessage(docId, inMessage);
        }
        return;
    }
    Map<String, Message> sourceMessages = null;
    Map<String, Object> sourceDocuments = null;
    if (sourceMapName != null) {
        sourceMessages = exchange.getProperty(sourceMapName, Map.class);
    }
    if (sourceMessages == null) {
        Object body = exchange.getIn().getBody();
        if (body instanceof Map) {
            sourceDocuments = (Map<String, Object>) body;
        } else {
            session.setDefaultSourceDocument(body);
        }
    }
    for (DataSource ds : sourceDataSources) {
        String docId = ds.getId();
        if (docId == null || docId.isEmpty()) {
            Object payload = extractPayload(ds, inMessage);
            session.setDefaultSourceDocument(payload);
        } else if (sourceMessages != null) {
            Object payload = extractPayload(ds, sourceMessages.get(docId));
            session.setSourceDocument(docId, payload);
            propertyStrategy.setSourceMessage(docId, sourceMessages.get(docId));
        } else if (sourceDocuments != null) {
            Object payload = sourceDocuments.get(docId);
            session.setSourceDocument(docId, payload);
        } else if (inMessage.getHeaders().containsKey(docId)) {
            Object payload = inMessage.getHeader(docId);
            session.setSourceDocument(docId, payload);
        } else if (exchange.getProperties().containsKey(docId)) {
            Object payload = exchange.getProperty(docId);
            session.setSourceDocument(docId, payload);
        } else {
            LOG.warn("Ignoring missing source document: '{}(ID:{})'", ds.getName(), ds.getId());
        }
    }
}
Also used : Message(org.apache.camel.Message) LoggerFactory(org.slf4j.LoggerFactory) DataSource(io.atlasmap.v2.DataSource) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) ArrayList(java.util.ArrayList) UriEndpoint(org.apache.camel.spi.UriEndpoint) UriParam(org.apache.camel.spi.UriParam) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ResourceEndpoint(org.apache.camel.component.ResourceEndpoint) AtlasContextFactory(io.atlasmap.api.AtlasContextFactory) ExchangePattern(org.apache.camel.ExchangePattern) Logger(org.slf4j.Logger) AtlasException(io.atlasmap.api.AtlasException) ADM(io.atlasmap.api.AtlasContextFactory.Format.ADM) MessageHelper(org.apache.camel.util.MessageHelper) DataSourceType(io.atlasmap.v2.DataSourceType) List(java.util.List) Audit(io.atlasmap.v2.Audit) JSON(io.atlasmap.api.AtlasContextFactory.Format.JSON) AtlasSession(io.atlasmap.api.AtlasSession) ObjectHelper(org.apache.camel.util.ObjectHelper) AtlasContext(io.atlasmap.api.AtlasContext) InputStream(java.io.InputStream) Message(org.apache.camel.Message) HashMap(java.util.HashMap) Map(java.util.Map) DataSource(io.atlasmap.v2.DataSource)

Aggregations

DataSource (io.atlasmap.v2.DataSource)54 AtlasMapping (io.atlasmap.v2.AtlasMapping)16 ArrayList (java.util.ArrayList)14 Mapping (io.atlasmap.v2.Mapping)11 XmlDataSource (io.atlasmap.xml.v2.XmlDataSource)10 Mappings (io.atlasmap.v2.Mappings)8 JsonDataSource (io.atlasmap.json.v2.JsonDataSource)7 Test (org.junit.Test)7 File (java.io.File)6 AtlasContext (io.atlasmap.api.AtlasContext)5 AtlasSession (io.atlasmap.api.AtlasSession)5 JavaField (io.atlasmap.java.v2.JavaField)5 AtlasException (io.atlasmap.api.AtlasException)4 XmlNamespaces (io.atlasmap.xml.v2.XmlNamespaces)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 AtlasModule (io.atlasmap.spi.AtlasModule)3 HashMap (java.util.HashMap)3 Exchange (org.apache.camel.Exchange)3