Search in sources :

Example 1 with FieldMapping

use of datawave.webservice.model.FieldMapping in project datawave by NationalSecurityAgency.

the class ModelBean method getModel.

/**
 * Retrieve the model and all of its mappings
 *
 * @param name
 *            model name
 * @param modelTableName
 *            name of the table that contains the model
 * @return datawave.webservice.model.Model
 * @RequestHeader X-ProxiedEntitiesChain use when proxying request for user
 *
 * @HTTP 200 success
 * @HTTP 404 model not found
 * @HTTP 500 internal server error
 */
@GET
@Produces({ "application/xml", "text/xml", "application/json", "text/yaml", "text/x-yaml", "application/x-yaml", "application/x-protobuf", "application/x-protostuff", "text/html" })
@Path("/{name}")
@GZIP
@Interceptors({ RequiredInterceptor.class, ResponseInterceptor.class })
public datawave.webservice.model.Model getModel(@Required("name") @PathParam("name") String name, @QueryParam("modelTableName") String modelTableName) {
    if (modelTableName == null) {
        modelTableName = defaultModelTableName;
    }
    datawave.webservice.model.Model response = new datawave.webservice.model.Model(jqueryUri, dataTablesUri);
    // Find out who/what called this method
    Principal p = ctx.getCallerPrincipal();
    String user = p.getName();
    Set<Authorizations> cbAuths = new HashSet<>();
    if (p instanceof DatawavePrincipal) {
        DatawavePrincipal cp = (DatawavePrincipal) p;
        user = cp.getShortName();
        for (Collection<String> auths : cp.getAuthorizations()) {
            cbAuths.add(new Authorizations(auths.toArray(new String[auths.size()])));
        }
    }
    log.trace(user + " has authorizations " + cbAuths);
    Connector connector = null;
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        try (Scanner scanner = ScannerHelper.createScanner(connector, this.checkModelTableName(modelTableName), cbAuths)) {
            IteratorSetting cfg = new IteratorSetting(21, "colfRegex", RegExFilter.class.getName());
            cfg.addOption(RegExFilter.COLF_REGEX, "^" + name + "(\\x00.*)?");
            scanner.addScanIterator(cfg);
            for (Entry<Key, Value> entry : scanner) {
                FieldMapping mapping = ModelKeyParser.parseKey(entry.getKey(), cbAuths);
                response.getFields().add(mapping);
            }
        }
    } catch (Exception e) {
        QueryException qe = new QueryException(DatawaveErrorCode.MODEL_FETCH_ERROR, e);
        log.error(qe);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    // return 404 if model not found
    if (response.getFields().isEmpty()) {
        throw new NotFoundException(null, response);
    }
    response.setName(name);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) Scanner(org.apache.accumulo.core.client.Scanner) FieldMapping(datawave.webservice.model.FieldMapping) NotFoundException(datawave.webservice.common.exception.NotFoundException) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) RegExFilter(org.apache.accumulo.core.iterators.user.RegExFilter) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) HashSet(java.util.HashSet) Authorizations(org.apache.accumulo.core.security.Authorizations) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) QueryException(datawave.webservice.query.exception.QueryException) IteratorSetting(org.apache.accumulo.core.client.IteratorSetting) DefaultValue(javax.ws.rs.DefaultValue) Value(org.apache.accumulo.core.data.Value) Principal(java.security.Principal) DatawavePrincipal(datawave.security.authorization.DatawavePrincipal) Key(org.apache.accumulo.core.data.Key) Path(javax.ws.rs.Path) Interceptors(javax.interceptor.Interceptors) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) GZIP(org.jboss.resteasy.annotations.GZIP)

Example 2 with FieldMapping

use of datawave.webservice.model.FieldMapping in project datawave by NationalSecurityAgency.

the class ModelBean method deleteMapping.

