use of io.hops.hopsworks.exceptions.GenericException in project hopsworks by logicalclocks.
the class ExperimentsBuilder method verifyExperimentsEndpoint.
private ExperimentsEndpointDTO verifyExperimentsEndpoint(Project userProject, String sEndpointId) throws GenericException, DatasetException {
Integer endpointId;
try {
endpointId = Integer.parseInt(sEndpointId);
} catch (NumberFormatException e) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "Provided Endpoint Id was malformed - expected a Integer ", e.getMessage(), e);
}
Project sharingProject = projectFacade.findById(endpointId).orElseThrow(() -> new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "Provided project cannot be accessed"));
Dataset dataset = datasetCtrl.getByName(sharingProject, Settings.HOPS_EXPERIMENTS_DATASET);
if (dataset != null && accessCtrl.hasAccess(userProject, dataset)) {
return ExperimentsEndpointDTO.fromDataset(dataset);
}
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "Provided Endpoint cannot be accessed");
}
use of io.hops.hopsworks.exceptions.GenericException in project hopsworks by logicalclocks.
the class ExperimentsBuilder method buildFilter.
private Pair<ProvStateParamBuilder, Map<Long, ExperimentsEndpointDTO>> buildFilter(Project project, Set<? extends AbstractFacade.FilterBy> filters) throws ProvenanceException, GenericException, DatasetException {
ProvStateParamBuilder provFilesParamBuilder = new ProvStateParamBuilder();
Map<Long, ExperimentsEndpointDTO> selectedEndpoints = new HashMap<>();
if (filters != null) {
Users filterUser = null;
Project filterUserProject = project;
for (AbstractFacade.FilterBy filterBy : filters) {
if (filterBy.getParam().compareToIgnoreCase(Filters.ENDPOINT_ID.name()) == 0) {
ExperimentsEndpointDTO endpoint = verifyExperimentsEndpoint(project, filterBy.getValue());
selectedEndpoints.put(endpoint.getParentProject().getInode().getId(), endpoint);
} else if (filterBy.getParam().compareToIgnoreCase(Filters.NAME_LIKE.name()) == 0) {
provFilesParamBuilder.filterLikeXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".name", filterBy.getValue());
} else if (filterBy.getParam().compareToIgnoreCase(Filters.NAME_EQ.name()) == 0) {
provFilesParamBuilder.filterByXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".name", filterBy.getValue());
} else if (filterBy.getParam().compareToIgnoreCase(Filters.DATE_START_LT.name()) == 0) {
Long timestamp = getDate(filterBy.getField(), filterBy.getValue()).getTime();
provFilesParamBuilder.filterByField(ProvStateParser.FieldsPF.CREATE_TIMESTAMP_LT, timestamp);
} else if (filterBy.getParam().compareToIgnoreCase(Filters.DATE_START_GT.name()) == 0) {
Long timestamp = getDate(filterBy.getField(), filterBy.getValue()).getTime();
provFilesParamBuilder.filterByField(ProvStateParser.FieldsPF.CREATE_TIMESTAMP_GT, timestamp);
} else if (filterBy.getParam().compareToIgnoreCase(Filters.USER.name()) == 0) {
try {
filterUser = userFacade.find(Integer.parseInt(filterBy.getValue()));
} catch (NumberFormatException e) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "expected int user id, found: " + filterBy.getValue());
}
} else if (filterBy.getParam().compareToIgnoreCase(Filters.USER_PROJECT.name()) == 0) {
try {
filterUserProject = projectFacade.find(Integer.parseInt(filterBy.getValue()));
} catch (NumberFormatException e) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "expected int user project id, found: " + filterBy.getValue());
}
} else if (filterBy.getParam().compareToIgnoreCase(Filters.STATE.name()) == 0) {
provFilesParamBuilder.filterLikeXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".state", filterBy.getValue());
} else if (filterBy.getParam().compareToIgnoreCase(Filters.ID_EQ.name()) == 0) {
provFilesParamBuilder.filterByXAttr(EXPERIMENT_SUMMARY_XATTR_NAME + ".id", filterBy.getValue());
} else {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "Filter by - found: " + filterBy.getParam() + " expected:" + EnumSet.allOf(Filters.class));
}
}
if (filterUser != null) {
ProjectTeam member = projectTeamFacade.findByPrimaryKey(filterUserProject, filterUser);
if (member == null) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "Selected user: " + filterUser.getUid() + " is not part of project:" + filterUserProject.getId());
}
String hdfsUserStr = hdfsUsersController.getHdfsUserName(filterUserProject, filterUser);
HdfsUsers hdfsUsers = hdfsUsersFacade.findByName(hdfsUserStr);
provFilesParamBuilder.filterByField(ProvStateParser.FieldsP.USER_ID, hdfsUsers.getId().toString());
}
}
// an endpoint always has to be selected, if none provided, then all accessible endpoints are used
if (selectedEndpoints.isEmpty()) {
for (ExperimentsEndpointDTO endpoint : experimentsController.getExperimentsEndpoints(project)) {
selectedEndpoints.put(endpoint.getParentProject().getInode().getId(), endpoint);
}
}
for (ExperimentsEndpointDTO endpoint : selectedEndpoints.values()) {
provFilesParamBuilder.filterByField(ProvStateParser.FieldsP.PROJECT_I_ID, endpoint.getParentProject().getInode().getId()).filterByField(ProvStateParser.FieldsP.DATASET_I_ID, endpoint.getDatasetInodeId());
}
return Pair.with(provFilesParamBuilder, selectedEndpoints);
}
use of io.hops.hopsworks.exceptions.GenericException in project hopsworks by logicalclocks.
the class ExperimentsResource method getModelsProjectAndCheckAccess.
private Project getModelsProjectAndCheckAccess(ExperimentDTO experimentDTO) throws ProjectException, DatasetException, GenericException {
Project modelProject;
if (experimentDTO.getModelProjectName() == null) {
modelProject = project;
} else {
modelProject = projectFacade.findByName(experimentDTO.getModelProjectName());
if (modelProject == null) {
throw new ProjectException(RESTCodes.ProjectErrorCode.PROJECT_NOT_FOUND, Level.INFO, "model project not found for experiment");
}
}
Dataset modelDataset = datasetCtrl.getByName(modelProject, Settings.HOPS_MODELS_DATASET);
if (!accessCtrl.hasAccess(project, modelDataset)) {
throw new GenericException(RESTCodes.GenericErrorCode.NOT_AUTHORIZED_TO_ACCESS, Level.INFO);
}
return modelProject;
}
use of io.hops.hopsworks.exceptions.GenericException in project hopsworks by logicalclocks.
the class ExperimentsResource method post.
@ApiOperation(value = "Create or update an experiment", response = ExperimentDTO.class)
@PUT
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
@JWTRequired(acceptedTokens = { Audience.API, Audience.JOB }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
public Response post(@PathParam("id") String id, ExperimentDTO experimentDTO, @QueryParam("type") ExperimentOperationType type, @Context HttpServletRequest req, @Context UriInfo uriInfo, @Context SecurityContext sc) throws DatasetException, ProvenanceException, PythonException, MetadataException, ProjectException, GenericException, ExperimentsException {
if (experimentDTO == null) {
throw new IllegalArgumentException("No Experiment configuration was provided");
}
Users user = jwtHelper.getUserPrincipal(sc);
Project experimentProject = project;
switch(type) {
case INIT:
{
String experimentPath = Utils.getProjectPath(project.getName()) + Settings.HOPS_EXPERIMENTS_DATASET + "/" + id + "/" + Settings.ENVIRONMENT_FILE;
experimentDTO.setEnvironment(environmentController.exportEnv(experimentProject, user, experimentPath));
try {
String program = experimentsController.versionProgram(experimentProject, user, experimentDTO.getJobName(), experimentDTO.getKernelId(), id);
experimentDTO.setProgram(program);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Could not version notebook " + e.getMessage());
}
}
break;
case MODEL_UPDATE:
{
Project modelProject = getModelsProjectAndCheckAccess(experimentDTO);
experimentsController.attachModel(user, experimentProject, id, modelProject, experimentDTO.getModel());
}
break;
case FULL_UPDATE:
{
// no need to update the summary in any way
}
break;
default:
{
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "unhandled experiment summary operation type:" + type);
}
}
experimentsController.attachExperiment(user, experimentProject, id, experimentDTO);
UriBuilder builder = uriInfo.getAbsolutePathBuilder().path(id);
switch(type) {
case INIT:
return Response.created(builder.build()).entity(experimentDTO).build();
case MODEL_UPDATE:
case FULL_UPDATE:
return Response.ok(builder.build()).entity(experimentDTO).build();
default:
{
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.INFO, "unhandled experiment summary operation type:" + type);
}
}
}
use of io.hops.hopsworks.exceptions.GenericException in project hopsworks by logicalclocks.
the class ProjectProvenanceResource method usage.
@GET
@Path("usage")
@Produces(MediaType.APPLICATION_JSON)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_SCIENTIST, AllowedProjectRoles.DATA_OWNER })
@JWTRequired(acceptedTokens = { Audience.API }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiKeyRequired(acceptedScopes = { ApiScope.PROJECT }, allowedUserRoles = { "HOPS_ADMIN", "HOPS_USER" })
@ApiOperation(value = "Artifact usage", response = ProvArtifactUsageParentDTO.class)
public Response usage(@QueryParam("artifact_id") String artifactId, @QueryParam("endpoint_id") Integer endpointId, @QueryParam("artifact_type") DatasetAccessType accessType, @BeanParam ProvUsageBeanParams params, @Context UriInfo uriInfo, @Context SecurityContext sc) throws ProvenanceException, GenericException, DatasetException, MetadataException, SchematizedTagException {
Users user = jWTHelper.getUserPrincipal(sc);
if (artifactId == null) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "artifactId id cannot be null");
}
Project targetProject = project;
if (endpointId != null) {
targetProject = projectFacade.findById(endpointId).orElseThrow(() -> new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "target project not found"));
}
Dataset targetEndpoint;
if (accessType != null) {
try {
switch(accessType) {
case FEATUREGROUPS:
targetEndpoint = fsCtrl.getProjectFeaturestoreDataset(targetProject);
break;
case TRAININGDATASETS:
String tdName = project.getName() + "_" + Settings.ServiceDataset.TRAININGDATASETS.getName();
targetEndpoint = datasetCtrl.getByName(targetProject, tdName);
break;
case MODELS:
targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_MODELS_DATASET);
break;
case EXPERIMENTS:
targetEndpoint = datasetCtrl.getByName(targetProject, Settings.HOPS_EXPERIMENTS_DATASET);
break;
default:
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_ARGUMENT, Level.FINE, "access type not supports:" + accessType);
}
} catch (FeaturestoreException | DatasetException e) {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "cannot access the dataset of the artifact");
}
} else {
throw new GenericException(RESTCodes.GenericErrorCode.ILLEGAL_STATE, Level.FINE, "access type not defined");
}
DatasetPath targetEndpointPath = datasetHelper.getTopLevelDatasetPath(project, targetEndpoint);
ProvArtifactUsageParentDTO status = usageBuilder.buildAccessible(uriInfo, user, targetEndpointPath, artifactId, params.getUsageType());
return Response.ok().entity(status).build();
}
Aggregations