60df9f4d648f545deb1d38a575cfcb397926e761
[public/netxms.git] / src / server / core / zone.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004, 2005 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 ** $module: zone.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25
26 //
27 // Zone class default constructor
28 //
29
30 Zone::Zone()
31 :NetObj()
32 {
33 m_dwId = 0;
34 m_dwZoneGUID = 0;
35 m_dwControllerIpAddr = 0;
36 _tcscpy(m_szName, _T("Zone 0"));
37 m_dwAddrListSize = 0;
38 m_pdwIpAddrList = NULL;
39 m_iZoneType = ZONE_TYPE_ACTIVE;
40 }
41
42
43 //
44 // Zone class destructor
45 //
46
47 Zone::~Zone()
48 {
49 safe_free(m_pdwIpAddrList);
50 }
51
52
53 //
54 // Create object from database data
55 //
56
57 BOOL Zone::CreateFromDB(DWORD dwId)
58 {
59 TCHAR szQuery[256];
60 DB_RESULT hResult;
61 DWORD i;
62
63 m_dwId = dwId;
64
65 if (!LoadCommonProperties())
66 return FALSE;
67
68 _sntprintf(szQuery, 256, _T("SELECT zone_guid,zone_type,controller_ip FROM zones WHERE id=%d"), dwId);
69 hResult = DBSelect(g_hCoreDB, szQuery);
70 if (hResult == NULL)
71 return FALSE; // Query failed
72
73 if (DBGetNumRows(hResult) == 0)
74 {
75 DBFreeResult(hResult);
76 if (dwId == BUILTIN_OID_ZONE0)
77 {
78 m_dwZoneGUID = 0;
79 m_iZoneType = ZONE_TYPE_ACTIVE;
80 return TRUE;
81 }
82 else
83 {
84 return FALSE;
85 }
86 }
87
88 m_dwZoneGUID = DBGetFieldULong(hResult, 0, 0);
89 m_iZoneType = DBGetFieldLong(hResult, 0, 1);
90 m_dwControllerIpAddr = DBGetFieldIPAddr(hResult, 0, 2);
91
92 DBFreeResult(hResult);
93
94 // Load IP address list
95 if (m_iZoneType == ZONE_TYPE_PASSIVE)
96 {
97 _sntprintf(szQuery, 256, _T("SELECT ip_addr FROM zone_ip_addr_list WHERE zone_id=%d"), m_dwId);
98 hResult = DBSelect(g_hCoreDB, szQuery);
99 if (hResult != NULL)
100 {
101 m_dwAddrListSize = DBGetNumRows(hResult);
102 m_pdwIpAddrList = (DWORD *)malloc(sizeof(DWORD) * m_dwAddrListSize);
103 for(i = 0; i < m_dwAddrListSize; i++)
104 m_pdwIpAddrList[i] = DBGetFieldIPAddr(hResult, i, 0);
105 DBFreeResult(hResult);
106 }
107 }
108
109 // Load access list
110 LoadACLFromDB();
111
112 return TRUE;
113 }
114
115
116 //
117 // Save object to database
118 //
119
120 BOOL Zone::SaveToDB(DB_HANDLE hdb)
121 {
122 BOOL bNewObject = TRUE;
123 TCHAR szIpAddr[16], szQuery[8192];
124 DB_RESULT hResult;
125 DWORD i;
126
127 LockData();
128
129 SaveCommonProperties(hdb);
130
131 // Check for object's existence in database
132 _sntprintf(szQuery, 8192, _T("SELECT id FROM zones WHERE id=%d"), m_dwId);
133 hResult = DBSelect(hdb, szQuery);
134 if (hResult != 0)
135 {
136 if (DBGetNumRows(hResult) > 0)
137 bNewObject = FALSE;
138 DBFreeResult(hResult);
139 }
140
141 // Form and execute INSERT or UPDATE query
142 if (bNewObject)
143 _sntprintf(szQuery, 8192, _T("INSERT INTO zones (id,zone_guid,zone_type,controller_ip)")
144 _T(" VALUES (%d,%d,%d,'%s')"),
145 m_dwId, m_dwZoneGUID, m_iZoneType,
146 IpToStr(m_dwControllerIpAddr, szIpAddr));
147 else
148 _sntprintf(szQuery, 8192, _T("UPDATE zones SET zone_guid=%d,zone_type=%d,")
149 _T("controller_ip='%s' WHERE id=%d"),
150 m_dwZoneGUID, m_iZoneType,
151 IpToStr(m_dwControllerIpAddr, szIpAddr), m_dwId);
152 DBQuery(hdb, szQuery);
153
154 // Save ip address list
155 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM zone_ip_addr_list WHERE zone_id=%d"), m_dwId);
156 DBQuery(hdb, szQuery);
157 if (m_iZoneType == ZONE_TYPE_PASSIVE)
158 {
159 for(i = 0; i < m_dwAddrListSize; i++)
160 {
161 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("INSERT INTO zone_ip_addr_list (zone_id,ip_addr) VALUES (%d,'%s')"),
162 m_dwId, IpToStr(m_pdwIpAddrList[i], szIpAddr));
163 DBQuery(hdb, szQuery);
164 }
165 }
166
167 SaveACLToDB(hdb);
168
169 // Unlock object and clear modification flag
170 m_bIsModified = FALSE;
171 UnlockData();
172 return TRUE;
173 }
174
175
176 //
177 // Delete zone object from database
178 //
179
180 BOOL Zone::DeleteFromDB(void)
181 {
182 TCHAR szQuery[256];
183 BOOL bSuccess;
184
185 bSuccess = NetObj::DeleteFromDB();
186 if (bSuccess)
187 {
188 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM zones WHERE id=%d"), m_dwId);
189 QueueSQLRequest(szQuery);
190 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM zone_ip_addr_list WHERE zone_id=%d"), m_dwId);
191 QueueSQLRequest(szQuery);
192 }
193 return bSuccess;
194 }
195
196
197 //
198 // Create CSCP message with object's data
199 //
200
201 void Zone::CreateMessage(CSCPMessage *pMsg)
202 {
203 NetObj::CreateMessage(pMsg);
204 pMsg->SetVariable(VID_ZONE_GUID, m_dwZoneGUID);
205 pMsg->SetVariable(VID_ZONE_TYPE, (WORD)m_iZoneType);
206 pMsg->SetVariable(VID_CONTROLLER_IP_ADDR, m_dwControllerIpAddr);
207 pMsg->SetVariable(VID_ADDR_LIST_SIZE, m_dwAddrListSize);
208 pMsg->SetVariableToInt32Array(VID_IP_ADDR_LIST, m_dwAddrListSize, m_pdwIpAddrList);
209 }
210
211
212 //
213 // Modify object from message
214 //
215
216 DWORD Zone::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
217 {
218 if (!bAlreadyLocked)
219 LockData();
220
221 return NetObj::ModifyFromMessage(pRequest, TRUE);
222 }