Search in sources :

Example 21 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedOperationChainHandler method doOperation.

@Override
public CloseableIterable<O_ITEM> doOperation(final FederatedOperationChain<I, O_ITEM> operation, final Context context, final Store store) throws OperationException {
    final Collection<Graph> graphs = ((FederatedStore) store).getGraphs(context.getUser(), operation.getOption(KEY_OPERATION_OPTIONS_GRAPH_IDS), operation);
    final List<Object> results = new ArrayList<>(graphs.size());
    for (final Graph graph : graphs) {
        final OperationChain opChain = operation.getOperationChain();
        OperationHandlerUtil.updateOperationInput(opChain, operation.getInput());
        final OperationChain updatedOp = FederatedStoreUtil.updateOperationForGraph(opChain, graph);
        if (null != updatedOp) {
            Object result = null;
            try {
                result = graph.execute(updatedOp, context);
            } catch (final Exception e) {
                if (!Boolean.valueOf(updatedOp.getOption(KEY_SKIP_FAILED_FEDERATED_STORE_EXECUTE))) {
                    throw new OperationException(FederatedStoreUtil.createOperationErrorMsg(operation, graph.getGraphId(), e), e);
                }
            }
            if (null != result) {
                results.add(result);
            }
        }
    }
    return mergeResults(results, operation, context, store);
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) FederatedOperationChain(uk.gov.gchq.gaffer.federatedstore.operation.FederatedOperationChain) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ArrayList(java.util.ArrayList) OperationException(uk.gov.gchq.gaffer.operation.OperationException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore)

Example 22 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedOperationChainValidator method validateAllGraphsIdViews.

/**
 * If the given view is valid for at least 1 of the graphIds, then view is valid and exit.
 * Else none are valid, return all the errors within the validation result.
 *
 * @param op               The operation with view to test
 * @param user             The requesting user
 * @param store            The current store
 * @param validationResult The result of validation
 * @param graphIdsCSV      The graphs to test the view against
 */
private void validateAllGraphsIdViews(final Operation op, final User user, final Store store, final ValidationResult validationResult, final String graphIdsCSV) {
    ValidationResult savedResult = new ValidationResult();
    ValidationResult currentResult = null;
    final Operation clonedOp = shallowCloneWithDeepOptions(op);
    Collection<Graph> graphs = ((FederatedStore) store).getGraphs(user, graphIdsCSV, clonedOp);
    for (final Graph graph : graphs) {
        String graphId = graph.getGraphId();
        final boolean graphIdValid = ((FederatedStore) store).getAllGraphIds(user).contains(graphId);
        // If graphId is not valid, then there is no schema to validate a view against.
        if (graphIdValid) {
            currentResult = new ValidationResult();
            clonedOp.addOption(FederatedStoreConstants.KEY_OPERATION_OPTIONS_GRAPH_IDS, graphId);
            if (!graph.hasTrait(StoreTrait.DYNAMIC_SCHEMA)) {
                super.validateViews(clonedOp, user, store, currentResult);
            }
            if (currentResult.isValid()) {
                // If any graph has a valid View, break with valid current result
                break;
            } else {
                ValidationResult prependGraphId = new ValidationResult();
                currentResult.getErrors().forEach(s -> prependGraphId.addError(String.format("(graphId: %s) %s", graphId, s)));
                savedResult.add(prependGraphId);
            }
        }
    }
    // What state did the for loop exit with?
    if (currentResult != null && !currentResult.isValid()) {
        validationResult.addError("View is not valid for graphIds:" + getGraphIds(op, user, (FederatedStore) store).stream().collect(Collectors.joining(",", "[", "]")));
        // If invalid, no graphs views where valid, so add all saved errors.
        validationResult.add(savedResult);
    }
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) Operation(uk.gov.gchq.gaffer.operation.Operation) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore)

Example 23 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedAddGraphHandlerParent method doOperation.

@Override
public Void doOperation(final OP operation, final Context context, final Store store) throws OperationException {
    final User user = context.getUser();
    boolean isLimitedToLibraryProperties = ((FederatedStore) store).isLimitedToLibraryProperties(user);
    if (isLimitedToLibraryProperties && null != operation.getStoreProperties()) {
        throw new OperationException(String.format(USER_IS_LIMITED_TO_ONLY_USING_PARENT_PROPERTIES_ID_FROM_GRAPHLIBRARY_BUT_FOUND_STORE_PROPERTIES_S, operation.getProperties().toString()));
    }
    final GraphSerialisable graphSerialisable;
    try {
        graphSerialisable = _makeGraph(operation, store);
    } catch (final Exception e) {
        throw new OperationException(String.format(ERROR_BUILDING_GRAPH_GRAPH_ID_S, operation.getGraphId()), e);
    }
    try {
        ((FederatedStore) store).addGraphs(operation.getGraphAuths(), context.getUser().getUserId(), operation.getIsPublic(), operation.isDisabledByDefault(), operation.getReadAccessPredicate(), operation.getWriteAccessPredicate(), graphSerialisable);
    } catch (final StorageException e) {
        throw new OperationException(e.getMessage(), e);
    } catch (final Exception e) {
        throw new OperationException(String.format(ERROR_ADDING_GRAPH_GRAPH_ID_S, operation.getGraphId()), e);
    }
    addGenericHandler((FederatedStore) store, graphSerialisable.getGraph());
    return null;
}
Also used : User(uk.gov.gchq.gaffer.user.User) GraphSerialisable(uk.gov.gchq.gaffer.graph.GraphSerialisable) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StorageException(uk.gov.gchq.gaffer.federatedstore.exception.StorageException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore)

