NMS-19486: Support deletion of mibgroups,resourcetypes and systemdefs#8280
Conversation
…rces | Expose services to delete list of mibgroups,resourcetypes and systemdefs
There was a problem hiding this comment.
Pull request overview
Adds REST v2 delete endpoints and DAO support for bulk deletion of SNMP data collection sources and related entities (MIB groups, resource types, system defs).
Changes:
- Introduces new REST payload models for bulk delete requests.
- Adds REST API interface methods + REST service implementations for delete operations.
- Extends DAOs with bulk-delete methods (HQL
delete ... where id in (...)) for sources and child entities.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/model/SnmpDataCollectionSystemDefDeletePayload.java | Adds request payload for deleting system defs by ID list |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/model/SnmpDataCollectionSourceDeletePayload.java | Adds request payload for deleting collection sources by ID list |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/model/SnmpDataCollectionResourceTypeDeletePayload.java | Adds request payload for deleting resource types by ID list |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/model/SnmpDataCollectionMibGroupDeletePayload.java | Adds request payload for deleting MIB groups by ID list |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/api/DataCollectionConfRestApi.java | Exposes new DELETE endpoints for the above bulk deletions |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/DataCollectionConfRestService.java | Implements REST endpoints and delegates deletions to DAO/service layer |
| opennms-webapp-rest/src/main/java/org/opennms/web/rest/v2/DataCollectionConfPersistenceService.java | Adds transactional delete operations with validation against DB state |
| opennms-dao/src/main/java/org/opennms/netmgt/dao/hibernate/SnmpCollectionSystemDefDaoHibernate.java | Adds bulk delete by source + systemDef IDs |
| opennms-dao/src/main/java/org/opennms/netmgt/dao/hibernate/SnmpCollectionSourceDaoHibernate.java | Adds bulk delete by source IDs |
| opennms-dao/src/main/java/org/opennms/netmgt/dao/hibernate/SnmpCollectionResourceTypeDaoHibernate.java | Adds bulk delete by source + resourceType IDs |
| opennms-dao/src/main/java/org/opennms/netmgt/dao/hibernate/SnmpCollectionMibGroupDaoHibernate.java | Adds bulk delete by source + mibGroup IDs |
| opennms-dao-api/src/main/java/org/opennms/netmgt/dao/api/SnmpCollectionSystemDefDao.java | Adds DAO API for bulk systemDef deletion |
| opennms-dao-api/src/main/java/org/opennms/netmgt/dao/api/SnmpCollectionSourceDao.java | Adds DAO API for bulk source deletion |
| opennms-dao-api/src/main/java/org/opennms/netmgt/dao/api/SnmpCollectionResourceTypeDao.java | Adds DAO API for bulk resourceType deletion |
| opennms-dao-api/src/main/java/org/opennms/netmgt/dao/api/SnmpCollectionMibGroupDao.java | Adds DAO API for bulk mibGroup deletion |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| final var mibGroupsIds = snmpCollectionMibGroupDao.findAllBySource(snmpDataCollectionSourceId); | ||
|
|
||
| final Set<Integer> databaseMibGroupIds = mibGroupsIds | ||
| .stream() | ||
| .map(SnmpCollectionMibGroup::getId) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| final var requestMibGroupIds = payload.getMibGroupsIds(); | ||
| final var existingMibGroupIds = requestMibGroupIds.stream() | ||
| .filter(databaseMibGroupIds::contains) | ||
| .toList(); |
There was a problem hiding this comment.
Do we need to load all mibgroups to delete ?
| public void deleteByMibGroupIds(Integer snmpDataCollectionSourceId, List<Integer> snmpCollectionMibGroupIds) { | ||
| int deletedCount = getHibernateTemplate().execute(session -> | ||
| session.createQuery("delete from SnmpCollectionMibGroup g where g.collectionSource.id = :snmpCollectionSourceId and g.id in (:Ids)") | ||
| .setParameter("snmpCollectionSourceId", snmpDataCollectionSourceId) | ||
| .setParameterList("Ids", snmpCollectionMibGroupIds) | ||
| .executeUpdate() | ||
| ); | ||
| LOG.info("Deleted {} SnmpCollectionMibGroup(g) with IDs: {} for snmpCollectionSourceId: {}", deletedCount, snmpCollectionMibGroupIds, snmpDataCollectionSourceId); | ||
| } |
There was a problem hiding this comment.
I think there is already code to delete and deleteAll in AbstractDaoHibernate, no need to re-implement these custom HQL
| } | ||
|
|
||
| try { | ||
| snmpCollectionSourceDao.deleteByIds(snmpDataCollectionSourceDeletePayload.getSnmpCollectionSourceIds()); |
There was a problem hiding this comment.
why are we calling dao here directly, can use service.
| } | ||
|
|
||
| @Override | ||
| public void deleteByIds(List<Integer> snmpDataCollectionIds) { |
There was a problem hiding this comment.
I think it would be good to have cascade relations defined for child entities. That way the deletion of children happen in Hibernate session.
| @ApiResponse(responseCode = "500", description = "Internal server error") | ||
| }) | ||
| Response deleteSnmpDataCollectionSources( | ||
| SnmpDataCollectionSourceDeletePayload snmpDataCollectionSourceDeletePayload, |
There was a problem hiding this comment.
Prefer to use simple query params with a list of ids here instead of using Payload
| int deletedCount = getHibernateTemplate().execute(session -> | ||
| session.createQuery("delete from SnmpCollectionSource s where s.id in (:ids)") | ||
| .setParameterList("ids", snmpDataCollectionIds) | ||
| .executeUpdate() | ||
| ); |
There was a problem hiding this comment.
don't need to write an actual SQL query or HQL. You can use entities,
something like this :
for (Integer id : ids) {
SnmpCollectionSource entity = get(id);
if (entity != null) {
delete(entity);
}
}
|
|
||
| PageResponse<SnmpCollectionResourceType> findByDataCollectionGroupId(Integer snmpCollectionSourceId, String resourceTypeFilter, String sortBy, String order, Integer totalRecords, Integer offset, Integer limit); | ||
|
|
||
| void deleteByResourceTypeIds(Integer snmpDataCollectionSourceId,List<Integer> snmpCollectionResourceTypeIds); |
There was a problem hiding this comment.
some of these apis are unused now
# Conflicts: # opennms-dao-api/src/main/java/org/opennms/netmgt/dao/api/SnmpCollectionMibGroupDao.java
| PageResponse<SnmpCollectionSource> filterDataCollectionSource(final String filter, final String sortBy, final String order, Integer totalRecords, | ||
| Integer offset, Integer limit); | ||
|
|
||
| void deleteByIds(List<Integer> snmpDataCollectionIds); |
|
|
||
| final T entity = finder.apply(sourceId, id); | ||
| if (entity == null) { | ||
| throw new EntityNotFoundException(entityLabel + " not found for sourceId=" + sourceId + ", id=" + id); |
There was a problem hiding this comment.
may be continue would be better here ?
| return handleDelete( | ||
| () -> { dataCollectionConfPersistenceService.deleteSnmpDataCollectionSystemDefs(snmpDataCollectionSourceId, ids); return null; }, | ||
| "Snmp Data Collection System Def deleted successfully" | ||
| ); | ||
| } | ||
|
|
||
| private Response handleDelete(final Supplier<Void> action, final String successMessage) { | ||
| try { | ||
| action.get(); | ||
|
|
There was a problem hiding this comment.
Can you revisit this code and avoid using Supplier kind of overhead ?
afd4990
into
features/snmp-datacollection-db
External References