1: using Microsoft.EntityFrameworkCore;
2: using System;
3: using System.Collections.Generic;
4: using System.Data;
5: using System.Linq;
6: using System.Text;
7:
8: namespace Ia.Ngn.Cl.Model.Data.Huawei
9: {
10: ////////////////////////////////////////////////////////////////////////////
11:
12: /// <summary publish="true">
13: /// Huawei's EMS ONT SIP INFO support class of Next Generation Network'a (NGN's) data model.
14: /// </summary>
15: ///
16: /// <remarks>
17: /// Copyright © 2016-2019 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
18: ///
19: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
20: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
21: ///
22: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24: ///
25: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
26: ///
27: /// Copyright notice: This notice may not be removed or altered from any source distribution.
28: /// </remarks>
29: public partial class OntSipInfo
30: {
31: /// <summary/>
32: public OntSipInfo() { }
33:
34: ////////////////////////////////////////////////////////////////////////////
35:
36: /// <summary>
37: /// Update a single EmsOntSipInfo item
38: /// </summary>
39: public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Response response, out Ia.Cl.Model.Result result)
40: {
41: bool isUpdated;
42: int queryFn, querySn, queryPn, queryOntId, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount, tableRowIndex, tel;
43: string id, ontId, queryCommand, queryDev, sipUserName, sipName;
44: DataColumnCollection columns;
45: DataRow columnDataRow;
46: Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Pon pon;
47: Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo ontSipInfo, newOntSipInfo;
48: Ia.Ngn.Cl.Model.Huawei.EmsOnt emsOnt;
49: List<string> itemIdList;
50: List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> ontSipInfoList;
51:
52: isUpdated = false;
53: tableRowIndex = 0;
54: readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
55: result = new Ia.Cl.Model.Result();
56:
57: if (response.CompletionCode == "COMPLD")
58: {
59: queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
60:
61: // ADD-VOIPPSTNUSER::DEV=MDU-JBA-943-002,FN=0,SN=3,PN=10:{ctag}::MGID=0,TID=1,DN=96524602285;
62: queryDev = Ia.Cl.Model.Default.Match(queryCommand, @"DEV=(.+?),");
63: queryFn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"FN=(\d+)"));
64: querySn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"SN=(\d+)"));
65: queryPn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"PN=(\d+)"));
66: queryOntId = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"ONTID=(\d+)"));
67:
68: pon = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.PonByOltEmsNameAndSnAndPnAndOptionallyOntId(queryDev, querySn, queryPn, queryOntId);
69:
70: if (pon != null)
71: {
72: ontId = Ia.Ngn.Cl.Model.Business.Huawei.Ont.OntId(pon.Id, queryOntId);
73:
74: using (var db = new Ia.Ngn.Cl.Model.Ngn())
75: {
76: emsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault();
77:
78: if (emsOnt != null)
79: {
80: ontSipInfoList = (from eo in db.EmsOntSipInfoes where eo.EmsOnt.Id == emsOnt.Id select eo).ToList();
81:
82: existingItemCount = ontSipInfoList.Count;
83:
84: if (response.QueryDataTable != null)
85: {
86: if (response.QueryDataTable.Rows.Count >= 1)
87: {
88: itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
89:
90: columns = response.QueryDataTable.Columns;
91: readItemCount = response.QueryDataTable.Rows.Count;
92:
93: columnDataRow = response.QueryDataTable.Rows[0];
94:
95: foreach (DataRow dataRow in response.QueryDataTable.Rows)
96: {
97: tableRowIndex++;
98:
99: sipUserName = columns.Contains("SIPUSERNAME") ? dataRow[columns.IndexOf("SIPUSERNAME")].ToString() : string.Empty;
100: sipName = columns.Contains("SIPNAME") ? dataRow[columns.IndexOf("SIPNAME")].ToString() : string.Empty;
101:
102: // we will check that the records is for a real number, otherwise we will not send it to the database
103: if ((Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToImpi(sipName) || Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToPridUser(sipName)) && Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToImpuAid(sipUserName))
104: {
105: tel = tableRowIndex;
106:
107: id = Ia.Ngn.Cl.Model.Business.Huawei.OntSipInfo.OntSipInfoId(ontId, tel);
108:
109: ontSipInfo = (from eo in ontSipInfoList where eo.Id == id select eo).SingleOrDefault();
110:
111: newOntSipInfo = new Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo()
112: {
113: Id = id,
114: SIPUSERNAME = sipUserName,
115: SIPUSERPWD = columns.Contains("SIPUSERPWD") ? dataRow[columns.IndexOf("SIPUSERPWD")].ToString() : string.Empty,
116: SIPNAME = sipName,
117: TEL = tel,
118: EmsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault()
119: };
120:
121: if (ontSipInfo == null)
122: {
123: newOntSipInfo.Created = newOntSipInfo.Updated = DateTime.UtcNow.AddHours(3);
124:
125: db.EmsOntSipInfoes.Add(newOntSipInfo);
126:
127: insertedItemCount++;
128: }
129: else // update
130: {
131: // below: copy values from newOntSipInfo to ontSipInfo
132:
133: if (ontSipInfo.Update(newOntSipInfo))
134: {
135: db.EmsOntSipInfoes.Attach(ontSipInfo);
136: db.Entry(ontSipInfo).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
137:
138: updatedItemCount++;
139: }
140: }
141:
142: itemIdList.Add(id); // keep at the end
143: }
144: }
145:
146: // below: this function will remove values that were not present in the reading
147: if (ontSipInfoList.Count > 0)
148: {
149: foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
150: {
151: if (!itemIdList.Contains(o.Id))
152: {
153: ontSipInfo = (from osi in db.EmsOntSipInfoes where osi.Id == o.Id select osi).SingleOrDefault();
154:
155: db.EmsOntSipInfoes.Remove(ontSipInfo);
156:
157: deletedItemCount++;
158: }
159: }
160: }
161: }
162: else //if (dataTable.Rows.Count == 0)
163: {
164: if (ontSipInfoList.Count > 0)
165: {
166: foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
167: {
168: ontSipInfo = (from osi in db.EmsOntSipInfoes where osi.Id == o.Id select osi).SingleOrDefault();
169:
170: db.EmsOntSipInfoes.Remove(ontSipInfo);
171:
172: deletedItemCount++;
173: }
174: }
175: }
176:
177: db.SaveChanges();
178: }
179: else
180: {
181: if (response.ResultCode == Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode.OntIsOffline)
182: {
183: var emsOntResultCodeIsUpdated = Ia.Ngn.Cl.Model.Data.Huawei.Ont.UpdateResultCode(emsOnt, response.ResultCode);
184:
185: result.AddWarning("LST-ONTSIPINFO: QueryDataTable is null, (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ") EmsOnt.ResultCode updated?: " + emsOntResultCodeIsUpdated.ToString().ToLower());
186: }
187: else
188: {
189: result.AddWarning("LST-ONTSIPINFO: QueryDataTable is null, (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
190: }
191: }
192: }
193: else
194: {
195: result.AddWarning("EmsOnt is null for ontId: " + ontId);
196: }
197: }
198:
199: if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
200: else isUpdated = false;
201:
202: result.AddSuccess("LST-ONTSIPINFO: " + response.CompletionCode + ", " + response.ResultCode.ToString() + " (" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
203: }
204: else
205: {
206: result.AddError("NDD PON is null");
207: }
208: }
209: else
210: {
211: result.AddWarning("LST-ONTSIPINFO: (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
212: }
213:
214: return isUpdated;
215: }
216:
217: ////////////////////////////////////////////////////////////////////////////
218:
219: /// <summary>
220: ///
221: /// </summary>
222: public static List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> List()
223: {
224: List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> list;
225:
226: using (var db = new Ia.Ngn.Cl.Model.Ngn())
227: {
228: list = (from o in db.EmsOntSipInfoes select o).ToList();
229: }
230:
231: return list;
232: }
233:
234: ////////////////////////////////////////////////////////////////////////////
235:
236: /// <summary>
237: /// List of service number ids within EmsOntSipInfos within a SIP designated OLT
238: /// </summary>
239: public static List<string> ServiceIdWithinSipOltList()
240: {
241: int serviceType;
242: string service, serviceId;
243: List<int> sipOltIdList;
244: List<string> list, serviceIdList;
245:
246: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
247: sipOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.SipOltIdList;
248:
249: using (var db = new Ia.Ngn.Cl.Model.Ngn())
250: {
251: list = (from o in db.EmsOntSipInfoes where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt) select o.SIPUSERNAME).ToList();
252: }
253:
254: if (list != null && list.Count > 0)
255: {
256: serviceIdList = new List<string>(list.Count);
257:
258: foreach (string s in list)
259: {
260: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
261:
262: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
263:
264: serviceIdList.Add(serviceId);
265: }
266: }
267: else
268: {
269: serviceIdList = new List<string>();
270: }
271:
272: return serviceIdList;
273: }
274:
275: ////////////////////////////////////////////////////////////////////////////
276:
277: /// <summary>
278: /// List of service number ids within EmsOntSipInfos within a SIP allowed to be provisioned or migrated OLT
279: /// </summary>
280: public static List<string> ServiceIdWithinAllowedSipOltToBeProvisionedOrMigratedList()
281: {
282: int serviceType;
283: string service, serviceId;
284: List<int> sipOltIdList;
285: List<string> list, serviceIdList;
286:
287: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
288: sipOltIdList = Ia.Ngn.Cl.Model.Data.Service.AllowedToBeProvisionedSipOltIdList.Union(Ia.Ngn.Cl.Model.Data.Service.AllowedToBeMigratedSipOltIdList).ToList();
289:
290: using (var db = new Ia.Ngn.Cl.Model.Ngn())
291: {
292: list = (from o in db.EmsOntSipInfoes where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt) select o.SIPUSERNAME).ToList();
293: }
294:
295: if (list != null && list.Count > 0)
296: {
297: serviceIdList = new List<string>(list.Count);
298:
299: foreach (string s in list)
300: {
301: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
302:
303: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
304:
305: serviceIdList.Add(serviceId);
306: }
307: }
308: else
309: {
310: serviceIdList = new List<string>();
311: }
312:
313: return serviceIdList;
314: }
315:
316: ////////////////////////////////////////////////////////////////////////////
317:
318: /// <summary>
319: /// List of ServiceOnts within EmsOntSipInfos within a SIP allowed to be provisioned or migrated OLT
320: /// </summary>
321: public static List<Ia.Ngn.Cl.Model.Business.ServiceOnt> ServiceOntWithinAllowedSipOltToBeProvisionedOrMigratedList()
322: {
323: int serviceType;
324: string service, serviceId;
325: List<int> sipOltIdList;
326: List<Ia.Ngn.Cl.Model.Business.ServiceOnt> tempSipUserNameList, serviceOntList;
327:
328: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
329: sipOltIdList = Ia.Ngn.Cl.Model.Data.Service.AllowedToBeProvisionedSipOltIdList.Union(Ia.Ngn.Cl.Model.Data.Service.AllowedToBeMigratedSipOltIdList).ToList();
330:
331: var ontAccessIdToOntForOltIdListDictionary = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntAccessIdToOntForOltIdListDictionary(sipOltIdList);
332:
333: using (var db = new Ia.Ngn.Cl.Model.Ngn())
334: {
335: tempSipUserNameList = (from eosi in db.EmsOntSipInfoes
336: where eosi.EmsOnt.Access != null && sipOltIdList.Contains(eosi.EmsOnt.Access.Olt)
337: // select o.SIPUSERNAME).ToList();
338: select new Ia.Ngn.Cl.Model.Business.ServiceOnt { Service = eosi.SIPUSERNAME, AccessId = eosi.EmsOnt.Access.Id, CreatedDateTime = eosi.Created }).ToList();
339: }
340:
341: if (tempSipUserNameList != null && tempSipUserNameList.Count > 0)
342: {
343: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceOnt>(tempSipUserNameList.Count);
344:
345: foreach (var tsun in tempSipUserNameList)
346: {
347: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(tsun.Service); // so.Service here is o.SIPUSERNAME
348:
349: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
350:
351: serviceOntList.Add(new Ia.Ngn.Cl.Model.Business.ServiceOnt
352: {
353: ServiceId = serviceId,
354: Service = service,
355: AccessId = tsun.AccessId,
356: Ont = ontAccessIdToOntForOltIdListDictionary.ContainsKey(tsun.AccessId) ? ontAccessIdToOntForOltIdListDictionary[tsun.AccessId] : null,
357: CreatedDateTime = tsun.CreatedDateTime
358: });
359: }
360: }
361: else
362: {
363: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceOnt>();
364: }
365:
366: return serviceOntList;
367: }
368:
369: ////////////////////////////////////////////////////////////////////////////
370:
371: /// <summary>
372: ///
373: /// </summary>
374: public static Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo Read(string id)
375: {
376: Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo item;
377:
378: using (var db = new Ia.Ngn.Cl.Model.Ngn())
379: {
380: item = (from o in db.EmsOntSipInfoes where o.Id == id select o).Include(o => o.EmsOnt).SingleOrDefault();
381: }
382:
383: return item;
384: }
385:
386: ////////////////////////////////////////////////////////////////////////////
387:
388: /// <summary>
389: ///
390: /// </summary>
391: public static List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> ReadByServiceIncludeEmsOntAndAccess(string service) //, Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Ont nddOnt)
392: {
393: string huaweiSipName, nokiaSipName;
394: List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> list;
395:
396: // send priv_96525212254 for Nokia switch and +96524602283@ims.moc.kw for Huawei switch
397: //if (nddOnt.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei)
398: //{
399: huaweiSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Impi(service);
400: //}
401: //else //if(ont.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Nokia)
402: //{
403: nokiaSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.PrividUser(service);
404: //}
405:
406: using (var db = new Ia.Ngn.Cl.Model.Ngn())
407: {
408: list = (from o in db.EmsOntSipInfoes
409: where o.SIPNAME == huaweiSipName || o.SIPNAME == nokiaSipName
410: select o).Include(o => o.EmsOnt).ThenInclude(o => o.Access).ToList(); //.FirstOrDefault(); //.SingleOrDefault();
411: }
412:
413: return list;
414: }
415:
416: ////////////////////////////////////////////////////////////////////////////
417:
418: /// <summary>
419: ///
420: /// </summary>
421: public static Dictionary<string, string> IdToOntIdDictionary
422: {
423: get
424: {
425: Dictionary<string, string> dictionary;
426:
427: using (var db = new Ia.Ngn.Cl.Model.Ngn())
428: {
429: dictionary = (from s in db.EmsOntSipInfoes select new { s.Id, OntId = s.EmsOnt.Id }).ToDictionary(u => u.Id, u => u.OntId);
430: }
431:
432: return dictionary.ToDictionary(u => u.Key, u => u.Value);
433: }
434: }
435:
436: ////////////////////////////////////////////////////////////////////////////
437: ////////////////////////////////////////////////////////////////////////////
438:
439: /// <summary>
440: ///
441: /// </summary>
442: public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo emsOntSipInfo)
443: {
444: StringBuilder sb;
445:
446: sb = new StringBuilder();
447:
448: //sb.AppendLine("Vendor: " + Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei.Name);
449: sb.AppendLine("TEL: " + emsOntSipInfo.TEL);
450: sb.AppendLine("SIPUSERNAME: " + emsOntSipInfo.SIPUSERNAME);
451: sb.AppendLine("SIPUSERPWD: " + emsOntSipInfo.SIPUSERPWD);
452: sb.AppendLine("SIPNAME: " + emsOntSipInfo.SIPNAME);
453:
454: return sb.ToString();
455: }
456:
457: ////////////////////////////////////////////////////////////////////////////
458: ////////////////////////////////////////////////////////////////////////////
459: }
460:
461: ////////////////////////////////////////////////////////////////////////////
462: ////////////////////////////////////////////////////////////////////////////
463: }