blob: 4102a075e0062ef0694ea7a5c4b619b4a8277323 [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;
Laszlo Papp5fd6a4f2018-06-11 15:36:50 +010031import org.snmp4j.smi.Variable;
Laszlo Papp504510d2018-05-22 15:38:24 +010032import org.snmp4j.smi.VariableBinding;
Laszlo Papp18efc972018-06-07 17:47:58 +010033import org.snmp4j.util.DefaultPDUFactory;
34import org.snmp4j.util.TableEvent;
35import org.snmp4j.util.TableUtils;
36
37import org.slf4j.Logger;
Laszlo Papp504510d2018-05-22 15:38:24 +010038
39import java.io.IOException;
Laszlo Papp18efc972018-06-07 17:47:58 +010040import java.util.ArrayList;
41import java.util.List;
Laszlo Papp504510d2018-05-22 15:38:24 +010042
43import static com.google.common.base.Preconditions.checkNotNull;
Laszlo Papp18efc972018-06-07 17:47:58 +010044import static org.slf4j.LoggerFactory.getLogger;
Laszlo Papp504510d2018-05-22 15:38:24 +010045
46/**
47 * SNP utility for Polatis SNMP drivers.
48 */
49public final class PolatisSnmpUtility {
50
51 private static final int MAX_SIZE_RESPONSE_PDU = 65535;
52
Laszlo Papp18efc972018-06-07 17:47:58 +010053 private static final Logger log = getLogger(PolatisSnmpUtility.class);
54
Laszlo Papp504510d2018-05-22 15:38:24 +010055 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 Papp5fd6a4f2018-06-11 15:36:50 +0100103 * @return the variable
Laszlo Papp504510d2018-05-22 15:38:24 +0100104 * @throws IOException if unable to retrieve the object value
105 */
Laszlo Papp5fd6a4f2018-06-11 15:36:50 +0100106 public static Variable get(DriverHandler handler, String oid) throws IOException {
Laszlo Papp18efc972018-06-07 17:47:58 +0100107 List<VariableBinding> vbs = new ArrayList<>();
108 vbs.add(new VariableBinding(new OID(oid)));
109 PDU pdu = new PDU(PDU.GET, vbs);
Laszlo Papp504510d2018-05-22 15:38:24 +0100110 Snmp session = getSession(handler);
111 CommunityTarget target = getTarget(handler);
112 ResponseEvent event = session.send(pdu, target);
Laszlo Papp5fd6a4f2018-06-11 15:36:50 +0100113 return event.getResponse().get(0).getVariable();
Laszlo Papp504510d2018-05-22 15:38:24 +0100114 }
Laszlo Papp18efc972018-06-07 17:47:58 +0100115
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 Papp504510d2018-05-22 15:38:24 +0100145}