2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2017 Victor Kirhenshtein
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.
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.
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.
26 * Dump index to console
28 void DumpIndex(CONSOLE_CTX pCtx
, InetAddressIndex
*index
);
31 * Zone class default constructor
33 Zone
::Zone() : NetObj()
37 _tcscpy(m_name
, _T("Default"));
39 m_idxNodeByAddr
= new InetAddressIndex
;
40 m_idxInterfaceByAddr
= new InetAddressIndex
;
41 m_idxSubnetByAddr
= new InetAddressIndex
;
45 * Constructor for new zone object
47 Zone
::Zone(UINT32 uin
, const TCHAR
*name
) : NetObj()
51 nx_strncpy(m_name
, name
, MAX_OBJECT_NAME
);
53 m_idxNodeByAddr
= new InetAddressIndex
;
54 m_idxInterfaceByAddr
= new InetAddressIndex
;
55 m_idxSubnetByAddr
= new InetAddressIndex
;
59 * Zone class destructor
63 delete m_idxNodeByAddr
;
64 delete m_idxInterfaceByAddr
;
65 delete m_idxSubnetByAddr
;
69 * Create object from database data
71 bool Zone
::loadFromDatabase(DB_HANDLE hdb
, UINT32 dwId
)
75 if (!loadCommonProperties(hdb
))
79 _sntprintf(szQuery
, 256, _T("SELECT zone_guid,proxy_node FROM zones WHERE id=%d"), dwId
);
80 DB_RESULT hResult
= DBSelect(hdb
, szQuery
);
82 return false; // Query failed
84 if (DBGetNumRows(hResult
) == 0)
86 DBFreeResult(hResult
);
87 if (dwId
== BUILTIN_OID_ZONE0
)
94 DbgPrintf(4, _T("Cannot load zone object %ld - missing record in \"zones\" table"), (long)m_id
);
99 m_uin
= DBGetFieldULong(hResult
, 0, 0);
100 m_proxyNodeId
= DBGetFieldULong(hResult
, 0, 1);
102 DBFreeResult(hResult
);
111 * Save object to database
113 bool Zone
::saveToDatabase(DB_HANDLE hdb
)
117 bool success
= saveCommonProperties(hdb
);
118 if (success
&& (m_modified
& MODIFY_OTHER
))
121 if (IsDatabaseRecordExist(hdb
, _T("zones"), _T("id"), m_id
))
123 hStmt
= DBPrepare(hdb
, _T("UPDATE zones SET zone_guid=?,proxy_node=? WHERE id=?"));
127 hStmt
= DBPrepare(hdb
, _T("INSERT INTO zones (zone_guid,proxy_node,id) VALUES (?,?,?)"));
131 DBBind(hStmt
, 1, DB_SQLTYPE_INTEGER
, m_uin
);
132 DBBind(hStmt
, 2, DB_SQLTYPE_INTEGER
, m_proxyNodeId
);
133 DBBind(hStmt
, 3, DB_SQLTYPE_INTEGER
, m_id
);
134 success
= DBExecute(hStmt
);
135 DBFreeStatement(hStmt
);
144 success
= saveACLToDB(hdb
);
146 // Unlock object and clear modification flag
153 * Delete zone object from database
155 bool Zone
::deleteFromDatabase(DB_HANDLE hdb
)
157 bool success
= NetObj
::deleteFromDatabase(hdb
);
159 success
= executeQueryOnObject(hdb
, _T("DELETE FROM zones WHERE id=?"));
164 * Create NXCP message with object's data
166 void Zone
::fillMessageInternal(NXCPMessage
*msg
)
168 NetObj
::fillMessageInternal(msg
);
169 msg
->setField(VID_ZONE_UIN
, m_uin
);
170 msg
->setField(VID_ZONE_PROXY
, m_proxyNodeId
);
174 * Modify object from message
176 UINT32 Zone
::modifyFromMessageInternal(NXCPMessage
*request
)
178 if (request
->isFieldExist(VID_ZONE_PROXY
))
179 m_proxyNodeId
= request
->getFieldAsUInt32(VID_ZONE_PROXY
);
181 return NetObj
::modifyFromMessageInternal(request
);
185 * Update interface index
187 void Zone
::updateInterfaceIndex(const InetAddress
& oldIp
, const InetAddress
& newIp
, Interface
*iface
)
189 m_idxInterfaceByAddr
->remove(oldIp
);
190 m_idxInterfaceByAddr
->put(newIp
, iface
);
194 * Called by client session handler to check if threshold summary should be shown for this object.
196 bool Zone
::showThresholdSummary()
202 * Remove interface from index
204 void Zone
::removeFromIndex(Interface
*iface
)
206 const ObjectArray
<InetAddress
> *list
= iface
->getIpAddressList()->getList();
207 for(int i
= 0; i
< list
->size(); i
++)
209 InetAddress
*addr
= list
->get(i
);
210 if (addr
->isValidUnicast())
212 NetObj
*o
= m_idxInterfaceByAddr
->get(*addr
);
213 if ((o
!= NULL
) && (o
->getId() == iface
->getId()))
215 m_idxInterfaceByAddr
->remove(*addr
);
222 * Create NXSL object for this object
224 NXSL_Value
*Zone
::createNXSLObject()
226 return new NXSL_Value(new NXSL_Object(&g_nxslZoneClass
, this));
230 * Dump interface index to console
232 void Zone
::dumpInterfaceIndex(CONSOLE_CTX console
)
234 DumpIndex(console
, m_idxInterfaceByAddr
);
238 * Dump node index to console
240 void Zone
::dumpNodeIndex(CONSOLE_CTX console
)
242 DumpIndex(console
, m_idxNodeByAddr
);
246 * Dump subnet index to console
248 void Zone
::dumpSubnetIndex(CONSOLE_CTX console
)
250 DumpIndex(console
, m_idxSubnetByAddr
);
254 * Serialize object to JSON
256 json_t
*Zone
::toJson()
258 json_t
*root
= NetObj
::toJson();
259 json_object_set_new(root
, "uin", json_integer(m_uin
));
260 json_object_set_new(root
, "proxyNodeId", json_integer(m_proxyNodeId
));