All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.cardinal.impl.OdlCardinalGet Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
/*
 * Copyright © 2015 TCS and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.cardinal.impl;

import org.slf4j.LoggerFactory;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class OdlCardinalGet {
    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(OdlCardinalGet.class);

    /**
     * @param host
     * @param community
     * @param strOID
     * @return method to get the snmp string from the agent.
     */
    public String snmpGet(String host, String community, String strOID) {
        String strResponse = "";
        ResponseEvent response;
        Snmp snmp;
        try {
            OctetString community1 = new OctetString(community);
            host = host + "/" + "161";
            final Address tempHost = new UdpAddress(host);
            TransportMapping transport = new DefaultUdpTransportMapping();
            transport.listen();
            CommunityTarget comtarget = new CommunityTarget();
            comtarget.setCommunity(community1);
            comtarget.setVersion(SnmpConstants.version1);
            comtarget.setAddress(tempHost);
            comtarget.setRetries(2);
            comtarget.setTimeout(5000);
            PDU pdu = new PDU();
            pdu.add(new VariableBinding(new OID(strOID)));
            pdu.setType(PDU.GET);
            snmp = new Snmp(transport);
            response = snmp.get(pdu, comtarget);
            if (response != null) {
                if(response.getResponse()!=null){
                if (response.getResponse().getErrorStatusText().equalsIgnoreCase("Success")) {
                    PDU pduresponse = response.getResponse();
                    strResponse = pduresponse.getVariableBindings().firstElement().toString();
                    if (strResponse.contains("=")) {
                        int len = strResponse.indexOf("=");
                        strResponse = strResponse.substring(len + 1, strResponse.length());
                    }
                }

                }else{
                    LOG.info("Getting null in response.getResponse...To handle later ");
                }
            } else {
                LOG.info("Looks like a TimeOut occured ");
            }
            snmp.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return strResponse;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy