Search in sources :

Example 11 with GenericException

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");
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) GenericException(io.hops.hopsworks.exceptions.GenericException)

Example 12 with GenericException

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);
}
Also used : HashMap(java.util.HashMap) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) Users(io.hops.hopsworks.persistence.entity.user.Users) GenericException(io.hops.hopsworks.exceptions.GenericException) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) ProvStateParamBuilder(io.hops.hopsworks.common.provenance.state.ProvStateParamBuilder) Project(io.hops.hopsworks.persistence.entity.project.Project) ProjectTeam(io.hops.hopsworks.persistence.entity.project.team.ProjectTeam) AbstractFacade(io.hops.hopsworks.common.dao.AbstractFacade) ExperimentsEndpointDTO(io.hops.hopsworks.api.experiments.dto.ExperimentsEndpointDTO)

Example 13 with GenericException

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;
}
Also used : ProjectException(io.hops.hopsworks.exceptions.ProjectException) Project(io.hops.hopsworks.persistence.entity.project.Project) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) GenericException(io.hops.hopsworks.exceptions.GenericException)

Example 14 with GenericException

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);
            }
    }
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) Users(io.hops.hopsworks.persistence.entity.user.Users) UriBuilder(javax.ws.rs.core.UriBuilder) GenericException(io.hops.hopsworks.exceptions.GenericException) ProjectException(io.hops.hopsworks.exceptions.ProjectException) ExperimentsException(io.hops.hopsworks.exceptions.ExperimentsException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) GenericException(io.hops.hopsworks.exceptions.GenericException) PythonException(io.hops.hopsworks.exceptions.PythonException) MetadataException(io.hops.hopsworks.exceptions.MetadataException) ProvenanceException(io.hops.hopsworks.exceptions.ProvenanceException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles) PUT(javax.ws.rs.PUT)

Example 15 with GenericException

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();
}
Also used : Project(io.hops.hopsworks.persistence.entity.project.Project) Dataset(io.hops.hopsworks.persistence.entity.dataset.Dataset) ProvArtifactUsageParentDTO(io.hops.hopsworks.api.provenance.ops.dto.ProvArtifactUsageParentDTO) Users(io.hops.hopsworks.persistence.entity.user.Users) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) FeaturestoreException(io.hops.hopsworks.exceptions.FeaturestoreException) GenericException(io.hops.hopsworks.exceptions.GenericException) DatasetException(io.hops.hopsworks.exceptions.DatasetException) Path(javax.ws.rs.Path) DatasetPath(io.hops.hopsworks.common.dataset.util.DatasetPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) JWTRequired(io.hops.hopsworks.jwt.annotation.JWTRequired) ApiOperation(io.swagger.annotations.ApiOperation) ApiKeyRequired(io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired) AllowedProjectRoles(io.hops.hopsworks.api.filter.AllowedProjectRoles)

Aggregations

GenericException (io.hops.hopsworks.exceptions.GenericException)43 Users (io.hops.hopsworks.persistence.entity.user.Users)17 Project (io.hops.hopsworks.persistence.entity.project.Project)16 ProjectException (io.hops.hopsworks.exceptions.ProjectException)13 DatasetException (io.hops.hopsworks.exceptions.DatasetException)12 ServiceException (io.hops.hopsworks.exceptions.ServiceException)12 IOException (java.io.IOException)12 Path (javax.ws.rs.Path)11 ElasticException (io.hops.hopsworks.exceptions.ElasticException)10 HopsSecurityException (io.hops.hopsworks.exceptions.HopsSecurityException)10 JobException (io.hops.hopsworks.exceptions.JobException)9 ProvenanceException (io.hops.hopsworks.exceptions.ProvenanceException)9 Produces (javax.ws.rs.Produces)9 Dataset (io.hops.hopsworks.persistence.entity.dataset.Dataset)8 ArrayList (java.util.ArrayList)8 TransactionAttribute (javax.ejb.TransactionAttribute)8 ServiceDiscoveryException (com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException)6 UserException (io.hops.hopsworks.exceptions.UserException)6 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)6 HdfsUsers (io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)6