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; |
Laszlo Papp | 5fd6a4f | 2018-06-11 15:36:50 +0100 | [diff] [blame^] | 31 | import org.snmp4j.smi.Variable; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 32 | import org.snmp4j.smi.VariableBinding; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 33 | import org.snmp4j.util.DefaultPDUFactory; |
| 34 | import org.snmp4j.util.TableEvent; |
| 35 | import org.snmp4j.util.TableUtils; |
| 36 | |
| 37 | import org.slf4j.Logger; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 38 | |
| 39 | import java.io.IOException; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 44 | import static org.slf4j.LoggerFactory.getLogger; |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * SNP utility for Polatis SNMP drivers. |
| 48 | */ |
| 49 | public final class PolatisSnmpUtility { |
| 50 | |
| 51 | private static final int MAX_SIZE_RESPONSE_PDU = 65535; |
| 52 | |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 53 | private static final Logger log = getLogger(PolatisSnmpUtility.class); |
| 54 | |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 55 | private PolatisSnmpUtility() { |
| 56 | } |
| 57 | |
| 58 | private static SnmpDevice getDevice(DriverHandler handler) { |
| 59 | SnmpController controller = checkNotNull(handler.get(SnmpController.class)); |
| 60 | SnmpDevice device = controller.getDevice(handler.data().deviceId()); |
| 61 | return device; |
| 62 | } |
| 63 | |
| 64 | private static CommunityTarget getTarget(DriverHandler handler) { |
| 65 | SnmpDevice device = getDevice(handler); |
| 66 | Address targetAddress = GenericAddress.parse(device.getProtocol() + |
| 67 | ":" + device.getSnmpHost() + |
| 68 | "/" + device.getSnmpPort()); |
| 69 | CommunityTarget target = new CommunityTarget(); |
| 70 | target.setCommunity(new OctetString(getDevice(handler).getCommunity())); |
| 71 | target.setAddress(targetAddress); |
| 72 | target.setRetries(3); |
| 73 | target.setTimeout(1000L * 3L); |
| 74 | target.setVersion(SnmpConstants.version2c); |
| 75 | target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU); |
| 76 | return target; |
| 77 | } |
| 78 | |
| 79 | private static Snmp getSession(DriverHandler handler) { |
| 80 | Snmp session = getDevice(handler).getSession(); |
| 81 | return session; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Sends a SET request. |
| 86 | * |
| 87 | * @param handler parent driver handler |
| 88 | * @param pdu SNMP protocol data unit |
| 89 | * @return the response event |
| 90 | * @throws IOException if unable to send a set request |
| 91 | */ |
| 92 | public static ResponseEvent set(DriverHandler handler, PDU pdu) throws IOException { |
| 93 | Snmp session = getSession(handler); |
| 94 | CommunityTarget target = getTarget(handler); |
| 95 | return session.set(pdu, target); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Retrieves static object value. |
| 100 | * |
| 101 | * @param handler parent driver handler |
| 102 | * @param oid object identifier |
Laszlo Papp | 5fd6a4f | 2018-06-11 15:36:50 +0100 | [diff] [blame^] | 103 | * @return the variable |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 104 | * @throws IOException if unable to retrieve the object value |
| 105 | */ |
Laszlo Papp | 5fd6a4f | 2018-06-11 15:36:50 +0100 | [diff] [blame^] | 106 | public static Variable get(DriverHandler handler, String oid) throws IOException { |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 107 | List<VariableBinding> vbs = new ArrayList<>(); |
| 108 | vbs.add(new VariableBinding(new OID(oid))); |
| 109 | PDU pdu = new PDU(PDU.GET, vbs); |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 110 | Snmp session = getSession(handler); |
| 111 | CommunityTarget target = getTarget(handler); |
| 112 | ResponseEvent event = session.send(pdu, target); |
Laszlo Papp | 5fd6a4f | 2018-06-11 15:36:50 +0100 | [diff] [blame^] | 113 | return event.getResponse().get(0).getVariable(); |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 114 | } |
Laszlo Papp | 18efc97 | 2018-06-07 17:47:58 +0100 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Retrieves a table. |
| 118 | * |
| 119 | * @param handler parent driver handler |
| 120 | * @param columnOIDs column oid object identifiers |
| 121 | * @return the table |
| 122 | * @throws IOException if unable to retrieve the object value |
| 123 | */ |
| 124 | public static List<TableEvent> getTable(DriverHandler handler, OID[] columnOIDs) throws IOException { |
| 125 | Snmp session = getSession(handler); |
| 126 | CommunityTarget target = getTarget(handler); |
| 127 | TableUtils tableUtils = new TableUtils(session, new DefaultPDUFactory()); |
| 128 | return tableUtils.getTable(target, columnOIDs, null, null); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Sends a synchronous SET request to the supplied target. |
| 133 | * |
| 134 | * @param handler parent driver handler |
| 135 | * @param vbs a list of variable bindings |
| 136 | * @return the response event |
| 137 | * @throws IOException if unable to set the target |
| 138 | */ |
| 139 | public static ResponseEvent set(DriverHandler handler, List<? extends VariableBinding> vbs) throws IOException { |
| 140 | Snmp session = getSession(handler); |
| 141 | CommunityTarget target = getTarget(handler); |
| 142 | PDU pdu = new PDU(PDU.SET, vbs); |
| 143 | return session.set(pdu, target); |
| 144 | } |
Laszlo Papp | 504510d | 2018-05-22 15:38:24 +0100 | [diff] [blame] | 145 | } |