Search in sources :

Example 16 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class TestStandardProcessScheduler method validateNeverEnablingServiceCanStillBeDisabled.

/**
 * Validates that service that is infinitely blocking in @OnEnabled can
 * still have DISABLE operation initiated. The service itself will be set to
 * DISABLING state at which point UI and all will know that such service can
 * not be transitioned any more into any other state until it finishes
 * enabling (which will never happen in our case thus should be addressed by
 * user). However, regardless of user's mistake NiFi will remain
 * functioning.
 */
@Test
public void validateNeverEnablingServiceCanStillBeDisabled() throws Exception {
    final StandardProcessScheduler scheduler = createScheduler();
    final StandardControllerServiceProvider provider = new StandardControllerServiceProvider(controller, scheduler, null, stateMgrProvider, variableRegistry, nifiProperties);
    final ControllerServiceNode serviceNode = provider.createControllerService(LongEnablingService.class.getName(), "1", systemBundle.getBundleDetails().getCoordinate(), null, false);
    final LongEnablingService ts = (LongEnablingService) serviceNode.getControllerServiceImplementation();
    ts.setLimit(Long.MAX_VALUE);
    scheduler.enableControllerService(serviceNode);
    Thread.sleep(100);
    assertTrue(serviceNode.isActive());
    assertEquals(1, ts.enableInvocationCount());
    Thread.sleep(1000);
    scheduler.disableControllerService(serviceNode);
    assertFalse(serviceNode.isActive());
    assertEquals(ControllerServiceState.DISABLING, serviceNode.getState());
    assertEquals(0, ts.disableInvocationCount());
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) StandardControllerServiceNode(org.apache.nifi.controller.service.StandardControllerServiceNode) StandardControllerServiceProvider(org.apache.nifi.controller.service.StandardControllerServiceProvider) Test(org.junit.Test)

Example 17 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class ControllerServiceAuditor method updateControllerServiceAdvice.

