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