/
MockSnmpStrategy.java
279 lines (235 loc) · 10.9 KB
/
MockSnmpStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.netmgt.snmp.mock;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.opennms.netmgt.snmp.CollectionTracker;
import org.opennms.netmgt.snmp.InetAddrUtils;
import org.opennms.netmgt.snmp.SnmpAgentAddress;
import org.opennms.netmgt.snmp.SnmpAgentConfig;
import org.opennms.netmgt.snmp.SnmpObjId;
import org.opennms.netmgt.snmp.SnmpStrategy;
import org.opennms.netmgt.snmp.SnmpTrapBuilder;
import org.opennms.netmgt.snmp.SnmpV1TrapBuilder;
import org.opennms.netmgt.snmp.SnmpV2TrapBuilder;
import org.opennms.netmgt.snmp.SnmpV3TrapBuilder;
import org.opennms.netmgt.snmp.SnmpV3User;
import org.opennms.netmgt.snmp.SnmpValue;
import org.opennms.netmgt.snmp.SnmpValueFactory;
import org.opennms.netmgt.snmp.SnmpWalker;
import org.opennms.netmgt.snmp.TrapNotificationListener;
import org.opennms.netmgt.snmp.TrapProcessorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
public class MockSnmpStrategy implements SnmpStrategy {
private static final transient Logger LOG = LoggerFactory.getLogger(MockSnmpStrategy.class);
public static final SnmpAgentAddress ALL_AGENTS = new SnmpAgentAddress(InetAddrUtils.addr("0.0.0.0"), 161);
private static final SnmpValue[] EMPTY_SNMP_VALUE_ARRAY = new SnmpValue[0];
// TOG's enterprise ID
private static int s_enterpriseId = 5813;
private static Map<SnmpAgentAddress,PropertyOidContainer> m_loaders = new HashMap<SnmpAgentAddress,PropertyOidContainer>();
public MockSnmpStrategy() {
}
protected PropertyOidContainer getOidContainer(final SnmpAgentConfig agentConfig) {
return getOidContainer(new SnmpAgentAddress(agentConfig.getAddress(), agentConfig.getPort()));
}
protected PropertyOidContainer getOidContainer(final SnmpAgentAddress aa) {
if (m_loaders.containsKey(aa)) {
return m_loaders.get(aa);
} else {
return m_loaders.get(ALL_AGENTS);
}
}
@Override
public SnmpWalker createWalker(final SnmpAgentConfig agentConfig, final String name, final CollectionTracker tracker) {
LOG.debug("createWalker({}/{}, {}, {})", InetAddrUtils.str(agentConfig.getAddress()), agentConfig.getPort(), name, tracker.getClass().getName());
final SnmpAgentAddress aa = new SnmpAgentAddress(agentConfig.getAddress(), agentConfig.getPort());
final PropertyOidContainer oidContainer = getOidContainer(aa);
return new MockSnmpWalker(aa, agentConfig.getVersion(), oidContainer, name, tracker, agentConfig.getMaxVarsPerPdu());
}
@Override
public SnmpValue set(final SnmpAgentConfig agentConfig, final SnmpObjId oid, final SnmpValue value) {
final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
if (oidContainer == null) return null;
return oidContainer.set(oid, value);
}
@Override
public SnmpValue[] set(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids, final SnmpValue[] values) {
final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
if (oidContainer == null) return new SnmpValue[values.length];
return oidContainer.set(oids, values);
}
@Override
public SnmpValue get(final SnmpAgentConfig agentConfig, final SnmpObjId oid) {
final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
if (oidContainer == null) return null;
SnmpValue val = oidContainer.findValueForOid(oid);
if (val.isNull()) {
return null;
}
return val;
}
@Override
public SnmpValue[] get(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids) {
final PropertyOidContainer container = getOidContainer(agentConfig);
if (container == null) return new SnmpValue[oids.length];
final List<SnmpValue> values = new ArrayList<SnmpValue>();
for (final SnmpObjId oid : oids) {
values.add(container.findValueForOid(oid));
}
return values.toArray(EMPTY_SNMP_VALUE_ARRAY);
}
@Override
public CompletableFuture<SnmpValue[]> getAsync(SnmpAgentConfig agentConfig, SnmpObjId[] oids) {
return CompletableFuture.completedFuture(get(agentConfig, oids));
}
@Override
public SnmpValue getNext(final SnmpAgentConfig agentConfig, final SnmpObjId oid) {
final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
if (oidContainer == null) return null;
return oidContainer.findNextValueForOid(oid);
}
@Override
public SnmpValue[] getNext(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids) {
final PropertyOidContainer oidContainer = getOidContainer(agentConfig);
if (oidContainer == null) return null;
final List<SnmpValue> values = new ArrayList<SnmpValue>();
for (final SnmpObjId oid : oids) {
values.add(oidContainer.findNextValueForOid(oid));
}
return values.toArray(EMPTY_SNMP_VALUE_ARRAY);
}
@Override
public SnmpValue[] getBulk(final SnmpAgentConfig agentConfig, final SnmpObjId[] oids) {
return getNext(agentConfig, oids);
}
@Override
public void registerForTraps(final TrapNotificationListener listener, final TrapProcessorFactory processorFactory, final InetAddress address, final int snmpTrapPort) throws IOException {
LOG.warn("Can't register for traps. No network in the MockSnmpStrategy!");
}
@Override
public void registerForTraps(final TrapNotificationListener listener, final TrapProcessorFactory processorFactory, final int snmpTrapPort) throws IOException {
LOG.warn("Can't register for traps. No network in the MockSnmpStrategy!");
}
@Override
public void registerForTraps(TrapNotificationListener listener, TrapProcessorFactory processorFactory, InetAddress address, int snmpTrapPort, List<SnmpV3User> snmpv3Users) throws IOException {
LOG.warn("Can't register for traps. No network in the MockSnmpStrategy!");
}
@Override
public void unregisterForTraps(final TrapNotificationListener listener, final InetAddress address, final int snmpTrapPort) throws IOException {
}
@Override
public void unregisterForTraps(final TrapNotificationListener listener, final int snmpTrapPort) throws IOException {
}
@Override
public SnmpValueFactory getValueFactory() {
return new MockSnmpValueFactory();
}
@Override
public SnmpV1TrapBuilder getV1TrapBuilder() {
throw new UnsupportedOperationException("Not yet implemented!");
// return new NullSnmpV1TrapBuilder();
}
@Override
public SnmpTrapBuilder getV2TrapBuilder() {
throw new UnsupportedOperationException("Not yet implemented!");
// return new NullSnmpTrapBuilder();
}
@Override
public SnmpV3TrapBuilder getV3TrapBuilder() {
throw new UnsupportedOperationException("Not yet implemented!");
// return new NullSnmpV3TrapBuilder();
}
@Override
public SnmpV2TrapBuilder getV2InformBuilder() {
throw new UnsupportedOperationException("Not yet implemented!");
// return new NullSnmpV2TrapBuilder();
}
@Override
public SnmpV3TrapBuilder getV3InformBuilder() {
throw new UnsupportedOperationException("Not yet implemented!");
// return new NullSnmpV3TrapBuilder();
}
@Override
public byte[] getLocalEngineID() {
// lovingly stolen from SNMP4J
final byte[] engineID = new byte[5];
engineID[0] = (byte) (0x80 | ((s_enterpriseId >> 24) & 0xFF));
engineID[1] = (byte) ((s_enterpriseId >> 16) & 0xFF);
engineID[2] = (byte) ((s_enterpriseId >> 8) & 0xFF);
engineID[3] = (byte) (s_enterpriseId & 0xFF);
final byte[] ip = InetAddrUtils.getLocalHostAddress().getAddress();
if (ip.length == 4) {
// IPv4
engineID[4] = 1;
} else if (ip.length == 16) {
// IPv6
engineID[4] = 2;
} else {
// Text
engineID[4] = 4;
}
final byte[] bytes = new byte[engineID.length+ip.length];
System.arraycopy(engineID, 0, bytes, 0, engineID.length);
System.arraycopy(ip, 0, bytes, engineID.length, ip.length);
return bytes;
}
public static void setDataForAddress(final SnmpAgentAddress agentAddress, final Resource resource) throws IOException {
m_loaders.put(agentAddress, new PropertyOidContainer(resource));
}
public static void updateIntValue(final SnmpAgentAddress agentAddress, String oid, int value) {
m_loaders.get(agentAddress).set(SnmpObjId.get(oid), new MockSnmpValueFactory().getInt32(value));
}
public static void updateStringValue(final SnmpAgentAddress agentAddress, String oid, String value) {
try {
m_loaders.get(agentAddress).set(SnmpObjId.get(oid), new MockSnmpValueFactory().getOctetString(value.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
// Should be impossible
}
}
public static void updateCounter32Value(final SnmpAgentAddress agentAddress, String oid, long value) {
m_loaders.get(agentAddress).set(SnmpObjId.get(oid), new MockSnmpValueFactory().getCounter32(value));
}
public static void updateCounter64Value(final SnmpAgentAddress agentAddress, String oid, BigInteger value) {
m_loaders.get(agentAddress).set(SnmpObjId.get(oid), new MockSnmpValueFactory().getCounter64(value));
}
public static void removeHost(final SnmpAgentAddress agentAddr) {
m_loaders.remove(agentAddr);
}
public static void resetData() {
m_loaders.clear();
}
}