Fixed bug in saving common agent policy properties to DB
[public/netxms.git] / src / server / core / agent_policy.cpp
CommitLineData
cd68963b 1/*
a6d66d3a 2** NetXMS - Network Management System
db091a1f 3** Copyright (C) 2003-2016 Victor Kirhenshtein
a6d66d3a
VK
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
27bbb906
VK
25/**
26 * Redefined status calculation for policy group
27 */
27f9598d 28void PolicyGroup::calculateCompoundStatus(BOOL bForcedRecalc)
a6d66d3a 29{
db091a1f 30 m_status = STATUS_NORMAL;
a6d66d3a
VK
31}
32
27bbb906
VK
33/**
34 * Called by client session handler to check if threshold summary should be shown for this object.
35 */
36bool PolicyGroup::showThresholdSummary()
37{
38 return false;
39}
a6d66d3a 40
27bbb906
VK
41/**
42 * Agent policy default constructor
43 */
94b0e18f 44AgentPolicy::AgentPolicy(int type) : NetObj()
a6d66d3a
VK
45{
46 m_version = 0x00010000;
47 m_policyType = type;
a6d66d3a
VK
48}
49
94b0e18f
VK
50/**
51 * Constructor for user-initiated object creation
52 */
53AgentPolicy::AgentPolicy(const TCHAR *name, int type) : NetObj()
45d84f8a 54{
c42b4551 55 nx_strncpy(m_name, name, MAX_OBJECT_NAME);
45d84f8a
VK
56 m_version = 0x00010000;
57 m_policyType = type;
a6d66d3a 58}
6e53f004 59
94b0e18f
VK
60/**
61 * Save common policy properties to database
62 */
63bool AgentPolicy::savePolicyCommonProperties(DB_HANDLE hdb)
6e53f004 64{
94b0e18f
VK
65 if (!saveCommonProperties(hdb))
66 return false;
f2bb4aa1 67
94b0e18f 68 DB_STATEMENT hStmt;
41187a33 69 if (!IsDatabaseRecordExist(hdb, _T("ap_common"), _T("id"), m_id))
94b0e18f 70 hStmt = DBPrepare(hdb, _T("INSERT INTO ap_common (policy_type,version,id) VALUES (?,?,?)"));
f2bb4aa1 71 else
94b0e18f
VK
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);
f2bb4aa1 81
f2bb4aa1 82 // Save access list
94b0e18f
VK
83 if (success)
84 success = saveACLToDB(hdb);
f2bb4aa1
VK
85
86 // Update node bindings
94b0e18f
VK
87 if (success)
88 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_bindings WHERE policy_id=?"));
89
bf788217 90 lockChildList(false);
94b0e18f 91 if (success && (m_childList->size() > 0))
f2bb4aa1 92 {
94b0e18f
VK
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 }
f2bb4aa1 108 }
db091a1f 109 unlockChildList();
45d84f8a
VK
110 return success;
111}
112
cd68963b 113/**
114 * Save to database
115 */
b4277312 116bool AgentPolicy::saveToDatabase(DB_HANDLE hdb)
45d84f8a 117{
c42b4551 118 lockProperties();
45d84f8a 119
b4277312 120 bool success = savePolicyCommonProperties(hdb);
45d84f8a 121
f2bb4aa1
VK
122 // Clear modifications flag and unlock object
123 if (success)
01152a54 124 m_isModified = false;
c42b4551 125 unlockProperties();
f2bb4aa1
VK
126
127 return success;
6e53f004
VK
128}
129
22ee6d97
VK
130/**
131 * Delete from database
132 */
c42b4551 133bool AgentPolicy::deleteFromDatabase(DB_HANDLE hdb)
6e53f004 134{
c42b4551 135 bool success = NetObj::deleteFromDatabase(hdb);
22ee6d97
VK
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;
6e53f004
VK
143}
144
22ee6d97
VK
145/**
146 * Load from database
147 */
9bd1bace 148bool AgentPolicy::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
6e53f004 149{
c42b4551 150 m_id = dwId;
f2bb4aa1 151
9bd1bace 152 if (!loadCommonProperties(hdb))
f2bb4aa1 153 {
35f836fe 154 DbgPrintf(2, _T("Cannot load common properties for agent policy object %d"), dwId);
94b0e18f 155 return false;
f2bb4aa1
VK
156 }
157
01152a54 158 if (!m_isDeleted)
f2bb4aa1
VK
159 {
160 TCHAR query[256];
161
9bd1bace 162 loadACLFromDB(hdb);
f2bb4aa1 163
fcf91b2e 164 _sntprintf(query, 256, _T("SELECT version FROM ap_common WHERE id=%d"), dwId);
9bd1bace 165 DB_RESULT hResult = DBSelect(hdb, query);
f2bb4aa1 166 if (hResult == NULL)
94b0e18f 167 return false;
f2bb4aa1
VK
168
169 m_version = DBGetFieldULong(hResult, 0, 0);
f2bb4aa1
VK
170 DBFreeResult(hResult);
171
172 // Load related nodes list
c42b4551 173 _sntprintf(query, 256, _T("SELECT node_id FROM ap_bindings WHERE policy_id=%d"), m_id);
9bd1bace 174 hResult = DBSelect(hdb, query);
f2bb4aa1
VK
175 if (hResult != NULL)
176 {
177 int numNodes = DBGetNumRows(hResult);
178 for(int i = 0; i < numNodes; i++)
179 {
967893bb 180 UINT32 nodeId = DBGetFieldULong(hResult, i, 0);
f2bb4aa1
VK
181 NetObj *object = FindObjectById(nodeId);
182 if (object != NULL)
183 {
c42b4551 184 if (object->getObjectClass() == OBJECT_NODE)
f2bb4aa1 185 {
1f8be1f4
VK
186 addChild(object);
187 object->addParent(this);
f2bb4aa1
VK
188 }
189 else
190 {
c42b4551 191 nxlog_write(MSG_AP_BINDING_NOT_NODE, EVENTLOG_ERROR_TYPE, "dd", m_id, nodeId);
f2bb4aa1
VK
192 }
193 }
194 else
195 {
c42b4551 196 nxlog_write(MSG_INVALID_AP_BINDING, EVENTLOG_ERROR_TYPE, "dd", m_id, nodeId);
f2bb4aa1
VK
197 }
198 }
199 DBFreeResult(hResult);
200 }
201 }
202
94b0e18f 203 return true;
6e53f004
VK
204}
205
8fe90adb
VK
206/**
207 * Create NXCP message with policy data
208 */
209void AgentPolicy::fillMessageInternal(NXCPMessage *msg)
6e53f004 210{
8fe90adb 211 NetObj::fillMessageInternal(msg);
b368969c
VK
212 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
213 msg->setField(VID_VERSION, m_version);
6e53f004
VK
214}
215
c42b4551
VK
216/**
217 * Modify policy from message
218 */
8fe90adb 219UINT32 AgentPolicy::modifyFromMessageInternal(NXCPMessage *pRequest)
6e53f004 220{
8fe90adb 221 return NetObj::modifyFromMessageInternal(pRequest);
6e53f004 222}
45d84f8a 223
8fe90adb
VK
224/**
225 * Create deployment message
226 */
b368969c 227bool AgentPolicy::createDeploymentMessage(NXCPMessage *msg)
45d84f8a 228{
b368969c 229 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
de4af576 230 msg->setField(VID_GUID, m_guid);
45d84f8a
VK
231 return true;
232}
93599cfd 233
de4af576
VK
234/**
235 * Create uninstall message
236 */
b368969c 237bool AgentPolicy::createUninstallMessage(NXCPMessage *msg)
93599cfd 238{
b368969c 239 msg->setField(VID_POLICY_TYPE, (WORD)m_policyType);
de4af576 240 msg->setField(VID_GUID, m_guid);
93599cfd
VK
241 return true;
242}
950b229b
VK
243
244/**
245 * Serialize object to JSON
246 */
247json_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}