- Initial DCI transformation support on server side
[public/netxms.git] / src / server / core / subnet.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003, 2004 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: subnet.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Subnet class default constructor
28 //
29
30 Subnet::Subnet()
31 :NetObj()
32 {
33 m_dwIpNetMask = 0;
34 }
35
36
37 //
38 // Subnet class constructor
39 //
40
41 Subnet::Subnet(DWORD dwAddr, DWORD dwNetMask)
42 {
43 char szBuffer[32];
44
45 m_dwIpAddr = dwAddr;
46 m_dwIpNetMask = dwNetMask;
47 sprintf(m_szName, "%s/%d", IpToStr(dwAddr, szBuffer), BitsInMask(dwNetMask));
48 }
49
50
51 //
52 // Subnet class destructor
53 //
54
55 Subnet::~Subnet()
56 {
57 }
58
59
60 //
61 // Create object from database data
62 //
63
64 BOOL Subnet::CreateFromDB(DWORD dwId)
65 {
66 char szQuery[256];
67 DB_RESULT hResult;
68
69 sprintf(szQuery, "SELECT id,name,status,ip_addr,ip_netmask,image_id,is_deleted FROM subnets WHERE id=%d", dwId);
70 hResult = DBSelect(g_hCoreDB, szQuery);
71 if (hResult == 0)
72 return FALSE; // Query failed
73
74 if (DBGetNumRows(hResult) == 0)
75 {
76 DBFreeResult(hResult);
77 return FALSE;
78 }
79
80 m_dwId = dwId;
81 strncpy(m_szName, DBGetField(hResult, 0, 1), MAX_OBJECT_NAME);
82 m_iStatus = DBGetFieldLong(hResult, 0, 2);
83 m_dwIpAddr = DBGetFieldULong(hResult, 0, 3);
84 m_dwIpNetMask = DBGetFieldULong(hResult, 0, 4);
85 m_dwImageId = DBGetFieldULong(hResult, 0, 5);
86 m_bIsDeleted = DBGetFieldLong(hResult, 0, 6);
87
88 DBFreeResult(hResult);
89
90 // Load access list
91 LoadACLFromDB();
92
93 return TRUE;
94 }
95
96
97 //
98 // Save subnet object to database
99 //
100
101 BOOL Subnet::SaveToDB(void)
102 {
103 char szQuery[1024];
104 DB_RESULT hResult;
105 DWORD i;
106 BOOL bNewObject = TRUE;
107
108 // Lock object's access
109 Lock();
110
111 // Check for object's existence in database
112 sprintf(szQuery, "SELECT id FROM subnets WHERE id=%ld", m_dwId);
113 hResult = DBSelect(g_hCoreDB, szQuery);
114 if (hResult != 0)
115 {
116 if (DBGetNumRows(hResult) > 0)
117 bNewObject = FALSE;
118 DBFreeResult(hResult);
119 }
120
121 // Form and execute INSERT or UPDATE query
122 if (bNewObject)
123 sprintf(szQuery, "INSERT INTO subnets (id,name,status,is_deleted,ip_addr,ip_netmask,image_id) "
124 "VALUES (%ld,'%s',%d,%d,%ld,%ld,%ld)",
125 m_dwId, m_szName, m_iStatus, m_bIsDeleted, m_dwIpAddr, m_dwIpNetMask, m_dwImageId);
126 else
127 sprintf(szQuery, "UPDATE subnets SET name='%s',status=%d,is_deleted=%d,ip_addr=%ld,"
128 "ip_netmask=%ld,image_id=%ld WHERE id=%ld",
129 m_szName, m_iStatus, m_bIsDeleted, m_dwIpAddr, m_dwIpNetMask, m_dwImageId, m_dwId);
130 DBQuery(g_hCoreDB, szQuery);
131
132 // Update node to subnet mapping
133 sprintf(szQuery, "DELETE FROM nsmap WHERE subnet_id=%d", m_dwId);
134 DBQuery(g_hCoreDB, szQuery);
135 for(i = 0; i < m_dwChildCount; i++)
136 {
137 sprintf(szQuery, "INSERT INTO nsmap (subnet_id,node_id) VALUES (%ld,%ld)", m_dwId, m_pChildList[i]->Id());
138 DBQuery(g_hCoreDB, szQuery);
139 }
140
141 // Save access list
142 SaveACLToDB();
143
144 // Clear modifications flag and unlock object
145 m_bIsModified = FALSE;
146 Unlock();
147
148 return TRUE;
149 }
150
151
152 //
153 // Delete subnet object from database
154 //
155
156 BOOL Subnet::DeleteFromDB(void)
157 {
158 char szQuery[256];
159 BOOL bSuccess;
160
161 bSuccess = NetObj::DeleteFromDB();
162 if (bSuccess)
163 {
164 sprintf(szQuery, "DELETE FROM subnets WHERE id=%ld", m_dwId);
165 QueueSQLRequest(szQuery);
166 sprintf(szQuery, "DELETE FROM nsmap WHERE subnet_id=%ld", m_dwId);
167 QueueSQLRequest(szQuery);
168 }
169 return bSuccess;
170 }
171
172
173 //
174 // Create CSCP message with object's data
175 //
176
177 void Subnet::CreateMessage(CSCPMessage *pMsg)
178 {
179 NetObj::CreateMessage(pMsg);
180 pMsg->SetVariable(VID_IP_NETMASK, m_dwIpNetMask);
181 }