object save optimization - object properties divided into groups and anly modified...
[public/netxms.git] / src / server / core / agent_policy.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: agent_policy.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Redefined status calculation for policy group
27 */
28 void PolicyGroup::calculateCompoundStatus(BOOL bForcedRecalc)
29 {
30 m_status = STATUS_NORMAL;
31 }
32
33 /**
34 * Called by client session handler to check if threshold summary should be shown for this object.
35 */
36 bool PolicyGroup::showThresholdSummary()
37 {
38 return false;
39 }
40
41 /**
42 * Agent policy default constructor
43 */
44 AgentPolicy::AgentPolicy(int type) : NetObj()
45 {
46 m_version = 0x00010000;
47 m_policyType = type;
48 }
49
50 /**
51 * Constructor for user-initiated object creation
52 */
53 AgentPolicy::AgentPolicy(const TCHAR *name, int type) : NetObj()
54 {
55 nx_strncpy(m_name, name, MAX_OBJECT_NAME);
56 m_version = 0x00010000;
57 m_policyType = type;
58 }
59
60 /**
61 * Save common policy properties to database
62 */
63 bool AgentPolicy::savePolicyCommonProperties(DB_HANDLE hdb)
64 {
65 if (!saveCommonProperties(hdb))
66 return false;
67
68 DB_STATEMENT hStmt;
69 if (!IsDatabaseRecordExist(hdb, _T("ap_common"), _T("id"), m_id))
70 hStmt = DBPrepare(hdb, _T("INSERT INTO ap_common (policy_type,version,id) VALUES (?,?,?)"));
71 else
72 hStmt = DBPrepare(hdb, _T("UPDATE ap_common SET policy_type=?,version=? WHERE id=?"));
73 if (hStmt == NULL)
74 return false;
75
76 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_policyType);
77 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_version);
78 DBBind(hStmt, 3, DB_SQLTYPE_INTEGER, m_id);
79 bool success = DBExecute(hStmt);
80 DBFreeStatement(hStmt);
81
82 // Save access list
83 if (success)
84 success = saveACLToDB(hdb);
85
86 // Update node bindings
87 if (success)
88 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_bindings WHERE policy_id=?"));
89
90 lockChildList(false);
91 if (success && (m_childList->size() > 0))
92 {
93 hStmt = DBPrepare(hdb, _T("INSERT INTO ap_bindings (policy_id,node_id) VALUES (?,?)"));
94 if (hStmt != NULL)
95 {
96 DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_id);
97 for(int i = 0; (i < m_childList->size()) && success; i++)
98 {
99 DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_childList->get(i)->getId());
100 success = DBExecute(hStmt);
101 }
102 DBFreeStatement(hStmt);
103 }
104 else
105 {
106 success = false;
107 }
108 }
109 unlockChildList();
110 return success;
111 }
112
113 /**
114 * Save to database
115 */
116 bool AgentPolicy::saveToDatabase(DB_HANDLE hdb)
117 {
118 lockProperties();
119
120 bool success = savePolicyCommonProperties(hdb);
121
122 // Clear modifications flag and unlock object
123 if (success)
124 m_modified = 0;
125 unlockProperties();
126
127 return success;
128 }
129
130 /**
131 * Delete from database
132 */
133 bool AgentPolicy::deleteFromDatabase(DB_HANDLE hdb)
134 {
135 bool success = NetObj::deleteFromDatabase(hdb);
136 if (success)
137 {
138 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_common WHERE id=?"));
139 if (success)
140 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_bindings WHERE policy_id=?"));
141 }
142 return success;
143 }
144
145 /**
146 * Load from database
147 */
148 bool AgentPolicy::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
149 {
150 m_id = dwId;
151
152 if (!loadCommonProperties(hdb))
153 {
154 DbgPrintf(2, _T("Cannot load common properties for agent policy object %d"), dwId);
155 return false;
156 }
157
158 if (!m_isDeleted)
159 {
160 TCHAR query[256];
161
162 loadACLFromDB(hdb);
163
164 _sntprintf(query, 256, _T("SELECT version FROM ap_common WHERE id=%d"), dwId);
165 DB_RESULT hResult = DBSelect(hdb, query);
166 if (hResult == NULL)
167 return false;
168
169 m_version = DBGetFieldULong(hResult, 0, 0);
170 DBFreeResult(hResult);
171
172 // Load related nodes list
173 _sntprintf(query, 256, _T("SELECT node_id FROM ap_bindings WHERE policy_id=%d"), m_id);
174 hResult = DBSelect(hdb, query);
175 if (hResult != NULL)
176 {
177 int numNodes = DBGetNumRows(hResult);
178 for(int i = 0; i < numNodes; i++)
179 {
180 UINT32 nodeId = DBGetFieldULong(hResult, i, 0);
181 NetObj *object = FindObjectById(nodeId);
182 if (object != NULL)
183 {
184 if (object->getObjectClass() == OBJECT_NODE)
185 {
186 addChild(object);
187 object->addParent(this);
188 }
189 else
190 {
191 nxlog_write(MSG_AP_BINDING_NOT_NODE, EVENTLOG_ERROR_TYPE, "dd", m_id, nodeId);
192 }
193 }
194 else
195 {
196 nxlog_write(MSG_INVALID_AP_BINDING, EVENTLOG_ERROR_TYPE, "dd", m_id, nodeId);
197 }
198 }
199 DBFreeResult(hResult);
200 }
201 }
202
203 return true;
204 }
205
206 /**
207 * Create NXCP message with policy data
208 */
209 void AgentPolicy::fillMessageInternal(NXCPMessage *msg)
210 {
211 NetObj::fillMessageInternal(msg);
212 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
213 msg->setField(VID_VERSION, m_version);
214 }
215
216 /**
217 * Modify policy from message
218 */
219 UINT32 AgentPolicy::modifyFromMessageInternal(NXCPMessage *pRequest)
220 {
221 return NetObj::modifyFromMessageInternal(pRequest);
222 }
223
224 /**
225 * Create deployment message
226 */
227 bool AgentPolicy::createDeploymentMessage(NXCPMessage *msg)
228 {
229 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
230 msg->setField(VID_GUID, m_guid);
231 return true;
232 }
233
234 /**
235 * Create uninstall message
236 */
237 bool AgentPolicy::createUninstallMessage(NXCPMessage *msg)
238 {
239 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
240 msg->setField(VID_GUID, m_guid);
241 return true;
242 }
243
244 /**
245 * Serialize object to JSON
246 */
247 json_t *AgentPolicy::toJson()
248 {
249 json_t *root = NetObj::toJson();
250 json_object_set_new(root, "version", json_integer(m_version));
251 json_object_set_new(root, "policyType", json_integer(m_policyType));
252 return root;
253 }