blob: 654294f697c5ab265eaae40ebea36882e5126c4a [file] [log] [blame]
Laszlo Papp504510d2018-05-22 15:38:24 +01001/*
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 */
16package org.onosproject.drivers.polatis.snmp;
17
18import org.onosproject.net.driver.DriverHandler;
19import org.onosproject.snmp.SnmpDevice;
20import org.onosproject.snmp.SnmpController;
21
22import org.snmp4j.CommunityTarget;
23import org.snmp4j.PDU;
24import org.snmp4j.Snmp;
25import org.snmp4j.event.ResponseEvent;
26import org.snmp4j.mp.SnmpConstants;
27import org.snmp4j.smi.Address;
28import org.snmp4j.smi.GenericAddress;
29import org.snmp4j.smi.OctetString;
30import org.snmp4j.smi.OID;
31import org.snmp4j.smi.VariableBinding;
32
33import java.io.IOException;
34
35import static com.google.common.base.Preconditions.checkNotNull;
36
37/**
38 * SNP utility for Polatis SNMP drivers.
39 */
40public final class PolatisSnmpUtility {
41
42 private static final int MAX_SIZE_RESPONSE_PDU = 65535;
43
44 private PolatisSnmpUtility() {
45 }
46
47 private static SnmpDevice getDevice(DriverHandler handler) {
48 SnmpController controller = checkNotNull(handler.get(SnmpController.class));
49 SnmpDevice device = controller.getDevice(handler.data().deviceId());
50 return device;
51 }
52
53 private static CommunityTarget getTarget(DriverHandler handler) {
54 SnmpDevice device = getDevice(handler);
55 Address targetAddress = GenericAddress.parse(device.getProtocol() +
56 ":" + device.getSnmpHost() +
57 "/" + device.getSnmpPort());
58 CommunityTarget target = new CommunityTarget();
59 target.setCommunity(new OctetString(getDevice(handler).getCommunity()));
60 target.setAddress(targetAddress);
61 target.setRetries(3);
62 target.setTimeout(1000L * 3L);
63 target.setVersion(SnmpConstants.version2c);
64 target.setMaxSizeRequestPDU(MAX_SIZE_RESPONSE_PDU);
65 return target;
66 }
67
68 private static Snmp getSession(DriverHandler handler) {
69 Snmp session = getDevice(handler).getSession();
70 return session;
71 }
72
73 /**
74 * Sends a SET request.
75 *
76 * @param handler parent driver handler
77 * @param pdu SNMP protocol data unit
78 * @return the response event
79 * @throws IOException if unable to send a set request
80 */
81 public static ResponseEvent set(DriverHandler handler, PDU pdu) throws IOException {
82 Snmp session = getSession(handler);
83 CommunityTarget target = getTarget(handler);
84 return session.set(pdu, target);
85 }
86
87 /**
88 * Retrieves static object value.
89 *
90 * @param handler parent driver handler
91 * @param oid object identifier
92 * @return the string value
93 * @throws IOException if unable to retrieve the object value
94 */
95 public static String getOid(DriverHandler handler, String oid) throws IOException {
96 PDU pdu = new PDU();
97 pdu.add(new VariableBinding(new OID(oid)));
98 pdu.setType(PDU.GET);
99 Snmp session = getSession(handler);
100 CommunityTarget target = getTarget(handler);
101 ResponseEvent event = session.send(pdu, target);
102 return event.getResponse().get(0).getVariable().toString();
103 }
104}