/**
 * Audits the configuration of a single controller service.
 *
 * @param proceedingJoinPoint join point
 * @param controllerServiceDTO dto
 * @param controllerServiceDAO dao
 * @return object
 * @throws Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ControllerServiceDAO+) && " + "execution(org.apache.nifi.controller.service.ControllerServiceNode updateControllerService(org.apache.nifi.web.api.dto.ControllerServiceDTO)) && " + "args(controllerServiceDTO) && " + "target(controllerServiceDAO)")
public Object updateControllerServiceAdvice(ProceedingJoinPoint proceedingJoinPoint, ControllerServiceDTO controllerServiceDTO, ControllerServiceDAO controllerServiceDAO) throws Throwable {
    // determine the initial values for each property/setting that's changing
    ControllerServiceNode controllerService = controllerServiceDAO.getControllerService(controllerServiceDTO.getId());
    final Map<String, String> values = extractConfiguredPropertyValues(controllerService, controllerServiceDTO);
    final boolean isDisabled = isDisabled(controllerService);
    // update the controller service state
    final ControllerServiceNode updatedControllerService = (ControllerServiceNode) proceedingJoinPoint.proceed();
    // if no exceptions were thrown, add the controller service action...
    controllerService = controllerServiceDAO.getControllerService(updatedControllerService.getIdentifier());
    // get the current user
    NiFiUser user = NiFiUserUtils.getNiFiUser();
    // ensure the user was found
    if (user != null) {
        // determine the updated values
        Map<String, String> updatedValues = extractConfiguredPropertyValues(controllerService, controllerServiceDTO);
        // create the controller service details
        FlowChangeExtensionDetails serviceDetails = new FlowChangeExtensionDetails();
        serviceDetails.setType(controllerService.getComponentType());
        // create a controller service action
        Date actionTimestamp = new Date();
        Collection<Action> actions = new ArrayList<>();
        // go through each updated value
        for (String property : updatedValues.keySet()) {
            String newValue = updatedValues.get(property);
            String oldValue = values.get(property);
            Operation operation = null;
            // determine the type of operation
            if (oldValue == null || newValue == null || !newValue.equals(oldValue)) {
                operation = Operation.Configure;
            }
            // create a configuration action accordingly
            if (operation != null) {
                // clear the value if this property is sensitive
                final PropertyDescriptor propertyDescriptor = controllerService.getControllerServiceImplementation().getPropertyDescriptor(property);
                if (propertyDescriptor != null && propertyDescriptor.isSensitive()) {
                    if (newValue != null) {
                        newValue = "********";
                    }
                    if (oldValue != null) {
                        oldValue = "********";
                    }
                } else if (ANNOTATION_DATA.equals(property)) {
                    if (newValue != null) {
                        newValue = "<annotation data not shown>";
                    }
                    if (oldValue != null) {
                        oldValue = "<annotation data not shown>";
                    }
                }
                final FlowChangeConfigureDetails actionDetails = new FlowChangeConfigureDetails();
                actionDetails.setName(property);
                actionDetails.setValue(newValue);
                actionDetails.setPreviousValue(oldValue);
                // create a configuration action
                FlowChangeAction configurationAction = new FlowChangeAction();
                configurationAction.setUserIdentity(user.getIdentity());
                configurationAction.setOperation(operation);
                configurationAction.setTimestamp(actionTimestamp);
                configurationAction.setSourceId(controllerService.getIdentifier());
                configurationAction.setSourceName(controllerService.getName());
                configurationAction.setSourceType(Component.ControllerService);
                configurationAction.setComponentDetails(serviceDetails);
                configurationAction.setActionDetails(actionDetails);
                actions.add(configurationAction);
            }
        }
        // determine the new executing state
        final boolean updateIsDisabled = isDisabled(updatedControllerService);
        // determine if the running state has changed and its not disabled
        if (isDisabled != updateIsDisabled) {
            // create a controller service action
            FlowChangeAction serviceAction = new FlowChangeAction();
            serviceAction.setUserIdentity(user.getIdentity());
            serviceAction.setTimestamp(new Date());
            serviceAction.setSourceId(controllerService.getIdentifier());
            serviceAction.setSourceName(controllerService.getName());
            serviceAction.setSourceType(Component.ControllerService);
            serviceAction.setComponentDetails(serviceDetails);
            // set the operation accordingly
            if (updateIsDisabled) {
                serviceAction.setOperation(Operation.Disable);
            } else {
                serviceAction.setOperation(Operation.Enable);
            }
            actions.add(serviceAction);
        }
        // ensure there are actions to record
        if (!actions.isEmpty()) {
            // save the actions
            saveActions(actions, logger);
        }
    }
    return updatedControllerService;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) NiFiUser(org.apache.nifi.authorization.user.NiFiUser) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) Operation(org.apache.nifi.action.Operation) Date(java.util.Date) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) FlowChangeConfigureDetails(org.apache.nifi.action.details.FlowChangeConfigureDetails) FlowChangeExtensionDetails(org.apache.nifi.action.component.details.FlowChangeExtensionDetails) FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Around(org.aspectj.lang.annotation.Around)

Example 18 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class ControllerServiceAuditor method createControllerServiceAdvice.

/**
 * Audits the creation of controller service via createControllerService().
 *
 * This method only needs to be run 'after returning'. However, in Java 7 the order in which these methods are returned from Class.getDeclaredMethods (even though there is no order guaranteed)
 * seems to differ from Java 6. SpringAOP depends on this ordering to determine advice precedence. By normalizing all advice into Around advice we can alleviate this issue.
 *
 * @param proceedingJoinPoint join point
 * @return node
 * @throws java.lang.Throwable ex
 */
@Around("within(org.apache.nifi.web.dao.ControllerServiceDAO+) && " + "execution(org.apache.nifi.controller.service.ControllerServiceNode createControllerService(org.apache.nifi.web.api.dto.ControllerServiceDTO))")
public ControllerServiceNode createControllerServiceAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    // update the controller service state
    ControllerServiceNode controllerService = (ControllerServiceNode) proceedingJoinPoint.proceed();
    // if no exceptions were thrown, add the controller service action...
    final Action action = generateAuditRecord(controllerService, Operation.Add);
    // save the actions
    if (action != null) {
        saveAction(action, logger);
    }
    return controllerService;
}
Also used : FlowChangeAction(org.apache.nifi.action.FlowChangeAction) Action(org.apache.nifi.action.Action) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) Around(org.aspectj.lang.annotation.Around)

Example 19 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class StandardAuthorizableLookup method findControllerServiceReferencingComponent.

