/
NodeDAO.ts
343 lines (301 loc) · 10.9 KB
/
NodeDAO.ts
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import {AbstractDAO} from './AbstractDAO';
import {Filter} from '../api/Filter';
import {IHasHTTP} from '../api/IHasHTTP';
import {IOnmsHTTP} from '../api/IOnmsHTTP';
import {OnmsError} from '../api/OnmsError';
import {Util} from '../internal/Util';
import {OnmsCategory} from '../model/OnmsCategory';
import {OnmsCollectType} from '../model/OnmsCollectType';
import {OnmsIpInterface} from '../model/OnmsIpInterface';
import {OnmsManagedType} from '../model/OnmsManagedType';
import {OnmsMonitoredService} from '../model/OnmsMonitoredService';
import {OnmsNode} from '../model/OnmsNode';
import {OnmsNodeLabelSource} from '../model/OnmsNodeLabelSource';
import {OnmsNodeType} from '../model/OnmsNodeType';
import {OnmsPrimaryType} from '../model/OnmsPrimaryType';
import {OnmsServiceType} from '../model/OnmsServiceType';
import {OnmsServiceStatusType} from '../model/OnmsServiceStatusType';
import {OnmsSnmpInterface} from '../model/OnmsSnmpInterface';
import {OnmsSnmpStatusType, SnmpStatusTypes} from '../model/OnmsSnmpStatusType';
import {PhysAddr} from '../model/PhysAddr';
import {log, catDao} from '../api/Log';
import {Category} from 'typescript-logging';
/** @hidden */
const cat = new Category('nodes', catDao);
/**
* Data access for [[OnmsNode]] objects.
* @module NodeDAO
*/
export class NodeDAO extends AbstractDAO<number, OnmsNode> {
constructor(impl: IHasHTTP | IOnmsHTTP) {
super(impl);
}
/**
* Get an node, given the node's ID.
*
* @param id - The node's ID.
* @param recurse - Optionally fetch all sub-model objects. (ipInterface, etc.)
*/
public async get(id: number, recurse = false): Promise<OnmsNode> {
return this.getOptions().then((opts) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + id, opts).then((result) => {
const node = this.fromData(result.data);
if (recurse) {
return this.fetch(node);
} else {
return node;
}
});
});
}
/** Search for nodes, given an optional filter. */
public async find(filter?: Filter): Promise<OnmsNode[]> {
return this.getOptions(filter).then((opts) => {
return this.http.get(this.pathToNodesEndpoint(), opts).then((result) => {
let data = result.data;
if (data !== null && this.getCount(data) > 0 && data.node) {
data = data.node;
} else {
data = [];
}
if (!Array.isArray(data)) {
if (data.id) {
data = [data];
} else {
throw new OnmsError('Expected an array of nodes but got "' + (typeof data) + '" instead.');
}
}
return data.map((nodeData) => {
return this.fromData(nodeData);
});
});
});
}
/** Given a node, fetch all the sub-model objects for that node. (ipInterfaces, snmpInterfaces, etc.) */
public async fetch(node: OnmsNode): Promise<OnmsNode> {
return this.snmpInterfaces(node).then((si) => {
node.snmpInterfaces = si;
si.forEach((iface) => {
iface.node = node;
});
return this.ipInterfaces(node).then((ifaces) => {
node.ipInterfaces = ifaces;
ifaces.forEach((iface) => {
iface.node = node;
});
return Promise.all(ifaces.map((iface) => {
return this.services(node, iface).then((services) => {
iface.services = services;
services.forEach((service) => {
service.node = node;
service.ipInterface = iface;
});
});
})).then(() => {
return node;
});
});
});
}
/** Given a node, get the IP interfaces for that node. */
public async ipInterfaces(node: number | OnmsNode, filter?: Filter): Promise<OnmsIpInterface[]> {
if (node instanceof OnmsNode) {
node = node.id;
}
return this.getOptions(filter).then((opts) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces', opts).then((result) => {
let data = result.data;
if (this.getCount(data) > 0 && data.ipInterface) {
data = data.ipInterface;
} else {
data = [];
}
if (!Array.isArray(data)) {
if (data.nodeId) {
data = [data];
} else {
throw new OnmsError('Expected an array of IP interfaces but got "' + (typeof data) + '" instead.');
}
}
return data.map((ifaceData) => {
return this.fromIpInterfaceData(ifaceData);
});
});
});
}
/** Given a node, get the SNMP interfaces for that node. */
public async snmpInterfaces(node: number | OnmsNode, filter?: Filter): Promise<OnmsSnmpInterface[]> {
return this.getOptions(filter).then((opts) => {
if (node instanceof OnmsNode) {
node = node.id;
}
return this.http.get(this.pathToNodesEndpoint() + '/' + node + '/snmpinterfaces', opts).then((result) => {
let data = result.data;
if (this.getCount(data) > 0 && data.snmpInterface) {
data = data.snmpInterface;
} else {
data = [];
}
if (!Array.isArray(data)) {
if (data.ifName) {
data = [data];
} else {
throw new OnmsError('Expected an array of SNMP interfaces but got "'
+ (typeof data) + '" instead.');
}
}
return data.map((ifaceData) => {
return this.fromSnmpData(ifaceData);
});
});
});
}
/** Given a node, get the IP interfaces for that node. */
public async services(
node: number | OnmsNode,
ipInterface: string | OnmsIpInterface,
filter?: Filter,
): Promise<OnmsMonitoredService[]> {
return this.getOptions(filter).then((opts) => {
if (node instanceof OnmsNode) {
node = node.id;
}
if (ipInterface instanceof OnmsIpInterface && ipInterface.ipAddress) {
ipInterface = ipInterface.ipAddress.address;
}
const url = this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces/' + ipInterface + '/services';
return this.http.get(url, opts).then((result) => {
let data = result.data;
if (this.getCount(data) > 0 && data.service) {
data = data.service;
} else {
data = [];
}
if (!Array.isArray(data)) {
if (data.lastGood) {
data = [data];
} else {
throw new OnmsError('Expected an array of services but got "' + (typeof data) + '" instead.');
}
}
return data.map((ifaceData) => {
return this.fromServiceData(ifaceData);
});
});
});
}
/**
* Create a node object from a JSON object.
* @hidden
*/
public fromData(data: any): OnmsNode {
const node = new OnmsNode();
node.id = this.toNumber(data.id);
node.label = data.label;
node.location = data.location;
node.foreignSource = data.foreignSource || undefined;
node.foreignId = data.foreignId || undefined;
node.sysContact = data.sysContact;
node.sysDescription = data.sysDescription;
node.sysLocation = data.sysLocation;
node.sysName = data.sysName;
node.sysObjectId = data.sysObjectId;
if (data.labelSource) {
node.labelSource = OnmsNodeLabelSource.forId(data.labelSource);
}
if (data.createTime) {
node.createTime = this.toDate(data.createTime);
}
if (data.lastCapsdPoll) {
node.lastCapsdPoll = this.toDate(data.lastCapsdPoll);
}
if (data.type) {
node.type = OnmsNodeType.forId(data.type);
}
node.categories = data.categories.map((c) => {
return OnmsCategory.for(c.id, c.name);
});
for (const key in data.assetRecord) {
if (data.assetRecord.hasOwnProperty(key)
&& data.assetRecord[key] !== null
&& data.assetRecord[key] !== undefined) {
node.assets[key] = data.assetRecord[key];
}
}
return node;
}
/**
* create an IP interface object from a JSON object
* @hidden
*/
public fromIpInterfaceData(data: any): OnmsIpInterface {
const iface = new OnmsIpInterface();
iface.id = this.toNumber(data.id);
iface.hostname = data.hostName || data.hostname;
iface.ipAddress = Util.toIPAddress(data.ipAddress);
iface.isManaged = OnmsManagedType.forId(data.isManaged);
iface.lastCapsdPoll = this.toDate(data.lastCapsdPoll);
iface.snmpPrimary = OnmsPrimaryType.forId(data.snmpPrimary);
if (data.snmpInterface && data.snmpInterface.id) {
iface.snmpInterfaceId = this.toNumber(data.snmpInterface.id);
}
return iface;
}
/**
* create an SNMP interface object from a JSON object
* @hidden
*/
public fromSnmpData(data: any): OnmsSnmpInterface {
const iface = new OnmsSnmpInterface();
iface.id = this.toNumber(data.id);
iface.ifIndex = this.toNumber(data.ifIndex);
iface.ifDescr = data.ifDescr;
iface.ifType = this.toNumber(data.ifType);
iface.ifName = data.ifName;
iface.ifSpeed = this.toNumber(data.ifSpeed);
iface.ifAdminStatus = OnmsSnmpStatusType.forId(this.toNumber(data.ifAdminStatus));
iface.ifOperStatus = OnmsSnmpStatusType.forId(this.toNumber(data.ifOperStatus));
iface.ifAlias = data.ifAlias;
iface.lastCapsdPoll = this.toDate(data.lastCapsdPoll);
iface.collect = OnmsCollectType.forId(data.collectFlag);
iface.poll = data.poll;
iface.lastSnmpPoll = this.toDate(data.lastSnmpPoll);
if (data.physAddr) {
iface.physAddr = new PhysAddr(data.physAddr);
}
return iface;
}
/**
* create a monitored service object from a JSON object
* @hidden
*/
public fromServiceData(data: any): OnmsMonitoredService {
const service = new OnmsMonitoredService();
service.id = this.toNumber(data.id);
service.lastFail = this.toDate(data.lastFail);
service.lastGood = this.toDate(data.lastGood);
if (data.serviceType) {
service.type = OnmsServiceType.for(data.serviceType.id, data.serviceType.name);
}
if (data.status) {
service.status = OnmsServiceStatusType.forId(data.status);
}
return service;
}
/**
* The path to the node search properties endpoint.
*/
protected searchPropertyPath(): string {
if (this.getApiVersion() < 2) {
throw new OnmsError('Search properties are not supported in Node ReSTv1.');
}
return this.pathToNodesEndpoint() + '/properties';
}
/**
* Get the path to the nodes endpoint for the appropriate API version.
* @hidden
*/
private pathToNodesEndpoint() {
return this.getApiVersion() === 2 ? 'api/v2/nodes' : 'rest/nodes';
}
}