Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.drivers.polatis.snmp; |
| 17 | |
| 18 | import org.onosproject.net.driver.DriverHandler; |
| 19 | import org.onosproject.snmp.SnmpDevice; |
| 20 | import org.onosproject.snmp.SnmpController; |
| 21 | |
| 22 | import org.snmp4j.CommunityTarget; |
| 23 | import org.snmp4j.PDU; |
| 24 | import org.snmp4j.Snmp; |
| 25 | import org.snmp4j.event.ResponseEvent; |
| 26 | import org.snmp4j.mp.SnmpConstants; |
| 27 | import org.snmp4j.smi.Address; |
| 28 | import org.snmp4j.smi.GenericAddress; |
| 29 | import org.snmp4j.smi.OctetString; |
| 30 | import org.snmp4j.smi.OID; |
| 31 | import org.snmp4j.smi.VariableBinding; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 32 | import org.snmp4j.util.DefaultPDUFactory; |
| 33 | import org.snmp4j.util.TableEvent; |
| 34 | import org.snmp4j.util.TableUtils; |
| 35 | |
| 36 | import org.slf4j.Logger; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 37 | |
| 38 | import java.io.IOException; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 41 | |
| 42 | import static com.google.common.base.Preconditions.checkNotNull; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * SNP utility for Polatis SNMP drivers. |
| 47 | */ |
| 48 | public final class PolatisSnmpUtility { |
| 49 | |
| 50 | private static final int MAX_SIZE_RESPONSE_PDU = 65535; |
| 51 | |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 52 | private static final Logger log = getLogger(PolatisSnmpUtility.class); |
| 53 | |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 54 | private PolatisSnmpUtility() { |
| 55 | } |
| 56 | |
| 57 | private static SnmpDevice getDevice(DriverHandler handler) { |
| 58 | SnmpController controller = checkNotNull(handler.get(SnmpController.class)); |
| 59 | SnmpDevice device = controller.getDevice(handler.data().deviceId()); |
| 60 | return device; |
| 61 | } |
| 62 | |
| 63 | private static CommunityTarget getTarget(DriverHandler handler) { |
| 64 | SnmpDevice device = getDevice(handler); |
| 65 | Address targetAddress = GenericAddress.parse(device.getProtocol() + |
| 66 | ":" + device.getSnmpHost() + |
| 67 | "/" + device.getSnmpPort()); |
| 68 | CommunityTarget target = new CommunityTarget(); |
| 69 | target.setCommunity(new OctetString(getDevice(handler).getCommunity())); |
| 70 | target.setAddress(targetAddress); |
| 71 | target.setRetries(3); |
| 72 | target.setTimeout(1000L * 3L); |
| 73 | target.setVersion(SnmpConstants.version2c); |
| 74 | target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU); |
| 75 | return target; |
| 76 | } |
| 77 | |
| 78 | private static Snmp getSession(DriverHandler handler) { |
| 79 | Snmp session = getDevice(handler).getSession(); |
| 80 | return session; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Sends a SET request. |
| 85 | * |
| 86 | * @param handler parent driver handler |
| 87 | * @param pdu SNMP protocol data unit |
| 88 | * @return the response event |
| 89 | * @throws IOException if unable to send a set request |
| 90 | */ |
| 91 | public static ResponseEvent set(DriverHandler handler, PDU pdu) throws IOException { |
| 92 | Snmp session = getSession(handler); |
| 93 | CommunityTarget target = getTarget(handler); |
| 94 | return session.set(pdu, target); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Retrieves static object value. |
| 99 | * |
| 100 | * @param handler parent driver handler |
| 101 | * @param oid object identifier |
| 102 | * @return the string value |
| 103 | * @throws IOException if unable to retrieve the object value |
| 104 | */ |
| 105 | public static String getOid(DriverHandler handler, String oid) throws IOException { |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 106 | List<VariableBinding> vbs = new ArrayList<>(); |
| 107 | vbs.add(new VariableBinding(new OID(oid))); |
| 108 | PDU pdu = new PDU(PDU.GET, vbs); |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 109 | Snmp session = getSession(handler); |
| 110 | CommunityTarget target = getTarget(handler); |
| 111 | ResponseEvent event = session.send(pdu, target); |
| 112 | return event.getResponse().get(0).getVariable().toString(); |
| 113 | } |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame^] | 114 | |
| 115 | /** |
| 116 | * Retrieves a table. |
| 117 | * |
| 118 | * @param handler parent driver handler |
| 119 | * @param columnOIDs column oid object identifiers |
| 120 | * @return the table |
| 121 | * @throws IOException if unable to retrieve the object value |
| 122 | */ |
| 123 | public static List<TableEvent> getTable(DriverHandler handler, OID[] columnOIDs) throws IOException { |
| 124 | Snmp session = getSession(handler); |
| 125 | CommunityTarget target = getTarget(handler); |
| 126 | TableUtils tableUtils = new TableUtils(session, new DefaultPDUFactory()); |
| 127 | return tableUtils.getTable(target, columnOIDs, null, null); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sends a synchronous SET request to the supplied target. |
| 132 | * |
| 133 | * @param handler parent driver handler |
| 134 | * @param vbs a list of variable bindings |
| 135 | * @return the response event |
| 136 | * @throws IOException if unable to set the target |
| 137 | */ |
| 138 | public static ResponseEvent set(DriverHandler handler, List<? extends VariableBinding> vbs) throws IOException { |
| 139 | Snmp session = getSession(handler); |
| 140 | CommunityTarget target = getTarget(handler); |
| 141 | PDU pdu = new PDU(PDU.SET, vbs); |
| 142 | return session.set(pdu, target); |
| 143 | } |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 144 | } |