1: using System;
2: using System.Collections.Generic;
3: using System.Data;
4: using System.Data.Entity;
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, accessName;
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.PonByOltEmsNameAndSnAndPn(queryDev, querySn, queryPn);
69:
70: if (pon != null)
71: {
72: accessName = pon.Name + "." + queryOntId;
73:
74: ontId = Ia.Ngn.Cl.Model.Business.Huawei.Ont.OntId(pon.Id, queryOntId);
75:
76: using (var db = new Ia.Ngn.Cl.Model.Ngn())
77: {
78: emsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault();
79:
80: if (emsOnt != null)
81: {
82: ontSipInfoList = (from eo in db.EmsOntSipInfos where eo.EmsOnt.Id == emsOnt.Id select eo).ToList();
83:
84: existingItemCount = ontSipInfoList.Count;
85:
86: if (response.QueryDataTable != null)
87: {
88: if (response.QueryDataTable.Rows.Count >= 1)
89: {
90: itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
91:
92: columns = response.QueryDataTable.Columns;
93: readItemCount = response.QueryDataTable.Rows.Count;
94:
95: columnDataRow = response.QueryDataTable.Rows[0];
96:
97: foreach (DataRow dataRow in response.QueryDataTable.Rows)
98: {
99: tableRowIndex++;
100:
101: sipUserName = columns.Contains("SIPUSERNAME") ? dataRow[columns.IndexOf("SIPUSERNAME")].ToString() : string.Empty;
102: sipName = columns.Contains("SIPNAME") ? dataRow[columns.IndexOf("SIPNAME")].ToString() : string.Empty;
103:
104: // we will check that the records is for a real number, otherwise we will not send it to the database
105: 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))
106: {
107: tel = tableRowIndex;
108:
109: id = Ia.Ngn.Cl.Model.Business.Huawei.OntSipInfo.OntSipInfoId(ontId, tel);
110:
111: ontSipInfo = (from eo in ontSipInfoList where eo.Id == id select eo).SingleOrDefault();
112:
113: newOntSipInfo = new Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo()
114: {
115: Id = id,
116: SIPUSERNAME = sipUserName,
117: SIPUSERPWD = columns.Contains("SIPUSERPWD") ? dataRow[columns.IndexOf("SIPUSERPWD")].ToString() : string.Empty,
118: SIPNAME = sipName,
119: TEL = tel,
120: EmsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault()
121: };
122:
123: if (ontSipInfo == null)
124: {
125: newOntSipInfo.Created = newOntSipInfo.Updated = DateTime.UtcNow.AddHours(3);
126:
127: db.EmsOntSipInfos.Add(newOntSipInfo);
128:
129: insertedItemCount++;
130: }
131: else // update
132: {
133: // below: copy values from newOntSipInfo to ontSipInfo
134:
135: if (ontSipInfo.Update(newOntSipInfo))
136: {
137: db.EmsOntSipInfos.Attach(ontSipInfo);
138: db.Entry(ontSipInfo).State = System.Data.Entity.EntityState.Modified;
139:
140: updatedItemCount++;
141: }
142: }
143:
144: itemIdList.Add(id); // keep at the end
145: }
146: }
147:
148: // below: this function will remove values that were not present in the reading
149: if (ontSipInfoList.Count > 0)
150: {
151: foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
152: {
153: if (!itemIdList.Contains(o.Id))
154: {
155: ontSipInfo = (from osi in db.EmsOntSipInfos where osi.Id == o.Id select osi).SingleOrDefault();
156:
157: db.EmsOntSipInfos.Remove(ontSipInfo);
158:
159: deletedItemCount++;
160: }
161: }
162: }
163: }
164: else //if (dataTable.Rows.Count == 0)
165: {
166: if (ontSipInfoList.Count > 0)
167: {
168: foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
169: {
170: ontSipInfo = (from osi in db.EmsOntSipInfos where osi.Id == o.Id select osi).SingleOrDefault();
171:
172: db.EmsOntSipInfos.Remove(ontSipInfo);
173:
174: deletedItemCount++;
175: }
176: }
177: }
178:
179: db.SaveChanges();
180: }
181: else
182: {
183: if (response.ResultCode == Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode.OntIsOffline)
184: {
185: var emsOntResultCodeIsUpdated = Ia.Ngn.Cl.Model.Data.Huawei.Ont.UpdateResultCode(emsOnt, response.ResultCode);
186:
187: result.AddWarning("LST-ONTSIPINFO: QueryDataTable is null, (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ") EmsOnt.ResultCode updated?: " + emsOntResultCodeIsUpdated.ToString().ToLower());
188: }
189: else
190: {
191: result.AddWarning("LST-ONTSIPINFO: QueryDataTable is null, (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
192: }
193: }
194: }
195: else
196: {
197: result.AddWarning("EmsOnt is null for ontId: " + ontId);
198: }
199: }
200:
201: if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
202: else isUpdated = false;
203:
204: result.AddSuccess("LST-ONTSIPINFO: " + accessName + " (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ") (" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
205: }
206: else
207: {
208: result.AddError("NDD PON is null");
209: }
210: }
211: else
212: {
213: result.AddWarning("LST-ONTSIPINFO: (" + response.CompletionCode + ", " + response.ResultCode.ToString() + ")");
214: }
215:
216: return isUpdated;
217: }
218:
219: ////////////////////////////////////////////////////////////////////////////
220:
221: /// <summary>
222: ///
223: /// </summary>
224: public static List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> List()
225: {
226: List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> list;
227:
228: using (var db = new Ia.Ngn.Cl.Model.Ngn())
229: {
230: list = (from o in db.EmsOntSipInfos select o).ToList();
231: }
232:
233: return list;
234: }
235:
236: ////////////////////////////////////////////////////////////////////////////
237:
238: /// <summary>
239: /// List of service number ids within EmsOntSipInfos within a SIP designated OLT
240: /// </summary>
241: public static List<string> ServiceIdWithinSipOltList()
242: {
243: int serviceType;
244: string service, serviceId;
245: List<int> sipOltIdList;
246: List<string> list, serviceIdList;
247:
248: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
249: sipOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.SipOltIdList;
250:
251: using (var db = new Ia.Ngn.Cl.Model.Ngn())
252: {
253: list = (from o in db.EmsOntSipInfos where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt) select o.SIPUSERNAME).ToList();
254: }
255:
256: if (list != null && list.Count > 0)
257: {
258: serviceIdList = new List<string>(list.Count);
259:
260: foreach (string s in list)
261: {
262: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
263:
264: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
265:
266: serviceIdList.Add(serviceId);
267: }
268: }
269: else
270: {
271: serviceIdList = new List<string>();
272: }
273:
274: return serviceIdList;
275: }
276:
277: ////////////////////////////////////////////////////////////////////////////
278:
279: /// <summary>
280: /// List of service number ids within EmsOntSipInfos within a SIP allowed to be provisioned or migrated OLT
281: /// </summary>
282: public static List<string> ServiceIdWithinAllowedSipOltToBeProvisionedOrMigratedList()
283: {
284: int serviceType;
285: string service, serviceId;
286: List<int> sipOltIdList;
287: List<string> list, serviceIdList;
288:
289: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
290: sipOltIdList = Ia.Ngn.Cl.Model.Data.Service.AllowedToBeProvisionedSipOltIdList.Union(Ia.Ngn.Cl.Model.Data.Service.AllowedToBeMigratedSipOltIdList).ToList();
291:
292: using (var db = new Ia.Ngn.Cl.Model.Ngn())
293: {
294: list = (from o in db.EmsOntSipInfos where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt) select o.SIPUSERNAME).ToList();
295: }
296:
297: if (list != null && list.Count > 0)
298: {
299: serviceIdList = new List<string>(list.Count);
300:
301: foreach (string s in list)
302: {
303: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
304:
305: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
306:
307: serviceIdList.Add(serviceId);
308: }
309: }
310: else
311: {
312: serviceIdList = new List<string>();
313: }
314:
315: return serviceIdList;
316: }
317:
318: ////////////////////////////////////////////////////////////////////////////
319:
320: /// <summary>
321: /// List of ServiceOnts within EmsOntSipInfos within a SIP allowed to be provisioned or migrated OLT
322: /// </summary>
323: public static List<Ia.Ngn.Cl.Model.Business.ServiceOnt> ServiceOntWithinAllowedSipOltToBeProvisionedOrMigratedList()
324: {
325: int serviceType;
326: string service, serviceId;
327: List<int> sipOltIdList;
328: List<Ia.Ngn.Cl.Model.Business.ServiceOnt> tempSipUserNamelist, serviceOntList;
329:
330: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.GponService;
331: sipOltIdList = Ia.Ngn.Cl.Model.Data.Service.AllowedToBeProvisionedSipOltIdList.Union(Ia.Ngn.Cl.Model.Data.Service.AllowedToBeMigratedSipOltIdList).ToList();
332:
333: var ontAccessIdToOntForOltIdListDictionary = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntAccessIdToOntForOltIdListDictionary(sipOltIdList);
334:
335: using (var db = new Ia.Ngn.Cl.Model.Ngn())
336: {
337: tempSipUserNamelist = (from o in db.EmsOntSipInfos
338: where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt)
339: // select o.SIPUSERNAME).ToList();
340: select new Ia.Ngn.Cl.Model.Business.ServiceOnt { Service = o.SIPUSERNAME, AccessId = o.EmsOnt.Access.Id, CreatedDateTime = o.Created }).ToList();
341: }
342:
343: if (tempSipUserNamelist != null && tempSipUserNamelist.Count > 0)
344: {
345: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceOnt>(tempSipUserNamelist.Count);
346:
347: foreach (var so in tempSipUserNamelist)
348: {
349: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(so.Service); // so.Service here is o.SIPUSERNAME
350:
351: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
352:
353: serviceOntList.Add(new Ia.Ngn.Cl.Model.Business.ServiceOnt
354: {
355: ServiceId = serviceId,
356: Service = service,
357: AccessId = so.AccessId,
358: Ont = ontAccessIdToOntForOltIdListDictionary.ContainsKey(so.AccessId) ? ontAccessIdToOntForOltIdListDictionary[so.AccessId] : null,
359: CreatedDateTime = so.CreatedDateTime
360: });
361: }
362: }
363: else
364: {
365: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceOnt>();
366: }
367:
368: return serviceOntList;
369: }
370:
371: ////////////////////////////////////////////////////////////////////////////
372:
373: /// <summary>
374: ///
375: /// </summary>
376: public static Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo Read(string id)
377: {
378: Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo item;
379:
380: using (var db = new Ia.Ngn.Cl.Model.Ngn())
381: {
382: item = (from o in db.EmsOntSipInfos where o.Id == id select o).Include(o => o.EmsOnt).SingleOrDefault();
383: }
384:
385: return item;
386: }
387:
388: ////////////////////////////////////////////////////////////////////////////
389:
390: /// <summary>
391: ///
392: /// </summary>
393: public static Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo ReadByServiceIncludeEmsOntAndAccess(string service) //, Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Ont nddOnt)
394: {
395: string huaweiSipName, nokiaSipName;
396: Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo item;
397:
398: // send priv_96525212254 for Nokia switch and +96524602283@ims.moc.kw for Huawei switch
399: //if (nddOnt.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei)
400: //{
401: huaweiSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Impi(service);
402: //}
403: //else //if(ont.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Nokia)
404: //{
405: nokiaSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.PrividUser(service);
406: //}
407:
408: using (var db = new Ia.Ngn.Cl.Model.Ngn())
409: {
410: item = (from o in db.EmsOntSipInfos
411: where o.SIPNAME == huaweiSipName || o.SIPNAME == nokiaSipName
412: select o).Include(o => o.EmsOnt).Include(o => o.EmsOnt.Access).FirstOrDefault(); //.SingleOrDefault();
413: }
414:
415: return item;
416: }
417:
418: ////////////////////////////////////////////////////////////////////////////
419:
420: /// <summary>
421: ///
422: /// </summary>
423: public static Dictionary<string, string> IdToOntIdDictionary
424: {
425: get
426: {
427: Dictionary<string, string> dictionary;
428:
429: using (var db = new Ia.Ngn.Cl.Model.Ngn())
430: {
431: dictionary = (from s in db.EmsOntSipInfos select new { s.Id, OntId = s.EmsOnt.Id }).ToDictionary(u => u.Id, u => u.OntId);
432: }
433:
434: return dictionary.ToDictionary(u => u.Key, u => u.Value);
435: }
436: }
437:
438: ////////////////////////////////////////////////////////////////////////////
439: ////////////////////////////////////////////////////////////////////////////
440:
441: /// <summary>
442: ///
443: /// </summary>
444: public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo emsOntSipInfo)
445: {
446: StringBuilder sb;
447:
448: sb = new StringBuilder();
449:
450: //sb.AppendLine("Vendor: " + Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei.Name);
451: sb.AppendLine("TEL: " + emsOntSipInfo.TEL);
452: sb.AppendLine("SIPUSERNAME: " + emsOntSipInfo.SIPUSERNAME);
453: sb.AppendLine("SIPUSERPWD: " + emsOntSipInfo.SIPUSERPWD);
454: sb.AppendLine("SIPNAME: " + emsOntSipInfo.SIPNAME);
455:
456: return sb.ToString();
457: }
458:
459: ////////////////////////////////////////////////////////////////////////////
460: ////////////////////////////////////////////////////////////////////////////
461: }
462:
463: ////////////////////////////////////////////////////////////////////////////
464: ////////////////////////////////////////////////////////////////////////////
465: }