Skip to content

Commit

Permalink
task #1992 Cleanup XML of action SNMP Trap
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdutra committed Jul 9, 2023
1 parent 6d39145 commit afdd386
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
import org.apache.hop.core.Const;
import org.apache.hop.core.Result;
import org.apache.hop.core.annotations.Action;
import org.apache.hop.core.encryption.Encr;
import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.exception.HopXmlException;
import org.apache.hop.core.util.Utils;
import org.apache.hop.core.variables.IVariables;
import org.apache.hop.core.xml.XmlHandler;
import org.apache.hop.i18n.BaseMessages;
import org.apache.hop.metadata.api.IHopMetadataProvider;
import org.apache.hop.metadata.api.HopMetadataProperty;
import org.apache.hop.workflow.action.ActionBase;
import org.apache.hop.workflow.action.IAction;
import org.snmp4j.CommunityTarget;
Expand All @@ -51,7 +47,6 @@
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.w3c.dom.Node;

import java.net.InetAddress;

Expand All @@ -67,16 +62,27 @@
public class ActionSNMPTrap extends ActionBase implements Cloneable, IAction {
private static final Class<?> PKG = ActionSNMPTrap.class; // For Translator

@HopMetadataProperty(key = "servername")
private String serverName;
@HopMetadataProperty(key = "port")
private String port;
@HopMetadataProperty(key = "timeout")
private String timeout;
@HopMetadataProperty(key = "nrretry")
private String nrretry;
@HopMetadataProperty(key = "comstring")
private String comString;
@HopMetadataProperty(key = "message")
private String message;
@HopMetadataProperty(key = "oid")
private String oid;
@HopMetadataProperty(key = "targettype")
private String targettype;
@HopMetadataProperty(key = "user")
private String user;
@HopMetadataProperty(key = "passphrase")
private String passphrase;
@HopMetadataProperty(key = "engineid")
private String engineid;

/** Default retries */
Expand Down Expand Up @@ -142,52 +148,6 @@ public String getTargetTypeCode(String tt) {
}
}

@Override
public String getXml() {
StringBuilder retval = new StringBuilder(200);

retval.append(super.getXml());
retval.append(" ").append(XmlHandler.addTagValue("port", port));
retval.append(" ").append(XmlHandler.addTagValue("servername", serverName));
retval.append(" ").append(XmlHandler.addTagValue("oid", oid));
retval.append(" ").append(XmlHandler.addTagValue("comstring", comString));
retval.append(" ").append(XmlHandler.addTagValue("message", message));
retval.append(" ").append(XmlHandler.addTagValue("timeout", timeout));
retval.append(" ").append(XmlHandler.addTagValue("nrretry", nrretry));
retval.append(" ").append(XmlHandler.addTagValue("targettype", targettype));
retval.append(" ").append(XmlHandler.addTagValue("user", user));
retval
.append(" ")
.append(
XmlHandler.addTagValue(
"passphrase", Encr.encryptPasswordIfNotUsingVariables(passphrase)));
retval.append(" ").append(XmlHandler.addTagValue("engineid", engineid));
return retval.toString();
}

@Override
public void loadXml(Node entrynode, IHopMetadataProvider metadataProvider, IVariables variables)
throws HopXmlException {
try {
super.loadXml(entrynode);
port = XmlHandler.getTagValue(entrynode, "port");
serverName = XmlHandler.getTagValue(entrynode, "servername");
oid = XmlHandler.getTagValue(entrynode, "oid");
message = XmlHandler.getTagValue(entrynode, "message");
comString = XmlHandler.getTagValue(entrynode, "comstring");
timeout = XmlHandler.getTagValue(entrynode, "timeout");
nrretry = XmlHandler.getTagValue(entrynode, "nrretry");
targettype = XmlHandler.getTagValue(entrynode, "targettype");
user = XmlHandler.getTagValue(entrynode, "user");
passphrase =
Encr.decryptPasswordOptionallyEncrypted(XmlHandler.getTagValue(entrynode, "passphrase"));
engineid = XmlHandler.getTagValue(entrynode, "engineid");

} catch (HopXmlException xe) {
throw new HopXmlException("Unable to load action of type 'SNMPTrap' from XML node", xe);
}
}