Example 24 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedOperationHandlerTest method shouldMergeResultsFromFieldObjects.

@Test
public final void shouldMergeResultsFromFieldObjects() throws Exception {
    // Given
    final Operation op = mock(Operation.class);
    final Operation opClone = mock(Operation.class);
    given(op.shallowClone()).willReturn(opClone);
    given(opClone.shallowClone()).willReturn(opClone);
    final OperationChain<?> opChainClone = OperationChain.wrap(opClone);
    Schema unusedSchema = new Schema.Builder().build();
    StoreProperties storeProperties = new StoreProperties();
    Store mockStore1 = getMockStore(unusedSchema, storeProperties);
    Store mockStore2 = getMockStore(unusedSchema, storeProperties);
    Store mockStore3 = getMockStore(unusedSchema, storeProperties);
    Store mockStore4 = getMockStore(unusedSchema, storeProperties);
    Graph graph1 = getGraphWithMockStore(mockStore1);
    Graph graph2 = getGraphWithMockStore(mockStore2);
    Graph graph3 = getGraphWithMockStore(mockStore3);
    Graph graph4 = getGraphWithMockStore(mockStore4);
    FederatedStore mockStore = mock(FederatedStore.class);
    LinkedHashSet<Graph> linkedGraphs = Sets.newLinkedHashSet();
    linkedGraphs.add(graph1);
    linkedGraphs.add(graph2);
    linkedGraphs.add(graph3);
    linkedGraphs.add(graph4);
    when(mockStore.getGraphs(user, null, op)).thenReturn(linkedGraphs);
    // When
    new FederatedOperationHandler().doOperation(op, context, mockStore);
    verify(mockStore1).execute(eq(opChainClone), any(Context.class));
    verify(mockStore2).execute(eq(opChainClone), any(Context.class));
    verify(mockStore3).execute(eq(opChainClone), any(Context.class));
    verify(mockStore4).execute(eq(opChainClone), any(Context.class));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) Graph(uk.gov.gchq.gaffer.graph.Graph) Schema(uk.gov.gchq.gaffer.store.schema.Schema) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Store(uk.gov.gchq.gaffer.store.Store) Operation(uk.gov.gchq.gaffer.operation.Operation) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) Test(org.junit.jupiter.api.Test)

Example 25 with FederatedStore

use of uk.gov.gchq.gaffer.federatedstore.FederatedStore in project Gaffer by gchq.

the class FederatedAddGraphHandlerTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    CacheServiceLoader.shutdown();
    this.store = new FederatedStore();
    federatedStoreProperties = new FederatedStoreProperties();
    federatedStoreProperties.setCacheProperties(CACHE_SERVICE_CLASS_STRING);
    testUser = testUser();
    authUser = authUser();
    blankUser = blankUser();
    ignore = new IgnoreOptions();
}
Also used : FederatedStoreProperties(uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties) FederatedStore(uk.gov.gchq.gaffer.federatedstore.FederatedStore) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

FederatedStore (uk.gov.gchq.gaffer.federatedstore.FederatedStore)38 Test (org.junit.jupiter.api.Test)25 Context (uk.gov.gchq.gaffer.store.Context)25 Schema (uk.gov.gchq.gaffer.store.schema.Schema)19 Graph (uk.gov.gchq.gaffer.graph.Graph)16 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)12 OperationException (uk.gov.gchq.gaffer.operation.OperationException)12 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)11 Store (uk.gov.gchq.gaffer.store.Store)10 FederatedOperationChain (uk.gov.gchq.gaffer.federatedstore.operation.FederatedOperationChain)8 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)7 FederatedStoreProperties (uk.gov.gchq.gaffer.federatedstore.FederatedStoreProperties)7 PredefinedFederatedStore (uk.gov.gchq.gaffer.federatedstore.PredefinedFederatedStore)7 Operation (uk.gov.gchq.gaffer.operation.Operation)7 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)6 User (uk.gov.gchq.gaffer.user.User)6 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Element (uk.gov.gchq.gaffer.data.element.Element)4 GraphConfig (uk.gov.gchq.gaffer.graph.GraphConfig)4 GraphSerialisable (uk.gov.gchq.gaffer.graph.GraphSerialisable)4