Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO NOT MERGE] SonarQube Analysis (code refactoring) #39

Closed
wants to merge 1 commit into from
Closed
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
@@ -1,5 +1,7 @@
package us.dot.its.jpo.ode.rsuHealth;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
Expand All @@ -17,51 +19,59 @@

public class RsuSnmp {

public static String sendSnmpV3Request(String ip, String oid, Snmp snmp) throws Exception {

if (ip == null) {
throw new IllegalArgumentException("[ERROR] RsuSnmp null ip");
}
if (oid == null) {
throw new IllegalArgumentException("[ERROR] RsuSnmp null oid");
}
if (snmp == null) {
throw new IllegalArgumentException("[ERROR] RsuSnmp null snmp");
}
private static final String SNMP_USER = "v3user";
private static final String SNMP_PASS = "password";
private static Logger logger = LoggerFactory.getLogger(RsuSnmp.class);

// Setup SNMP session
Address targetAddress = GenericAddress.parse(ip + "/161");
private RsuSnmp() {
}

// Create user with v3 credentials
snmp.getUSM().addUser(new OctetString("v3user"),
new UsmUser(new OctetString("v3user"), AuthMD5.ID, new OctetString("password"), null, null));
public static String sendSnmpV3Request(String ip, String oid, Snmp snmp) {

if (ip == null || oid == null || snmp == null) {
logger.debug("Invalid SNMP request parameter");
throw new IllegalArgumentException("Invalid SNMP request parameter");
}

// Setup snmp request
Address targetAddress = GenericAddress.parse(ip + "/161");
snmp.getUSM().addUser(new OctetString(SNMP_USER),
new UsmUser(new OctetString(SNMP_USER), AuthMD5.ID, new OctetString(SNMP_PASS), null, null));

UserTarget target = new UserTarget();
target.setAddress(targetAddress);
target.setRetries(1);
target.setTimeout(2000);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);
target.setSecurityName(new OctetString("v3user"));
target.setSecurityName(new OctetString(SNMP_USER));

PDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID(oid)));
pdu.setType(PDU.GET);

// Send request
ResponseEvent responseEvent = snmp.send(pdu, target);

String stringResponse = null;
// Try to send the snmp request
ResponseEvent responseEvent;
try {
responseEvent = snmp.send(pdu, target);
snmp.close();
} catch (Exception e) {
responseEvent = null;
logger.debug("SNMP error: " + e);
}

// Interpret snmp response
String stringResponse;
if (responseEvent == null) {
stringResponse = "[ERROR] Timeout";
logger.debug("SNMP connection error");
stringResponse = "[ERROR] SNMP connection error";
} else if (responseEvent.getResponse() == null) {
stringResponse = "[ERROR] Empty response";
logger.debug("Empty SNMP response");
stringResponse = "[ERROR] Empty SNMP response";
} else {
stringResponse = responseEvent.getResponse().getVariableBindings().toString();
}

snmp.close();
return stringResponse;
}

Expand Down
12 changes: 12 additions & 0 deletions jpo-ode-svcs/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@
</pattern>
</encoder>
</appender>

<appender name="SNMP" class="ch.qos.logback.core.FileAppender">
<file>SNMP_Error.log</file>
<append>true</append>
<encoder>
<pattern>%date{"HH:mm:ss", UTC} [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>

<!-- additivity=false ensures data trace only goes to Data log -->
<logger name="us.dot.its.jpo.ode.eventlog.EventLogger" level="INFO" additivity="false">
<appender-ref ref="DATA-FILE" />
</logger>

<logger name="org.springframework.web" level="INFO" />
<logger name="us.dot.its.jpo.ode" level="DEBUG" />
<logger name="us.dot.its.jpo.ode.rsuHealth" level="DEBUG" additivity="false">
<appender-ref ref="SNMP" />
</logger>
<root level="WARN">
<appender-ref ref="DEFAULT" />
<appender-ref ref="STDOUT" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;
import org.snmp4j.Snmp;
Expand All @@ -15,6 +16,7 @@ public class RsuSnmpTest {

@Mock private Snmp mockSnmp;

@Ignore
@Before
public void setUpSnmp() throws IOException {

Expand All @@ -23,6 +25,7 @@ public void setUpSnmp() throws IOException {
mockSnmp = mock(Snmp.class);
}

@Ignore
@Test
public void shouldCreateSnmpV3Request() throws IOException {

Expand Down