Search in sources :

Example 1 with SnmpInfo

use of org.opennms.web.svclayer.model.SnmpInfo in project opennms by OpenNMS.

the class SnmpConfigRestService method getSnmpInfo.

/**
     * <p>getSnmpInfo</p>
     *
     * @param ipAddr a {@link java.lang.String} object.
     * @return a {@link org.opennms.web.snmpinfo.SnmpInfo} object.
     */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML })
@Path("{ipAddr}")
public SnmpInfo getSnmpInfo(@PathParam("ipAddr") String ipAddr, @QueryParam("location") String location) {
    final InetAddress addr = InetAddressUtils.addr(ipAddr);
    if (addr == null) {
        throw getException(Status.BAD_REQUEST, "Malformed IP Address: {}.", ipAddr);
    }
    final SnmpAgentConfig config = m_accessService.getAgentConfig(addr, location);
    return new SnmpInfo(config);
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo) SnmpAgentConfig(org.opennms.netmgt.snmp.SnmpAgentConfig) InetAddress(java.net.InetAddress) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with SnmpInfo

use of org.opennms.web.svclayer.model.SnmpInfo in project opennms by OpenNMS.

the class SnmpConfigServlet method createFromRequest.

/**
	 * Creates an {@link SnmpInfo} object from the given request.
	 * @param request The http request.
	 * @return The object parsed from the http request.
	 */
private SnmpInfo createFromRequest(HttpServletRequest request) {
    SnmpInfo snmpInfo = new SnmpInfo();
    // general parameters
    String version = request.getParameter("version");
    String timeout = request.getParameter("timeout");
    String retryCount = request.getParameter("retryCount");
    String port = request.getParameter("port");
    String maxRequestSize = request.getParameter("maxRequestSize");
    String maxVarsPerPdu = request.getParameter("maxVarsPerPdu");
    String maxRepetitions = request.getParameter("maxRepetitions");
    String proxyHost = request.getParameter("proxyHost");
    String location = request.getParameter("location");
    // v1/v2c specifics
    String readCommunityString = request.getParameter("readCommunityString");
    String writeCommunityString = request.getParameter("writeCommunityString");
    // v3 specifics
    String securityName = request.getParameter("securityName");
    String securityLevel = request.getParameter("securityLevel");
    String authPassPhrase = request.getParameter("authPassPhrase");
    String authProtocol = request.getParameter("authProtocol");
    String privPassPhrase = request.getParameter("privPassPhrase");
    String privProtocol = request.getParameter("privProtocol");
    String engineId = request.getParameter("engineId");
    String contextEngineId = request.getParameter("contextEngineId");
    String contextName = request.getParameter("contextName");
    String enterpriseId = request.getParameter("enterpriseId");
    // save in snmpInfo
    if (!Strings.isNullOrEmpty(authPassPhrase))
        snmpInfo.setAuthPassPhrase(authPassPhrase);
    if (!Strings.isNullOrEmpty(authProtocol))
        snmpInfo.setAuthProtocol(authProtocol);
    if (!Strings.isNullOrEmpty(contextEngineId))
        snmpInfo.setContextEngineId(contextEngineId);
    if (!Strings.isNullOrEmpty(contextName))
        snmpInfo.setContextName(contextName);
    if (!Strings.isNullOrEmpty(engineId))
        snmpInfo.setEngineId(engineId);
    if (!Strings.isNullOrEmpty(enterpriseId))
        snmpInfo.setEnterpriseId(enterpriseId);
    if (!Strings.isNullOrEmpty(maxRepetitions))
        snmpInfo.setMaxRepetitions(Integer.parseInt(maxRepetitions));
    if (!Strings.isNullOrEmpty(maxRequestSize))
        snmpInfo.setMaxRequestSize(Integer.parseInt(maxRequestSize));
    if (!Strings.isNullOrEmpty(maxVarsPerPdu))
        snmpInfo.setMaxVarsPerPdu(Integer.parseInt(maxVarsPerPdu));
    if (!Strings.isNullOrEmpty(port))
        snmpInfo.setPort(Integer.parseInt(port));
    if (!Strings.isNullOrEmpty(privPassPhrase))
        snmpInfo.setPrivPassPhrase(privPassPhrase);
    if (!Strings.isNullOrEmpty(privProtocol))
        snmpInfo.setPrivProtocol(privProtocol);
    if (!Strings.isNullOrEmpty(proxyHost))
        snmpInfo.setProxyHost(proxyHost);
    if (!Strings.isNullOrEmpty(readCommunityString))
        snmpInfo.setReadCommunity(readCommunityString);
    if (!Strings.isNullOrEmpty(retryCount))
        snmpInfo.setRetries(Integer.parseInt(retryCount));
    if (!Strings.isNullOrEmpty(securityLevel))
        snmpInfo.setSecurityLevel(Integer.parseInt(securityLevel));
    if (!Strings.isNullOrEmpty(securityName))
        snmpInfo.setSecurityName(securityName);
    if (!Strings.isNullOrEmpty(timeout))
        snmpInfo.setTimeout(Integer.parseInt(timeout));
    if (!Strings.isNullOrEmpty(version))
        snmpInfo.setVersion(version);
    if (!Strings.isNullOrEmpty(writeCommunityString))
        snmpInfo.setWriteCommunity(writeCommunityString);
    if (!Strings.isNullOrEmpty(location))
        snmpInfo.setLocation(location);
    return snmpInfo;
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo)

