-
Notifications
You must be signed in to change notification settings - Fork 1
/
IpNetworkModel.java
348 lines (281 loc) · 12.4 KB
/
IpNetworkModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package mx.nic.rdap.db.model;
import java.io.IOException;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import mx.nic.rdap.IpUtils;
import mx.nic.rdap.core.catalog.IpVersion;
import mx.nic.rdap.core.db.Entity;
import mx.nic.rdap.core.db.IpNetwork;
import mx.nic.rdap.db.QueryGroup;
import mx.nic.rdap.db.exception.InvalidValueException;
import mx.nic.rdap.db.exception.ObjectNotFoundException;
import mx.nic.rdap.db.exception.RdapDataAccessException;
import mx.nic.rdap.db.exception.RequiredValueNotFoundException;
import mx.nic.rdap.db.objects.IpNetworkDbObj;
/**
* Model for the {@link IpNetworkModel} Object
*
*/
public class IpNetworkModel {
private final static Logger logger = Logger.getLogger(EntityModel.class.getName());
private final static String QUERY_GROUP = "IpNetwork";
protected static QueryGroup queryGroup = null;
private static final String GET_BY_IPV4 = "getByIPv4";
private static final String GET_BY_IPV6 = "getByIPv6";
private static final String STORE_TO_DATABASE = "storeToDatabase";
private static final String GET_BY_ENTITY_ID = "getByEntityId";
private static final String GET_BY_DOMAIN_ID = "getByDomainId";
private static final String GET_BY_HANDLE = "getByHandle";
public static void loadQueryGroup(String schema) {
try {
QueryGroup qG = new QueryGroup(QUERY_GROUP, schema);
setQueryGroup(qG);
} catch (IOException e) {
throw new RuntimeException("Error loading query group");
}
}
private static void setQueryGroup(QueryGroup qG) {
queryGroup = qG;
}
private static QueryGroup getQueryGroup() {
return queryGroup;
}
private static void isValidForStore(IpNetwork ipNetwork) throws RdapDataAccessException {
if (ipNetwork.getHandle() == null)
throw new RequiredValueNotFoundException("handle", "IpNetwork");
if (ipNetwork.getStartAddress() == null) {
throw new RequiredValueNotFoundException("startAddress", "IpNetwork");
}
if (ipNetwork.getEndAddress() == null) {
throw new RequiredValueNotFoundException("endAddress", "IpNetwork");
}
if (ipNetwork.getIpVersion() == null) {
throw new RequiredValueNotFoundException("ipVersion", "IpNetwork");
}
if (ipNetwork.getIpVersion() == IpVersion.V4) {
if (ipNetwork.getStartAddress() instanceof Inet6Address
|| ipNetwork.getEndAddress() instanceof Inet6Address) {
throw new RuntimeException("IP version doesn't match with the IpVersion set to the object");
}
} else {
if (ipNetwork.getStartAddress() instanceof Inet4Address
|| ipNetwork.getEndAddress() instanceof Inet4Address) {
throw new RuntimeException("IP version doesn't match with the IpVersion set to the object");
}
}
IpUtils.validateIpCidr(ipNetwork.getStartAddress(), ipNetwork.getCidr());
IpUtils.validateLastIpCidr(ipNetwork.getEndAddress(), ipNetwork.getCidr());
}
public static Long storeToDatabase(IpNetwork ipNetwork, Connection connection)
throws RdapDataAccessException, SQLException {
isValidForStore(ipNetwork);
String query = getQueryGroup().getQuery(STORE_TO_DATABASE);
Long resultId = null;
try (PreparedStatement statement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);) {
((IpNetworkDbObj) ipNetwork).storeToDatabase(statement);
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
statement.executeUpdate();
ResultSet resultSet = statement.getGeneratedKeys();
resultSet.next();
resultId = resultSet.getLong(1);
ipNetwork.setId(resultId);
}
storeNestedObjects(ipNetwork, connection);
return resultId;
}
private static void storeNestedObjects(IpNetwork ipNetwork, Connection connection)
throws SQLException, RequiredValueNotFoundException {
StatusModel.storeIpNetworkStatusToDatabase(ipNetwork.getStatus(), ipNetwork.getId(), connection);
RemarkModel.storeIpNetworkRemarksToDatabase(ipNetwork.getRemarks(), ipNetwork.getId(), connection);
LinkModel.storeIpNetworkLinksToDatabase(ipNetwork.getLinks(), ipNetwork.getId(), connection);
EventModel.storeIpNetworkEventsToDatabase(ipNetwork.getEvents(), ipNetwork.getId(), connection);
for (Entity ent : ipNetwork.getEntities()) {
Long entId = EntityModel.getIdByHandle(ent.getHandle(), connection);
if (entId == null) {
throw new NullPointerException(
"Entity: " + ent.getHandle() + "was not inserted previously to the database");
}
ent.setId(entId);
}
RolModel.storeIpNetworkEntityRoles(ipNetwork.getEntities(), ipNetwork.getId(), connection);
}
private static void loadNestedObjects(IpNetwork ipNetwork, Connection connection) throws SQLException {
Long ipNetworkId = ipNetwork.getId();
loadSimpleNestedObjects(ipNetwork, connection);
// Retrieve the entities
ipNetwork.getEntities().addAll(EntityModel.getEntitiesByIpNetworkId(ipNetworkId, connection));
}
private static void loadSimpleNestedObjects(IpNetwork ipNetwork, Connection connection) throws SQLException {
Long ipNetworkId = ipNetwork.getId();
// Retrieve the events
ipNetwork.getEvents().addAll(EventModel.getByIpNetworkId(ipNetworkId, connection));
// Retrieve the links
ipNetwork.getLinks().addAll(LinkModel.getByIpNetworkId(ipNetworkId, connection));
// Retrieve the status
ipNetwork.getStatus().addAll(StatusModel.getByIpNetworkId(ipNetworkId, connection));
// Retrieve the remarks
try {
ipNetwork.getRemarks().addAll(RemarkModel.getByIpNetworkId(ipNetworkId, connection));
} catch (ObjectNotFoundException onfe) {
// Do nothing, remarks is not required
}
}
private static IpNetwork getByInet4Address(Inet4Address inetAddress, Integer cidr, Connection connection)
throws UnknownHostException, SQLException, InvalidValueException, ObjectNotFoundException {
InetAddress lastAddressFromNetwork = IpUtils.getLastAddressFromNetwork(inetAddress, cidr);
BigInteger start = IpUtils.addressToNumber(inetAddress);
BigInteger end = IpUtils.addressToNumber((Inet4Address) lastAddressFromNetwork);
String query = getQueryGroup().getQuery(GET_BY_IPV4);
IpNetworkDbObj ipDao = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setInt(1, cidr);
statement.setString(2, start.toString());
statement.setString(3, end.toString());
ResultSet rs = statement.executeQuery();
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
if (!rs.next()) {
throw new ObjectNotFoundException("Object not found");
}
ipDao = new IpNetworkDbObj();
ipDao.loadFromDatabase(rs);
}
return ipDao;
}
private static IpNetwork getByInet6Address(Inet6Address inetAddress, Integer cidr, Connection connection)
throws UnknownHostException, SQLException, InvalidValueException, ObjectNotFoundException {
InetAddress lastAddressFromNetwork = IpUtils.getLastAddressFromNetwork(inetAddress, cidr);
BigInteger startUpperPart = IpUtils.inet6AddressToUpperPart(inetAddress);
BigInteger startLowerPart = IpUtils.inet6AddressToLowerPart(inetAddress);
BigInteger endUpperPart = IpUtils.inet6AddressToUpperPart((Inet6Address) lastAddressFromNetwork);
BigInteger endLowerPart = IpUtils.inet6AddressToLowerPart((Inet6Address) lastAddressFromNetwork);
String query = getQueryGroup().getQuery(GET_BY_IPV6);
IpNetworkDbObj ipDao = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setInt(1, cidr);
statement.setString(2, startUpperPart.toString());
statement.setString(3, startLowerPart.toString());
statement.setString(4, endUpperPart.toString());
statement.setString(5, endLowerPart.toString());
ResultSet rs = statement.executeQuery();
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
if (!rs.next()) {
throw new ObjectNotFoundException("Object not found");
}
ipDao = new IpNetworkDbObj();
ipDao.loadFromDatabase(rs);
}
return ipDao;
}
public static IpNetwork getByInetAddress(InetAddress inetAddress, Connection connection)
throws SQLException, InvalidValueException, ObjectNotFoundException {
return getByInetAddress(inetAddress, IpUtils.getMaxValidCidr(inetAddress), connection);
}
public static IpNetwork getByInetAddress(String ipAddress, Connection connection)
throws SQLException, InvalidValueException, ObjectNotFoundException {
InetAddress inetAddress;
inetAddress = IpUtils.validateIpAddress(ipAddress);
return getByInetAddress(inetAddress, connection);
}
public static IpNetwork getByInetAddress(String ipAddress, Integer cidr, Connection connection)
throws SQLException, InvalidValueException, ObjectNotFoundException {
InetAddress inetAddress;
inetAddress = IpUtils.validateIpAddress(ipAddress);
return getByInetAddress(inetAddress, cidr, connection);
}
public static IpNetwork getByInetAddress(InetAddress inetAddress, Integer cidr, Connection connection)
throws SQLException, InvalidValueException, ObjectNotFoundException {
IpNetwork result = null;
if (inetAddress instanceof Inet4Address) {
IpUtils.validateIpv4Cidr(cidr);
try {
result = getByInet4Address((Inet4Address) inetAddress, cidr, connection);
} catch (UnknownHostException e) {
throw new InvalidValueException(e.getMessage(), e);
}
} else if (inetAddress instanceof Inet6Address) {
IpUtils.validateIpv6Cidr(cidr);
try {
result = getByInet6Address((Inet6Address) inetAddress, cidr, connection);
} catch (UnknownHostException e) {
throw new InvalidValueException(e.getMessage(), e);
}
} else {
throw new UnsupportedOperationException("Unsupported class:" + inetAddress.getClass().getName());
}
loadNestedObjects(result, connection);
return result;
}
public static IpNetwork getByDomainId(long domainId, Connection connection)
throws SQLException, ObjectNotFoundException {
String query = getQueryGroup().getQuery(GET_BY_DOMAIN_ID);
IpNetworkDbObj result = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, domainId);
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
throw new ObjectNotFoundException("Object not found");
}
result = new IpNetworkDbObj();
result.loadFromDatabase(rs);
loadSimpleNestedObjects(result, connection);
}
return result;
}
public static List<IpNetwork> getByEntityId(long entityId, Connection connection) throws SQLException {
return getByRdapObjectId(entityId, connection, GET_BY_ENTITY_ID);
}
private static List<IpNetwork> getByRdapObjectId(long id, Connection connection, String getQueryId)
throws SQLException {
String query = getQueryGroup().getQuery(getQueryId);
List<IpNetwork> results = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, id);
logger.log(Level.INFO, "Executing QUERY:" + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
return Collections.emptyList();
}
results = new ArrayList<IpNetwork>();
do {
IpNetworkDbObj ipNetwork = new IpNetworkDbObj();
ipNetwork.loadFromDatabase(rs);
results.add(ipNetwork);
} while (rs.next());
}
for (IpNetwork ip : results) {
loadSimpleNestedObjects(ip, connection);
}
return results;
}
public static IpNetworkDbObj getByHandle(String handle, Connection connection)
throws SQLException, ObjectNotFoundException {
String query = getQueryGroup().getQuery(GET_BY_HANDLE);
IpNetworkDbObj result = null;
try (PreparedStatement statement = connection.prepareStatement(query);) {
statement.setString(1, handle);
logger.log(Level.INFO, "Executing QUERY : " + statement.toString());
ResultSet rs = statement.executeQuery();
if (!rs.next()) {
throw new ObjectNotFoundException("Object not found");
}
result = new IpNetworkDbObj();
result.loadFromDatabase(rs);
}
loadNestedObjects(result, connection);
return result;
}
}