private ConfiguredComponent findControllerServiceReferencingComponent(final ControllerServiceReference referencingComponents, final String id) {
    ConfiguredComponent reference = null;
    for (final ConfiguredComponent component : referencingComponents.getReferencingComponents()) {
        if (component.getIdentifier().equals(id)) {
            reference = component;
            break;
        }
        if (component instanceof ControllerServiceNode) {
            final ControllerServiceNode refControllerService = (ControllerServiceNode) component;
            reference = findControllerServiceReferencingComponent(refControllerService.getReferences(), id);
            if (reference != null) {
                break;
            }
        }
    }
    return reference;
}
Also used : ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) ConfiguredComponent(org.apache.nifi.controller.ConfiguredComponent)

Example 20 with ControllerServiceNode

use of org.apache.nifi.controller.service.ControllerServiceNode in project nifi by apache.

the class TestFlowController method testReloadControllerService.

@Test
public void testReloadControllerService() {
    final String id = "ServiceA" + System.currentTimeMillis();
    final BundleCoordinate coordinate = systemBundle.getBundleDetails().getCoordinate();
    final ControllerServiceNode controllerServiceNode = controller.createControllerService(ServiceA.class.getName(), id, coordinate, null, true);
    final String originalName = controllerServiceNode.getName();
    assertEquals(id, controllerServiceNode.getIdentifier());
    assertEquals(id, controllerServiceNode.getComponent().getIdentifier());
    assertEquals(coordinate.getCoordinate(), controllerServiceNode.getBundleCoordinate().getCoordinate());
    assertEquals(ServiceA.class.getCanonicalName(), controllerServiceNode.getCanonicalClassName());
    assertEquals(ServiceA.class.getSimpleName(), controllerServiceNode.getComponentType());
    assertEquals(ServiceA.class.getCanonicalName(), controllerServiceNode.getComponent().getClass().getCanonicalName());
    controller.reload(controllerServiceNode, ServiceB.class.getName(), coordinate, Collections.emptySet());
    // ids and coordinate should stay the same
    assertEquals(id, controllerServiceNode.getIdentifier());
    assertEquals(id, controllerServiceNode.getComponent().getIdentifier());
    assertEquals(coordinate.getCoordinate(), controllerServiceNode.getBundleCoordinate().getCoordinate());
    // in this test we happened to change between two services that have different canonical class names
    // but in the running application the DAO layer would call verifyCanUpdateBundle and would prevent this so
    // for the sake of this test it is ok that the canonical class name hasn't changed
    assertEquals(originalName, controllerServiceNode.getName());
    assertEquals(ServiceA.class.getCanonicalName(), controllerServiceNode.getCanonicalClassName());
    assertEquals(ServiceA.class.getSimpleName(), controllerServiceNode.getComponentType());
    assertEquals(ServiceB.class.getCanonicalName(), controllerServiceNode.getComponent().getClass().getCanonicalName());
}
Also used : ServiceB(org.apache.nifi.controller.service.mock.ServiceB) ServiceA(org.apache.nifi.controller.service.mock.ServiceA) ControllerServiceNode(org.apache.nifi.controller.service.ControllerServiceNode) BundleCoordinate(org.apache.nifi.bundle.BundleCoordinate) Test(org.junit.Test)

Aggregations

ControllerServiceNode (org.apache.nifi.controller.service.ControllerServiceNode)88 HashSet (java.util.HashSet)29 ProcessGroup (org.apache.nifi.groups.ProcessGroup)26 HashMap (java.util.HashMap)25 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)25 ArrayList (java.util.ArrayList)24 Map (java.util.Map)24 LinkedHashSet (java.util.LinkedHashSet)22 Test (org.junit.Test)19 BundleCoordinate (org.apache.nifi.bundle.BundleCoordinate)18 ProcessorNode (org.apache.nifi.controller.ProcessorNode)18 ConfiguredComponent (org.apache.nifi.controller.ConfiguredComponent)17 VersionedProcessGroup (org.apache.nifi.registry.flow.VersionedProcessGroup)17 Set (java.util.Set)16 Connection (org.apache.nifi.connectable.Connection)16 List (java.util.List)15 Port (org.apache.nifi.connectable.Port)15 Label (org.apache.nifi.controller.label.Label)15 RemoteProcessGroup (org.apache.nifi.groups.RemoteProcessGroup)15 RemoteGroupPort (org.apache.nifi.remote.RemoteGroupPort)15