private VoidResponse deleteMapping(datawave.webservice.model.Model model, String modelTableName, boolean reloadCache) {
    VoidResponse response = new VoidResponse();
    Connector connector = null;
    BatchWriter writer = null;
    String tableName = this.checkModelTableName(modelTableName);
    try {
        Map<String, String> trackingMap = connectionFactory.getTrackingMap(Thread.currentThread().getStackTrace());
        connector = connectionFactory.getConnection(AccumuloConnectionFactory.Priority.LOW, trackingMap);
        writer = connector.createBatchWriter(tableName, new BatchWriterConfig().setMaxLatency(BATCH_WRITER_MAX_LATENCY, TimeUnit.MILLISECONDS).setMaxMemory(BATCH_WRITER_MAX_MEMORY).setMaxWriteThreads(BATCH_WRITER_MAX_THREADS));
        for (FieldMapping mapping : model.getFields()) {
            Mutation m = ModelKeyParser.createDeleteMutation(mapping, model.getName());
            writer.addMutation(m);
        }
    } catch (Exception e) {
        log.error("Could not delete mapping.", e);
        QueryException qe = new QueryException(DatawaveErrorCode.MAPPING_DELETION_ERROR, e);
        response.addException(qe.getBottomQueryException());
        throw new DatawaveWebApplicationException(qe, response);
    } finally {
        if (null != writer) {
            try {
                writer.close();
            } catch (MutationsRejectedException e1) {
                QueryException qe = new QueryException(DatawaveErrorCode.WRITER_CLOSE_ERROR, e1);
                log.error(qe);
                response.addException(qe);
                throw new DatawaveWebApplicationException(qe, response);
            }
        }
        if (null != connector) {
            try {
                connectionFactory.returnConnection(connector);
            } catch (Exception e) {
                log.error("Error returning connection to factory", e);
            }
        }
    }
    if (reloadCache)
        cache.reloadCache(tableName);
    return response;
}
Also used : Connector(org.apache.accumulo.core.client.Connector) QueryException(datawave.webservice.query.exception.QueryException) VoidResponse(datawave.webservice.result.VoidResponse) FieldMapping(datawave.webservice.model.FieldMapping) BatchWriterConfig(org.apache.accumulo.core.client.BatchWriterConfig) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) DatawaveWebApplicationException(datawave.webservice.common.exception.DatawaveWebApplicationException) PreConditionFailedException(datawave.webservice.common.exception.PreConditionFailedException) NotFoundException(datawave.webservice.common.exception.NotFoundException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) QueryException(datawave.webservice.query.exception.QueryException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException)

Example 3 with FieldMapping

use of datawave.webservice.model.FieldMapping in project datawave by NationalSecurityAgency.

the class ModelKeyParser method parseKey.

public static FieldMapping parseKey(Key key, Set<Authorizations> auths) {
    FieldMapping mapping = new FieldMapping();
    String row = key.getRow().toString();
    String[] colf = key.getColumnFamily().toString().split(NULL_BYTE);
    String[] colq = key.getColumnQualifier().toString().split(NULL_BYTE);
    String cv = key.getColumnVisibility().toString();
    String datatype = null;
    Direction direction;
    String dataField;
    String modelField;
    Boolean indexOnlyField = false;
    if (colf.length == 1) {
    // Older style, no datatype
    } else if (colf.length == 2) {
        datatype = colf[1];
    } else {
        throw new IllegalArgumentException("Key in unknown format, colf parts: " + colf.length);
    }
    if (2 == colq.length) {
        direction = Direction.getDirection(colq[1]);
        if (Direction.REVERSE.equals(direction)) {
            dataField = row;
            modelField = colq[0];
        } else {
            dataField = colq[0];
            modelField = row;
        }
    } else if (3 == colq.length && Direction.FORWARD == Direction.getDirection(colq[2])) {
        dataField = colq[0];
        modelField = row;
        // we already checked it in the if condition
        direction = Direction.FORWARD;
    } else {
        log.error("Error parsing key: " + key);
        throw new IllegalArgumentException("Key in unknown format, colq parts: " + colq.length);
    }
    mapping.setColumnVisibility(cv);
    mapping.setDatatype(datatype);
    mapping.setDirection(direction);
    mapping.setFieldName(dataField);
    mapping.setModelFieldName(modelField);
    return mapping;
}
Also used : FieldMapping(datawave.webservice.model.FieldMapping) Direction(datawave.webservice.model.Direction)

