custom attribute change only marks object as modified if attribute value really chang...
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 16 Oct 2017 16:04:00 +0000 (19:04 +0300)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 16 Oct 2017 16:04:00 +0000 (19:04 +0300)
src/server/core/events.cpp
src/server/core/ndd.cpp
src/server/core/netobj.cpp
src/server/core/node.cpp
src/server/core/nxsl_classes.cpp
src/server/core/nxslext.cpp
src/server/include/nms_objects.h

index a2cf07f..3c0d3e4 100644 (file)
@@ -882,13 +882,14 @@ TCHAR *Event::expandText(Event *event, UINT32 sourceObject, const TCHAR *textTem
                                                {
                                                        scriptName[i] = 0;
                                                        StrStrip(scriptName);
-                                                       const TCHAR *temp = pObject->getCustomAttribute(scriptName);
+                                                       TCHAR *temp = pObject->getCustomAttributeCopy(scriptName);
                                                        if (temp != NULL)
                                                        {
                                                                dwSize += (UINT32)_tcslen(temp);
                                                                pText = (TCHAR *)realloc(pText, dwSize * sizeof(TCHAR));
                                                                _tcscpy(&pText[dwPos], temp);
                                                                dwPos += (UINT32)_tcslen(temp);
+                                                               free(temp);
                                                        }
                                                }
                                                break;
index 5486f9a..5a92941 100644 (file)
@@ -175,8 +175,8 @@ NetworkDeviceDriver *FindDriverForNode(Node *node, SNMP_Transport *pTransport)
        }
 
    // Manual driver selection
-   const TCHAR *driverName = node->getCustomAttribute(_T("snmp.driver"));
-   if (driverName != NULL)
+       TCHAR driverName[64];
+   if (node->getCustomAttribute(_T("snmp.driver"), driverName, 64) != NULL)
    {
       NetworkDeviceDriver *driver = FindDriverByName(driverName);
       if (driver != NULL)
index 9f5d7e9..0829d31 100644 (file)
@@ -2226,6 +2226,101 @@ void NetObj::leaveMaintenanceMode()
    unlockChildList();
 }
 
+/**
+ * Set custom attribute
+ */
+void NetObj::setCustomAttribute(const TCHAR *name, const TCHAR *value)
+{
+   lockProperties();
+   const TCHAR *curr = m_customAttributes.get(name);
+   if ((curr == NULL) || _tcscmp(curr, value))
+   {
+      m_customAttributes.set(name, value);
+      setModified();
+   }
+   unlockProperties();
+}
+
+/**
+ * Set custom attribute (value is preallocated)
+ */
+void NetObj::setCustomAttributePV(const TCHAR *name, TCHAR *value)
+{
+   lockProperties();
+   const TCHAR *curr = m_customAttributes.get(name);
+   if ((curr == NULL) || _tcscmp(curr, value))
+   {
+      m_customAttributes.setPreallocated(_tcsdup(name), value);
+      setModified();
+   }
+   else
+   {
+      free(value);
+   }
+   unlockProperties();
+}
+
+/**
+ * Delete custom attribute
+ */
+void NetObj::deleteCustomAttribute(const TCHAR *name)
+{
+   lockProperties();
+   if (m_customAttributes.contains(name))
+   {
+      m_customAttributes.remove(name);
+      setModified();
+   }
+   unlockProperties();
+}
+
+/**
+ * Get custom attribute into buffer
+ */
+TCHAR *NetObj::getCustomAttribute(const TCHAR *name, TCHAR *buffer, size_t size) const
+{
+   TCHAR *result;
+   lockProperties();
+   const TCHAR *value = m_customAttributes.get(name);
+   if (value != NULL)
+   {
+      _tcslcpy(buffer, value, size);
+      result = buffer;
+   }
+   else
+   {
+      result = NULL;
+   }
+   unlockProperties();
+   return result;
+}
+
+/**
+ * Get copy of custom attribute. Returned value must be freed by caller
+ */
+TCHAR *NetObj::getCustomAttributeCopy(const TCHAR *name) const
+{
+   lockProperties();
+   const TCHAR *value = m_customAttributes.get(name);
+   TCHAR *result = _tcsdup_ex(value);
+   unlockProperties();
+   return result;
+}
+
+/**
+ * Get custom attribute as NXSL value
+ */
+NXSL_Value *NetObj::getCustomAttributeForNXSL(const TCHAR *name) const
+{
+   NXSL_Value *value = NULL;
+   lockProperties();
+   const TCHAR *av = m_customAttributes.get(name);
+   if (av != NULL)
+      value = new NXSL_Value(av);
+   unlockProperties();
+   return value;
+}
+
 /**
  * Get all custom attributes as NXSL hash map
  */
index ffaed19..cac7abc 100644 (file)
@@ -3700,10 +3700,9 @@ bool Node::connectToSMCLP()
       DbgPrintf(7, _T("Node::connectToSMCLP(%s [%d]): existing connection reset"), m_name, m_id);
    }
 
-   const TCHAR *login = getCustomAttribute(_T("iLO.login"));
-   const TCHAR *password = getCustomAttribute(_T("iLO.password"));
-
-   if ((login != NULL) && (password != NULL))
+   TCHAR login[64], password[64];
+   if ((getCustomAttribute(_T("iLO.login"), login, 64) != NULL) &&
+       (getCustomAttribute(_T("iLO.password"), password, 64) != NULL))
       return m_smclpConnection->connect(login, password);
    return false;
 }