Example 3 with SnmpInfo

use of org.opennms.web.svclayer.model.SnmpInfo in project opennms by OpenNMS.

the class SnmpConfigRestServiceIT method testSetRanges.

@Test
public void testSetRanges() throws Exception {
    String url = "/snmpConfig/1.1.1.1";
    String urlRange = "/snmpConfig/1.1.1.1-2.2.2.2";
    // Testing GET Collection
    SnmpInfo changedConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class);
    SnmpInfo expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.1.1.1");
    // check if expected defaults matches actual defaults
    assertConfiguration(expectedConfig, changedConfig);
    // change values
    changedConfig.setAuthPassPhrase("authPassPhrase");
    changedConfig.setAuthProtocol("authProtocol");
    changedConfig.setReadCommunity("readCommunity");
    changedConfig.setWriteCommunity("writeCommunity");
    changedConfig.setContextEngineId("contextEngineId");
    changedConfig.setContextName("contextName");
    changedConfig.setEngineId("engineId");
    changedConfig.setEnterpriseId("enterpriseId");
    changedConfig.setMaxRepetitions(1000);
    changedConfig.setMaxVarsPerPdu(2000);
    changedConfig.setPort(3000);
    changedConfig.setProxyHost("127.0.0.1");
    changedConfig.setPrivPassPhrase("privPassPhrase");
    changedConfig.setPrivProtocol("privProtocol");
    changedConfig.setRetries(4000);
    changedConfig.setSecurityLevel(5000);
    changedConfig.setSecurityName("securityName");
    changedConfig.setTimeout(6000);
    changedConfig.setVersion("v3");
    changedConfig.setMaxRequestSize(7000);
    // store them via REST
    putXmlObject(m_jaxbContext, urlRange, 204, changedConfig);
    // prepare expected Result
    expectedConfig = new SnmpInfo();
    expectedConfig.setAuthPassPhrase("authPassPhrase");
    expectedConfig.setAuthProtocol("authProtocol");
    expectedConfig.setContextEngineId("contextEngineId");
    expectedConfig.setContextName("contextName");
    expectedConfig.setEngineId("engineId");
    expectedConfig.setEnterpriseId("enterpriseId");
    expectedConfig.setMaxRepetitions(1000);
    expectedConfig.setMaxVarsPerPdu(2000);
    expectedConfig.setPort(3000);
    expectedConfig.setProxyHost("127.0.0.1");
    expectedConfig.setPrivPassPhrase("privPassPhrase");
    expectedConfig.setPrivProtocol("privProtocol");
    expectedConfig.setRetries(4000);
    expectedConfig.setSecurityLevel(5000);
    expectedConfig.setSecurityName("securityName");
    expectedConfig.setTimeout(6000);
    expectedConfig.setVersion("v3");
    expectedConfig.setMaxRequestSize(7000);
    expectedConfig.setReadCommunity(null);
    expectedConfig.setWriteCommunity(null);
    // read via REST
    SnmpInfo newConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class);
    // check ...
    // ... if changes were made
    assertConfiguration(expectedConfig, newConfig);
    SnmpInfo otherConfig = createSnmpInfoWithDefaultsForSnmpV3("3.3.3.3");
    assertFalse(newConfig.equals(otherConfig));
    expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.2.3.4");
    newConfig = getXmlObject(m_jaxbContext, "/snmpConfig/1.2.3.4", 200, SnmpInfo.class);
    assertEquals(expectedConfig, newConfig);
    dumpConfig();
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo) Test(org.junit.Test)

Example 4 with SnmpInfo

use of org.opennms.web.svclayer.model.SnmpInfo in project opennms by OpenNMS.

the class SnmpConfigRestServiceIT method testSetNewValueForSnmpV2c.

