blob: 50bc25492cfc514c2dacacd6898f4115edd38881 [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;
Laszlo Papp18efc972018-06-07 17:47:58 +010032import org.snmp4j.util.DefaultPDUFactory;
33import org.snmp4j.util.TableEvent;
34import org.snmp4j.util.TableUtils;
35
36import org.slf4j.Logger;
Laszlo Papp504510d2018-05-22 15:38:24 +010037
38import java.io.IOException;
Laszlo Papp18efc972018-06-07 17:47:58 +010039import java.util.ArrayList;
40import java.util.List;
Laszlo Papp504510d2018-05-22 15:38:24 +010041
42import static com.google.common.base.Preconditions.checkNotNull;
Laszlo Papp18efc972018-06-07 17:47:58 +010043import static org.slf4j.LoggerFactory.getLogger;
Laszlo Papp504510d2018-05-22 15:38:24 +010044
45/**
46 * SNP utility for Polatis SNMP drivers.
47 */
48public final class PolatisSnmpUtility {
49
50 private static final int MAX_SIZE_RESPONSE_PDU = 65535;
51
Laszlo Papp18efc972018-06-07 17:47:58 +010052 private static final Logger log = getLogger(PolatisSnmpUtility.class);
53
Laszlo Papp504510d2018-05-22 15:38:24 +010054 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 Papp18efc972018-06-07 17:47:58 +0100106 List<VariableBinding> vbs = new ArrayList<>();
107 vbs.add(new VariableBinding(new OID(oid)));
108 PDU pdu = new PDU(PDU.GET, vbs);
Laszlo Papp504510d2018-05-22 15:38:24 +0100109 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 Papp18efc972018-06-07 17:47:58 +0100114
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 Papp504510d2018-05-22 15:38:24 +0100144}