Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public interface EventConfSourceDao extends OnmsDao<EventConfSource, Long> {
void deleteAll(final Collection<EventConfSource> list);

void updateEnabledFlag(final Collection<Long> sourceIds, boolean enabled, boolean cascadeToEvents);

void deleteBySourceIds(List<Long> sourceIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public void updateEnabledFlag(Collection<Long> sourceIds, boolean enabled, boole
}


@Override
public void deleteBySourceIds(List<Long> sourceIds) {
int deletedCount = getHibernateTemplate().execute(session ->
session.createQuery("delete from EventConfSource s where s.id in (:ids)")
.setParameterList("ids", sourceIds)
.executeUpdate()
);
LOG.info("Deleted {} EventConfSource(s) with IDs: {}", deletedCount, sourceIds);
}

@Override
public void saveOrUpdate(EventConfSource source) {
super.saveOrUpdate(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -272,6 +273,68 @@ public void testLoadAndPersistMultipleEventConfFiles() throws Exception {
assertEquals("Total event count mismatch across all files", totalExpectedEventCount, allEvents.size());
}

@Test
@Transactional
public void testDeleteBySourceIds() throws Exception {
// List of XML files to test
String[] xmlFiles = {
"eventconf-test-1.xml",
"eventconf-test-2.xml"
};

int totalExpectedEventCount = 0;
List<Long> allSourceIds = new ArrayList<>();

for (int i = 0; i < xmlFiles.length; i++) {
String file = xmlFiles[i];

EventConfSource source = new EventConfSource();
source.setName("test-source-" + i);
source.setEnabled(true);
source.setCreatedTime(new Date());
source.setFileOrder(i + 1);
source.setDescription("Source for " + file);
source.setVendor("JUnitVendor");
source.setUploadedBy("JUnitTest");
source.setLastModified(new Date());

org.opennms.netmgt.xml.eventconf.Events events =
JaxbUtils.unmarshal(org.opennms.netmgt.xml.eventconf.Events.class,
getClass().getClassLoader().getResourceAsStream(file));

assertNotNull("Events should not be null for file: " + file, events);
assertFalse("Event list should not be empty for file: " + file, events.getEvents().isEmpty());

int eventCount = events.getEvents().size();
totalExpectedEventCount += eventCount;
source.setEventCount(eventCount);

m_dao.saveOrUpdate(source);
m_dao.flush();
allSourceIds.add(source.getId());

for (var xmlEvent : events.getEvents()) {
EventConfEvent jpaEvent = new EventConfEvent();
jpaEvent.setUei(xmlEvent.getUei());
jpaEvent.setDescription(xmlEvent.getDescr());
jpaEvent.setXmlContent(xmlEvent.toString());
jpaEvent.setEnabled(true);
jpaEvent.setCreatedTime(new Date());
jpaEvent.setLastModified(new Date());
jpaEvent.setModifiedBy("XMLTest");
jpaEvent.setSource(source);

m_eventDao.saveOrUpdate(jpaEvent);
}

m_eventDao.flush();
}
final var eventConfSources = m_dao.findAll();
m_dao.deleteBySourceIds(eventConfSources.stream().map(EventConfSource::getId).collect(Collectors.toList()));
final var deletedEventConfSources = m_dao.findAll();
assertEquals(0,deletedEventConfSources.size());
}


@Test
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to The OpenNMS Group, Inc (TOG) under one or more
* contributor license agreements. See the LICENSE.md file
* distributed with this work for additional information
* regarding copyright ownership.
*
* TOG licenses this file to You under the GNU Affero General
* Public License Version 3 (the "License") or (at your option)
* any later version. You may not use this file except in
* compliance with the License. You may obtain a copy of the
* License at:
*
* https://www.gnu.org/licenses/agpl-3.0.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.opennms.netmgt.model.events;

import java.util.List;

public class EventConfSourceDeletePayload {
private List<Long> sourceIds;

public EventConfSourceDeletePayload() {

}

public List<Long> getSourceIds() {
return sourceIds;
}

public void setSourceIds(List<Long> sourceIds) {
this.sourceIds = sourceIds;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.opennms.netmgt.dao.api.EventConfSourceDao;
import org.opennms.netmgt.model.EventConfEvent;
import org.opennms.netmgt.model.EventConfSource;
import org.opennms.netmgt.model.events.EventConfSourceDeletePayload;
import org.opennms.netmgt.model.events.EventConfSourceMetadataDto;
import org.opennms.netmgt.model.events.EventConfSrcEnableDisablePayload;
import org.opennms.netmgt.xml.eventconf.Events;
Expand Down Expand Up @@ -70,6 +71,11 @@ public void updateSourceAndEventEnabled(final EventConfSrcEnableDisablePayload e
}


@Transactional
public void deleteEventConfSources(EventConfSourceDeletePayload eventConfSourceDeletePayload) throws Exception {
eventConfSourceDao.deleteBySourceIds(eventConfSourceDeletePayload.getSourceIds());
}

private EventConfSource createOrUpdateSource(final EventConfSourceMetadataDto eventConfSourceMetadataDto) {
EventConfSource source = eventConfSourceDao.findByName(eventConfSourceMetadataDto.getFilename());
if (source == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.opennms.core.xml.JaxbUtils;
import org.opennms.netmgt.model.events.EventConfSourceDeletePayload;
import org.opennms.netmgt.model.EventConfEvent;
import org.opennms.netmgt.model.EventConfEventDto;
import org.opennms.netmgt.model.events.EventConfSourceMetadataDto;
Expand Down Expand Up @@ -141,6 +142,29 @@ public Response enableDisableEventConfSources(final EventConfSrcEnableDisablePay
}


@Override
public Response deleteEventConfSources(EventConfSourceDeletePayload payload, SecurityContext securityContext) throws Exception {

if (payload == null) {
return Response.status(Response.Status.BAD_REQUEST).entity("Request body cannot be null").build();
}

if (payload.getSourceIds() == null || payload.getSourceIds().isEmpty()) {
return Response.status(Response.Status.BAD_REQUEST).entity("At least one sourceId must be provided.").build();
}

try {
eventConfPersistenceService.deleteEventConfSources(payload);
return Response.ok().entity("EventConf sources deleted successfully.").build();

} catch (EntityNotFoundException ex) {
return Response.status(Response.Status.NOT_FOUND).entity("One or more sourceIds were not found: " + ex.getMessage()).build();
} catch (Exception ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Unexpected error occurred: " + ex.getMessage()).build();
}

}

private List<String> determineFileOrder(final Attachment eventconfXmlAttachment, final Set<String> uploadedFiles) {
List<String> ordered = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.opennms.netmgt.model.events.EventConfSrcEnableDisablePayload;
import org.opennms.netmgt.model.events.EventConfSourceDeletePayload;


import javax.ws.rs.QueryParam;
Expand All @@ -27,6 +28,7 @@
import javax.ws.rs.Path;
import javax.ws.rs.PATCH;
import javax.ws.rs.Produces;
import javax.ws.rs.DELETE;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -91,4 +93,21 @@ Response filterEventConf(
@ApiResponse(responseCode = "400", description = "Invalid request")
})
Response enableDisableEventConfSources(EventConfSrcEnableDisablePayload eventConfSrcEnableDisablePayload, @Context SecurityContext securityContext) throws Exception;

@DELETE
@Path("/sources")
@Consumes(MediaType.APPLICATION_JSON)
@Produces("application/json")
@Operation(
summary = "Delete EventConf Sources",
description = "Delete one or more eventConf sources by their IDs.",
operationId = "deleteEventConfSources"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Sources deleted successfully"),
@ApiResponse(responseCode = "400", description = "Invalid request (missing/invalid IDs)"),
@ApiResponse(responseCode = "404", description = "One or more sources not found")
})
Response deleteEventConfSources(EventConfSourceDeletePayload eventConfSourceDeletePayload,
@Context SecurityContext securityContext) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.opennms.netmgt.dao.api.EventConfSourceDao;
import org.opennms.netmgt.model.EventConfEvent;
import org.opennms.netmgt.model.EventConfSource;
import org.opennms.netmgt.model.events.EventConfSourceDeletePayload;
import org.opennms.netmgt.model.events.EventConfSourceMetadataDto;
import org.opennms.netmgt.model.events.EventConfSrcEnableDisablePayload;
import org.opennms.netmgt.xml.eventconf.Event;
Expand Down Expand Up @@ -346,4 +347,66 @@ public void testUnmarshallEventConfEventXmlContent() throws Exception {
}
}
}

@Test
@JUnitTemporaryDatabase
public void testDeleteEventConfSources() throws Exception {
String username = "test_user";
Date now = new Date();

String filename1 = "source-file-1.xml";
EventConfSourceMetadataDto metadata1 = new EventConfSourceMetadataDto.Builder()
.filename(filename1)
.eventCount(1)
.fileOrder(1)
.username(username)
.now(now)
.vendor("vendor-1")
.description("first entry")
.build();

Event event1 = new Event();
event1.setUei("uei.opennms.org/test/update/1");
event1.setEventLabel("Event One");
event1.setDescr("Description for Event One");
event1.setSeverity("Normal");

Events events1 = new Events();
events1.getEvents().add(event1);

eventConfPersistenceService.persistEventConfFile(events1, metadata1);

String filename2 = "source-file-2.xml";
EventConfSourceMetadataDto metadata2 = new EventConfSourceMetadataDto.Builder()
.filename(filename2)
.eventCount(1)
.fileOrder(2)
.username(username)
.now(now)
.vendor("vendor-2")
.description("second entry")
.build();

Event event2 = new Event();
event2.setUei("uei.opennms.org/test/update/2");
event2.setEventLabel("Event Two");
event2.setDescr("Description for Event Two");
event2.setSeverity("Warning");

Events events2 = new Events();
events2.getEvents().add(event2);

eventConfPersistenceService.persistEventConfFile(events2, metadata2);

List<Long> sourcesIds = eventConfSourceDao.findAll()
.stream().map(EventConfSource::getId).toList();
// delete eventConfSources and its related events
EventConfSourceDeletePayload eventConfSrcDisablePayload = new EventConfSourceDeletePayload();
eventConfSrcDisablePayload.setSourceIds(sourcesIds);
eventConfPersistenceService.deleteEventConfSources(eventConfSrcDisablePayload);
List<EventConfSource> eventConfSources = eventConfSourceDao.findAll();
assertTrue(eventConfSources.isEmpty());

}

}
Loading