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;
}
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;
}
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;
}
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);
}
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);
}
Aggregations