Example 4 with FieldMapping

use of datawave.webservice.model.FieldMapping in project datawave by NationalSecurityAgency.

the class ModelKeyParserTest method testForwardMappingIndexOnlyParse.

@Test
public void testForwardMappingIndexOnlyParse() throws Exception {
    // Test with datatype
    FieldMapping forwardMapping = new FieldMapping();
    forwardMapping.setColumnVisibility(COLVIZ);
    forwardMapping.setDatatype(DATATYPE);
    forwardMapping.setDirection(FORWARD);
    forwardMapping.setFieldName(FIELD_NAME);
    forwardMapping.setModelFieldName(MODEL_FIELD_NAME);
    Key expectedForwardKey = new Key(MODEL_FIELD_NAME, MODEL_NAME + ModelKeyParser.NULL_BYTE + DATATYPE, FIELD_NAME + ModelKeyParser.NULL_BYTE + FORWARD.getValue(), COLVIZ, TIMESTAMP);
    EasyMock.expect(System.currentTimeMillis()).andReturn(TIMESTAMP);
    PowerMock.replayAll();
    Key k = ModelKeyParser.createKey(forwardMapping, MODEL_NAME);
    Assert.assertEquals(expectedForwardKey, k);
    // Test without datatype
    PowerMock.resetAll();
    forwardMapping = new FieldMapping();
    forwardMapping.setColumnVisibility(COLVIZ);
    forwardMapping.setDirection(FORWARD);
    forwardMapping.setFieldName(FIELD_NAME);
    forwardMapping.setModelFieldName(MODEL_FIELD_NAME);
    expectedForwardKey = new Key(MODEL_FIELD_NAME, MODEL_NAME, FIELD_NAME + ModelKeyParser.NULL_BYTE + FORWARD.getValue(), COLVIZ, TIMESTAMP);
    EasyMock.expect(System.currentTimeMillis()).andReturn(TIMESTAMP);
    PowerMock.replayAll();
    k = ModelKeyParser.createKey(forwardMapping, MODEL_NAME);
    Assert.assertEquals(expectedForwardKey, k);
}
Also used : FieldMapping(datawave.webservice.model.FieldMapping) Key(org.apache.accumulo.core.data.Key) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with FieldMapping

use of datawave.webservice.model.FieldMapping in project datawave by NationalSecurityAgency.

the class ModelKeyParserTest method testParseKeyNullCV.

@Test
public void testParseKeyNullCV() throws Exception {
    FieldMapping mapping = ModelKeyParser.parseKey(NULL_CV_KEY, AUTHS);
    Assert.assertEquals(NULL_CV_MAPPING, mapping);
}
Also used : FieldMapping(datawave.webservice.model.FieldMapping) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

FieldMapping (datawave.webservice.model.FieldMapping)12 Key (org.apache.accumulo.core.data.Key)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Mutation (org.apache.accumulo.core.data.Mutation)4 DatawaveWebApplicationException (datawave.webservice.common.exception.DatawaveWebApplicationException)3 NotFoundException (datawave.webservice.common.exception.NotFoundException)3 PreConditionFailedException (datawave.webservice.common.exception.PreConditionFailedException)3 QueryException (datawave.webservice.query.exception.QueryException)3 Connector (org.apache.accumulo.core.client.Connector)3 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)3 QueryModel (datawave.query.model.QueryModel)2 Model (datawave.webservice.model.Model)2 VoidResponse (datawave.webservice.result.VoidResponse)2 Interceptors (javax.interceptor.Interceptors)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 JAXBContext (javax.xml.bind.JAXBContext)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 BatchWriter (org.apache.accumulo.core.client.BatchWriter)2