diff --git a/api/src/main/java/org/opennms/integration/api/v1/config/events/EventConfExtension.java b/api/src/main/java/org/opennms/integration/api/v1/config/events/EventConfExtension.java new file mode 100644 index 00000000..755175b8 --- /dev/null +++ b/api/src/main/java/org/opennms/integration/api/v1/config/events/EventConfExtension.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * This file is part of OpenNMS(R). + * + * Copyright (C) 2018 The OpenNMS Group, Inc. + * OpenNMS(R) is Copyright (C) 1999-2018 The OpenNMS Group, Inc. + * + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. + * + * OpenNMS(R) is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * OpenNMS(R) is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with OpenNMS(R). If not, see: + * http://www.gnu.org/licenses/ + * + * For more information contact: + * OpenNMS(R) Licensing + * http://www.opennms.org/ + * http://www.opennms.com/ + *******************************************************************************/ + +package org.opennms.integration.api.v1.config.events; + +import java.util.List; + +import org.opennms.integration.api.v1.annotations.Exposable; + +/** + * Used to expose event definitions. + * + * @since 1.0.0 + */ +@Exposable +public interface EventConfExtension { + + List getEventDefinitions(); + +} diff --git a/archetypes/example-kar-plugin/src/main/resources/archetype-resources/plugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/archetypes/example-kar-plugin/src/main/resources/archetype-resources/plugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml index a32abdc5..5ce249fa 100644 --- a/archetypes/example-kar-plugin/src/main/resources/archetype-resources/plugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/archetypes/example-kar-plugin/src/main/resources/archetype-resources/plugin/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -54,4 +54,9 @@ + + + + + \ No newline at end of file diff --git a/config/src/main/java/org/opennms/integration/api/xml/ClasspathEventDefinitionLoader.java b/config/src/main/java/org/opennms/integration/api/xml/ClasspathEventDefinitionLoader.java new file mode 100644 index 00000000..16e2ee8d --- /dev/null +++ b/config/src/main/java/org/opennms/integration/api/xml/ClasspathEventDefinitionLoader.java @@ -0,0 +1,447 @@ +/******************************************************************************* + * This file is part of OpenNMS(R). + * + * Copyright (C) 2018-2022 The OpenNMS Group, Inc. + * OpenNMS(R) is Copyright (C) 1999-2022 The OpenNMS Group, Inc. + * + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. + * + * OpenNMS(R) is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * OpenNMS(R) is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with OpenNMS(R). If not, see: + * http://www.gnu.org/licenses/ + * + * For more information contact: + * OpenNMS(R) Licensing + * http://www.opennms.org/ + * http://www.opennms.com/ + *******************************************************************************/ + +package org.opennms.integration.api.xml; + +import java.io.File; +import java.util.List; +import java.util.stream.Collectors; + +import org.opennms.integration.api.v1.config.events.AlarmData; +import org.opennms.integration.api.v1.config.events.AlarmType; +import org.opennms.integration.api.v1.config.events.AttributeType; +import org.opennms.integration.api.v1.config.events.CollectionGroup; +import org.opennms.integration.api.v1.config.events.EventDefinition; +import org.opennms.integration.api.v1.config.events.LogMessage; +import org.opennms.integration.api.v1.config.events.LogMsgDestType; +import org.opennms.integration.api.v1.config.events.ManagedObject; +import org.opennms.integration.api.v1.config.events.Mask; +import org.opennms.integration.api.v1.config.events.MaskElement; +import org.opennms.integration.api.v1.config.events.Parameter; +import org.opennms.integration.api.v1.config.events.UpdateField; +import org.opennms.integration.api.v1.config.events.Varbind; +import org.opennms.integration.api.v1.model.Severity; +import org.opennms.integration.api.xml.schema.eventconf.Event; +import org.opennms.integration.api.xml.schema.eventconf.Events; +import org.opennms.integration.api.xml.schema.eventconf.LogDestType; +import org.opennms.integration.api.xml.schema.eventconf.Logmsg; +import org.opennms.integration.api.xml.schema.eventconf.Maskelement; + +/** + * Used to load XML event definitions from the class-path. + * + * @author jwhite + * @since 1.0.0 + */ +public class ClasspathEventDefinitionLoader extends ClasspathXmlLoader { + + public ClasspathEventDefinitionLoader(Class clazz, String... fileNames) { + super(clazz, Events.class, "events", fileNames); + } + + public List getEventDefinitions() { + return getObjects().stream() + .flatMap(e -> toEventDefinitions(e).stream()) + .collect(Collectors.toList()); + } + + private static List toEventDefinitions(Events events) { + return events.getEvents().stream() + .map(ClasspathEventDefinitionLoader::toEventDefinition) + .collect(Collectors.toList()); + } + + private static EventDefinition toEventDefinition(Event e) { + final Severity severity = Severity.get(e.getSeverity()); + final LogMessage logMessage = toLogMessage(e.getLogmsg()); + final AlarmData alarmData = toAlarmData(e.getAlarmData()); + final Mask mask = toMask(e.getMask()); + final List parameters = e.getParameters().stream() + .map(ClasspathEventDefinitionLoader::toParameter) + .collect(Collectors.toList()); + final List collectionGroups = e.getCollectionGroup().stream() + .map(ClasspathEventDefinitionLoader::toCollectionGroup) + .collect(Collectors.toList()); + final EventDefinition def = new EventDefinition() { + @Override + public int getPriority() { + return e.getPriority(); + } + + @Override + public String getUei() { + return e.getUei(); + } + + @Override + public String getLabel() { + return e.getEventLabel(); + } + + @Override + public Severity getSeverity() { + return severity; + } + + @Override + public String getDescription() { + return e.getDescr(); + } + + @Override + public String getOperatorInstructions() { + return e.getOperinstruct(); + } + + @Override + public LogMessage getLogMessage() { + return logMessage; + } + + @Override + public AlarmData getAlarmData() { + return alarmData; + } + + @Override + public Mask getMask() { + return mask; + } + + @Override + public List getParameters() { + return parameters; + } + + @Override + public List getCollectionGroup() { + return collectionGroups; + } + }; + return def; + } + + private static Parameter toParameter(org.opennms.integration.api.xml.schema.eventconf.Parameter p) { + final Parameter parm = new Parameter() { + @Override + public String getName() { + return p.getName(); + } + + @Override + public String getValue() { + return p.getValue(); + } + + @Override + public boolean shouldExpand() { + return p.getExpand(); + } + }; + return parm; + } + + private static Mask toMask(org.opennms.integration.api.xml.schema.eventconf.Mask eMask) { + if (eMask == null) { + return null; + } + final List maskElements = eMask.getMaskelements().stream() + .map(ClasspathEventDefinitionLoader::toMaskElemement) + .collect(Collectors.toList()); + final List varbinds = eMask.getVarbinds().stream() + .map(ClasspathEventDefinitionLoader::toVarbind) + .collect(Collectors.toList()); + final Mask mask = new Mask() { + @Override + public List getMaskElements() { + return maskElements; + } + + @Override + public List getVarbinds() { + return varbinds; + } + }; + return mask; + } + + private static MaskElement toMaskElemement(Maskelement el) { + final MaskElement maskElement = new MaskElement() { + @Override + public String getName() { + return el.getMename(); + } + + @Override + public List getValues() { + return el.getMevalues(); + } + }; + return maskElement; + } + + private static Varbind toVarbind(org.opennms.integration.api.xml.schema.eventconf.Varbind vb) { + final Varbind varbind = new Varbind() { + @Override + public String getTextualConvention() { + return vb.getTextualConvention(); + } + + @Override + public Integer getNumber() { + return vb.getVbnumber(); + } + + @Override + public List getValues() { + return vb.getVbvalues(); + } + }; + return varbind; + } + + private static LogMessage toLogMessage(Logmsg log) { + final LogMsgDestType dest = toLogMsgDestType(log.getDest()); + final LogMessage logMsg = new LogMessage() { + @Override + public String getContent() { + return log.getContent(); + } + + @Override + public LogMsgDestType getDestination() { + return dest; + } + }; + return logMsg; + } + + private static LogMsgDestType toLogMsgDestType(LogDestType dest) { + if (dest != null) { + switch(dest) { + case LOGNDISPLAY: + return LogMsgDestType.LOGNDISPLAY; + case DISPLAYONLY: + return LogMsgDestType.DISPLAYONLY; + case LOGONLY: + return LogMsgDestType.LOGONLY; + case SUPPRESS: + return LogMsgDestType.SUPPRESS; + case DONOTPERSIST: + return LogMsgDestType.DONOTPERSIST; + case DISCARDTRAPS: + return LogMsgDestType.DISCARDTRAPS; + } + } + return LogMsgDestType.LOGNDISPLAY; + } + + private static AlarmData toAlarmData(org.opennms.integration.api.xml.schema.eventconf.AlarmData alarm) { + if (alarm == null) { + return null; + } + final List updateFields = alarm.getUpdateFields().stream() + .map(ClasspathEventDefinitionLoader::toUpdateField) + .collect(Collectors.toList()); + final AlarmType type = AlarmType.fromId(alarm.getAlarmType()); + final ManagedObject managedObject = toManagedObject(alarm.getManagedObject()); + final AlarmData alarmData = new AlarmData() { + @Override + public String getReductionKey() { + return alarm.getReductionKey(); + } + + @Override + public AlarmType getType() { + return type; + } + + @Override + public String getClearKey() { + return alarm.getClearKey(); + } + + @Override + public boolean isAutoClean() { + return alarm.getAutoClean(); + } + + @Override + public List getUpdateFields() { + return updateFields; + } + + @Override + public ManagedObject getManagedObject() { + return managedObject; + } + }; + return alarmData; + } + + private static ManagedObject toManagedObject(org.opennms.integration.api.xml.schema.eventconf.ManagedObject mo) { + if (mo == null) { + return null; + } + final ManagedObject managedObject = new ManagedObject() { + @Override + public String getType() { + return mo.getType(); + } + }; + return managedObject; + } + + private static UpdateField toUpdateField(org.opennms.integration.api.xml.schema.eventconf.UpdateField field) { + final UpdateField updateField = new UpdateField() { + @Override + public String getName() { + return field.getFieldName(); + } + + @Override + public boolean isUpdatedOnReduction() { + return field.getUpdateOnReduction(); + } + }; + return updateField; + } + + private static CollectionGroup toCollectionGroup(org.opennms.integration.api.xml.schema.eventconf.CollectionGroup eCollectionGroup) { + if (eCollectionGroup == null) { + return null; + } + final List collections = eCollectionGroup.getCollection().stream() + .map(ClasspathEventDefinitionLoader::toCollection) + .collect(Collectors.toList()); + final CollectionGroup collectionGroup = new CollectionGroup() { + @Override + public String getName() { + return eCollectionGroup.getName(); + } + + @Override + public String getResourceType() { + return eCollectionGroup.getResourceType(); + } + + @Override + public String getInstance() { + return eCollectionGroup.getInstance(); + } + + @Override + public Rrd getRrd() { + return toRrd(eCollectionGroup.getRrd()); + } + + @Override + public List getCollection() { + return collections; + } + }; + return collectionGroup; + } + + private static CollectionGroup.Rrd toRrd(org.opennms.integration.api.xml.schema.eventconf.CollectionGroup.Rrd eRrd) { + if (eRrd == null) { + return null; + } + final CollectionGroup.Rrd rrd = new CollectionGroup.Rrd(){ + @Override + public Integer getStep() { + return eRrd.getStep(); + } + + @Override + public int getHeartBeat() { + return eRrd.getHeartBeat(); + } + + @Override + public List getRras() { + return eRrd.getRras(); + } + + @Override + public File getBaseDir() { + return eRrd.getBaseDir(); + } + }; + return rrd; + } + + private static CollectionGroup.Collection toCollection(org.opennms.integration.api.xml.schema.eventconf.CollectionGroup.Collection eCollection) { + if (eCollection == null) { + return null; + } + final List paramValues = eCollection.getParamValue().stream() + .map(ClasspathEventDefinitionLoader::toParamValue) + .collect(Collectors.toList()); + final CollectionGroup.Collection collection = new CollectionGroup.Collection(){ + @Override + public String getName() { + return eCollection.getName(); + } + + @Override + public String getRename() { + return eCollection.getRename(); + } + + @Override + public AttributeType getType() { + return eCollection.getType(); + } + + @Override + public List getParamValue() { + return paramValues; + } + }; + return collection; + } + + private static CollectionGroup.ParamValue toParamValue(org.opennms.integration.api.xml.schema.eventconf.CollectionGroup.ParamValue eParamValue) { + if (eParamValue == null) { + return null; + } + final CollectionGroup.ParamValue paramValue = new CollectionGroup.ParamValue(){ + @Override + public String getName() { + return eParamValue.getName(); + } + + @Override + public Double getValue() { + return eParamValue.getValue(); + } + }; + return paramValue; + } +} diff --git a/sample/src/main/java/org/opennms/integration/api/sample/MyEventConfExtension.java b/sample/src/main/java/org/opennms/integration/api/sample/MyEventConfExtension.java new file mode 100644 index 00000000..66579213 --- /dev/null +++ b/sample/src/main/java/org/opennms/integration/api/sample/MyEventConfExtension.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * This file is part of OpenNMS(R). + * + * Copyright (C) 2018 The OpenNMS Group, Inc. + * OpenNMS(R) is Copyright (C) 1999-2018 The OpenNMS Group, Inc. + * + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. + * + * OpenNMS(R) is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * OpenNMS(R) is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with OpenNMS(R). If not, see: + * http://www.gnu.org/licenses/ + * + * For more information contact: + * OpenNMS(R) Licensing + * http://www.opennms.org/ + * http://www.opennms.com/ + *******************************************************************************/ + +package org.opennms.integration.api.sample; + +import java.util.List; + +import org.opennms.integration.api.v1.config.events.EventConfExtension; +import org.opennms.integration.api.v1.config.events.EventDefinition; +import org.opennms.integration.api.xml.ClasspathEventDefinitionLoader; + +public class MyEventConfExtension implements EventConfExtension { + + private final ClasspathEventDefinitionLoader classpathEventDefinitionLoader = new ClasspathEventDefinitionLoader( + MyEventConfExtension.class, + "SNMP_Link_UpDown.xml", + "sample.xml"); + + @Override + public List getEventDefinitions() { + return classpathEventDefinitionLoader.getEventDefinitions(); + } +} diff --git a/sample/src/main/java/org/opennms/integration/api/sample/MySyslogMatchExtension.java b/sample/src/main/java/org/opennms/integration/api/sample/MySyslogMatchExtension.java index 1c04bfab..536a65db 100644 --- a/sample/src/main/java/org/opennms/integration/api/sample/MySyslogMatchExtension.java +++ b/sample/src/main/java/org/opennms/integration/api/sample/MySyslogMatchExtension.java @@ -37,7 +37,7 @@ public class MySyslogMatchExtension implements SyslogMatchExtension { private final ClasspathSyslogMatchLoader classpathSyslogMatchLoader = new ClasspathSyslogMatchLoader( - MySyslogMatchExtension.class, + MyEventConfExtension.class, "Cisco.syslog.xml"); @Override diff --git a/sample/src/main/resources/blueprint-core.xml b/sample/src/main/resources/blueprint-core.xml index 143b2a9e..9c4ec383 100644 --- a/sample/src/main/resources/blueprint-core.xml +++ b/sample/src/main/resources/blueprint-core.xml @@ -81,6 +81,10 @@ + + + + diff --git a/sample/src/test/java/org/opennms/integration/api/sample/MyEventConfExtensionTest.java b/sample/src/test/java/org/opennms/integration/api/sample/MyEventConfExtensionTest.java new file mode 100644 index 00000000..77447889 --- /dev/null +++ b/sample/src/test/java/org/opennms/integration/api/sample/MyEventConfExtensionTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * This file is part of OpenNMS(R). + * + * Copyright (C) 2018 The OpenNMS Group, Inc. + * OpenNMS(R) is Copyright (C) 1999-2018 The OpenNMS Group, Inc. + * + * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc. + * + * OpenNMS(R) is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, + * or (at your option) any later version. + * + * OpenNMS(R) is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with OpenNMS(R). If not, see: + * http://www.gnu.org/licenses/ + * + * For more information contact: + * OpenNMS(R) Licensing + * http://www.opennms.org/ + * http://www.opennms.com/ + *******************************************************************************/ + +package org.opennms.integration.api.sample; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.core.IsEqual.equalTo; + +import java.util.List; + +import org.junit.Test; +import org.opennms.integration.api.v1.config.events.AlarmType; +import org.opennms.integration.api.v1.config.events.CollectionGroup; +import org.opennms.integration.api.v1.config.events.EventDefinition; + +public class MyEventConfExtensionTest { + + @Test + public void canReadEventDefinitionsFromExtension() { + MyEventConfExtension myEventConfExtension = new MyEventConfExtension(); + List eventDefinitions = myEventConfExtension.getEventDefinitions(); + assertThat(eventDefinitions, hasSize(4)); + EventDefinition linkDown = eventDefinitions.get(0); + assertThat(linkDown.getUei(), equalTo("uei.opennms.org/generic/traps/SNMP_Link_Down")); + assertThat(linkDown.getAlarmData().getManagedObject().getType(), equalTo("mytype")); + assertThat(linkDown.getAlarmData().getType(), equalTo(AlarmType.PROBLEM)); + assertThat(linkDown.getPriority(), equalTo(20)); + + EventDefinition linkUp = eventDefinitions.get(1); + assertThat(linkUp.getUei(), equalTo("uei.opennms.org/generic/traps/SNMP_Link_Up")); + assertThat(linkUp.getAlarmData().getManagedObject().getType(), equalTo("mytype")); + assertThat(linkUp.getAlarmData().getType(), equalTo(AlarmType.RESOLUTION)); + assertThat(linkUp.getPriority(), equalTo(10)); + + EventDefinition trigger = eventDefinitions.get(2); + assertThat(trigger.getUei(), equalTo("uei.opennms.org/oia/sample/trigger")); + assertThat(trigger.getCollectionGroup(), hasSize(1)); + CollectionGroup collectionGroup = trigger.getCollectionGroup().get(0); + assertThat(collectionGroup.getName(), equalTo("nodeGroup")); + assertThat(collectionGroup.getRrd().getRras(), hasSize(1)); + assertThat(collectionGroup.getCollection(), hasSize(2)); + assertThat(collectionGroup.getCollection().get(1).getParamValue(), hasSize(2)); + } +}