NTP Analyzer  0.8.2
Analyze the operation of time servers
TimeServerDatabaseMapper.cs
Go to the documentation of this file.
1 //
2 // Copyright (c) 2013-2017 Carsten Sonne Larsen <cs@innolan.net>
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 using System;
23 using System.Diagnostics.CodeAnalysis;
24 using System.Net;
25 using Ntp.Analyzer.Data.Import;
26 using Ntp.Analyzer.Data.Log;
27 using Ntp.Analyzer.Objects;
28 using Ntp.Common.Log;
29 using Ntp.Data.Log;
30 
31 namespace Ntp.Analyzer.Data.Sql
32 {
36  public sealed class TimeServerDatabaseMapper : SqlDatabaseMapper<TimeServer>
37  {
39  : base(log)
40  {
41  }
42 
43  private const string SelectSql =
44  "SELECT id, stratum, countryCode, hostName, ip, ip6, " +
45  "useDns+0 as dns, poolMember+0 as pool, location, displayLocation, organization, " +
46  "coordinates, synchronization, serviceArea, accessPolicy, " +
47  "accessDetails, notification+0 as noti, autoKeyUrl, symmetricKeyType, " +
48  "symmetricKeyURL, serverContact, providerPage, providerUrl, " +
49  "updated " +
50  "FROM timeServer;";
51 
52  private const string InsertSql =
53  "INSERT INTO timeServer( id, stratum, countryCode, hostName, ip, ip6, " +
54  "useDns, poolMember, location, displayLocation, organization, " +
55  "coordinates, synchronization, serviceArea, accessPolicy, " +
56  "accessDetails, notification, autoKeyUrl, symmetricKeyType, " +
57  "symmetricKeyURL, serverContact, providerPage, providerUrl, " +
58  "updated ) " +
59  "VALUES ( @id, @stratum, @countryCode, @hostName, @ip, @ip6, " +
60  "@useDns, @poolMember, @location, @displayLocation, @organization, " +
61  "@coordinates, @synchronization, @serviceArea, @accessPolicy, " +
62  "@accessDetails, @notification, @autoKeyUrl, @symmetricKeyType, " +
63  "@symmetricKeyURL, @serverContact, @providerPage, @providerUrl, " +
64  "@updated );";
65 
66  protected override bool UseCache => true;
67 
68  protected override string TableName => "timeServer";
69 
70  protected override string CreateSql => "CREATE TABLE timeServer ( " +
71  " id INT NOT NULL PRIMARY KEY, " +
72  " stratum INT NOT NULL, " +
73  " countryCode CHAR(7) NOT NULL, " +
74  " hostName VARCHAR(50) NOT NULL, " +
75  " ip VARCHAR(15) NULL, " +
76  " ip6 VARCHAR(30) NULL, " +
77  " useDns BIT(1) NULL, " +
78  " poolMember BIT(1) NULL, " +
79  " location VARCHAR(100) NULL, " +
80  " displayLocation VARCHAR(60) NOT NULL, " +
81  " organization VARCHAR(50) NULL, " +
82  " coordinates VARCHAR(50) NULL, " +
83  " synchronization VARCHAR(255) NULL, " +
84  " serviceArea VARCHAR(100) NULL, " +
85  " accessPolicy VARCHAR(255) NULL, " +
86  " accessDetails VARCHAR(512) NULL, " +
87  " notification BIT(1) NULL, " +
88  " autoKeyUrl VARCHAR(255) NULL, " +
89  " symmetricKeyType VARCHAR(100) NULL, " +
90  " symmetricKeyURL VARCHAR(100) NULL, " +
91  " serverContact VARCHAR(255) NULL, " +
92  " updated TIMESTAMP NOT NULL, " +
93  " providerPage VARCHAR(100) NULL, " +
94  " providerUrl VARCHAR(255) NULL " +
95  "){1};";
96 
102  protected override TimeServer FetchExternal(int id)
103  {
104  var adapter = TimeServerWebAdapter.Create(Log);
105 
106  if (adapter == null)
107  return null;
108 
109  TimeServer server;
110 
111  try
112  {
113  server = adapter.Import(id);
114  Insert(server);
115  AddItem(server);
116  }
117  catch (Exception e)
118  {
119  Log.TimeServerFetchError(e);
120  return null;
121  }
122 
123  return server;
124  }
125 
126  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
127  protected override void Insert(TimeServer item)
128  {
129  string countryCode = item.Country?.PadRight(7).Substring(0, 7).TrimEnd();
130  string hostName = item.Name?.PadRight(50).Substring(0, 50).TrimEnd();
131  string ip = item.Address?.ToString().PadRight(15).Substring(0, 15).TrimEnd();
132  string ip6 = item.V6Address?.PadRight(30).Substring(0, 30).TrimEnd();
133  string location = item.Location?.PadRight(100).Substring(0, 100).TrimEnd();
134  string displayLocation = item.DisplayLocation?.PadRight(60).Substring(0, 60).TrimEnd();
135  string organization = item.Organization?.PadRight(50).Substring(0, 50).TrimEnd();
136  string coordinates = item.Geo?.PadRight(50).Substring(0, 50).TrimEnd();
137  string synchronization = item.Server?.PadRight(255).Substring(0, 255).TrimEnd();
138  string serviceArea = item.ServiceArea?.PadRight(100).Substring(0, 100).TrimEnd();
139  string accessPolicy = item.AccessPolicy?.PadRight(255).Substring(0, 255).TrimEnd();
140  string accessDetails = item.AccessDetails?.PadRight(512).Substring(0, 512).TrimEnd();
141  string autoKeyUrl = item.AutoKey?.PadRight(255).Substring(0, 255).TrimEnd();
142  string symmetricKeyType = item.SymKey?.PadRight(100).Substring(0, 100).TrimEnd();
143  string symmetricKeyUrl = item.SymUrl?.PadRight(100).Substring(0, 100).TrimEnd();
144  string serverContact = item.Contact?.PadRight(255).Substring(0, 255).TrimEnd();
145  string providerPage = item.ProviderPage?.PadRight(100).Substring(0, 100).TrimEnd();
146  string providerUrl = item.ProviderUrl?.PadRight(255).Substring(0, 255).TrimEnd();
147 
148  lock (MapperLocker)
149  {
150  try
151  {
152  Open();
153  Command.CommandText = PrepareInsertSql(InsertSql);
154  Command.Parameters.Add(CreateParameter("@id", item.Id));
155  Command.Parameters.Add(CreateParameter("@stratum", item.Stratum));
156  Command.Parameters.Add(CreateParameter("@countryCode", countryCode));
157  Command.Parameters.Add(CreateParameter("@hostName", hostName));
158  Command.Parameters.Add(CreateParameter("@ip", ip));
159  Command.Parameters.Add(CreateParameter("@ip6", ip6));
160  Command.Parameters.Add(CreateParameter("@useDns", item.ShouldUseDns));
161  Command.Parameters.Add(CreateParameter("@poolMember", item.IsPoolMember));
162  Command.Parameters.Add(CreateParameter("@location", location));
163  Command.Parameters.Add(CreateParameter("@displayLocation", displayLocation));
164  Command.Parameters.Add(CreateParameter("@organization", organization));
165  Command.Parameters.Add(CreateParameter("@coordinates", coordinates));
166  Command.Parameters.Add(CreateParameter("@synchronization", synchronization));
167  Command.Parameters.Add(CreateParameter("@serviceArea", serviceArea));
168  Command.Parameters.Add(CreateParameter("@accessPolicy", accessPolicy));
169  Command.Parameters.Add(CreateParameter("@accessDetails", accessDetails));
170  Command.Parameters.Add(CreateParameter("@notification", item.ShouldNotify));
171  Command.Parameters.Add(CreateParameter("@autoKeyUrl", autoKeyUrl));
172  Command.Parameters.Add(CreateParameter("@symmetricKeyType", symmetricKeyType));
173  Command.Parameters.Add(CreateParameter("@symmetricKeyURL", symmetricKeyUrl));
174  Command.Parameters.Add(CreateParameter("@serverContact", serverContact));
175  Command.Parameters.Add(CreateParameter("@providerPage", providerPage));
176  Command.Parameters.Add(CreateParameter("@providerUrl", providerUrl));
177  Command.Parameters.Add(CreateParameter("@updated", item.Updated));
178  Command.Prepare();
179  Log.SqlExecute(Command.CommandText, Command.Parameters);
180  Command.ExecuteNonQuery();
181  item.SetId(item.Id);
182  }
183  catch (Exception e)
184  {
185  Log.InsertError(TableName, e);
186  }
187  finally
188  {
189  Close();
190  }
191  }
192  }
193 
194  [SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security vulnerabilities")]
195  protected override void ReadContent()
196  {
197  try
198  {
199  Open();
200  Command.CommandText = PrepareSql(SelectSql);
201  Log.SqlExecute(Command.CommandText);
202  Reader = Command.ExecuteReader();
203 
204  while (Reader.Read())
205  {
206  int id = Convert.ToInt32(Reader["id"]);
207  int stratum = Convert.ToInt32(Reader["stratum"]);
208  string countryCode = Reader["countryCode"].ToString();
209  string hostName = Reader["hostName"].ToString();
210  string ip4 = Reader["ip"] != DBNull.Value ? Reader["ip"].ToString() : null;
211  string ip6 = Reader["ip6"] != DBNull.Value ? Reader["ip6"].ToString() : null;
212  bool? useDns = Reader["dns"] != DBNull.Value ? Convert.ToBoolean(Reader["dns"]) : (bool?) null;
213  bool? poolMember = Reader["pool"] != DBNull.Value ? Convert.ToBoolean(Reader["pool"]) : (bool?) null;
214  string location = Reader["location"].ToString();
215  string displayLocation = Reader["displayLocation"].ToString();
216  string organization = Reader["organization"] != DBNull.Value
217  ? Reader["organization"].ToString()
218  : null;
219  string coordinates = Reader["coordinates"] != DBNull.Value
220  ? Reader["organization"].ToString()
221  : null;
222  string synchronization = Reader["synchronization"].ToString();
223  string serviceArea = Reader["serviceArea"].ToString();
224  string accessPolicy = Reader["accessPolicy"].ToString();
225  string accessDetails = Reader["accessDetails"] != DBNull.Value
226  ? Reader["accessDetails"].ToString()
227  : null;
228  bool? notification = Reader["noti"] != DBNull.Value
229  ? Convert.ToBoolean(Reader["noti"])
230  : (bool?) null;
231  string autoKeyUrl = Reader["autoKeyUrl"] != DBNull.Value ? Reader["autoKeyUrl"].ToString() : null;
232  string symmetricKeyType = Reader["symmetricKeyType"] != DBNull.Value
233  ? Reader["symmetricKeyType"].ToString()
234  : null;
235  string symmetricKeyUrl = Reader["symmetricKeyURL"] != DBNull.Value
236  ? Reader["symmetricKeyURL"].ToString()
237  : null;
238  string serverContact = Reader["serverContact"] != DBNull.Value
239  ? Reader["serverContact"].ToString()
240  : null;
241  string providerPage = Reader["providerPage"] != DBNull.Value
242  ? Reader["providerPage"].ToString()
243  : null;
244  string providerUrl = Reader["providerUrl"] != DBNull.Value
245  ? Reader["providerUrl"].ToString()
246  : null;
247  DateTime updated = Convert.ToDateTime(Reader["updated"]);
248 
249  IPAddress address = null;
250  if (ip4 != null)
251  IPAddress.TryParse(ip4, out address);
252 
253  var server = new CalgaryTimeServer(
254  id, stratum, countryCode, hostName, address, ip6, useDns, poolMember, location,
255  displayLocation, organization, coordinates, synchronization, serviceArea, accessDetails,
256  accessPolicy, notification, autoKeyUrl, symmetricKeyType, symmetricKeyUrl, serverContact,
257  providerPage, providerUrl, updated);
258 
259  AddItem(server);
260  }
261  }
262  catch (Exception e)
263  {
264  Log.ReadError(TableName, e);
265  }
266  finally
267  {
268  Close();
269  }
270  }
271 
272  protected override void Update(TimeServer item)
273  {
274  throw new NotSupportedException(string.Format(LogMessages.DatabaseNoUpdate, TableName));
275  }
276  }
277 }
abstract string ProviderPage
Definition: TimeServer.cs:78
abstract string Organization
Definition: TimeServer.cs:56
override TimeServer FetchExternal(int id)
Fetch time server from external source.
abstract string AccessDetails
Definition: TimeServer.cs:66
void SetId(int id)
Sets the identifier after the object have been stored in persistent storage.
var e
Definition: bootstrap.min.js:6
abstract string AccessPolicy
Definition: TimeServer.cs:64
static TimeServerWebAdapter Create(LogBase log)
Base class for OR/M mappers. Can be used for mapping objects stored in SQL databases.
abstract IPAddress Address
Definition: TimeServer.cs:44
abstract DateTime Updated
Definition: TimeServer.cs:82
abstract string DisplayLocation
Definition: TimeServer.cs:54