Search in sources :

Example 16 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationCache method add.

private void add(final NamedOperationDetail namedOperation, final boolean overwrite, final User user, final String adminAuth) throws CacheOperationFailedException {
    String name;
    try {
        name = namedOperation.getOperationName();
    } catch (final NullPointerException e) {
        throw new CacheOperationFailedException("NamedOperation cannot be null", e);
    }
    if (null == name) {
        throw new CacheOperationFailedException("NamedOperation name cannot be null");
    }
    if (!overwrite) {
        addToCache(name, namedOperation, false);
        return;
    }
    NamedOperationDetail existing;
    try {
        existing = getFromCache(name);
    } catch (final CacheOperationFailedException e) {
        // if there is no existing named Operation add one
        addToCache(name, namedOperation, false);
        return;
    }
    if (existing.hasWriteAccess(user, adminAuth)) {
        addToCache(name, namedOperation, true);
    } else {
        throw new CacheOperationFailedException("User " + user.getUserId() + " does not have permission to overwrite");
    }
}
Also used : NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)

Example 17 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldExecuteNamedOperationWithoutOverridingInput.

@Test
public void shouldExecuteNamedOperationWithoutOverridingInput() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    final NamedOperationDetail extendedNamedOperation = mock(NamedOperationDetail.class);
    final GetAdjacentIds op1 = mock(GetAdjacentIds.class);
    final GetElements op2 = mock(GetElements.class);
    final OperationChain namedOpChain = new OperationChain(Arrays.asList(op1, op2));
    final Iterable<?> input = mock(CloseableIterable.class);
    final Map<String, Object> params = null;
    given(op1.getInput()).willReturn(mock(CloseableIterable.class));
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    given(extendedNamedOperation.getOperationChain(params)).willReturn(namedOpChain);
    // When
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).input(input).build()).build();
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertSame(op1, opChain.getOperations().get(0));
    verify(op1, never()).setInput((Iterable) input);
    assertSame(op2, opChain.getOperations().get(1));
    verify(op2, never()).setInput((Iterable) input);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 18 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldResolveNamedOperationWithParameter.

@Test
public void shouldResolveNamedOperationWithParameter() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("param1", 1L);
    ParameterDetail param = new ParameterDetail.Builder().defaultValue(1L).description("Limit param").valueClass(Long.class).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("param1", param);
    // Make a real NamedOperationDetail with a parameter
    final NamedOperationDetail extendedNamedOperation = new NamedOperationDetail.Builder().operationName(opName).description("standard operation").operationChain("{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.Limit\", \"resultLimit\": \"${param1}\" } ] }").parameters(paramDetailMap).build();
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    final OperationChain<Object> opChain = new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).parameters(paramMap).build()).build();
    // When
    resolver.preExecute(opChain, new Context(user));
    // Then
    assertEquals(opChain.getOperations().get(0).getClass(), GetAllElements.class);
    assertEquals(opChain.getOperations().get(1).getClass(), Limit.class);
    // Check the parameter has been inserted
    assertEquals((long) ((Limit) opChain.getOperations().get(1)).getResultLimit(), 1L);
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 19 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project Gaffer by gchq.

the class NamedOperationResolverTest method shouldNotExecuteNamedOperationWithMissingRequiredArg.

