one default zone proxy node for all protocols
[public/netxms.git] / src / server / core / zone.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 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: zone.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Zone class default constructor
27 */
28 Zone::Zone() : NetObj()
29 {
30 m_id = 0;
31 m_zoneId = 0;
32 _tcscpy(m_name, _T("Default"));
33 m_proxyNodeId = 0;
34 m_idxNodeByAddr = new InetAddressIndex;
35 m_idxInterfaceByAddr = new InetAddressIndex;
36 m_idxSubnetByAddr = new InetAddressIndex;
37 }
38
39 /**
40 * Constructor for new zone object
41 */
42 Zone::Zone(UINT32 zoneId, const TCHAR *name) : NetObj()
43 {
44 m_id = 0;
45 m_zoneId = zoneId;
46 nx_strncpy(m_name, name, MAX_OBJECT_NAME);
47 m_proxyNodeId = 0;
48 m_idxNodeByAddr = new InetAddressIndex;
49 m_idxInterfaceByAddr = new InetAddressIndex;
50 m_idxSubnetByAddr = new InetAddressIndex;
51 }
52
53 /**
54 * Zone class destructor
55 */
56 Zone::~Zone()
57 {
58 delete m_idxNodeByAddr;
59 delete m_idxInterfaceByAddr;
60 delete m_idxSubnetByAddr;
61 }
62
63 /**
64 * Create object from database data
65 */
66 bool Zone::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
67 {
68 m_id = dwId;
69
70 if (!loadCommonProperties(hdb))
71 return false;
72
73 TCHAR szQuery[256];
74 _sntprintf(szQuery, 256, _T("SELECT zone_guid,proxy_node FROM zones WHERE id=%d"), dwId);
75 DB_RESULT hResult = DBSelect(hdb, szQuery);
76 if (hResult == NULL)
77 return false; // Query failed
78
79 if (DBGetNumRows(hResult) == 0)
80 {
81 DBFreeResult(hResult);
82 if (dwId == BUILTIN_OID_ZONE0)
83 {
84 m_zoneId = 0;
85 return true;
86 }
87 else
88 {
89 DbgPrintf(4, _T("Cannot load zone object %ld - missing record in \"zones\" table"), (long)m_id);
90 return false;
91 }
92 }
93
94 m_zoneId = DBGetFieldULong(hResult, 0, 0);
95 m_proxyNodeId = DBGetFieldULong(hResult, 0, 1);
96
97 DBFreeResult(hResult);
98
99 // Load access list
100 loadACLFromDB(hdb);
101
102 return true;
103 }
104
105 /**
106 * Save object to database
107 */
108 BOOL Zone::saveToDatabase(DB_HANDLE hdb)
109 {
110 lockProperties();
111
112 bool success = saveCommonProperties(hdb);
113 if (success)
114 {
115 DB_STATEMENT hStmt;
116 if (IsDatabaseRecordExist(hdb, _T("zones"), _T("id"), m_id))
117 {
118 hStmt = DBPrepare(hdb, _T("UPDATE zones SET zone_guid=?,proxy_node=? WHERE id=?"));
119 }
120 else
121 {
122 hStmt = DBPrepare(hdb, _T("INSERT INTO zones (zone_guid,proxy_node,id) VALUES (?,?,?)"));
123 }
124 if (hStmt != NULL)
125 {
126 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_zoneId);
127 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_proxyNodeId);
128 DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, m_id);
129 success = DBExecute(hStmt);
130 DBFreeStatement(hStmt);
131 }
132 else
133 {
134 success = false;
135 }
136
137 if (success)
138 success = saveACLToDB(hdb);
139 }
140 // Unlock object and clear modification flag
141 m_isModified = false;
142 unlockProperties();
143 return success;
144 }
145
146 /**
147 * Delete zone object from database
148 */
149 bool Zone::deleteFromDatabase(DB_HANDLE hdb)
150 {
151 bool success = NetObj::deleteFromDatabase(hdb);
152 if (success)
153 success = executeQueryOnObject(hdb, _T("DELETE FROM zones WHERE id=?"));
154 return success;
155 }
156
157 /**
158 * Create NXCP message with object's data
159 */
160 void Zone::fillMessageInternal(NXCPMessage *msg)
161 {
162 NetObj::fillMessageInternal(msg);
163 msg->setField(VID_ZONE_ID, m_zoneId);
164 msg->setField(VID_ZONE_PROXY, m_proxyNodeId);
165 }
166
167 /**
168 * Modify object from message
169 */
170 UINT32 Zone::modifyFromMessageInternal(NXCPMessage *request)
171 {
172 if (request->isFieldExist(VID_ZONE_PROXY))
173 m_proxyNodeId = request->getFieldAsUInt32(VID_ZONE_PROXY);
174
175 return NetObj::modifyFromMessageInternal(request);
176 }
177
178 /**
179 * Update interface index
180 */
181 void Zone::updateInterfaceIndex(const InetAddress& oldIp, const InetAddress& newIp, Interface *iface)
182 {
183 m_idxInterfaceByAddr->remove(oldIp);
184 m_idxInterfaceByAddr->put(newIp, iface);
185 }
186
187 /**
188 * Called by client session handler to check if threshold summary should be shown for this object.
189 */
190 bool Zone::showThresholdSummary()
191 {
192 return true;
193 }
194
195 /**
196 * Remove interface from index
197 */
198 void Zone::removeFromIndex(Interface *iface)
199 {
200 const ObjectArray<InetAddress> *list = iface->getIpAddressList()->getList();
201 for(int i = 0; i < list->size(); i++)
202 {
203 InetAddress *addr = list->get(i);
204 if (addr->isValidUnicast())
205 {
206 NetObj *o = m_idxInterfaceByAddr->get(*addr);
207 if ((o != NULL) && (o->getId() == iface->getId()))
208 {
209 m_idxInterfaceByAddr->remove(*addr);
210 }
211 }
212 }
213 }
214
215 /**
216 * Create NXSL object for this object
217 */
218 NXSL_Value *Zone::createNXSLObject()
219 {
220 return new NXSL_Value(new NXSL_Object(&g_nxslZoneClass, this));
221 }