48c3a15d862ccacd6bb266b5d58b4fbbea2735bc
[public/netxms.git] / src / server / core / nxsl_classes.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2017 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: nxsl_classes.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * clearGeoLocation()
27 */
28 NXSL_METHOD_DEFINITION(NetObj, clearGeoLocation)
29 {
30 ((NetObj *)object->getData())->setGeoLocation(GeoLocation());
31 *result = new NXSL_Value;
32 return 0;
33 }
34
35 /**
36 * setGeoLocation(loc)
37 */
38 NXSL_METHOD_DEFINITION(NetObj, setGeoLocation)
39 {
40 if (!argv[0]->isObject())
41 return NXSL_ERR_NOT_OBJECT;
42
43 NXSL_Object *o = argv[0]->getValueAsObject();
44 if (_tcscmp(o->getClass()->getName(), g_nxslGeoLocationClass.getName()))
45 return NXSL_ERR_BAD_CLASS;
46
47 GeoLocation *gl = (GeoLocation *)o->getData();
48 ((NetObj *)object->getData())->setGeoLocation(*gl);
49 *result = new NXSL_Value;
50 return 0;
51 }
52
53 /**
54 * setMapImage(image)
55 */
56 NXSL_METHOD_DEFINITION(NetObj, setMapImage)
57 {
58 if (!argv[0]->isString())
59 return NXSL_ERR_NOT_STRING;
60
61 bool success = false;
62 DB_HANDLE hdb = DBConnectionPoolAcquireConnection();
63 DB_STATEMENT hStmt = DBPrepare(hdb, _T("SELECT guid FROM images WHERE upper(guid)=upper(?) OR upper(name)=upper(?)"));
64 if (hStmt != NULL)
65 {
66 DBBind(hStmt, 1, DB_SQLTYPE_VARCHAR, argv[0]->getValueAsCString(), DB_BIND_STATIC);
67 DBBind(hStmt, 2, DB_SQLTYPE_VARCHAR, argv[0]->getValueAsCString(), DB_BIND_STATIC);
68 DB_RESULT hResult = DBSelectPrepared(hStmt);
69 if (hResult != NULL)
70 {
71 if (DBGetNumRows(hResult) > 0)
72 {
73 uuid guid = DBGetFieldGUID(hResult, 0, 0);
74 ((NetObj *)object->getData())->setMapImage(guid);
75 }
76 DBFreeResult(hResult);
77 }
78 DBFreeStatement(hStmt);
79 }
80 DBConnectionPoolReleaseConnection(hdb);
81 *result = new NXSL_Value(success ? 1 : 0);
82 return 0;
83 }
84
85 /**
86 * setStatusCalculation(type, ...)
87 */
88 NXSL_METHOD_DEFINITION(NetObj, setStatusCalculation)
89 {
90 if (argc < 1)
91 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
92
93 if (!argv[0]->isInteger())
94 return NXSL_ERR_NOT_INTEGER;
95
96 INT32 success = 1;
97 int method = argv[0]->getValueAsInt32();
98 NetObj *netobj = (NetObj *)object->getData();
99 switch(method)
100 {
101 case SA_CALCULATE_DEFAULT:
102 case SA_CALCULATE_MOST_CRITICAL:
103 netobj->setStatusCalculation(method);
104 break;
105 case SA_CALCULATE_SINGLE_THRESHOLD:
106 if (argc < 2)
107 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
108 if (!argv[1]->isInteger())
109 return NXSL_ERR_NOT_INTEGER;
110 netobj->setStatusCalculation(method, argv[1]->getValueAsInt32());
111 break;
112 case SA_CALCULATE_MULTIPLE_THRESHOLDS:
113 if (argc < 5)
114 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
115 for(int i = 1; i <= 4; i++)
116 {
117 if (!argv[i]->isInteger())
118 return NXSL_ERR_NOT_INTEGER;
119 }
120 netobj->setStatusCalculation(method, argv[1]->getValueAsInt32(), argv[2]->getValueAsInt32(), argv[3]->getValueAsInt32(), argv[4]->getValueAsInt32());
121 break;
122 default:
123 success = 0; // invalid method
124 break;
125 }
126 *result = new NXSL_Value(success);
127 return 0;
128 }
129
130 /**
131 * setStatusPropagation(type, ...)
132 */
133 NXSL_METHOD_DEFINITION(NetObj, setStatusPropagation)
134 {
135 if (argc < 1)
136 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
137
138 if (!argv[0]->isInteger())
139 return NXSL_ERR_NOT_INTEGER;
140
141 INT32 success = 1;
142 int method = argv[0]->getValueAsInt32();
143 NetObj *netobj = (NetObj *)object->getData();
144 switch(method)
145 {
146 case SA_PROPAGATE_DEFAULT:
147 case SA_PROPAGATE_UNCHANGED:
148 netobj->setStatusPropagation(method);
149 break;
150 case SA_PROPAGATE_FIXED:
151 case SA_PROPAGATE_RELATIVE:
152 if (argc < 2)
153 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
154 if (!argv[1]->isInteger())
155 return NXSL_ERR_NOT_INTEGER;
156 netobj->setStatusPropagation(method, argv[1]->getValueAsInt32());
157 break;
158 case SA_PROPAGATE_TRANSLATED:
159 if (argc < 5)
160 return NXSL_ERR_INVALID_ARGUMENT_COUNT;
161 for(int i = 1; i <= 4; i++)
162 {
163 if (!argv[i]->isInteger())
164 return NXSL_ERR_NOT_INTEGER;
165 }
166 netobj->setStatusPropagation(method, argv[1]->getValueAsInt32(), argv[2]->getValueAsInt32(), argv[3]->getValueAsInt32(), argv[4]->getValueAsInt32());
167 break;
168 default:
169 success = 0; // invalid method
170 break;
171 }
172 *result = new NXSL_Value(success);
173 return 0;
174 }
175
176 /**
177 * NXSL class NetObj: constructor
178 */
179 NXSL_NetObjClass::NXSL_NetObjClass() : NXSL_Class()
180 {
181 setName(_T("NetObj"));
182
183 NXSL_REGISTER_METHOD(NetObj, clearGeoLocation, 0);
184 NXSL_REGISTER_METHOD(NetObj, setGeoLocation, 1);
185 NXSL_REGISTER_METHOD(NetObj, setMapImage, 1);
186 NXSL_REGISTER_METHOD(NetObj, setStatusCalculation, -1);
187 NXSL_REGISTER_METHOD(NetObj, setStatusPropagation, -1);
188 }
189
190 /**
191 * Object creation handler
192 */
193 void NXSL_NetObjClass::onObjectCreate(NXSL_Object *object)
194 {
195 ((NetObj *)object->getData())->incRefCount();
196 }
197
198 /**
199 * Object destruction handler
200 */
201 void NXSL_NetObjClass::onObjectDelete(NXSL_Object *object)
202 {
203 ((NetObj *)object->getData())->decRefCount();
204 }
205
206 /**
207 * NXSL class NetObj: get attribute
208 */
209 NXSL_Value *NXSL_NetObjClass::getAttr(NXSL_Object *_object, const TCHAR *attr)
210 {
211 NXSL_Value *value = NULL;
212 NetObj *object = (NetObj *)_object->getData();
213 if (!_tcscmp(attr, _T("alarms")))
214 {
215 ObjectArray<Alarm> *alarms = GetAlarms(object->getId(), true);
216 alarms->setOwner(false);
217 NXSL_Array *array = new NXSL_Array;
218 for(int i = 0; i < alarms->size(); i++)
219 array->append(new NXSL_Value(new NXSL_Object(&g_nxslAlarmClass, alarms->get(i))));
220 value = new NXSL_Value(array);
221 delete alarms;
222 }
223 else if (!_tcscmp(attr, _T("city")))
224 {
225 value = new NXSL_Value(object->getPostalAddress()->getCity());
226 }
227 else if (!_tcscmp(attr, _T("comments")))
228 {
229 value = new NXSL_Value(object->getComments());
230 }
231 else if (!_tcscmp(attr, _T("country")))
232 {
233 value = new NXSL_Value(object->getPostalAddress()->getCountry());
234 }
235 else if (!_tcscmp(attr, _T("customAttributes")))
236 {
237 value = object->getCustomAttributesForNXSL();
238 }
239 else if (!_tcscmp(attr, _T("geolocation")))
240 {
241 value = NXSL_GeoLocationClass::createObject(object->getGeoLocation());
242 }
243 else if (!_tcscmp(attr, _T("guid")))
244 {
245 TCHAR buffer[64];
246 value = new NXSL_Value(object->getGuid().toString(buffer));
247 }
248 else if (!_tcscmp(attr, _T("id")))
249 {
250 value = new NXSL_Value(object->getId());
251 }
252 else if (!_tcscmp(attr, _T("ipAddr")))
253 {
254 TCHAR buffer[64];
255 GetObjectIpAddress(object).toString(buffer);
256 value = new NXSL_Value(buffer);
257 }
258 else if (!_tcscmp(attr, _T("mapImage")))
259 {
260 TCHAR buffer[64];
261 value = new NXSL_Value(object->getMapImage().toString(buffer));
262 }
263 else if (!_tcscmp(attr, _T("name")))
264 {
265 value = new NXSL_Value(object->getName());
266 }
267 else if (!_tcscmp(attr, _T("postcode")))
268 {
269 value = new NXSL_Value(object->getPostalAddress()->getPostCode());
270 }
271 else if (!_tcscmp(attr, _T("status")))
272 {
273 value = new NXSL_Value((LONG)object->getStatus());
274 }
275 else if (!_tcscmp(attr, _T("streetAddress")))
276 {
277 value = new NXSL_Value(object->getPostalAddress()->getStreetAddress());
278 }
279 else if (!_tcscmp(attr, _T("type")))
280 {
281 value = new NXSL_Value((LONG)object->getObjectClass());
282 }
283 else
284 {
285 value = object->getCustomAttributeForNXSL(attr);
286 }
287 return value;
288 }
289
290 /**
291 * NXSL class Zone: constructor
292 */
293 NXSL_ZoneClass::NXSL_ZoneClass() : NXSL_NetObjClass()
294 {
295 setName(_T("Zone"));
296 }
297
298 /**
299 * NXSL class Zone: get attribute
300 */
301 NXSL_Value *NXSL_ZoneClass::getAttr(NXSL_Object *object, const TCHAR *attr)
302 {
303 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
304 if (value != NULL)
305 return value;
306
307 Zone *zone = (Zone *)object->getData();
308 if (!_tcscmp(attr, _T("proxyNode")))
309 {
310 Node *node = (Node *)FindObjectById(zone->getProxyNodeId(), OBJECT_NODE);
311 value = (node != NULL) ? new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, node)) : new NXSL_Value();
312 }
313 else if (!_tcscmp(attr, _T("proxyNodeId")))
314 {
315 value = new NXSL_Value(zone->getProxyNodeId());
316 }
317 else if (!_tcscmp(attr, _T("uin")))
318 {
319 value = new NXSL_Value(zone->getUIN());
320 }
321 return value;
322 }
323
324 /**
325 * Generic implementation for flag changing methods
326 */
327 static int ChangeFlagMethod(NXSL_Object *object, NXSL_Value *arg, NXSL_Value **result, UINT32 flag)
328 {
329 if (!arg->isInteger())
330 return NXSL_ERR_NOT_INTEGER;
331
332 Node *node = (Node *)object->getData();
333 if (arg->getValueAsInt32())
334 node->clearFlag(flag);
335 else
336 node->setFlag(flag);
337
338 *result = new NXSL_Value;
339 return 0;
340 }
341
342 /**
343 * enableAgent(enabled) method
344 */
345 NXSL_METHOD_DEFINITION(Node, enableAgent)
346 {
347 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_NXCP);
348 }
349
350 /**
351 * enableConfigurationPolling(enabled) method
352 */
353 NXSL_METHOD_DEFINITION(Node, enableConfigurationPolling)
354 {
355 return ChangeFlagMethod(object, argv[0], result, DCF_DISABLE_CONF_POLL);
356 }
357
358 /**
359 * enableDiscoveryPolling(enabled) method
360 */
361 NXSL_METHOD_DEFINITION(Node, enableDiscoveryPolling)
362 {
363 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_DISCOVERY_POLL);
364 }
365
366 /**
367 * enableIcmp(enabled) method
368 */
369 NXSL_METHOD_DEFINITION(Node, enableIcmp)
370 {
371 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_ICMP);
372 }
373
374 /**
375 * enableRoutingTablePolling(enabled) method
376 */
377 NXSL_METHOD_DEFINITION(Node, enableRoutingTablePolling)
378 {
379 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_ROUTE_POLL);
380 }
381
382 /**
383 * enableSnmp(enabled) method
384 */
385 NXSL_METHOD_DEFINITION(Node, enableSnmp)
386 {
387 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_SNMP);
388 }
389
390 /**
391 * enableStatusPolling(enabled) method
392 */
393 NXSL_METHOD_DEFINITION(Node, enableStatusPolling)
394 {
395 return ChangeFlagMethod(object, argv[0], result, DCF_DISABLE_STATUS_POLL);
396 }
397
398 /**
399 * enableTopologyPolling(enabled) method
400 */
401 NXSL_METHOD_DEFINITION(Node, enableTopologyPolling)
402 {
403 return ChangeFlagMethod(object, argv[0], result, NF_DISABLE_TOPOLOGY_POLL);
404 }
405
406 /**
407 * NXSL class Node: constructor
408 */
409 NXSL_NodeClass::NXSL_NodeClass() : NXSL_NetObjClass()
410 {
411 setName(_T("Node"));
412
413 NXSL_REGISTER_METHOD(Node, enableAgent, 1);
414 NXSL_REGISTER_METHOD(Node, enableConfigurationPolling, 1);
415 NXSL_REGISTER_METHOD(Node, enableDiscoveryPolling, 1);
416 NXSL_REGISTER_METHOD(Node, enableIcmp, 1);
417 NXSL_REGISTER_METHOD(Node, enableRoutingTablePolling, 1);
418 NXSL_REGISTER_METHOD(Node, enableSnmp, 1);
419 NXSL_REGISTER_METHOD(Node, enableStatusPolling, 1);
420 NXSL_REGISTER_METHOD(Node, enableTopologyPolling, 1);
421 }
422
423 /**
424 * NXSL class Node: get attribute
425 */
426 NXSL_Value *NXSL_NodeClass::getAttr(NXSL_Object *object, const TCHAR *attr)
427 {
428 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
429 if (value != NULL)
430 return value;
431
432 Node *node = (Node *)object->getData();
433 if (!_tcscmp(attr, _T("agentVersion")))
434 {
435 value = new NXSL_Value(node->getAgentVersion());
436 }
437 else if (!_tcscmp(attr, _T("bootTime")))
438 {
439 value = new NXSL_Value((INT64)node->getBootTime());
440 }
441 else if (!_tcscmp(attr, _T("bridgeBaseAddress")))
442 {
443 TCHAR buffer[64];
444 value = new NXSL_Value(BinToStr(node->getBridgeId(), MAC_ADDR_LENGTH, buffer));
445 }
446 else if (!_tcscmp(attr, _T("components")))
447 {
448 value = new NXSL_Value(new NXSL_Object(&g_nxslComponentClass, node->getComponents()->getRoot()));
449 }
450 else if (!_tcscmp(attr, _T("driver")))
451 {
452 value = new NXSL_Value(node->getDriverName());
453 }
454 else if (!_tcscmp(attr, _T("flags")))
455 {
456 value = new NXSL_Value(node->getFlags());
457 }
458 else if (!_tcscmp(attr, _T("isAgent")))
459 {
460 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_NATIVE_AGENT) ? 1 : 0));
461 }
462 else if (!_tcscmp(attr, _T("isBridge")))
463 {
464 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_BRIDGE) ? 1 : 0));
465 }
466 else if (!_tcscmp(attr, _T("isCDP")))
467 {
468 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_CDP) ? 1 : 0));
469 }
470 else if (!_tcscmp(attr, _T("isInMaintenanceMode")))
471 {
472 value = new NXSL_Value((LONG)(node->isInMaintenanceMode() ? 1 : 0));
473 }
474 else if (!_tcscmp(attr, _T("isLLDP")))
475 {
476 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_LLDP) ? 1 : 0));
477 }
478 else if (!_tcscmp(attr, _T("isLocalMgmt")) || !_tcscmp(attr, _T("isLocalManagement")))
479 {
480 value = new NXSL_Value((LONG)((node->isLocalManagement()) ? 1 : 0));
481 }
482 else if (!_tcscmp(attr, _T("isPAE")) || !_tcscmp(attr, _T("is802_1x")))
483 {
484 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_8021X) ? 1 : 0));
485 }
486 else if (!_tcscmp(attr, _T("isPrinter")))
487 {
488 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_PRINTER) ? 1 : 0));
489 }
490 else if (!_tcscmp(attr, _T("isRouter")))
491 {
492 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_ROUTER) ? 1 : 0));
493 }
494 else if (!_tcscmp(attr, _T("isSMCLP")))
495 {
496 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_SMCLP) ? 1 : 0));
497 }
498 else if (!_tcscmp(attr, _T("isSNMP")))
499 {
500 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_SNMP) ? 1 : 0));
501 }
502 else if (!_tcscmp(attr, _T("isSONMP")))
503 {
504 value = new NXSL_Value((LONG)((node->getCapabilities() & NC_IS_NDP) ? 1 : 0));
505 }
506 else if (!_tcscmp(attr, _T("lastAgentCommTime")))
507 {
508 value = new NXSL_Value((INT64)node->getLastAgentCommTime());
509 }
510 else if (!_tcscmp(attr, _T("platformName")))
511 {
512 value = new NXSL_Value(node->getPlatformName());
513 }
514 else if (!_tcscmp(attr, _T("rack")))
515 {
516 Rack *rack = (Rack *)FindObjectById(node->getRackId(), OBJECT_RACK);
517 if (rack != NULL)
518 {
519 value = rack->createNXSLObject();
520 }
521 else
522 {
523 value = new NXSL_Value;
524 }
525 }
526 else if (!_tcscmp(attr, _T("rackId")))
527 {
528 value = new NXSL_Value(node->getRackId());
529 }
530 else if (!_tcscmp(attr, _T("rackHeight")))
531 {
532 value = new NXSL_Value(node->getRackHeight());
533 }
534 else if (!_tcscmp(attr, _T("rackPosition")))
535 {
536 value = new NXSL_Value(node->getRackPosition());
537 }
538 else if (!_tcscmp(attr, _T("runtimeFlags")))
539 {
540 value = new NXSL_Value(node->getRuntimeFlags());
541 }
542 else if (!_tcscmp(attr, _T("snmpOID")))
543 {
544 value = new NXSL_Value(node->getSNMPObjectId());
545 }
546 else if (!_tcscmp(attr, _T("snmpSysContact")))
547 {
548 value = new NXSL_Value(node->getSysContact());
549 }
550 else if (!_tcscmp(attr, _T("snmpSysLocation")))
551 {
552 value = new NXSL_Value(node->getSysLocation());
553 }
554 else if (!_tcscmp(attr, _T("snmpSysName")))
555 {
556 value = new NXSL_Value(node->getSysName());
557 }
558 else if (!_tcscmp(attr, _T("snmpVersion")))
559 {
560 value = new NXSL_Value((LONG)node->getSNMPVersion());
561 }
562 else if (!_tcscmp(attr, _T("subType")))
563 {
564 value = new NXSL_Value(node->getSubType());
565 }
566 else if (!_tcscmp(attr, _T("sysDescription")))
567 {
568 value = new NXSL_Value(node->getSysDescription());
569 }
570 else if (!_tcscmp(attr, _T("type")))
571 {
572 value = new NXSL_Value((INT32)node->getType());
573 }
574 else if (!_tcscmp(attr, _T("zone")))
575 {
576 if (g_flags & AF_ENABLE_ZONING)
577 {
578 Zone *zone = FindZoneByUIN(node->getZoneUIN());
579 if (zone != NULL)
580 {
581 value = new NXSL_Value(new NXSL_Object(&g_nxslZoneClass, zone));
582 }
583 else
584 {
585 value = new NXSL_Value;
586 }
587 }
588 else
589 {
590 value = new NXSL_Value;
591 }
592 }
593 else if (!_tcscmp(attr, _T("zoneUIN")))
594 {
595 value = new NXSL_Value(node->getZoneUIN());
596 }
597 return value;
598 }
599
600 /**
601 * Interface::setExcludeFromTopology(enabled) method
602 */
603 NXSL_METHOD_DEFINITION(Interface, setExcludeFromTopology)
604 {
605 if (!argv[0]->isInteger())
606 return NXSL_ERR_NOT_INTEGER;
607
608 Interface *iface = (Interface *)object->getData();
609 iface->setExcludeFromTopology(argv[0]->getValueAsInt32() != 0);
610 *result = new NXSL_Value;
611 return 0;
612 }
613
614 /**
615 * Interface::setExpectedState(state) method
616 */
617 NXSL_METHOD_DEFINITION(Interface, setExpectedState)
618 {
619 int state;
620 if (argv[0]->isInteger())
621 {
622 state = argv[0]->getValueAsInt32();
623 }
624 else if (argv[0]->isString())
625 {
626 static const TCHAR *stateNames[] = { _T("UP"), _T("DOWN"), _T("IGNORE"), NULL };
627 const TCHAR *name = argv[0]->getValueAsCString();
628 for(state = 0; stateNames[state] != NULL; state++)
629 if (!_tcsicmp(stateNames[state], name))
630 break;
631 }
632 else
633 {
634 return NXSL_ERR_NOT_STRING;
635 }
636
637 if ((state >= 0) && (state <= 2))
638 ((Interface *)object->getData())->setExpectedState(state);
639
640 *result = new NXSL_Value;
641 return 0;
642 }
643
644 /**
645 * NXSL class Interface: constructor
646 */
647 NXSL_InterfaceClass::NXSL_InterfaceClass() : NXSL_NetObjClass()
648 {
649 setName(_T("Interface"));
650
651 NXSL_REGISTER_METHOD(Interface, setExcludeFromTopology, 1);
652 NXSL_REGISTER_METHOD(Interface, setExpectedState, 1);
653 }
654
655 /**
656 * NXSL class Interface: get attribute
657 */
658 NXSL_Value *NXSL_InterfaceClass::getAttr(NXSL_Object *object, const TCHAR *attr)
659 {
660 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
661 if (value != NULL)
662 return value;
663
664 Interface *iface = (Interface *)object->getData();
665 if (!_tcscmp(attr, _T("adminState")))
666 {
667 value = new NXSL_Value((LONG)iface->getAdminState());
668 }
669 else if (!_tcscmp(attr, _T("alias")))
670 {
671 value = new NXSL_Value(iface->getAlias());
672 }
673 else if (!_tcscmp(attr, _T("bridgePortNumber")))
674 {
675 value = new NXSL_Value(iface->getBridgePortNumber());
676 }
677 else if (!_tcscmp(attr, _T("description")))
678 {
679 value = new NXSL_Value(iface->getDescription());
680 }
681 else if (!_tcscmp(attr, _T("dot1xBackendAuthState")))
682 {
683 value = new NXSL_Value((LONG)iface->getDot1xBackendAuthState());
684 }
685 else if (!_tcscmp(attr, _T("dot1xPaeAuthState")))
686 {
687 value = new NXSL_Value((LONG)iface->getDot1xPaeAuthState());
688 }
689 else if (!_tcscmp(attr, _T("expectedState")))
690 {
691 value = new NXSL_Value((iface->getFlags() & IF_EXPECTED_STATE_MASK) >> 28);
692 }
693 else if (!_tcscmp(attr, _T("flags")))
694 {
695 value = new NXSL_Value(iface->getFlags());
696 }
697 else if (!_tcscmp(attr, _T("ifIndex")))
698 {
699 value = new NXSL_Value(iface->getIfIndex());
700 }
701 else if (!_tcscmp(attr, _T("ifType")))
702 {
703 value = new NXSL_Value(iface->getIfType());
704 }
705 else if (!_tcscmp(attr, _T("ipAddressList")))
706 {
707 const InetAddressList *addrList = iface->getIpAddressList();
708 NXSL_Array *a = new NXSL_Array();
709 for(int i = 0; i < addrList->size(); i++)
710 {
711 a->append(new NXSL_Value(NXSL_InetAddressClass::createObject(addrList->get(i))));
712 }
713 value = new NXSL_Value(a);
714 }
715 else if (!_tcscmp(attr, _T("ipNetMask")))
716 {
717 value = new NXSL_Value(iface->getIpAddressList()->getFirstUnicastAddress().getMaskBits());
718 }
719 else if (!_tcscmp(attr, _T("isExcludedFromTopology")))
720 {
721 value = new NXSL_Value((LONG)(iface->isExcludedFromTopology() ? 1 : 0));
722 }
723 else if (!_tcscmp(attr, _T("isLoopback")))
724 {
725 value = new NXSL_Value((LONG)(iface->isLoopback() ? 1 : 0));
726 }
727 else if (!_tcscmp(attr, _T("isManuallyCreated")))
728 {
729 value = new NXSL_Value((LONG)(iface->isManuallyCreated() ? 1 : 0));
730 }
731 else if (!_tcscmp(attr, _T("isPhysicalPort")))
732 {
733 value = new NXSL_Value((LONG)(iface->isPhysicalPort() ? 1 : 0));
734 }
735 else if (!_tcscmp(attr, _T("macAddr")))
736 {
737 TCHAR buffer[256];
738 value = new NXSL_Value(BinToStr(iface->getMacAddr(), MAC_ADDR_LENGTH, buffer));
739 }
740 else if (!_tcscmp(attr, _T("mtu")))
741 {
742 value = new NXSL_Value(iface->getMTU());
743 }
744 else if (!_tcscmp(attr, _T("node")))
745 {
746 Node *parentNode = iface->getParentNode();
747 if (parentNode != NULL)
748 {
749 value = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, parentNode));
750 }
751 else
752 {
753 value = new NXSL_Value;
754 }
755 }
756 else if (!_tcscmp(attr, _T("operState")))
757 {
758 value = new NXSL_Value((LONG)iface->getOperState());
759 }
760 else if (!_tcscmp(attr, _T("peerInterface")))
761 {
762 Interface *peerIface = (Interface *)FindObjectById(iface->getPeerInterfaceId(), OBJECT_INTERFACE);
763 if (peerIface != NULL)
764 {
765 if (g_flags & AF_CHECK_TRUSTED_NODES)
766 {
767 Node *parentNode = iface->getParentNode();
768 Node *peerNode = peerIface->getParentNode();
769 if ((parentNode != NULL) && (peerNode != NULL))
770 {
771 if (peerNode->isTrustedNode(parentNode->getId()))
772 {
773 value = new NXSL_Value(new NXSL_Object(&g_nxslInterfaceClass, peerIface));
774 }
775 else
776 {
777 // No access, return null
778 value = new NXSL_Value;
779 DbgPrintf(4, _T("NXSL::Interface::peerInterface(%s [%d]): access denied for node %s [%d]"),
780 iface->getName(), iface->getId(), peerNode->getName(), peerNode->getId());
781 }
782 }
783 else
784 {
785 value = new NXSL_Value;
786 DbgPrintf(4, _T("NXSL::Interface::peerInterface(%s [%d]): parentNode=%p peerNode=%p"), iface->getName(), iface->getId(), parentNode, peerNode);
787 }
788 }
789 else
790 {
791 value = new NXSL_Value(new NXSL_Object(&g_nxslInterfaceClass, peerIface));
792 }
793 }
794 else
795 {
796 value = new NXSL_Value;
797 }
798 }
799 else if (!_tcscmp(attr, _T("peerNode")))
800 {
801 Node *peerNode = (Node *)FindObjectById(iface->getPeerNodeId(), OBJECT_NODE);
802 if (peerNode != NULL)
803 {
804 if (g_flags & AF_CHECK_TRUSTED_NODES)
805 {
806 Node *parentNode = iface->getParentNode();
807 if ((parentNode != NULL) && (peerNode->isTrustedNode(parentNode->getId())))
808 {
809 value = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, peerNode));
810 }
811 else
812 {
813 // No access, return null
814 value = new NXSL_Value;
815 DbgPrintf(4, _T("NXSL::Interface::peerNode(%s [%d]): access denied for node %s [%d]"),
816 iface->getName(), iface->getId(), peerNode->getName(), peerNode->getId());
817 }
818 }
819 else
820 {
821 value = new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, peerNode));
822 }
823 }
824 else
825 {
826 value = new NXSL_Value;
827 }
828 }
829 else if (!_tcscmp(attr, _T("port")))
830 {
831 value = new NXSL_Value(iface->getPortNumber());
832 }
833 else if (!_tcscmp(attr, _T("slot")))
834 {
835 value = new NXSL_Value(iface->getSlotNumber());
836 }
837 else if (!_tcscmp(attr, _T("speed")))
838 {
839 value = new NXSL_Value(iface->getSpeed());
840 }
841 else if (!_tcscmp(attr, _T("zone")))
842 {
843 if (g_flags & AF_ENABLE_ZONING)
844 {
845 Zone *zone = FindZoneByUIN(iface->getZoneUIN());
846 if (zone != NULL)
847 {
848 value = new NXSL_Value(new NXSL_Object(&g_nxslZoneClass, zone));
849 }
850 else
851 {
852 value = new NXSL_Value;
853 }
854 }
855 else
856 {
857 value = new NXSL_Value;
858 }
859 }
860 else if (!_tcscmp(attr, _T("zoneUIN")))
861 {
862 value = new NXSL_Value(iface->getZoneUIN());
863 }
864 return value;
865 }
866
867 /**
868 * NXSL class Mobile Device: constructor
869 */
870 NXSL_MobileDeviceClass::NXSL_MobileDeviceClass() : NXSL_NetObjClass()
871 {
872 setName(_T("MobileDevice"));
873 }
874
875 /**
876 * NXSL class Mobile Device: get attribute
877 */
878 NXSL_Value *NXSL_MobileDeviceClass::getAttr(NXSL_Object *object, const TCHAR *attr)
879 {
880 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
881 if (value != NULL)
882 return value;
883
884 MobileDevice *mobDevice = (MobileDevice *)object->getData();
885 if (!_tcscmp(attr, _T("deviceId")))
886 {
887 value = new NXSL_Value(mobDevice->getDeviceId());
888 }
889 else if (!_tcscmp(attr, _T("vendor")))
890 {
891 value = new NXSL_Value(mobDevice->getVendor());
892 }
893 else if (!_tcscmp(attr, _T("model")))
894 {
895 value = new NXSL_Value(mobDevice->getModel());
896 }
897 else if (!_tcscmp(attr, _T("serialNumber")))
898 {
899 value = new NXSL_Value(mobDevice->getSerialNumber());
900 }
901 else if (!_tcscmp(attr, _T("osName")))
902 {
903 value = new NXSL_Value(mobDevice->getOsName());
904 }
905 else if (!_tcscmp(attr, _T("osVersion")))
906 {
907 value = new NXSL_Value(mobDevice->getOsVersion());
908 }
909 else if (!_tcscmp(attr, _T("userId")))
910 {
911 value = new NXSL_Value(mobDevice->getUserId());
912 }
913 else if (!_tcscmp(attr, _T("batteryLevel")))
914 {
915 value = new NXSL_Value(mobDevice->getBatteryLevel());
916 }
917
918 return value;
919 }
920
921 /**
922 * NXSL class "Chassis" constructor
923 */
924 NXSL_ChassisClass::NXSL_ChassisClass() : NXSL_NetObjClass()
925 {
926 setName(_T("Chassis"));
927 }
928
929 /**
930 * NXSL class "Chassis" attributes
931 */
932 NXSL_Value *NXSL_ChassisClass::getAttr(NXSL_Object *object, const TCHAR *attr)
933 {
934 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
935 if (value != NULL)
936 return value;
937
938 Chassis *chassis = (Chassis *)object->getData();
939 if (!_tcscmp(attr, _T("controller")))
940 {
941 Node *node = (Node *)FindObjectById(chassis->getControllerId(), OBJECT_NODE);
942 if (node != NULL)
943 {
944 value = node->createNXSLObject();
945 }
946 else
947 {
948 value = new NXSL_Value;
949 }
950 }
951 else if (!_tcscmp(attr, _T("controllerId")))
952 {
953 value = new NXSL_Value(chassis->getControllerId());
954 }
955 else if (!_tcscmp(attr, _T("flags")))
956 {
957 value = new NXSL_Value(chassis->getFlags());
958 }
959 else if (!_tcscmp(attr, _T("rack")))
960 {
961 Rack *rack = (Rack *)FindObjectById(chassis->getRackId(), OBJECT_RACK);
962 if (rack != NULL)
963 {
964 value = rack->createNXSLObject();
965 }
966 else
967 {
968 value = new NXSL_Value;
969 }
970 }
971 else if (!_tcscmp(attr, _T("rackId")))
972 {
973 value = new NXSL_Value(chassis->getRackId());
974 }
975 else if (!_tcscmp(attr, _T("rackHeight")))
976 {
977 value = new NXSL_Value(chassis->getRackHeight());
978 }
979 else if (!_tcscmp(attr, _T("rackPosition")))
980 {
981 value = new NXSL_Value(chassis->getRackPosition());
982 }
983 return value;
984 }
985
986 /**
987 * Cluster::getResourceOwner() method
988 */
989 NXSL_METHOD_DEFINITION(Cluster, getResourceOwner)
990 {
991 if (!argv[0]->isString())
992 return NXSL_ERR_NOT_STRING;
993
994 UINT32 ownerId = ((Cluster *)object->getData())->getResourceOwner(argv[0]->getValueAsCString());
995 if (ownerId != 0)
996 {
997 NetObj *object = FindObjectById(ownerId);
998 *result = (object != NULL) ? object->createNXSLObject() : new NXSL_Value();
999 }
1000 else
1001 {
1002 *result = new NXSL_Value();
1003 }
1004 return 0;
1005 }
1006
1007 /**
1008 * NXSL class "Cluster" constructor
1009 */
1010 NXSL_ClusterClass::NXSL_ClusterClass() : NXSL_NetObjClass()
1011 {
1012 setName(_T("Cluster"));
1013
1014 NXSL_REGISTER_METHOD(Cluster, getResourceOwner, 1);
1015 }
1016
1017 /**
1018 * NXSL class "Cluster" attributes
1019 */
1020 NXSL_Value *NXSL_ClusterClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1021 {
1022 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
1023 if (value != NULL)
1024 return value;
1025
1026 Cluster *cluster = (Cluster *)object->getData();
1027 if (!_tcscmp(attr, _T("nodes")))
1028 {
1029 value = new NXSL_Value(cluster->getNodesForNXSL());
1030 }
1031 else if (!_tcscmp(attr, _T("zone")))
1032 {
1033 if (g_flags & AF_ENABLE_ZONING)
1034 {
1035 Zone *zone = FindZoneByUIN(cluster->getZoneUIN());
1036 if (zone != NULL)
1037 {
1038 value = zone->createNXSLObject();
1039 }
1040 else
1041 {
1042 value = new NXSL_Value;
1043 }
1044 }
1045 else
1046 {
1047 value = new NXSL_Value;
1048 }
1049 }
1050 else if (!_tcscmp(attr, _T("zoneUIN")))
1051 {
1052 value = new NXSL_Value(cluster->getZoneUIN());
1053 }
1054 return value;
1055 }
1056
1057 /**
1058 * Container::setAutoBindMode() method
1059 */
1060 NXSL_METHOD_DEFINITION(Container, setAutoBindMode)
1061 {
1062 if (!argv[0]->isInteger() || !argv[1]->isInteger())
1063 return NXSL_ERR_NOT_INTEGER;
1064
1065 ((Container *)object->getData())->setAutoBindMode(argv[0]->getValueAsBoolean(), argv[1]->getValueAsBoolean());
1066 *result = new NXSL_Value();
1067 return 0;
1068 }
1069
1070 /**
1071 * Container::setAutoBindScript() method
1072 */
1073 NXSL_METHOD_DEFINITION(Container, setAutoBindScript)
1074 {
1075 if (!argv[0]->isString())
1076 return NXSL_ERR_NOT_STRING;
1077
1078 ((Container *)object->getData())->setAutoBindFilter(argv[0]->getValueAsCString());
1079 *result = new NXSL_Value();
1080 return 0;
1081 }
1082
1083 /**
1084 * NXSL class "Container" constructor
1085 */
1086 NXSL_ContainerClass::NXSL_ContainerClass() : NXSL_NetObjClass()
1087 {
1088 setName(_T("Container"));
1089
1090 NXSL_REGISTER_METHOD(Container, setAutoBindMode, 2);
1091 NXSL_REGISTER_METHOD(Container, setAutoBindScript, 1);
1092 }
1093
1094 /**
1095 * NXSL class "Cluster" attributes
1096 */
1097 NXSL_Value *NXSL_ContainerClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1098 {
1099 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
1100 if (value != NULL)
1101 return value;
1102
1103 Container *container = (Container *)object->getData();
1104 if (!_tcscmp(attr, _T("autoBindScript")))
1105 {
1106 const TCHAR *script = container->getAutoBindScriptSource();
1107 value = new NXSL_Value(CHECK_NULL_EX(script));
1108 }
1109 else if (!_tcscmp(attr, _T("isAutoBindEnabled")))
1110 {
1111 value = new NXSL_Value(container->isAutoBindEnabled() ? 1 : 0);
1112 }
1113 else if (!_tcscmp(attr, _T("isAutoUnbindEnabled")))
1114 {
1115 value = new NXSL_Value(container->isAutoUnbindEnabled() ? 1 : 0);
1116 }
1117 return value;
1118 }
1119
1120 /**
1121 * Event::setMessage() method
1122 */
1123 NXSL_METHOD_DEFINITION(Event, setMessage)
1124 {
1125 if (!argv[0]->isString())
1126 return NXSL_ERR_NOT_STRING;
1127
1128 Event *event = (Event *)object->getData();
1129 event->setMessage(argv[0]->getValueAsCString());
1130 *result = new NXSL_Value();
1131 return 0;
1132 }
1133
1134 /**
1135 * Event::setSeverity() method
1136 */
1137 NXSL_METHOD_DEFINITION(Event, setSeverity)
1138 {
1139 if (!argv[0]->isInteger())
1140 return NXSL_ERR_NOT_STRING;
1141
1142 int s = argv[0]->getValueAsInt32();
1143 if ((s >= SEVERITY_NORMAL) && (s <= SEVERITY_CRITICAL))
1144 {
1145 Event *event = (Event *)object->getData();
1146 event->setSeverity(s);
1147 }
1148 *result = new NXSL_Value();
1149 return 0;
1150 }
1151
1152 /**
1153 * Event::setUserTag() method
1154 */
1155 NXSL_METHOD_DEFINITION(Event, setUserTag)
1156 {
1157 if (!argv[0]->isString())
1158 return NXSL_ERR_NOT_STRING;
1159
1160 Event *event = (Event *)object->getData();
1161 event->setUserTag(argv[0]->getValueAsCString());
1162 *result = new NXSL_Value();
1163 return 0;
1164 }
1165
1166 /**
1167 * Event::toJson() method
1168 */
1169 NXSL_METHOD_DEFINITION(Event, toJson)
1170 {
1171 Event *event = (Event *)object->getData();
1172 *result = new NXSL_Value(event->createJson());
1173 return 0;
1174 }
1175
1176 /**
1177 * NXSL class Event: constructor
1178 */
1179 NXSL_EventClass::NXSL_EventClass() : NXSL_Class()
1180 {
1181 setName(_T("Event"));
1182
1183 NXSL_REGISTER_METHOD(Event, setMessage, 1);
1184 NXSL_REGISTER_METHOD(Event, setSeverity, 1);
1185 NXSL_REGISTER_METHOD(Event, setUserTag, 1);
1186 NXSL_REGISTER_METHOD(Event, toJson, 0);
1187 }
1188
1189 /**
1190 * NXSL class Event: get attribute
1191 */
1192 NXSL_Value *NXSL_EventClass::getAttr(NXSL_Object *pObject, const TCHAR *attr)
1193 {
1194 NXSL_Value *value = NULL;
1195
1196 Event *event = (Event *)pObject->getData();
1197 if (!_tcscmp(attr, _T("code")))
1198 {
1199 value = new NXSL_Value(event->getCode());
1200 }
1201 else if (!_tcscmp(attr, _T("customMessage")))
1202 {
1203 value = new NXSL_Value(event->getCustomMessage());
1204 }
1205 else if (!_tcscmp(attr, _T("id")))
1206 {
1207 value = new NXSL_Value(event->getId());
1208 }
1209 else if (!_tcscmp(attr, _T("message")))
1210 {
1211 value = new NXSL_Value(event->getMessage());
1212 }
1213 else if (!_tcscmp(attr, _T("name")))
1214 {
1215 value = new NXSL_Value(event->getName());
1216 }
1217 else if (!_tcscmp(attr, _T("parameters")))
1218 {
1219 NXSL_Array *array = new NXSL_Array;
1220 for(int i = 0; i < event->getParametersCount(); i++)
1221 array->set(i + 1, new NXSL_Value(event->getParameter(i)));
1222 value = new NXSL_Value(array);
1223 }
1224 else if (!_tcscmp(attr, _T("severity")))
1225 {
1226 value = new NXSL_Value(event->getSeverity());
1227 }
1228 else if (!_tcscmp(attr, _T("source")))
1229 {
1230 NetObj *object = FindObjectById(event->getSourceId());
1231 value = (object != NULL) ? object->createNXSLObject() : new NXSL_Value();
1232 }
1233 else if (!_tcscmp(attr, _T("sourceId")))
1234 {
1235 value = new NXSL_Value(event->getSourceId());
1236 }
1237 else if (!_tcscmp(attr, _T("timestamp")))
1238 {
1239 value = new NXSL_Value((INT64)event->getTimeStamp());
1240 }
1241 else if (!_tcscmp(attr, _T("userTag")))
1242 {
1243 value = new NXSL_Value(event->getUserTag());
1244 }
1245 else
1246 {
1247 if (attr[0] == _T('$'))
1248 {
1249 // Try to find parameter with given index
1250 TCHAR *eptr;
1251 int index = _tcstol(&attr[1], &eptr, 10);
1252 if ((index > 0) && (*eptr == 0))
1253 {
1254 const TCHAR *s = event->getParameter(index - 1);
1255 if (s != NULL)
1256 {
1257 value = new NXSL_Value(s);
1258 }
1259 }
1260 }
1261
1262 // Try to find named parameter with given name
1263 if (value == NULL)
1264 {
1265 const TCHAR *s = event->getNamedParameter(attr);
1266 if (s != NULL)
1267 {
1268 value = new NXSL_Value(s);
1269 }
1270 }
1271 }
1272 return value;
1273 }
1274
1275 /**
1276 * Alarm::acknowledge() method
1277 */
1278 NXSL_METHOD_DEFINITION(Alarm, acknowledge)
1279 {
1280 Alarm *alarm = (Alarm *)object->getData();
1281 *result = new NXSL_Value(AckAlarmById(alarm->getAlarmId(), NULL, false, 0));
1282 return 0;
1283 }
1284
1285 /**
1286 * Alarm::resolve() method
1287 */
1288 NXSL_METHOD_DEFINITION(Alarm, resolve)
1289 {
1290 Alarm *alarm = (Alarm *)object->getData();
1291 *result = new NXSL_Value(ResolveAlarmById(alarm->getAlarmId(), NULL, false));
1292 return 0;
1293 }
1294
1295 /**
1296 * Alarm::terminate() method
1297 */
1298 NXSL_METHOD_DEFINITION(Alarm, terminate)
1299 {
1300 Alarm *alarm = (Alarm *)object->getData();
1301 *result = new NXSL_Value(ResolveAlarmById(alarm->getAlarmId(), NULL, true));
1302 return 0;
1303 }
1304
1305 /**
1306 * NXSL class Alarm: constructor
1307 */
1308 NXSL_AlarmClass::NXSL_AlarmClass() : NXSL_Class()
1309 {
1310 setName(_T("Alarm"));
1311
1312 NXSL_REGISTER_METHOD(Alarm, acknowledge, 0);
1313 NXSL_REGISTER_METHOD(Alarm, resolve, 0);
1314 NXSL_REGISTER_METHOD(Alarm, terminate, 0);
1315 }
1316
1317 /**
1318 * NXSL object destructor
1319 */
1320 void NXSL_AlarmClass::onObjectDelete(NXSL_Object *object)
1321 {
1322 delete (Alarm *)object->getData();
1323 }
1324
1325 /**
1326 * NXSL class Alarm: get attribute
1327 */
1328 NXSL_Value *NXSL_AlarmClass::getAttr(NXSL_Object *pObject, const TCHAR *attr)
1329 {
1330 NXSL_Value *value = NULL;
1331 Alarm *alarm = (Alarm *)pObject->getData();
1332
1333 if (!_tcscmp(attr, _T("ackBy")))
1334 {
1335 value = new NXSL_Value(alarm->getAckByUser());
1336 }
1337 else if (!_tcscmp(attr, _T("creationTime")))
1338 {
1339 value = new NXSL_Value((INT64)alarm->getCreationTime());
1340 }
1341 else if (!_tcscmp(attr, _T("dciId")))
1342 {
1343 value = new NXSL_Value(alarm->getDciId());
1344 }
1345 else if (!_tcscmp(attr, _T("eventCode")))
1346 {
1347 value = new NXSL_Value(alarm->getSourceEventCode());
1348 }
1349 else if (!_tcscmp(attr, _T("eventId")))
1350 {
1351 value = new NXSL_Value(alarm->getSourceEventId());
1352 }
1353 else if (!_tcscmp(attr, _T("helpdeskReference")))
1354 {
1355 value = new NXSL_Value(alarm->getHelpDeskRef());
1356 }
1357 else if (!_tcscmp(attr, _T("helpdeskState")))
1358 {
1359 value = new NXSL_Value(alarm->getHelpDeskState());
1360 }
1361 else if (!_tcscmp(attr, _T("id")))
1362 {
1363 value = new NXSL_Value(alarm->getAlarmId());
1364 }
1365 else if (!_tcscmp(attr, _T("key")))
1366 {
1367 value = new NXSL_Value(alarm->getKey());
1368 }
1369 else if (!_tcscmp(attr, _T("lastChangeTime")))
1370 {
1371 value = new NXSL_Value((INT64)alarm->getLastChangeTime());
1372 }
1373 else if (!_tcscmp(attr, _T("message")))
1374 {
1375 value = new NXSL_Value(alarm->getMessage());
1376 }
1377 else if (!_tcscmp(attr, _T("originalSeverity")))
1378 {
1379 value = new NXSL_Value(alarm->getOriginalSeverity());
1380 }
1381 else if (!_tcscmp(attr, _T("repeatCount")))
1382 {
1383 value = new NXSL_Value(alarm->getRepeatCount());
1384 }
1385 else if (!_tcscmp(attr, _T("resolvedBy")))
1386 {
1387 value = new NXSL_Value(alarm->getResolvedByUser());
1388 }
1389 else if (!_tcscmp(attr, _T("severity")))
1390 {
1391 value = new NXSL_Value(alarm->getCurrentSeverity());
1392 }
1393 else if (!_tcscmp(attr, _T("sourceObject")))
1394 {
1395 value = new NXSL_Value(alarm->getSourceObject());
1396 }
1397 else if (!_tcscmp(attr, _T("state")))
1398 {
1399 value = new NXSL_Value(alarm->getState());
1400 }
1401 return value;
1402 }
1403
1404 /**
1405 * Implementation of "DCI" class: constructor
1406 */
1407 NXSL_DciClass::NXSL_DciClass() : NXSL_Class()
1408 {
1409 setName(_T("DCI"));
1410 }
1411
1412 /**
1413 * Object destructor
1414 */
1415 void NXSL_DciClass::onObjectDelete(NXSL_Object *object)
1416 {
1417 delete (DCObjectInfo *)object->getData();
1418 }
1419
1420 /**
1421 * Implementation of "DCI" class: get attribute
1422 */
1423 NXSL_Value *NXSL_DciClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1424 {
1425 DCObjectInfo *dci;
1426 NXSL_Value *value = NULL;
1427
1428 dci = (DCObjectInfo *)object->getData();
1429 if (!_tcscmp(attr, _T("comments")))
1430 {
1431 value = new NXSL_Value(dci->getComments());
1432 }
1433 else if (!_tcscmp(attr, _T("dataType")) && (dci->getType() == DCO_TYPE_ITEM))
1434 {
1435 value = new NXSL_Value(dci->getDataType());
1436 }
1437 else if (!_tcscmp(attr, _T("description")))
1438 {
1439 value = new NXSL_Value(dci->getDescription());
1440 }
1441 else if (!_tcscmp(attr, _T("errorCount")))
1442 {
1443 value = new NXSL_Value(dci->getErrorCount());
1444 }
1445 else if (!_tcscmp(attr, _T("id")))
1446 {
1447 value = new NXSL_Value(dci->getId());
1448 }
1449 else if ((dci->getType() == DCO_TYPE_ITEM) && !_tcscmp(attr, _T("instance")))
1450 {
1451 value = new NXSL_Value(dci->getInstance());
1452 }
1453 else if (!_tcscmp(attr, _T("lastPollTime")))
1454 {
1455 value = new NXSL_Value((INT64)dci->getLastPollTime());
1456 }
1457 else if (!_tcscmp(attr, _T("name")))
1458 {
1459 value = new NXSL_Value(dci->getName());
1460 }
1461 else if (!_tcscmp(attr, _T("origin")))
1462 {
1463 value = new NXSL_Value((LONG)dci->getOrigin());
1464 }
1465 else if (!_tcscmp(attr, _T("status")))
1466 {
1467 value = new NXSL_Value((LONG)dci->getStatus());
1468 }
1469 else if (!_tcscmp(attr, _T("systemTag")))
1470 {
1471 value = new NXSL_Value(dci->getSystemTag());
1472 }
1473 else if (!_tcscmp(attr, _T("type")))
1474 {
1475 value = new NXSL_Value((LONG)dci->getType());
1476 }
1477 return value;
1478 }
1479
1480 /**
1481 * Implementation of "Sensor" class: constructor
1482 */
1483 NXSL_SensorClass::NXSL_SensorClass() : NXSL_NetObjClass()
1484 {
1485 setName(_T("Sensor"));
1486 }
1487
1488 /**
1489 * Implementation of "Sensor" class: get attribute
1490 */
1491 NXSL_Value *NXSL_SensorClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1492 {
1493 NXSL_Value *value = NXSL_NetObjClass::getAttr(object, attr);
1494 if (value != NULL)
1495 return value;
1496
1497 Sensor *s;
1498
1499 s = (Sensor*)object->getData();
1500 if (!_tcscmp(attr, _T("description")))
1501 {
1502 value = new NXSL_Value(s->getDescription());
1503 }
1504 else if (!_tcscmp(attr, _T("frameCount")))
1505 {
1506 value = new NXSL_Value(s->getFrameCount());
1507 }
1508 else if (!_tcscmp(attr, _T("lastContact")))
1509 {
1510 value = new NXSL_Value((UINT32)s->getLastContact());
1511 }
1512 else if (!_tcscmp(attr, _T("metaType")))
1513 {
1514 value = new NXSL_Value(s->getMetaType());
1515 }
1516 else if (!_tcscmp(attr, _T("protocol")))
1517 {
1518 value = new NXSL_Value(s->getCommProtocol());
1519 }
1520 else if (!_tcscmp(attr, _T("serial")))
1521 {
1522 value = new NXSL_Value(s->getSerialNumber());
1523 }
1524 else if (!_tcscmp(attr, _T("type")))
1525 {
1526 value = new NXSL_Value(s->getSensorClass());
1527 }
1528 else if (!_tcscmp(attr, _T("vendor")))
1529 {
1530 value = new NXSL_Value(s->getVendor());
1531 }
1532
1533 return value;
1534 }
1535
1536 /**
1537 * Implementation of "SNMP_Transport" class: constructor
1538 */
1539 NXSL_SNMPTransportClass::NXSL_SNMPTransportClass() : NXSL_Class()
1540 {
1541 setName(_T("SNMP_Transport"));
1542 }
1543
1544 /**
1545 * Implementation of "SNMP_Transport" class: get attribute
1546 */
1547 NXSL_Value *NXSL_SNMPTransportClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1548 {
1549 NXSL_Value *value = NULL;
1550 SNMP_Transport *t;
1551
1552 t = (SNMP_Transport*)object->getData();
1553 if (!_tcscmp(attr, _T("snmpVersion")))
1554 {
1555 const TCHAR *versions[] = { _T("1"), _T("2c"), _T("3") };
1556 value = new NXSL_Value((const TCHAR*)versions[t->getSnmpVersion()]);
1557 }
1558
1559 return value;
1560 }
1561
1562 /**
1563 * Implementation of "SNMP_Transport" class: NXSL object destructor
1564 */
1565 void NXSL_SNMPTransportClass::onObjectDelete(NXSL_Object *object)
1566 {
1567 delete (SNMP_Transport *)object->getData();
1568 }
1569
1570 /**
1571 * NXSL class SNMP_VarBind: constructor
1572 */
1573 NXSL_SNMPVarBindClass::NXSL_SNMPVarBindClass() : NXSL_Class()
1574 {
1575 setName(_T("SNMP_VarBind"));
1576 }
1577
1578 /**
1579 * NXSL class SNMP_VarBind: get attribute
1580 */
1581 NXSL_Value *NXSL_SNMPVarBindClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1582 {
1583 NXSL_Value *value = NULL;
1584 SNMP_Variable *t = (SNMP_Variable *)object->getData();
1585 if (!_tcscmp(attr, _T("type")))
1586 {
1587 value = new NXSL_Value((UINT32)t->getType());
1588 }
1589 else if (!_tcscmp(attr, _T("name")))
1590 {
1591 value = new NXSL_Value(t->getName().toString());
1592 }
1593 else if (!_tcscmp(attr, _T("value")))
1594 {
1595 TCHAR strValue[1024];
1596 value = new NXSL_Value(t->getValueAsString(strValue, 1024));
1597 }
1598 else if (!_tcscmp(attr, _T("printableValue")))
1599 {
1600 TCHAR strValue[1024];
1601 bool convToHex = true;
1602 t->getValueAsPrintableString(strValue, 1024, &convToHex);
1603 value = new NXSL_Value(strValue);
1604 }
1605 else if (!_tcscmp(attr, _T("valueAsIp")))
1606 {
1607 TCHAR strValue[128];
1608 t->getValueAsIPAddr(strValue);
1609 value = new NXSL_Value(strValue);
1610 }
1611 else if (!_tcscmp(attr, _T("valueAsMac")))
1612 {
1613 TCHAR strValue[128];
1614 t->getValueAsMACAddr(strValue);
1615 value = new NXSL_Value(strValue);
1616 }
1617
1618 return value;
1619 }
1620
1621 /**
1622 * NXSL class SNMP_VarBind: NXSL object desctructor
1623 */
1624 void NXSL_SNMPVarBindClass::onObjectDelete(NXSL_Object *object)
1625 {
1626 delete (SNMP_Variable *)object->getData();
1627 }
1628
1629 /**
1630 * NXSL class ComponentClass: constructor
1631 */
1632 NXSL_ComponentClass::NXSL_ComponentClass() : NXSL_Class()
1633 {
1634 setName(_T("Component"));
1635 }
1636
1637 /**
1638 * NXSL class ComponentClass: get attribute
1639 */
1640 NXSL_Value *NXSL_ComponentClass::getAttr(NXSL_Object *object, const TCHAR *attr)
1641 {
1642 const UINT32 classCount = 12;
1643 static const TCHAR *className[classCount] = { _T(""), _T("other"), _T("unknown"), _T("chassis"), _T("backplane"),
1644 _T("container"), _T("power supply"), _T("fan"), _T("sensor"),
1645 _T("module"), _T("port"), _T("stack") };
1646 NXSL_Value *value = NULL;
1647 Component *component = (Component *)object->getData();
1648 if (!_tcscmp(attr, _T("class")))
1649 {
1650 if (component->getClass() >= classCount)
1651 value = new NXSL_Value(className[2]); // Unknown class
1652 else
1653 value = new NXSL_Value(className[component->getClass()]);
1654 }
1655 else if (!_tcscmp(attr, _T("children")))
1656 {
1657 value = new NXSL_Value(component->getChildrenForNXSL());
1658 }
1659 else if (!_tcscmp(attr, _T("firmware")))
1660 {
1661 value = new NXSL_Value(component->getFirmware());
1662 }
1663 else if (!_tcscmp(attr, _T("model")))
1664 {
1665 value = new NXSL_Value(component->getModel());
1666 }
1667 else if (!_tcscmp(attr, _T("name")))
1668 {
1669 value = new NXSL_Value(component->getName());
1670 }
1671 else if (!_tcscmp(attr, _T("serial")))
1672 {
1673 value = new NXSL_Value(component->getSerial());
1674 }
1675 else if (!_tcscmp(attr, _T("vendor")))
1676 {
1677 value = new NXSL_Value(component->getVendor());
1678 }
1679 return value;
1680 }
1681
1682 /**
1683 * Class objects
1684 */
1685 NXSL_AlarmClass g_nxslAlarmClass;
1686 NXSL_ChassisClass g_nxslChassisClass;
1687 NXSL_ClusterClass g_nxslClusterClass;
1688 NXSL_ComponentClass g_nxslComponentClass;
1689 NXSL_ContainerClass g_nxslContainerClass;
1690 NXSL_DciClass g_nxslDciClass;
1691 NXSL_EventClass g_nxslEventClass;
1692 NXSL_InterfaceClass g_nxslInterfaceClass;
1693 NXSL_MobileDeviceClass g_nxslMobileDeviceClass;
1694 NXSL_NetObjClass g_nxslNetObjClass;
1695 NXSL_NodeClass g_nxslNodeClass;
1696 NXSL_SensorClass g_nxslSensorClass;
1697 NXSL_SNMPTransportClass g_nxslSnmpTransportClass;
1698 NXSL_SNMPVarBindClass g_nxslSnmpVarBindClass;
1699 NXSL_ZoneClass g_nxslZoneClass;