use of org.apache.nifi.action.details.ActionDetails in project nifi by apache.
the class ProcessGroupAuditor method updateProcessGroupAdvice.
/**
* Audits the update of process group configuration.
*
* @param proceedingJoinPoint join point
* @param processGroupDTO dto
* @return group
* @throws Throwable ex
*/
@Around("within(org.apache.nifi.web.dao.ProcessGroupDAO+) && " + "execution(org.apache.nifi.groups.ProcessGroup updateProcessGroup(org.apache.nifi.web.api.dto.ProcessGroupDTO)) && " + "args(processGroupDTO)")
public ProcessGroup updateProcessGroupAdvice(ProceedingJoinPoint proceedingJoinPoint, ProcessGroupDTO processGroupDTO) throws Throwable {
ProcessGroupDAO processGroupDAO = getProcessGroupDAO();
ProcessGroup processGroup = processGroupDAO.getProcessGroup(processGroupDTO.getId());
String name = processGroup.getName();
String comments = processGroup.getComments();
// perform the underlying operation
ProcessGroup updatedProcessGroup = (ProcessGroup) proceedingJoinPoint.proceed();
// get the current user
NiFiUser user = NiFiUserUtils.getNiFiUser();
// ensure the user was found
if (user != null) {
Collection<ActionDetails> details = new ArrayList<>();
// see if the name has changed
if (name != null && updatedProcessGroup.getName() != null && !name.equals(updatedProcessGroup.getName())) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("name");
configDetails.setValue(updatedProcessGroup.getName());
configDetails.setPreviousValue(name);
details.add(configDetails);
}
// see if the comments has changed
if (comments != null && updatedProcessGroup.getComments() != null && !comments.equals(updatedProcessGroup.getComments())) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("comments");
configDetails.setValue(updatedProcessGroup.getComments());
configDetails.setPreviousValue(comments);
details.add(configDetails);
}
// hold all actions
Collection<Action> actions = new ArrayList<>();
// save the actions if necessary
if (!details.isEmpty()) {
Date timestamp = new Date();
// create the actions
for (ActionDetails detail : details) {
// determine the type of operation being performed
Operation operation = Operation.Configure;
if (detail instanceof FlowChangeMoveDetails) {
operation = Operation.Move;
}
// create the port action for updating the name
FlowChangeAction processGroupAction = new FlowChangeAction();
processGroupAction.setUserIdentity(user.getIdentity());
processGroupAction.setOperation(operation);
processGroupAction.setTimestamp(timestamp);
processGroupAction.setSourceId(updatedProcessGroup.getIdentifier());
processGroupAction.setSourceName(updatedProcessGroup.getName());
processGroupAction.setSourceType(Component.ProcessGroup);
processGroupAction.setActionDetails(detail);
actions.add(processGroupAction);
}
}
// save actions if necessary
if (!actions.isEmpty()) {
saveActions(actions, logger);
}
}
return updatedProcessGroup;
}
use of org.apache.nifi.action.details.ActionDetails in project nifi by apache.
the class RemoteProcessGroupAuditor method auditUpdateProcessGroupConfiguration.
/**
* Audits the update of remote process group configuration.
*
* @param proceedingJoinPoint join point
* @param remoteProcessGroupDTO dto
* @param remoteProcessGroupDAO dao
* @return group
* @throws Throwable ex
*/
@Around("within(org.apache.nifi.web.dao.RemoteProcessGroupDAO+) && " + "execution(org.apache.nifi.groups.RemoteProcessGroup updateRemoteProcessGroup(org.apache.nifi.web.api.dto.RemoteProcessGroupDTO)) && " + "args(remoteProcessGroupDTO) && " + "target(remoteProcessGroupDAO)")
public RemoteProcessGroup auditUpdateProcessGroupConfiguration(ProceedingJoinPoint proceedingJoinPoint, RemoteProcessGroupDTO remoteProcessGroupDTO, RemoteProcessGroupDAO remoteProcessGroupDAO) throws Throwable {
final RemoteProcessGroup remoteProcessGroup = remoteProcessGroupDAO.getRemoteProcessGroup(remoteProcessGroupDTO.getId());
// record the current value of this remoteProcessGroups configuration for comparisons later
final boolean transmissionState = remoteProcessGroup.isTransmitting();
final Map<String, Object> previousValues = ConfigurationRecorder.capturePreviousValues(CONFIG_RECORDERS, remoteProcessGroup);
// perform the underlying operation
final RemoteProcessGroup updatedRemoteProcessGroup = (RemoteProcessGroup) proceedingJoinPoint.proceed();
// get the current user
NiFiUser user = NiFiUserUtils.getNiFiUser();
// ensure the user was found
if (user != null) {
final Collection<ActionDetails> details = new ArrayList<>();
// see if any property has changed
ConfigurationRecorder.checkConfigured(CONFIG_RECORDERS, remoteProcessGroupDTO, updatedRemoteProcessGroup, previousValues, details);
final Date timestamp = new Date();
final Collection<Action> actions = new ArrayList<>();
// create the remote process group details
final FlowChangeRemoteProcessGroupDetails remoteProcessGroupDetails = createFlowChangeDetails(remoteProcessGroup);
// save the actions if necessary
if (!details.isEmpty()) {
// create the actions
for (ActionDetails detail : details) {
// create a configure action for each updated property
FlowChangeAction remoteProcessGroupAction = createFlowChangeAction(user, timestamp, updatedRemoteProcessGroup, remoteProcessGroupDetails);
remoteProcessGroupAction.setOperation(Operation.Configure);
remoteProcessGroupAction.setActionDetails(detail);
actions.add(remoteProcessGroupAction);
}
}
// determine the new executing state
boolean updatedTransmissionState = updatedRemoteProcessGroup.isTransmitting();
// determine if the running state has changed
if (transmissionState != updatedTransmissionState) {
// create a remote process group action
FlowChangeAction remoteProcessGroupAction = createFlowChangeAction(user, timestamp, updatedRemoteProcessGroup, remoteProcessGroupDetails);
// set the operation accordingly
if (updatedTransmissionState) {
remoteProcessGroupAction.setOperation(Operation.Start);
} else {
remoteProcessGroupAction.setOperation(Operation.Stop);
}
actions.add(remoteProcessGroupAction);
}
// ensure there are actions to record
if (!actions.isEmpty()) {
// save the actions
saveActions(actions, logger);
}
}
return updatedRemoteProcessGroup;
}
use of org.apache.nifi.action.details.ActionDetails in project nifi by apache.
the class StandardActionDAO method findActions.
@Override
public History findActions(HistoryQuery historyQuery) throws DataAccessException {
// get the sort column
String sortColumn = "ACTION_TIMESTAMP";
if (StringUtils.isNotBlank(historyQuery.getSortColumn())) {
String rawColumnName = historyQuery.getSortColumn();
if (!columnMap.containsKey(rawColumnName)) {
throw new IllegalArgumentException(String.format("Unrecognized column name '%s'.", rawColumnName));
}
sortColumn = columnMap.get(rawColumnName);
}
// get the sort order
String sortOrder = "desc";
if (StringUtils.isNotBlank(historyQuery.getSortOrder())) {
sortOrder = historyQuery.getSortOrder();
}
History actionResult = new History();
Collection<Action> actions = new ArrayList<>();
PreparedStatement statement = null;
ResultSet rs = null;
try {
List<String> where = new ArrayList<>();
// append the start time
if (historyQuery.getStartDate() != null) {
where.add("ACTION_TIMESTAMP >= ?");
}
// append the end time
if (historyQuery.getEndDate() != null) {
where.add("ACTION_TIMESTAMP <= ?");
}
// append the user id as necessary
if (historyQuery.getUserIdentity() != null) {
where.add("UPPER(IDENTITY) LIKE ?");
}
// append the source id as necessary
if (historyQuery.getSourceId() != null) {
where.add("SOURCE_ID = ?");
}
String sql = SELECT_ACTION_COUNT;
if (!where.isEmpty()) {
sql += " WHERE " + StringUtils.join(where, " AND ");
}
// get the total number of actions
statement = connection.prepareStatement(sql);
int paramIndex = 1;
// set the start date as necessary
if (historyQuery.getStartDate() != null) {
statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getStartDate().getTime()));
}
// set the end date as necessary
if (historyQuery.getEndDate() != null) {
statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getEndDate().getTime()));
}
// set the user id as necessary
if (historyQuery.getUserIdentity() != null) {
statement.setString(paramIndex++, "%" + historyQuery.getUserIdentity().toUpperCase() + "%");
}
// set the source id as necessary
if (historyQuery.getSourceId() != null) {
statement.setString(paramIndex, historyQuery.getSourceId());
}
// execute the statement
rs = statement.executeQuery();
// ensure there are results
if (rs.next()) {
actionResult.setTotal(rs.getInt("ACTION_COUNT"));
} else {
throw new DataAccessException("Unable to determine total action count.");
}
sql = SELECT_ACTIONS;
if (!where.isEmpty()) {
sql += " WHERE " + StringUtils.join(where, " AND ");
}
// append the sort criteria
sql += (" ORDER BY " + sortColumn + " " + sortOrder);
// append the offset and limit
sql += " LIMIT ? OFFSET ?";
// close the previous statement
statement.close();
// create the statement
statement = connection.prepareStatement(sql);
paramIndex = 1;
// set the start date as necessary
if (historyQuery.getStartDate() != null) {
statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getStartDate().getTime()));
}
// set the end date as necessary
if (historyQuery.getEndDate() != null) {
statement.setTimestamp(paramIndex++, new java.sql.Timestamp(historyQuery.getEndDate().getTime()));
}
// set the user id as necessary
if (historyQuery.getUserIdentity() != null) {
statement.setString(paramIndex++, "%" + historyQuery.getUserIdentity().toUpperCase() + "%");
}
// set the source id as necessary
if (historyQuery.getSourceId() != null) {
statement.setString(paramIndex++, historyQuery.getSourceId());
}
// set the limit
statement.setInt(paramIndex++, historyQuery.getCount());
// set the offset according to the currented page calculated above
statement.setInt(paramIndex, historyQuery.getOffset());
// execute the query
rs = statement.executeQuery();
// create each corresponding action
while (rs.next()) {
final Integer actionId = rs.getInt("ID");
final Operation operation = Operation.valueOf(rs.getString("OPERATION"));
final Component component = Component.valueOf(rs.getString("SOURCE_TYPE"));
FlowChangeAction action = new FlowChangeAction();
action.setId(actionId);
action.setUserIdentity(rs.getString("IDENTITY"));
action.setOperation(Operation.valueOf(rs.getString("OPERATION")));
action.setTimestamp(new Date(rs.getTimestamp("ACTION_TIMESTAMP").getTime()));
action.setSourceId(rs.getString("SOURCE_ID"));
action.setSourceName(rs.getString("SOURCE_NAME"));
action.setSourceType(Component.valueOf(rs.getString("SOURCE_TYPE")));
// get the component details if appropriate
ComponentDetails componentDetails = null;
if (Component.Processor.equals(component) || Component.ControllerService.equals(component) || Component.ReportingTask.equals(component)) {
componentDetails = getExtensionDetails(actionId);
} else if (Component.RemoteProcessGroup.equals(component)) {
componentDetails = getRemoteProcessGroupDetails(actionId);
}
if (componentDetails != null) {
action.setComponentDetails(componentDetails);
}
// get the action details if appropriate
ActionDetails actionDetails = null;
if (Operation.Move.equals(operation)) {
actionDetails = getMoveDetails(actionId);
} else if (Operation.Configure.equals(operation)) {
actionDetails = getConfigureDetails(actionId);
} else if (Operation.Connect.equals(operation) || Operation.Disconnect.equals(operation)) {
actionDetails = getConnectDetails(actionId);
} else if (Operation.Purge.equals(operation)) {
actionDetails = getPurgeDetails(actionId);
}
// set the action details
if (actionDetails != null) {
action.setActionDetails(actionDetails);
}
// add the action
actions.add(action);
}
// populate the action result
actionResult.setActions(actions);
} catch (SQLException sqle) {
throw new DataAccessException(sqle);
} finally {
RepositoryUtils.closeQuietly(rs);
RepositoryUtils.closeQuietly(statement);
}
return actionResult;
}
use of org.apache.nifi.action.details.ActionDetails in project nifi by apache.
the class StandardActionDAO method createAction.
@Override
public Action createAction(Action action) throws DataAccessException {
if (action.getUserIdentity() == null) {
throw new IllegalArgumentException("User cannot be null.");
}
if (action.getTimestamp() == null) {
throw new IllegalArgumentException("Action timestamp cannot be null.");
}
PreparedStatement statement = null;
ResultSet rs = null;
try {
// obtain a statement to insert to the action table
statement = connection.prepareStatement(INSERT_ACTION, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, StringUtils.left(action.getUserIdentity(), 4096));
statement.setString(2, action.getSourceId());
statement.setString(3, StringUtils.left(action.getSourceName(), 1000));
statement.setString(4, action.getSourceType().name());
statement.setString(5, action.getOperation().name());
statement.setTimestamp(6, new java.sql.Timestamp(action.getTimestamp().getTime()));
// insert the action
int updateCount = statement.executeUpdate();
final FlowChangeAction createdAction = new FlowChangeAction();
createdAction.setUserIdentity(action.getUserIdentity());
createdAction.setSourceId(action.getSourceId());
createdAction.setSourceName(action.getSourceName());
createdAction.setSourceType(action.getSourceType());
createdAction.setOperation(action.getOperation());
createdAction.setTimestamp(action.getTimestamp());
createdAction.setActionDetails(action.getActionDetails());
createdAction.setComponentDetails(action.getComponentDetails());
// get the action id
rs = statement.getGeneratedKeys();
if (updateCount == 1 && rs.next()) {
createdAction.setId(rs.getInt(1));
} else {
throw new DataAccessException("Unable to insert action.");
}
// close the previous statement
statement.close();
// determine the type of component
ComponentDetails componentDetails = createdAction.getComponentDetails();
if (componentDetails instanceof FlowChangeExtensionDetails) {
createExtensionDetails(createdAction.getId(), (ExtensionDetails) componentDetails);
} else if (componentDetails instanceof FlowChangeRemoteProcessGroupDetails) {
createRemoteProcessGroupDetails(createdAction.getId(), (RemoteProcessGroupDetails) componentDetails);
}
// determine the type of action
ActionDetails details = createdAction.getActionDetails();
if (details instanceof FlowChangeConnectDetails) {
createConnectDetails(createdAction.getId(), (ConnectDetails) details);
} else if (details instanceof FlowChangeMoveDetails) {
createMoveDetails(createdAction.getId(), (MoveDetails) details);
} else if (details instanceof FlowChangeConfigureDetails) {
createConfigureDetails(createdAction.getId(), (ConfigureDetails) details);
} else if (details instanceof FlowChangePurgeDetails) {
createPurgeDetails(createdAction.getId(), (PurgeDetails) details);
}
return createdAction;
} catch (SQLException sqle) {
throw new DataAccessException(sqle);
} finally {
RepositoryUtils.closeQuietly(rs);
RepositoryUtils.closeQuietly(statement);
}
}
use of org.apache.nifi.action.details.ActionDetails in project nifi by apache.
the class PortAuditor method updatePortAdvice.
/**
* Audits the update of a port.
*
* @param proceedingJoinPoint join point
* @param portDTO port dto
* @param portDAO port dao
* @return port
* @throws Throwable ex
*/
@Around("within(org.apache.nifi.web.dao.PortDAO+) && " + "execution(org.apache.nifi.connectable.Port updatePort(org.apache.nifi.web.api.dto.PortDTO)) && " + "args(portDTO) && " + "target(portDAO)")
public Port updatePortAdvice(ProceedingJoinPoint proceedingJoinPoint, PortDTO portDTO, PortDAO portDAO) throws Throwable {
final Port port = portDAO.getPort(portDTO.getId());
final ScheduledState scheduledState = port.getScheduledState();
final String name = port.getName();
final String comments = port.getComments();
final int maxConcurrentTasks = port.getMaxConcurrentTasks();
final Set<String> existingUsers = new HashSet<>();
final Set<String> existingGroups = new HashSet<>();
boolean isRootGroupPort = false;
if (port instanceof RootGroupPort) {
isRootGroupPort = true;
existingUsers.addAll(((RootGroupPort) port).getUserAccessControl());
existingGroups.addAll(((RootGroupPort) port).getGroupAccessControl());
}
// perform the underlying operation
final Port updatedPort = (Port) proceedingJoinPoint.proceed();
// get the current user
NiFiUser user = NiFiUserUtils.getNiFiUser();
// ensure the user was found
if (user != null) {
Collection<ActionDetails> configurationDetails = new ArrayList<>();
// see if the name has changed
if (name != null && portDTO.getName() != null && !name.equals(updatedPort.getName())) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("Name");
configDetails.setValue(updatedPort.getName());
configDetails.setPreviousValue(name);
configurationDetails.add(configDetails);
}
// see if the comments has changed
if (comments != null && portDTO.getComments() != null && !comments.equals(updatedPort.getComments())) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("Comments");
configDetails.setValue(updatedPort.getComments());
configDetails.setPreviousValue(comments);
configurationDetails.add(configDetails);
}
// if this is a root group port, consider concurrent tasks
if (isRootGroupPort) {
if (portDTO.getConcurrentlySchedulableTaskCount() != null && updatedPort.getMaxConcurrentTasks() != maxConcurrentTasks) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("Concurrent Tasks");
configDetails.setValue(String.valueOf(updatedPort.getMaxConcurrentTasks()));
configDetails.setPreviousValue(String.valueOf(maxConcurrentTasks));
configurationDetails.add(configDetails);
}
// if user access control was specified in the request
if (portDTO.getUserAccessControl() != null) {
final Set<String> newUsers = new HashSet<>(portDTO.getUserAccessControl());
newUsers.removeAll(existingUsers);
final Set<String> removedUsers = new HashSet<>(existingUsers);
removedUsers.removeAll(portDTO.getUserAccessControl());
// if users were added/removed
if (newUsers.size() > 0 || removedUsers.size() > 0) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("User Access Control");
configDetails.setValue(StringUtils.join(portDTO.getUserAccessControl(), ", "));
configDetails.setPreviousValue(StringUtils.join(existingUsers, ", "));
configurationDetails.add(configDetails);
}
}
// if group access control was specified in the request
if (portDTO.getGroupAccessControl() != null) {
final Set<String> newGroups = new HashSet<>(portDTO.getGroupAccessControl());
newGroups.removeAll(existingGroups);
final Set<String> removedGroups = new HashSet<>(existingGroups);
removedGroups.removeAll(portDTO.getGroupAccessControl());
// if groups were added/removed
if (newGroups.size() > 0 || removedGroups.size() > 0) {
// create the config details
FlowChangeConfigureDetails configDetails = new FlowChangeConfigureDetails();
configDetails.setName("Group Access Control");
configDetails.setValue(StringUtils.join(portDTO.getGroupAccessControl(), ", "));
configDetails.setPreviousValue(StringUtils.join(existingGroups, ", "));
configurationDetails.add(configDetails);
}
}
}
final Collection<Action> actions = new ArrayList<>();
// determine the type of port
Component componentType = Component.OutputPort;
if (ConnectableType.INPUT_PORT == updatedPort.getConnectableType()) {
componentType = Component.InputPort;
}
// add each configuration detail
if (!configurationDetails.isEmpty()) {
// create the timestamp for the update
Date timestamp = new Date();
// create the actions
for (ActionDetails detail : configurationDetails) {
// create the port action for updating the name
FlowChangeAction portAction = new FlowChangeAction();
portAction.setUserIdentity(user.getIdentity());
portAction.setOperation(Operation.Configure);
portAction.setTimestamp(timestamp);
portAction.setSourceId(updatedPort.getIdentifier());
portAction.setSourceName(updatedPort.getName());
portAction.setSourceType(componentType);
portAction.setActionDetails(detail);
actions.add(portAction);
}
}
// determine the new executing state
final ScheduledState updatedScheduledState = updatedPort.getScheduledState();
// determine if the running state has changed
if (scheduledState != updatedScheduledState) {
// create a processor action
FlowChangeAction processorAction = new FlowChangeAction();
processorAction.setUserIdentity(user.getIdentity());
processorAction.setTimestamp(new Date());
processorAction.setSourceId(updatedPort.getIdentifier());
processorAction.setSourceName(updatedPort.getName());
processorAction.setSourceType(componentType);
// set the operation accordingly
if (ScheduledState.RUNNING.equals(updatedScheduledState)) {
processorAction.setOperation(Operation.Start);
} else if (ScheduledState.DISABLED.equals(updatedScheduledState)) {
processorAction.setOperation(Operation.Disable);
} else {
// state is now stopped... consider the previous state
if (ScheduledState.RUNNING.equals(scheduledState)) {
processorAction.setOperation(Operation.Stop);
} else if (ScheduledState.DISABLED.equals(scheduledState)) {
processorAction.setOperation(Operation.Enable);
}
}
actions.add(processorAction);
}
// ensure there are actions to record
if (!actions.isEmpty()) {
// save the actions
saveActions(actions, logger);
}
}
return updatedPort;
}
Aggregations