@@ -7257,13 +7256,14 @@ TCHAR *Node::expandText(const TCHAR *textTemplate, StringMap *inputFields, const
                   {
                      scriptName[i] = 0;
                      StrStrip(scriptName);
-                     const TCHAR *temp = getCustomAttribute(scriptName);
+                     TCHAR *temp = getCustomAttributeCopy(scriptName);
                      if (temp != NULL)
                      {
                         dwSize += (UINT32)_tcslen(temp);
                         pText = (TCHAR *)realloc(pText, dwSize * sizeof(TCHAR));
                         _tcscpy(&pText[dwPos], temp);
                         dwPos += (UINT32)_tcslen(temp);
+                        free(temp);
                      }
                   }
                   break;
index 0052389..48c3a15 100644 (file)
@@ -282,11 +282,7 @@ NXSL_Value *NXSL_NetObjClass::getAttr(NXSL_Object *_object, const TCHAR *attr)
    }
        else
        {
-               const TCHAR *attrValue = object->getCustomAttribute(attr);
-               if (attrValue != NULL)
-               {
-                       value = new NXSL_Value(attrValue);
-               }
+               value = object->getCustomAttributeForNXSL(attr);
        }
    return value;
 }
index 036a191..eb003dc 100644 (file)
@@ -44,30 +44,19 @@ int F_GetSyslogRuleMatchCount(int argc, NXSL_Value **argv, NXSL_Value **result,
  */
 static int F_GetCustomAttribute(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_VM *vm)
 {
-       NXSL_Object *object;
-       const TCHAR *value;
-
        if (!argv[0]->isObject())
                return NXSL_ERR_NOT_OBJECT;
 
        if (!argv[1]->isString())
                return NXSL_ERR_NOT_STRING;
 
-       object = argv[0]->getValueAsObject();
+       NXSL_Object *object = argv[0]->getValueAsObject();
        if (!object->getClass()->instanceOf(g_nxslNetObjClass.getName()))
                return NXSL_ERR_BAD_CLASS;
 
        NetObj *netxmsObject = (NetObj *)object->getData();
-       value = netxmsObject->getCustomAttribute(argv[1]->getValueAsCString());
-       if (value != NULL)
-       {
-               *ppResult = new NXSL_Value(value);
-       }
-       else
-       {
-               *ppResult = new NXSL_Value;     // Return NULL if attribute not found
-       }
-
+       NXSL_Value *value = netxmsObject->getCustomAttributeForNXSL(argv[1]->getValueAsCString());
+       *ppResult = (value != NULL) ? value : new NXSL_Value(); // Return NULL if attribute not found
        return 0;
 }
 
@@ -78,32 +67,20 @@ static int F_GetCustomAttribute(int argc, NXSL_Value **argv, NXSL_Value **ppResu
  */
 static int F_SetCustomAttribute(int argc, NXSL_Value **argv, NXSL_Value **ppResult, NXSL_VM *vm)
 {
-       NXSL_Object *object;
-       const TCHAR *value;
-
        if (!argv[0]->isObject())
                return NXSL_ERR_NOT_OBJECT;
 
        if (!argv[1]->isString() || !argv[2]->isString())
                return NXSL_ERR_NOT_STRING;
 
-       object = argv[0]->getValueAsObject();
+       NXSL_Object *object = argv[0]->getValueAsObject();
        if (!object->getClass()->instanceOf(g_nxslNetObjClass.getName()))
                return NXSL_ERR_BAD_CLASS;
 
        NetObj *netxmsObject = (NetObj *)object->getData();
-       value = netxmsObject->getCustomAttribute(argv[1]->getValueAsCString());
-       if (value != NULL)
-       {
-               *ppResult = new NXSL_Value(value);
-       }
-       else
-       {
-               *ppResult = new NXSL_Value;     // Return NULL if attribute not found
-       }
-
+   NXSL_Value *value = netxmsObject->getCustomAttributeForNXSL(argv[1]->getValueAsCString());
+   *ppResult = (value != NULL) ? value : new NXSL_Value(); // Return NULL if attribute not found
        netxmsObject->setCustomAttribute(argv[1]->getValueAsCString(), argv[2]->getValueAsCString());
-
        return 0;
 }
 
index a3cae91..b564e08 100644 (file)
@@ -641,11 +641,13 @@ public:
    void addChildNodesToList(ObjectArray<Node> *nodeList, UINT32 dwUserId);
    void addChildDCTargetsToList(ObjectArray<DataCollectionTarget> *dctList, UINT32 dwUserId);
 
-   const TCHAR *getCustomAttribute(const TCHAR *name) { return m_customAttributes.get(name); }
-   void setCustomAttribute(const TCHAR *name, const TCHAR *value) { m_customAttributes.set(name, value); setModified(); }
-   void setCustomAttributePV(const TCHAR *name, TCHAR *value) { m_customAttributes.setPreallocated(_tcsdup(name), value); setModified(); }
-   void deleteCustomAttribute(const TCHAR *name) { m_customAttributes.remove(name); setModified(); }
+   TCHAR *getCustomAttribute(const TCHAR *name, TCHAR *buffer, size_t size) const;
+   TCHAR *getCustomAttributeCopy(const TCHAR *name) const;
+   NXSL_Value *getCustomAttributeForNXSL(const TCHAR *name) const;
    NXSL_Value *getCustomAttributesForNXSL() const;
+   void setCustomAttribute(const TCHAR *name, const TCHAR *value);
+   void setCustomAttributePV(const TCHAR *name, TCHAR *value);
+   void deleteCustomAttribute(const TCHAR *name);
 
    virtual NXSL_Value *createNXSLObject();