@Test
public void testSetNewValueForSnmpV2c() throws Exception {
    String url = "/snmpConfig/1.1.1.1";
    // Testing GET Collection
    SnmpInfo config = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class);
    SnmpInfo expectedConfig = createSnmpInfoWithDefaultsForSnmpV3("1.1.1.1");
    // check if expected defaults matches actual defaults
    assertConfiguration(expectedConfig, config);
    // change values
    config.setAuthPassPhrase("authPassPhrase");
    config.setAuthProtocol("authProtocol");
    config.setReadCommunity("readCommunity");
    config.setWriteCommunity("writeCommunity");
    config.setContextEngineId("contextEngineId");
    config.setContextName("contextName");
    config.setEngineId("engineId");
    config.setEnterpriseId("enterpriseId");
    config.setMaxRepetitions(1000);
    config.setMaxVarsPerPdu(2000);
    config.setPort(3000);
    config.setPrivPassPhrase("privPassPhrase");
    config.setPrivProtocol("privProtocol");
    config.setProxyHost("127.0.0.1");
    config.setRetries(4000);
    config.setSecurityLevel(5000);
    config.setSecurityName("securityName");
    config.setTimeout(6000);
    config.setVersion("v2c");
    config.setMaxRequestSize(7000);
    // store them via REST
    putXmlObject(m_jaxbContext, url, 204, config);
    // prepare expected Result
    expectedConfig = new SnmpInfo();
    expectedConfig.setMaxRepetitions(1000);
    expectedConfig.setMaxVarsPerPdu(2000);
    expectedConfig.setPort(3000);
    expectedConfig.setRetries(4000);
    expectedConfig.setTimeout(6000);
    expectedConfig.setVersion("v2c");
    expectedConfig.setMaxRequestSize(7000);
    expectedConfig.setReadCommunity("readCommunity");
    expectedConfig.setWriteCommunity("writeCommunity");
    expectedConfig.setAuthPassPhrase(null);
    expectedConfig.setAuthProtocol(null);
    expectedConfig.setContextEngineId(null);
    expectedConfig.setContextName(null);
    expectedConfig.setEngineId(null);
    expectedConfig.setEnterpriseId(null);
    expectedConfig.setPrivPassPhrase(null);
    expectedConfig.setPrivProtocol(null);
    expectedConfig.setProxyHost("127.0.0.1");
    expectedConfig.setSecurityLevel(null);
    expectedConfig.setSecurityName(null);
    // read via REST
    SnmpInfo newConfig = getXmlObject(m_jaxbContext, url, 200, SnmpInfo.class);
    // check ...
    // ... if Changes were made
    assertConfiguration(expectedConfig, newConfig);
    dumpConfig();
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo) Test(org.junit.Test)

Example 5 with SnmpInfo

use of org.opennms.web.svclayer.model.SnmpInfo in project opennms by OpenNMS.

the class SnmpConfigRestService method updateInterface.

/**
     * Updates a specific interface
     *
     * @param ipAddress a {@link java.lang.String} object.
     * @param params a {@link org.opennms.web.rest.support.MultivaluedMapImpl} object.
     * @return a {@link javax.ws.rs.core.Response} object.
     */
@PUT
@Path("{ipAddr}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Transactional
public Response updateInterface(@PathParam("ipAddr") final String ipAddress, final MultivaluedMapImpl params) {
    writeLock();
    try {
        final SnmpInfo info = new SnmpInfo();
        setProperties(params, info);
        final SnmpEventInfo eventInfo;
        if (ipAddress.contains("-")) {
            final String[] addrs = SnmpConfigRestService.getAddresses(ipAddress);
            eventInfo = info.createEventInfo(addrs[0], addrs[1]);
        } else {
            eventInfo = info.createEventInfo(ipAddress);
        }
        m_accessService.define(eventInfo);
        return Response.noContent().build();
    } catch (final Throwable e) {
        throw getException(Status.INTERNAL_SERVER_ERROR, "Can't update SNMP configuration for {} : {}", ipAddress, e.getMessage());
    } finally {
        writeUnlock();
    }
}
Also used : SnmpInfo(org.opennms.web.svclayer.model.SnmpInfo) SnmpEventInfo(org.opennms.netmgt.config.SnmpEventInfo) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

SnmpInfo (org.opennms.web.svclayer.model.SnmpInfo)10 Test (org.junit.Test)5 Path (javax.ws.rs.Path)2 SnmpEventInfo (org.opennms.netmgt.config.SnmpEventInfo)2 SnmpAgentConfig (org.opennms.netmgt.snmp.SnmpAgentConfig)2 InetAddress (java.net.InetAddress)1 RequestDispatcher (javax.servlet.RequestDispatcher)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Produces (javax.ws.rs.Produces)1 Transactional (org.springframework.transaction.annotation.Transactional)1