code refactoring; preparation for zoning support
[public/netxms.git] / src / server / core / zone.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2011 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 //
27 // Zone class default constructor
28 //
29
30 Zone::Zone()
31 :NetObj()
32 {
33 m_dwId = 0;
34 m_zoneId = 0;
35 _tcscpy(m_szName, _T("Default"));
36 m_agentProxy = 0;
37 m_snmpProxy = 0;
38 m_icmpProxy = 0;
39 }
40
41
42 //
43 // Zone class destructor
44 //
45
46 Zone::~Zone()
47 {
48 }
49
50
51 //
52 // Create object from database data
53 //
54
55 BOOL Zone::CreateFromDB(DWORD dwId)
56 {
57 TCHAR szQuery[256];
58 DB_RESULT hResult;
59
60 m_dwId = dwId;
61
62 if (!LoadCommonProperties())
63 return FALSE;
64
65 _sntprintf(szQuery, 256, _T("SELECT zone_guid,agent_proxy,snmp_proxy,icmp_proxy FROM zones WHERE id=%d"), dwId);
66 hResult = DBSelect(g_hCoreDB, szQuery);
67 if (hResult == NULL)
68 return FALSE; // Query failed
69
70 if (DBGetNumRows(hResult) == 0)
71 {
72 DBFreeResult(hResult);
73 if (dwId == BUILTIN_OID_ZONE0)
74 {
75 m_zoneId = 0;
76 return TRUE;
77 }
78 else
79 {
80 return FALSE;
81 }
82 }
83
84 m_zoneId = DBGetFieldULong(hResult, 0, 0);
85 m_agentProxy = DBGetFieldULong(hResult, 0, 1);
86 m_snmpProxy = DBGetFieldULong(hResult, 0, 2);
87 m_icmpProxy = DBGetFieldULong(hResult, 0, 3);
88
89 DBFreeResult(hResult);
90
91 // Load access list
92 LoadACLFromDB();
93
94 return TRUE;
95 }
96
97
98 //
99 // Save object to database
100 //
101
102 BOOL Zone::SaveToDB(DB_HANDLE hdb)
103 {
104 BOOL bNewObject = TRUE;
105 TCHAR szQuery[8192];
106 DB_RESULT hResult;
107
108 LockData();
109
110 SaveCommonProperties(hdb);
111
112 // Check for object's existence in database
113 _sntprintf(szQuery, 8192, _T("SELECT id FROM zones WHERE id=%d"), m_dwId);
114 hResult = DBSelect(hdb, szQuery);
115 if (hResult != 0)
116 {
117 if (DBGetNumRows(hResult) > 0)
118 bNewObject = FALSE;
119 DBFreeResult(hResult);
120 }
121
122 // Form and execute INSERT or UPDATE query
123 if (bNewObject)
124 _sntprintf(szQuery, 8192, _T("INSERT INTO zones (id,zone_guid,agent_proxy,snmp_proxy,icmp_proxy)")
125 _T(" VALUES (%d,%d,%d,%d,%d)"),
126 m_dwId, m_zoneId, m_agentProxy, m_snmpProxy, m_icmpProxy);
127 else
128 _sntprintf(szQuery, 8192, _T("UPDATE zones SET zone_guid=%d,agent_proxy=%d,")
129 _T("snmp_proxy=%d,icmp_proxy=%d WHERE id=%d"),
130 m_zoneId, m_agentProxy, m_snmpProxy, m_icmpProxy, m_dwId);
131 DBQuery(hdb, szQuery);
132
133 SaveACLToDB(hdb);
134
135 // Unlock object and clear modification flag
136 m_bIsModified = FALSE;
137 UnlockData();
138 return TRUE;
139 }
140
141
142 //
143 // Delete zone object from database
144 //
145
146 BOOL Zone::DeleteFromDB()
147 {
148 TCHAR szQuery[256];
149 BOOL bSuccess;
150
151 bSuccess = NetObj::DeleteFromDB();
152 if (bSuccess)
153 {
154 _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("DELETE FROM zones WHERE id=%d"), m_dwId);
155 QueueSQLRequest(szQuery);
156 }
157 return bSuccess;
158 }
159
160
161 //
162 // Create CSCP message with object's data
163 //
164
165 void Zone::CreateMessage(CSCPMessage *pMsg)
166 {
167 NetObj::CreateMessage(pMsg);
168 pMsg->SetVariable(VID_ZONE_ID, m_zoneId);
169 pMsg->SetVariable(VID_AGENT_PROXY, m_agentProxy);
170 pMsg->SetVariable(VID_SNMP_PROXY, m_snmpProxy);
171 pMsg->SetVariable(VID_ICMP_PROXY, m_icmpProxy);
172 }
173
174
175 //
176 // Modify object from message
177 //
178
179 DWORD Zone::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
180 {
181 if (!bAlreadyLocked)
182 LockData();
183
184 if (pRequest->IsVariableExist(VID_AGENT_PROXY))
185 m_agentProxy = pRequest->GetVariableLong(VID_AGENT_PROXY);
186
187 if (pRequest->IsVariableExist(VID_SNMP_PROXY))
188 m_agentProxy = pRequest->GetVariableLong(VID_SNMP_PROXY);
189
190 if (pRequest->IsVariableExist(VID_ICMP_PROXY))
191 m_agentProxy = pRequest->GetVariableLong(VID_ICMP_PROXY);
192
193 return NetObj::ModifyFromMessage(pRequest, TRUE);
194 }