-
Notifications
You must be signed in to change notification settings - Fork 8
/
NodeDAO.ts
261 lines (234 loc) · 8.02 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
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 {OnmsIpInterface} from '../model/OnmsIpInterface';
import {OnmsMonitoredService} from '../model/OnmsMonitoredService';
import {OnmsNode} from '../model/OnmsNode';
import {OnmsSnmpInterface} from '../model/OnmsSnmpInterface';
/**
* Data access for [[OnmsNode]] objects.
* @category DAO
*/
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((builder) => {
return this.http.get(this.pathToNodesEndpoint() + '/' + id, builder.build()).then((result) => {
const node = this.fromData(result.data);
if (!node) {
throw new OnmsError(`NodeDAO.get id={id} ReST request succeeded, but did not return a valid node.`);
}
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((builder) => {
return this.http.get(this.pathToNodesEndpoint(), builder.build()).then((result) => {
let data = result.data;
if (data !== null && this.getCount(data, result.code) > 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: any) => {
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(passedNode: number | OnmsNode, filter?: Filter): Promise<OnmsIpInterface[]> {
let node: string;
if (passedNode instanceof OnmsNode) {
node = String(passedNode.id);
} else {
node = String(passedNode);
}
return this.getOptions(filter).then((builder) => {
return this.http.get(
this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces',
builder.build(),
).then((result) => {
let data = result.data;
if (this.getCount(data, result.code) > 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: any) => {
return this.fromIpInterfaceData(ifaceData);
});
});
});
}
/** Given a node, get the SNMP interfaces for that node. */
public async snmpInterfaces(passedNode: number | OnmsNode, filter?: Filter): Promise<OnmsSnmpInterface[]> {
const node = String(this.getNodeId(passedNode));
return this.getOptions(filter).then((builder) => {
return this.http.get(
this.pathToNodesEndpoint() + '/' + node + '/snmpinterfaces',
builder.build(),
).then((result) => {
let data = result.data;
if (this.getCount(data, result.code) > 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: any) => {
return this.fromSnmpData(ifaceData);
});
});
});
}
/** Given a node, get the IP interfaces for that node. */
public async services(
passedNode: number | OnmsNode,
ipInterface: string | OnmsIpInterface,
filter?: Filter,
): Promise<OnmsMonitoredService[]> {
const node = String(this.getNodeId(passedNode));
return this.getOptions(filter).then((builder) => {
if (ipInterface instanceof OnmsIpInterface && ipInterface.ipAddress) {
ipInterface = ipInterface.ipAddress.address;
}
const url = this.pathToNodesEndpoint() + '/' + node + '/ipinterfaces/' + ipInterface + '/services';
return this.http.get(url, builder.build()).then((result) => {
let data = result.data;
if (this.getCount(data, result.code) > 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: any) => {
return this.fromServiceData(ifaceData);
});
});
});
}
/**
* Create a node object from a JSON object.
* @hidden
*/
public fromData(data: any) {
return OnmsNode.fromData(data);
}
/**
* create an IP interface object from a JSON object
* @hidden
*/
public fromIpInterfaceData(data: any): OnmsIpInterface {
return OnmsIpInterface.fromData(data);
}
/**
* create an SNMP interface object from a JSON object
* @hidden
*/
public fromSnmpData(data: any): OnmsSnmpInterface {
return OnmsSnmpInterface.fromData(data);
}
/**
* create a monitored service object from a JSON object
* @hidden
*/
public fromServiceData(data: any): OnmsMonitoredService {
return OnmsMonitoredService.fromData(data);
}
/**
* 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 node's ID
* @param node the node
*/
private getNodeId(node: number | OnmsNode): number | undefined {
if (node instanceof OnmsNode) {
return node.id;
}
return node;
}
/**
* Get the path to the nodes endpoint for the appropriate API version.
* @hidden
*/
private pathToNodesEndpoint() {
return this.getApiVersion() === 2 ? 'api/v2/nodes' : 'rest/nodes';
}
}