@Test
public void shouldNotExecuteNamedOperationWithMissingRequiredArg() throws OperationException, CacheOperationFailedException {
    // Given
    final String opName = "opName";
    final NamedOperationCache cache = mock(NamedOperationCache.class);
    final NamedOperationResolver resolver = new NamedOperationResolver(cache);
    final User user = mock(User.class);
    // Don't set any parameters
    Map<String, Object> paramMap = Maps.newHashMap();
    ParameterDetail param = new ParameterDetail.Builder().description("Limit param").valueClass(Long.class).required(true).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("param1", param);
    // Make a real NamedOperationDetail with a parameter
    final NamedOperationDetail extendedNamedOperation = new NamedOperationDetail.Builder().operationName(opName).description("standard operation").operationChain("{ \"operations\": [ { \"class\":\"uk.gov.gchq.gaffer.operation.impl.get.GetAllElements\" }, { \"class\":\"uk.gov.gchq.gaffer.operation.impl.Limit\", \"resultLimit\": \"${param1}\" } ] }").parameters(paramDetailMap).build();
    given(cache.getNamedOperation(opName, user)).willReturn(extendedNamedOperation);
    // When
    assertThatIllegalArgumentException().isThrownBy(() -> resolver.preExecute(new OperationChain.Builder().first(new NamedOperation.Builder<>().name(opName).parameters(paramMap).build()).build(), new Context(user)));
}
Also used : Context(uk.gov.gchq.gaffer.store.Context) User(uk.gov.gchq.gaffer.user.User) NamedOperationCache(uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Test(org.junit.jupiter.api.Test)

Example 20 with NamedOperationDetail

use of uk.gov.gchq.gaffer.named.operation.NamedOperationDetail in project gaffer-doc by gchq.

the class NamedOperations method run.

public CloseableIterable<? extends Element> run() throws OperationException, IOException {
    // / [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
    // ---------------------------------------------------------
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
    // generateElements - generating edges from the data (note these are directed edges)
    // addElements - add the edges to the graph
    // ---------------------------------------------------------
    final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [add named operation] create an operation chain to be executed as a named operation
    // ---------------------------------------------------------
    final AddNamedOperation addOperation = new AddNamedOperation.Builder().operationChain(new OperationChain.Builder().first(new GetElements.Builder().view(new View.Builder().edge("RoadUse").build()).build()).then(new Limit.Builder<>().resultLimit(10).build()).build()).description("named operation limit query").name("2-limit").readAccessRoles("read-user").writeAccessRoles("write-user").score(2).overwrite().build();
    graph.execute(addOperation, user);
    // ---------------------------------------------------------
    // [get all named operations] Get all named operations
    // ---------------------------------------------------------
    final CloseableIterable<NamedOperationDetail> details = graph.execute(new GetAllNamedOperations(), user);
    // ---------------------------------------------------------
    for (final NamedOperationDetail detail : details) {
        print("ALL_NAMED_OPERATIONS", detail.toString());
    }
    // [create named operation] create the named operation
    // ---------------------------------------------------------
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operation = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("2-limit").input(new EntitySeed("10")).build();
    // ---------------------------------------------------------
    // [execute named operation] Get the results
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> results = graph.execute(operation, user);
    // ---------------------------------------------------------
    for (final Object result : results) {
        print("NAMED_OPERATION_RESULTS", result.toString());
    }
    // [add named operation with parameters] create an operation chain to be executed as a named operation
    // with parameters
    // ---------------------------------------------------------
    String opChainString = "{" + "  \"operations\" : [ {" + "    \"class\" : \"uk.gov.gchq.gaffer.operation.impl.get.GetElements\"," + "    \"view\" : {" + "      \"edges\" : {" + "        \"RoadUse\" : { }" + "      }," + "      \"entities\" : { }" + "    }" + "  }, {" + "    \"class\" : \"uk.gov.gchq.gaffer.operation.impl.Limit\"," + "    \"resultLimit\" : \"${limitParam}\"" + "  } ]" + "}";
    ParameterDetail param = new ParameterDetail.Builder().defaultValue(1L).description("Limit param").valueClass(Long.class).build();
    Map<String, ParameterDetail> paramDetailMap = Maps.newHashMap();
    paramDetailMap.put("limitParam", param);
    final AddNamedOperation addOperationWithParams = new AddNamedOperation.Builder().operationChain(opChainString).description("named operation limit query").name("custom-limit").readAccessRoles("read-user").writeAccessRoles("write-user").parameters(paramDetailMap).overwrite().build();
    graph.execute(addOperationWithParams, user);
    // ---------------------------------------------------------
    // [create named operation with parameters] create the named operation with a parameter
    // ---------------------------------------------------------
    Map<String, Object> paramMap = Maps.newHashMap();
    paramMap.put("limitParam", 3L);
    final NamedOperation<EntityId, CloseableIterable<? extends Element>> operationWithParams = new NamedOperation.Builder<EntityId, CloseableIterable<? extends Element>>().name("custom-limit").input(new EntitySeed("10")).parameters(paramMap).build();
    // ---------------------------------------------------------
    // [execute named operation with parameters] Get the results
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> namedOperationResults = graph.execute(operationWithParams, user);
    for (final Object result : namedOperationResults) {
        print("NAMED_OPERATION_WITH_PARAMETER_RESULTS", result.toString());
    }
    return namedOperationResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetAllNamedOperations(uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations) NamedOperationDetail(uk.gov.gchq.gaffer.named.operation.NamedOperationDetail) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) AddNamedOperation(uk.gov.gchq.gaffer.named.operation.AddNamedOperation) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) ParameterDetail(uk.gov.gchq.gaffer.named.operation.ParameterDetail) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Aggregations

NamedOperationDetail (uk.gov.gchq.gaffer.named.operation.NamedOperationDetail)40 Test (org.junit.jupiter.api.Test)27 User (uk.gov.gchq.gaffer.user.User)11 GetAllNamedOperations (uk.gov.gchq.gaffer.named.operation.GetAllNamedOperations)10 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)10 NamedOperationCache (uk.gov.gchq.gaffer.store.operation.handler.named.cache.NamedOperationCache)10 AddNamedOperation (uk.gov.gchq.gaffer.named.operation.AddNamedOperation)9 Context (uk.gov.gchq.gaffer.store.Context)9 CacheOperationFailedException (uk.gov.gchq.gaffer.named.operation.cache.exception.CacheOperationFailedException)7 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)7 ParameterDetail (uk.gov.gchq.gaffer.named.operation.ParameterDetail)5 ArrayList (java.util.ArrayList)4 Element (uk.gov.gchq.gaffer.data.element.Element)4 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 Store (uk.gov.gchq.gaffer.store.Store)4 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)3 AddNamedOperationHandler (uk.gov.gchq.gaffer.store.operation.handler.named.AddNamedOperationHandler)3 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2