/** @return Returns the serverName. */
public String getServerName() {
return serverName;
Expand All @@ -199,12 +159,12 @@ public void setServerName(String serverName) {
}

/** @return Returns the OID. */
public String getOID() {
public String getOid() {
return oid;
}

/** @param oid The oid to set. */
public void setOID(String oid) {
public void setOid(String oid) {
this.oid = oid;
}

Expand All @@ -228,31 +188,31 @@ public String getUser() {
return user;
}

/** @param user The passphrase to set. */
public void setPassPhrase(String passphrase) {
/** @param passphrase The passphrase to set. */
public void setPassphrase(String passphrase) {
this.passphrase = passphrase;
}

/** @return Returns the passphrase. */
public String getPassPhrase() {
public String getPassphrase() {
return passphrase;
}

/** @param user The engineid to set. */
public void setEngineID(String engineid) {
/** @param engineid The engineid to set. */
public void setEngineid(String engineid) {
this.engineid = engineid;
}

/** @return Returns the engineid. */
public String getEngineID() {
public String getEngineid() {
return engineid;
}

public String getTargetType() {
public String getTargettype() {
return targettype;
}

public void setTargetType(String targettypein) {
public void setTargettype(String targettypein) {
this.targettype = getTargetTypeCode(targettypein);
}

Expand All @@ -261,7 +221,7 @@ public void setMessage(String message) {
this.message = message;
}

/** @return Returns the comString. */
/** @return Returns the message. */
public String getMessage() {
return message;
}
Expand All @@ -287,12 +247,12 @@ public String getTimeout() {
}

/** @param nrretry The nrretry to set. */
public void setRetry(String nrretry) {
public void setNrretry(String nrretry) {
this.nrretry = nrretry;
}

/** @return Returns the nrretry. */
public String getRetry() {
public String getNrretry() {
return nrretry;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,15 +500,15 @@ public void getData() {
wName.setText(Const.nullToEmpty(action.getName()));
wServerName.setText(Const.NVL(action.getServerName(), ""));
wPort.setText(action.getPort());
wOID.setText(Const.NVL(action.getOID(), ""));
wOID.setText(Const.NVL(action.getOid(), ""));
wTimeout.setText("" + action.getTimeout());
wRetry.setText("" + action.getRetry());
wRetry.setText("" + action.getNrretry());
wComString.setText(Const.NVL(action.getComString(), ""));
wMessage.setText(Const.NVL(action.getMessage(), ""));
wTargetType.setText(action.getTargetTypeDesc(action.getTargetType()));
wTargetType.setText(action.getTargetTypeDesc(action.getTargettype()));
wUser.setText(Const.NVL(action.getUser(), ""));
wPassphrase.setText(Const.NVL(action.getPassPhrase(), ""));
wEngineID.setText(Const.NVL(action.getEngineID(), ""));
wPassphrase.setText(Const.NVL(action.getPassphrase(), ""));
wEngineID.setText(Const.NVL(action.getEngineid(), ""));

wName.selectAll();
wName.setFocus();
Expand All @@ -531,15 +531,15 @@ private void ok() {
action.setName(wName.getText());
action.setPort(wPort.getText());
action.setServerName(wServerName.getText());
action.setOID(wOID.getText());
action.setOid(wOID.getText());
action.setTimeout(wTimeout.getText());
action.setRetry(wTimeout.getText());
action.setNrretry(wTimeout.getText());
action.setComString(wComString.getText());
action.setMessage(wMessage.getText());
action.setTargetType(wTargetType.getText());
action.setTargettype(wTargetType.getText());
action.setUser(wUser.getText());
action.setPassPhrase(wPassphrase.getText());
action.setEngineID(wEngineID.getText());
action.setPassphrase(wPassphrase.getText());
action.setEngineid(wEngineID.getText());
dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.apache.hop.workflow.actions.snmptrap;

import org.apache.hop.core.HopClientEnvironment;
import org.apache.hop.core.logging.HopLogStore;
import org.apache.hop.workflow.WorkflowMeta;
import org.apache.hop.workflow.action.ActionMeta;
import org.apache.hop.workflow.action.ActionSerializationTestUtil;
import org.apache.hop.workflow.engine.IWorkflowEngine;
import org.apache.hop.workflow.engines.local.LocalWorkflowEngine;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class ActionSNMPTrapTest {

private IWorkflowEngine<WorkflowMeta> workflow;
private ActionSNMPTrap action;

@BeforeClass
public static void setUpBeforeClass() {
HopLogStore.init();
}

@AfterClass
public static void tearDownAfterClass() {}

@Before
public void setUp() throws Exception {
workflow = new LocalWorkflowEngine(new WorkflowMeta());
action = new ActionSNMPTrap();
workflow.getWorkflowMeta().addAction(new ActionMeta(action));
action.setParentWorkflow(workflow);
workflow.setStopped(false);
}

@Test
public void testSerialization() throws Exception {
HopClientEnvironment.init();

ActionSNMPTrap action =
ActionSerializationTestUtil. testSerialization(
"/snmp-trap-action.xml", ActionSNMPTrap.class);

assertEquals("awesomeserver", action.getServerName());
assertEquals("5000", action.getTimeout());
assertEquals("162", action.getPort());
assertEquals("2", action.getNrretry());
assertEquals("13131", action.getOid());
assertEquals("averynicemessage", action.getMessage());
assertEquals("community", action.getTargettype());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ protected Map<String, String> createGettersMap() {
"serverName", "getServerName",
"port", "getPort",
"timeout", "getTimeout",
"nrretry", "getRetry",
"nrretry", "getNrretry",
"comString", "getComString",
"message", "getMessage",
"oid", "getOID",
"targettype", "getTargetType",
"oid", "getOid",
"targettype", "getTargettype",
"user", "getUser",
"passphrase", "getPassPhrase",
"engineid", "getEngineID");
"passphrase", "getPassphrase",
"engineid", "getEngineid");
}

@Override
Expand All @@ -86,13 +86,13 @@ protected Map<String, String> createSettersMap() {
"serverName", "setServerName",
"port", "setPort",
"timeout", "setTimeout",
"nrretry", "setRetry",
"nrretry", "setNrretry",
"comString", "setComString",
"message", "setMessage",
"oid", "setOID",
"targettype", "setTargetType",
"oid", "setOid",
"targettype", "setTargettype",
"user", "setUser",
"passphrase", "setPassPhrase",
"engineid", "setEngineID");
"passphrase", "setPassphrase",
"engineid", "setEngineid");
}
}
41 changes: 41 additions & 0 deletions plugins/actions/snmptrap/src/test/resources/snmp-trap-action.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more
~
contributor license agreements. See the NOTICE file distributed with ~
this
work for additional information regarding copyright ownership. ~ The
ASF
licenses this file to You under the Apache License, Version 2.0 ~ (the
"License"); you may not use this file except in compliance with ~ the License.
You may obtain a copy of the License at ~ ~
http://www.apache.org/licenses/LICENSE-2.0
~ ~ 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. ~ -->
<action>
<name>Send SNMP trap</name>
<description>a</description>
<type>SNMP_TRAP</type>
<attributes/>
<port>162</port>
<servername>awesomeserver</servername>
<oid>13131</oid>
<comstring>public</comstring>
<message>averynicemessage</message>
<timeout>5000</timeout>
<nrretry>2</nrretry>
<targettype>community</targettype>
<user/>
<passphrase>Encrypted </passphrase>
<engineid/>
<parallel>N</parallel>
<xloc>368</xloc>
<yloc>208</yloc>
<attributes_hac/>
</action>

0 comments on commit afdd386

Please sign in to comment.